1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
mod gen_ctx;
pub use self::gen_ctx::GenerationContext;
#[cfg(target_os = "windows")]
use winapi::um::winbase::SetThreadAffinityMask;
#[cfg(target_os = "windows")]
use winapi::um::errhandlingapi::GetLastError;
#[cfg(target_os = "windows")]
use std::os::windows::io::AsRawHandle;
#[cfg(not(target_os = "windows"))]
use libc::{pthread_setaffinity_np, cpu_set_t};
#[cfg(not(target_os = "windows"))]
use std::os::unix::thread::JoinHandleExt;
#[cfg(not(target_os = "windows"))]
use std::mem;
use std::thread::JoinHandle;
use image::DynamicImage;
pub fn init_image(size: (usize, usize, usize)) -> Vec<DynamicImage> {
(0..size.2).map(|_| DynamicImage::new_rgb8(size.0 as u32, size.1 as u32)).collect()
}
pub fn points_to_generate(size: (usize, usize, usize)) -> u64 {
10u64.pow(((size.0 * size.1 * size.2) as f64).log10().ceil() as u32 + 1)
}
pub fn filename_to_save(size: (usize, usize, usize), z: usize) -> String {
format!("mandala-{}x{}x{}-{:05}.png", size.0, size.1, size.2, z)
}
pub fn affine_thread<T>(thread: &JoinHandle<T>, to_cpu: usize) {
let mask = 1usize << to_cpu;
#[cfg(target_os = "windows")]
{
if unsafe { SetThreadAffinityMask(thread.as_raw_handle(), mask) } == 0 {
eprintln!("Couldn't set affinity mask to cpu {}: {:#010x}", to_cpu, unsafe { GetLastError() });
}
}
#[cfg(not(target_os = "windows"))]
{
let err = unsafe { pthread_setaffinity_np(thread.as_pthread_t(), mem::size_of_val(&mask), &mask as *const _ as *const cpu_set_t) };
if err != 0 {
eprintln!("Couldn't set affinity mask to cpu {}: {}", to_cpu, err);
}
}
}