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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#![allow(dead_code)]
extern crate libc;
extern crate aligned_alloc;
extern crate num_cpus;
pub mod task;
pub mod exec;
pub mod instrument;
use std::mem;
use std::env;
use std::sync::{Once, Arc};
use std::ffi::CStr;
use std::path::{Path, PathBuf};
pub use task::ISPCTaskFn;
pub use exec::{TaskSystem, Parallel};
pub use instrument::{Instrument, SimpleInstrument};
#[macro_export]
macro_rules! ispc_module {
($lib:ident) => (
include!(concat!(env!("ISPC_OUT_DIR"), "/", stringify!($lib), ".rs"));
)
}
pub struct PackagedModule {
path: Option<PathBuf>,
lib: String,
}
impl PackagedModule {
pub fn new(lib: &str) -> PackagedModule {
PackagedModule {path: None, lib: lib.to_owned()}
}
pub fn lib_path<P: AsRef<Path>>(&mut self, path: P) -> &mut PackagedModule {
self.path = Some(path.as_ref().to_path_buf());
self
}
pub fn link(&self) {
let libfile = self.lib.clone() + &env::var("HOST").unwrap();
let bindgen_file = self.lib.clone() + ".rs";
println!("cargo:rerun-if-changed={}", libfile);
println!("cargo:rerun-if-changed={}", bindgen_file);
println!("cargo:rustc-link-lib=static={}", libfile);
let path = self.get_lib_path();
println!("cargo:rustc-link-search=native={}", path.display());
println!("cargo:rustc-env=ISPC_OUT_DIR={}", path.display());
}
fn get_lib_path(&self) -> PathBuf {
let p = self.path.clone().unwrap_or_else(|| {
env::var_os("OUT_DIR").map(PathBuf::from).unwrap()
});
if p.is_relative() {
env::current_dir().unwrap().join(p)
} else {
p
}
}
}
static mut TASK_SYSTEM: Option<&'static dyn TaskSystem> = None;
static TASK_INIT: Once = Once::new();
static mut INSTRUMENT: Option<&'static dyn Instrument> = None;
static INSTRUMENT_INIT: Once = Once::new();
pub fn set_task_system<F: FnOnce() -> Arc<dyn TaskSystem>>(f: F) {
TASK_INIT.call_once(|| {
let task_sys = f();
unsafe {
let s = &*task_sys as *const (dyn TaskSystem + 'static);
mem::forget(task_sys);
TASK_SYSTEM = Some(&*s);
}
});
}
fn get_task_system() -> &'static dyn TaskSystem {
TASK_INIT.call_once(|| {
unsafe {
let task_sys = Parallel::new() as Arc<dyn TaskSystem>;
let s = &*task_sys as *const (dyn TaskSystem + 'static);
mem::forget(task_sys);
TASK_SYSTEM = Some(&*s);
}
});
unsafe { TASK_SYSTEM.unwrap() }
}
pub fn set_instrument<F: FnOnce() -> Arc<dyn Instrument>>(f: F) {
INSTRUMENT_INIT.call_once(|| {
let instrument = f();
unsafe {
let s = &*instrument as *const (dyn Instrument + 'static);
mem::forget(instrument);
INSTRUMENT = Some(&*s);
}
});
}
pub fn print_instrumenting_summary() {
get_instrument().print_summary();
}
fn get_instrument() -> &'static dyn Instrument {
INSTRUMENT_INIT.call_once(|| {
unsafe {
let instrument = Arc::new(SimpleInstrument) as Arc<dyn Instrument>;
let s = &*instrument as *const (dyn Instrument + 'static);
mem::forget(instrument);
INSTRUMENT = Some(&*s);
}
});
unsafe { INSTRUMENT.unwrap() }
}
#[allow(non_snake_case)]
#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn ISPCAlloc(handle_ptr: *mut *mut libc::c_void, size: i64,
align: i32) -> *mut libc::c_void {
get_task_system().alloc(handle_ptr, size, align)
}
#[allow(non_snake_case)]
#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn ISPCLaunch(handle_ptr: *mut *mut libc::c_void, f: *mut libc::c_void,
data: *mut libc::c_void, count0: libc::c_int,
count1: libc::c_int, count2: libc::c_int) {
let task_fn: ISPCTaskFn = mem::transmute(f);
get_task_system().launch(handle_ptr, task_fn, data, count0 as i32, count1 as i32, count2 as i32);
}
#[allow(non_snake_case)]
#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn ISPCSync(handle: *mut libc::c_void){
get_task_system().sync(handle);
}
#[allow(non_snake_case)]
#[doc(hidden)]
#[no_mangle]
pub unsafe extern "C" fn ISPCInstrument(cfile: *const libc::c_char, cnote: *const libc::c_char,
line: libc::c_int, mask: u64) {
let file_name = CStr::from_ptr(cfile);
let note = CStr::from_ptr(cnote);
let active_count = (mask as u64).count_ones();
get_instrument().instrument(file_name, note, line as i32, mask as u64, active_count);
}