Skip to content

Commit 1284d13

Browse files
committed
added support for stop flag
1 parent b3fb817 commit 1284d13

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

src/math/perturbation.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ use std::sync::Arc;
1212
pub struct Perturbation {}
1313

1414
impl Perturbation {
15-
pub fn iterate_normal(pixel_data: &mut [PixelData], reference: &Reference, pixels_complete: &Arc<RelaxedCounter>) {
15+
pub fn iterate_normal(pixel_data: &mut [PixelData], reference: &Reference, pixels_complete: &Arc<RelaxedCounter>, stop_flag: &Arc<RelaxedCounter>) {
1616
pixel_data.par_chunks_mut(4)
1717
.for_each(|pixel_data| {
18+
if stop_flag.get() >= 1 {
19+
return;
20+
}
21+
1822
let mut new_pixels_complete = 0;
1923

2024
for pixel in pixel_data {
@@ -96,9 +100,13 @@ impl Perturbation {
96100
});
97101
}
98102

99-
pub fn iterate_normal_plus_derivative(pixel_data: &mut [PixelData], reference: &Reference, pixels_complete: &Arc<RelaxedCounter>) {
103+
pub fn iterate_normal_plus_derivative(pixel_data: &mut [PixelData], reference: &Reference, pixels_complete: &Arc<RelaxedCounter>, stop_flag: &Arc<RelaxedCounter>) {
100104
pixel_data.par_chunks_mut(4)
101105
.for_each(|pixel_data| {
106+
if stop_flag.get() >= 1 {
107+
return;
108+
}
109+
102110
let mut new_pixels_complete = 0;
103111

104112
for pixel in pixel_data {

src/math/reference.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Reference {
7373
}
7474

7575

76-
pub fn run(&mut self, reference_counter: &Arc<RelaxedCounter>, reference_maximum_iteration_counter: &Arc<RelaxedCounter>) {
76+
pub fn run(&mut self, reference_counter: &Arc<RelaxedCounter>, reference_maximum_iteration_counter: &Arc<RelaxedCounter>, stop_flag: &Arc<RelaxedCounter>) {
7777
let z_fixed = to_fixed(&self.z);
7878
let z_tolerance = self.glitch_tolerance * z_fixed.norm_sqr();
7979

@@ -100,6 +100,10 @@ impl Reference {
100100
}
101101
}
102102

103+
if stop_flag.get() >= 1 {
104+
return
105+
};
106+
103107
reference_counter.inc();
104108

105109
if !self.step() {

src/math/series_approximation.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl SeriesApproximation {
5959
}
6060
}
6161

62-
pub fn generate_approximation(&mut self, center_reference: &Reference, series_approximation_counter: &Arc<RelaxedCounter>) {
62+
pub fn generate_approximation(&mut self, center_reference: &Reference, series_approximation_counter: &Arc<RelaxedCounter>, stop_flag: &Arc<RelaxedCounter>) {
6363
// Reset the coefficients
6464
self.coefficients = vec![vec![ComplexExtended::new2(0.0, 0.0, 0); self.order as usize + 1]; 1];
6565

@@ -75,6 +75,10 @@ impl SeriesApproximation {
7575
// Can be changed later into a better loop - this function could also return some more information
7676
// Go through all remaining iterations
7777
for i in 1..self.maximum_iteration {
78+
if stop_flag.get() >= 1 {
79+
return
80+
};
81+
7882
// This is checking if the approximation can step forward so takes the next iteration
7983
next_coefficients[0] = center_reference.reference_data[i].z_extended;
8084
next_coefficients[1] = previous_coefficients[0] * previous_coefficients[1] * 2.0 + add_value;

src/renderer.rs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct FractalRenderer {
4040
experimental: bool,
4141
show_output: bool,
4242
pub progress: ProgressCounters,
43-
pub render_time: u128,
43+
pub render_time: u128
4444
}
4545

4646
impl FractalRenderer {
@@ -196,7 +196,7 @@ impl FractalRenderer {
196196
self.series_approximation.maximum_iteration = self.maximum_iteration;
197197
}
198198

199-
pub fn render_frame(&mut self, frame_index: usize, filename: String) {
199+
pub fn render_frame(&mut self, frame_index: usize, filename: String, stop_flag: Option<Arc<RelaxedCounter>>) {
200200
if self.show_output {
201201
print!("{:<6}", frame_index + self.frame_offset);
202202
print!("| {:<15}", extended_to_string_short(self.zoom));
@@ -206,6 +206,11 @@ impl FractalRenderer {
206206
let frame_time = Instant::now();
207207
let approximation_time = Instant::now();
208208

209+
let stop_flag = match stop_flag {
210+
Some(stop_flag) => stop_flag,
211+
None => Arc::new(RelaxedCounter::new(0))
212+
};
213+
209214
let (tx, rx) = mpsc::channel();
210215

211216
if self.show_output {
@@ -244,9 +249,15 @@ impl FractalRenderer {
244249
if frame_index == 0 {
245250
self.data_export.maximum_iteration = self.maximum_iteration;
246251

247-
self.center_reference.run(&self.progress.reference, &self.progress.reference_maximum);
252+
self.center_reference.run(&self.progress.reference, &self.progress.reference_maximum, &stop_flag);
253+
254+
if stop_flag.get() >= 1 {
255+
self.render_time = frame_time.elapsed().as_millis();
256+
return;
257+
};
258+
248259
self.series_approximation.maximum_iteration = self.center_reference.current_iteration;
249-
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation);
260+
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation, &stop_flag);
250261
} else {
251262
// If the image width/height changes intraframe (GUI) we need to regenerate some things
252263
if self.data_export.image_width != self.image_width || self.data_export.image_height != self.image_height {
@@ -263,9 +274,14 @@ impl FractalRenderer {
263274
// TODO make it so that the value is set back to zero, rather than remade
264275
// self.progress.reset_series_approximation();
265276

266-
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation);
277+
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation, &stop_flag);
267278
}
268279
}
280+
281+
if stop_flag.get() >= 1 {
282+
self.render_time = frame_time.elapsed().as_millis();
283+
return;
284+
};
269285

270286
let cos_rotate = self.rotate.cos();
271287
let sin_rotate = self.rotate.sin();
@@ -297,7 +313,10 @@ impl FractalRenderer {
297313

298314
self.progress.min_series_approximation.add(self.series_approximation.min_valid_iteration);
299315

300-
// self.data_export.maximum_iteration = self.maximum_iteration;
316+
if stop_flag.get() >= 1 {
317+
self.render_time = frame_time.elapsed().as_millis();
318+
return;
319+
};
301320

302321
tx.send(()).unwrap();
303322

@@ -391,6 +410,11 @@ impl FractalRenderer {
391410
print!("| {:<15}", packing_time.elapsed().as_millis());
392411
std::io::stdout().flush().unwrap();
393412
};
413+
414+
if stop_flag.get() >= 1 {
415+
self.render_time = frame_time.elapsed().as_millis();
416+
return;
417+
};
394418

395419
let iteration_time = Instant::now();
396420

@@ -420,9 +444,9 @@ impl FractalRenderer {
420444

421445
// This one has no offset because it is not a glitch resolving reference
422446
if self.analytic_derivative {
423-
Perturbation::iterate_normal_plus_derivative(&mut pixel_data, &self.center_reference, &self.progress.iteration);
447+
Perturbation::iterate_normal_plus_derivative(&mut pixel_data, &self.center_reference, &self.progress.iteration, &stop_flag);
424448
} else {
425-
Perturbation::iterate_normal(&mut pixel_data, &self.center_reference, &self.progress.iteration);
449+
Perturbation::iterate_normal(&mut pixel_data, &self.center_reference, &self.progress.iteration, &stop_flag);
426450
};
427451

428452
tx.send(()).unwrap();
@@ -477,7 +501,12 @@ impl FractalRenderer {
477501
let mut glitch_reference = self.series_approximation.get_reference(glitch_reference_pixel.delta_centre, &self.center_reference);
478502

479503
correction_references += 1;
480-
glitch_reference.run(&Arc::new(RelaxedCounter::new(0)), &Arc::new(RelaxedCounter::new(0)));
504+
glitch_reference.run(&Arc::new(RelaxedCounter::new(0)), &Arc::new(RelaxedCounter::new(0)), &stop_flag);
505+
506+
if stop_flag.get() >= 1 {
507+
self.render_time = frame_time.elapsed().as_millis();
508+
return;
509+
};
481510

482511
let delta_current_reference = self.series_approximation.evaluate(glitch_reference_pixel.delta_centre, glitch_reference.start_iteration);
483512

@@ -501,9 +530,9 @@ impl FractalRenderer {
501530
}
502531

503532
if self.analytic_derivative {
504-
Perturbation::iterate_normal_plus_derivative(&mut pixel_data, &glitch_reference, &self.progress.iteration);
533+
Perturbation::iterate_normal_plus_derivative(&mut pixel_data, &glitch_reference, &self.progress.iteration, &stop_flag);
505534
} else {
506-
Perturbation::iterate_normal(&mut pixel_data, &glitch_reference, &self.progress.iteration);
535+
Perturbation::iterate_normal(&mut pixel_data, &glitch_reference, &self.progress.iteration, &stop_flag);
507536
};
508537

509538
self.data_export.export_pixels(&pixel_data, &glitch_reference, delta_pixel_extended);
@@ -550,7 +579,7 @@ impl FractalRenderer {
550579
while self.remaining_frames > 0 && self.zoom.to_float() > 0.5 {
551580
self.render_frame(count,
552581
format!("output/{:08}_{}", count + self.frame_offset,
553-
extended_to_string_short(self.zoom)));
582+
extended_to_string_short(self.zoom)), None);
554583

555584
self.zoom.mantissa /= self.zoom_scale_factor;
556585
self.zoom.reduce();
@@ -565,7 +594,7 @@ impl FractalRenderer {
565594
// Overwrite the series approximation order
566595
self.series_approximation.order = 8;
567596
self.series_approximation.maximum_iteration = self.center_reference.current_iteration;
568-
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation);
597+
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation, &Arc::new(RelaxedCounter::new(0)));
569598
}
570599

571600
// Logic in here to automatically adjust the maximum number of iterations
@@ -583,10 +612,10 @@ impl FractalRenderer {
583612
} else {
584613
if self.series_approximation.min_valid_iteration < 1000 && self.series_approximation.order > 16 {
585614
self.series_approximation.order = 16;
586-
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation);
615+
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation, &Arc::new(RelaxedCounter::new(0)));
587616
} else if self.series_approximation.min_valid_iteration < 10000 && self.series_approximation.order > 32 {
588617
self.series_approximation.order = 32;
589-
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation);
618+
self.series_approximation.generate_approximation(&self.center_reference, &self.progress.series_approximation, &Arc::new(RelaxedCounter::new(0)));
590619
}
591620
}
592621

src/util/progress.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub struct ProgressCounters {
1010
pub series_validation: Arc<RelaxedCounter>,
1111
pub iteration: Arc<RelaxedCounter>,
1212
pub glitched_maximum: Arc<RelaxedCounter>,
13-
pub stop_flag: Arc<RelaxedCounter>
1413
}
1514

1615
impl ProgressCounters {
@@ -22,8 +21,7 @@ impl ProgressCounters {
2221
min_series_approximation: Arc::new(RelaxedCounter::new(0)),
2322
series_validation: Arc::new(RelaxedCounter::new(0)),
2423
iteration: Arc::new(RelaxedCounter::new(0)),
25-
glitched_maximum: Arc::new(RelaxedCounter::new(0)),
26-
stop_flag: Arc::new(RelaxedCounter::new(0)),
24+
glitched_maximum: Arc::new(RelaxedCounter::new(0))
2725
}
2826
}
2927

0 commit comments

Comments
 (0)