diffsl 0.9.1

A compiler for a domain-specific language for ordinary differential equations (ODE).
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use anyhow::{anyhow, Result};
use std::collections::HashMap;

type UIntType = u32;

pub type BarrierInitFunc = unsafe extern "C" fn();

pub type SetConstantsFunc = unsafe extern "C" fn(thread_id: UIntType, thread_dim: UIntType);

pub type StopFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    data: *mut T,
    root: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type RhsFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    data: *mut T,
    rr: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type RhsGradFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    du: *const T,
    data: *const T,
    ddata: *mut T,
    rr: *const T,
    drr: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type RhsRevGradFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    du: *mut T,
    data: *const T,
    ddata: *mut T,
    rr: *const T,
    drr: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type RhsSensGradFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    data: *const T,
    ddata: *mut T,
    rr: *const T,
    drr: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type RhsSensRevGradFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    data: *const T,
    ddata: *mut T,
    rr: *const T,
    drr: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type MassFunc<T> = unsafe extern "C" fn(
    time: T,
    v: *const T,
    data: *mut T,
    mv: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type MassRevGradFunc<T> = unsafe extern "C" fn(
    time: T,
    v: *const T,
    dv: *mut T,
    data: *const T,
    ddata: *mut T,
    mv: *const T,
    dmv: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type U0Func<T> =
    unsafe extern "C" fn(u: *mut T, data: *mut T, thread_id: UIntType, thread_dim: UIntType);
pub type U0SensGradFunc<T> = unsafe extern "C" fn(
    u: *const T,
    du: *mut T,
    data: *const T,
    ddata: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type U0GradFunc<T> = unsafe extern "C" fn(
    u: *const T,
    du: *mut T,
    data: *const T,
    ddata: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type U0RevGradFunc<T> = unsafe extern "C" fn(
    u: *const T,
    du: *mut T,
    data: *const T,
    ddata: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type CalcOutFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    data: *mut T,
    out: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type CalcOutGradFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    du: *const T,
    data: *const T,
    ddata: *mut T,
    out: *const T,
    dout: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type CalcOutRevGradFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    du: *mut T,
    data: *const T,
    ddata: *mut T,
    out: *const T,
    dout: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type CalcOutSensGradFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    data: *const T,
    ddata: *mut T,
    out: *const T,
    dout: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type CalcOutSensRevGradFunc<T> = unsafe extern "C" fn(
    time: T,
    u: *const T,
    data: *const T,
    ddata: *mut T,
    out: *const T,
    dout: *mut T,
    thread_id: UIntType,
    thread_dim: UIntType,
);
pub type GetDimsFunc = unsafe extern "C" fn(
    states: *mut UIntType,
    inputs: *mut UIntType,
    outputs: *mut UIntType,
    data: *mut UIntType,
    stop: *mut UIntType,
    has_mass: *mut UIntType,
);
pub type SetInputsFunc<T> = unsafe extern "C" fn(inputs: *const T, data: *mut T);
pub type GetInputsFunc<T> = unsafe extern "C" fn(inputs: *mut T, data: *const T);
pub type SetInputsGradFunc<T> =
    unsafe extern "C" fn(inputs: *const T, dinputs: *const T, data: *const T, ddata: *mut T);
pub type SetInputsRevGradFunc<T> =
    unsafe extern "C" fn(inputs: *const T, dinputs: *mut T, data: *const T, ddata: *mut T);
pub type SetIdFunc<T> = unsafe extern "C" fn(id: *mut T);
pub type GetTensorFunc<T> =
    unsafe extern "C" fn(data: *const T, tensor_data: *mut *mut T, tensor_size: *mut UIntType);
pub type GetConstantFunc<T> =
    unsafe extern "C" fn(tensor_data: *mut *const T, tensor_size: *mut UIntType);

pub(crate) struct JitFunctions<T> {
    pub(crate) set_u0: U0Func<T>,
    pub(crate) rhs: RhsFunc<T>,
    pub(crate) mass: MassFunc<T>,
    pub(crate) calc_out: CalcOutFunc<T>,
    pub(crate) calc_stop: StopFunc<T>,
    pub(crate) set_id: SetIdFunc<T>,
    pub(crate) get_dims: GetDimsFunc,
    pub(crate) set_inputs: SetInputsFunc<T>,
    pub(crate) get_inputs: GetInputsFunc<T>,
    #[allow(dead_code)]
    pub(crate) barrier_init: Option<BarrierInitFunc>,
    pub(crate) set_constants: SetConstantsFunc,
}

impl<T> JitFunctions<T> {
    pub(crate) fn new(symbol_map: &HashMap<String, *const u8>) -> Result<Self> {
        // check if all required symbols are present
        let required_symbols = [
            "set_u0",
            "rhs",
            "mass",
            "calc_out",
            "calc_stop",
            "set_id",
            "get_dims",
            "set_inputs",
            "get_inputs",
            "set_constants",
        ];
        for symbol in &required_symbols {
            if !symbol_map.contains_key(*symbol) {
                return Err(anyhow!("Missing required symbol: {}", symbol));
            }
        }
        let set_u0 = unsafe { std::mem::transmute::<*const u8, U0Func<T>>(symbol_map["set_u0"]) };
        let rhs = unsafe { std::mem::transmute::<*const u8, RhsFunc<T>>(symbol_map["rhs"]) };
        let mass = unsafe { std::mem::transmute::<*const u8, MassFunc<T>>(symbol_map["mass"]) };
        let calc_out =
            unsafe { std::mem::transmute::<*const u8, CalcOutFunc<T>>(symbol_map["calc_out"]) };
        let calc_stop =
            unsafe { std::mem::transmute::<*const u8, StopFunc<T>>(symbol_map["calc_stop"]) };
        let set_id =
            unsafe { std::mem::transmute::<*const u8, SetIdFunc<T>>(symbol_map["set_id"]) };
        let get_dims =
            unsafe { std::mem::transmute::<*const u8, GetDimsFunc>(symbol_map["get_dims"]) };
        let set_inputs =
            unsafe { std::mem::transmute::<*const u8, SetInputsFunc<T>>(symbol_map["set_inputs"]) };
        let get_inputs =
            unsafe { std::mem::transmute::<*const u8, GetInputsFunc<T>>(symbol_map["get_inputs"]) };
        let barrier_init = symbol_map.get("barrier_init").map(|func_ptr| unsafe {
            std::mem::transmute::<*const u8, BarrierInitFunc>(*func_ptr)
        });
        let set_constants = unsafe {
            std::mem::transmute::<*const u8, SetConstantsFunc>(symbol_map["set_constants"])
        };

        Ok(Self {
            set_u0,
            rhs,
            mass,
            calc_out,
            calc_stop,
            set_id,
            get_dims,
            set_inputs,
            get_inputs,
            barrier_init,
            set_constants,
        })
    }
}

pub(crate) struct JitGradFunctions<T> {
    pub(crate) set_u0_grad: U0GradFunc<T>,
    pub(crate) rhs_grad: RhsGradFunc<T>,
    pub(crate) calc_out_grad: CalcOutGradFunc<T>,
    pub(crate) set_inputs_grad: SetInputsGradFunc<T>,
}

impl<T> JitGradFunctions<T> {
    pub(crate) fn new(symbol_map: &HashMap<String, *const u8>) -> Result<Self> {
        // check if all required symbols are present
        let required_symbols = [
            "set_u0_grad",
            "rhs_grad",
            "calc_out_grad",
            "set_inputs_grad",
        ];
        for symbol in &required_symbols {
            if !symbol_map.contains_key(*symbol) {
                return Err(anyhow!("Missing required symbol: {}", symbol));
            }
        }
        let set_u0_grad =
            unsafe { std::mem::transmute::<*const u8, U0GradFunc<T>>(symbol_map["set_u0_grad"]) };
        let rhs_grad =
            unsafe { std::mem::transmute::<*const u8, RhsGradFunc<T>>(symbol_map["rhs_grad"]) };
        let calc_out_grad = unsafe {
            std::mem::transmute::<*const u8, CalcOutGradFunc<T>>(symbol_map["calc_out_grad"])
        };
        let set_inputs_grad = unsafe {
            std::mem::transmute::<*const u8, SetInputsGradFunc<T>>(symbol_map["set_inputs_grad"])
        };

        Ok(Self {
            set_u0_grad,
            rhs_grad,
            calc_out_grad,
            set_inputs_grad,
        })
    }
}

pub(crate) struct JitGradRFunctions<T> {
    pub(crate) set_u0_rgrad: U0RevGradFunc<T>,
    pub(crate) rhs_rgrad: RhsRevGradFunc<T>,
    pub(crate) mass_rgrad: MassRevGradFunc<T>,
    pub(crate) calc_out_rgrad: CalcOutRevGradFunc<T>,
    pub(crate) set_inputs_rgrad: SetInputsRevGradFunc<T>,
}

impl<T> JitGradRFunctions<T> {
    pub(crate) fn new(symbol_map: &HashMap<String, *const u8>) -> Result<Self> {
        let required_symbols = [
            "set_u0_rgrad",
            "rhs_rgrad",
            "mass_rgrad",
            "calc_out_rgrad",
            "set_inputs_rgrad",
        ];
        for symbol in &required_symbols {
            if !symbol_map.contains_key(*symbol) {
                return Err(anyhow!("Missing required symbol: {}", symbol));
            }
        }
        let set_u0_rgrad = unsafe {
            std::mem::transmute::<*const u8, U0RevGradFunc<T>>(symbol_map["set_u0_rgrad"])
        };
        let rhs_rgrad =
            unsafe { std::mem::transmute::<*const u8, RhsRevGradFunc<T>>(symbol_map["rhs_rgrad"]) };
        let mass_rgrad = unsafe {
            std::mem::transmute::<*const u8, MassRevGradFunc<T>>(symbol_map["mass_rgrad"])
        };
        let calc_out_rgrad = unsafe {
            std::mem::transmute::<*const u8, CalcOutRevGradFunc<T>>(symbol_map["calc_out_rgrad"])
        };
        let set_inputs_rgrad = unsafe {
            std::mem::transmute::<*const u8, SetInputsRevGradFunc<T>>(
                symbol_map["set_inputs_rgrad"],
            )
        };

        Ok(Self {
            set_u0_rgrad,
            rhs_rgrad,
            mass_rgrad,
            calc_out_rgrad,
            set_inputs_rgrad,
        })
    }
}

pub(crate) struct JitSensGradFunctions<T> {
    pub(crate) set_u0_sgrad: U0SensGradFunc<T>,
    pub(crate) rhs_sgrad: RhsSensGradFunc<T>,
    pub(crate) calc_out_sgrad: CalcOutSensGradFunc<T>,
}

impl<T> JitSensGradFunctions<T> {
    pub(crate) fn new(symbol_map: &HashMap<String, *const u8>) -> Result<Self> {
        let required_symbols = ["rhs_sgrad", "calc_out_sgrad", "set_u0_sgrad"];
        for symbol in &required_symbols {
            if !symbol_map.contains_key(*symbol) {
                return Err(anyhow!("Missing required symbol: {}", symbol));
            }
        }
        let rhs_sgrad = unsafe {
            std::mem::transmute::<*const u8, RhsSensGradFunc<T>>(symbol_map["rhs_sgrad"])
        };
        let calc_out_sgrad = unsafe {
            std::mem::transmute::<*const u8, CalcOutSensGradFunc<T>>(symbol_map["calc_out_sgrad"])
        };
        let set_u0_sgrad = unsafe {
            std::mem::transmute::<*const u8, U0SensGradFunc<T>>(symbol_map["set_u0_sgrad"])
        };

        Ok(Self {
            rhs_sgrad,
            calc_out_sgrad,
            set_u0_sgrad,
        })
    }
}

pub(crate) struct JitSensRevGradFunctions<T> {
    pub(crate) rhs_rgrad: RhsSensRevGradFunc<T>,
    pub(crate) calc_out_rgrad: CalcOutSensRevGradFunc<T>,
}

impl<T> JitSensRevGradFunctions<T> {
    pub(crate) fn new(symbol_map: &HashMap<String, *const u8>) -> Result<Self> {
        let required_symbols = ["rhs_srgrad", "calc_out_srgrad"];
        for symbol in &required_symbols {
            if !symbol_map.contains_key(*symbol) {
                return Err(anyhow!("Missing required symbol: {}", symbol));
            }
        }
        let rhs_rgrad = unsafe {
            std::mem::transmute::<*const u8, RhsSensRevGradFunc<T>>(symbol_map["rhs_srgrad"])
        };
        let calc_out_rgrad = unsafe {
            std::mem::transmute::<*const u8, CalcOutSensRevGradFunc<T>>(
                symbol_map["calc_out_srgrad"],
            )
        };

        Ok(Self {
            rhs_rgrad,
            calc_out_rgrad,
        })
    }
}

pub(crate) struct JitGetTensorFunctions<T> {
    pub(crate) data_map: HashMap<String, GetTensorFunc<T>>,
    pub(crate) constant_map: HashMap<String, GetConstantFunc<T>>,
}

impl<T> JitGetTensorFunctions<T> {
    pub(crate) fn new(symbol_map: &HashMap<String, *const u8>) -> Result<Self> {
        let mut data_map = HashMap::new();
        let mut constant_map = HashMap::new();
        let data_prefix = "get_tensor_";
        let constant_prefix = "get_constant_";
        for (name, func_ptr) in symbol_map.iter() {
            if name.starts_with(data_prefix) {
                let func = unsafe { std::mem::transmute::<*const u8, GetTensorFunc<T>>(*func_ptr) };
                data_map.insert(name.strip_prefix(data_prefix).unwrap().to_string(), func);
            } else if name.starts_with(constant_prefix) {
                let func =
                    unsafe { std::mem::transmute::<*const u8, GetConstantFunc<T>>(*func_ptr) };
                constant_map.insert(
                    name.strip_prefix(constant_prefix).unwrap().to_string(),
                    func,
                );
            }
        }
        Ok(Self {
            data_map,
            constant_map,
        })
    }
}