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
use crate::CPU;

#[cfg(feature = "opencl")]
use crate::opencl::chosen_cl_idx;

#[cfg(feature = "cuda")]
use crate::cuda::chosen_cu_idx;

#[cfg(not(feature = "no-std"))]
thread_local! {
    pub static GLOBAL_CPU: CPU = CPU::new();
}

#[cfg(not(feature = "no-std"))]
#[inline]
pub fn static_cpu() -> &'static CPU {
    // Safety: GLOBAL_CPU should live long enough
    unsafe {
        GLOBAL_CPU
            .with(|device| device as *const CPU)
            .as_ref()
            .unwrap()
    }
}

#[cfg(feature = "no-std")]
pub static GLOBAL_CPU: Option<CPU> = None;

#[cfg(feature = "no-std")]
pub fn static_cpu() -> &'static CPU {
    if let Some(cpu) = &GLOBAL_CPU {
        cpu
    } else {
        GLOBAL_CPU = Some(CPU::new())
    }
}

#[cfg(feature = "opencl")]
thread_local! {
    pub static GLOBAL_OPENCL: crate::OpenCL = {
        crate::OpenCL::new(chosen_cl_idx()).expect("Could not create a static OpenCL device.")
    };
}

#[cfg(feature = "cuda")]
thread_local! {
    pub static GLOBAL_CUDA: crate::CUDA = {
        crate::CUDA::new(chosen_cu_idx()).expect("Could not create a static CUDA device.")
    };
}

#[cfg(feature = "opencl")]
#[inline]
pub fn static_opencl() -> &'static crate::OpenCL {
    // Safety: GLOBAL_OPENCL should live long enough
    unsafe {
        GLOBAL_OPENCL
            .with(|device| device as *const crate::OpenCL)
            .as_ref()
            .unwrap()
    }
}

#[cfg(feature = "cuda")]
#[inline]
pub fn static_cuda() -> &'static crate::CUDA {
    // Safety: GLOBAL_CUDA should live long enough
    unsafe {
        GLOBAL_CUDA
            .with(|device| device as *const crate::CUDA)
            .as_ref()
            .unwrap()
    }
}

#[cfg(test)]
mod tests {
    #[cfg(not(feature = "no-std"))]
    use crate::Buffer;

    #[cfg(not(feature = "no-std"))]
    #[cfg(not(feature = "realloc"))]
    #[test]
    fn test_static_cpu_cache() {
        // for: cargo test -- --test-threads=1
        set_count(0);
        use super::static_cpu;
        use crate::{set_count, Cache, Ident};

        let cpu = static_cpu();

        let a = Buffer::from(&[1, 2, 3, 4]);
        let b = Buffer::from(&[1, 2, 3, 4]);

        let out = Cache::get::<i32, ()>(cpu, a.len(), (&a, &b));

        let cache = static_cpu().cache.borrow();
        let cached = cache
            .nodes
            .get(&Ident {
                idx: 0,
                len: out.len(),
            })
            .unwrap();

        assert_eq!(cached.ptr, out.ptr.ptr as *mut u8);
    }

    #[cfg(feature = "opencl")]
    #[test]
    fn test_to_cl() {
        let buf = Buffer::from(&[1f32, 2., 3.]);
        let mut cl = buf.to_cl();

        assert_eq!(cl.read(), vec![1., 2., 3.,]);

        cl.clear();

        assert_eq!(cl.read(), vec![0.; 3]);
    }

    #[cfg(feature = "cuda")]
    #[test]
    fn test_to_cuda() {
        let buf = Buffer::from(&[1f32, 2., 3.]);
        let mut cuda = buf.to_cuda();

        assert_eq!(cuda.read(), vec![1., 2., 3.,]);

        cuda.clear();

        assert_eq!(cuda.read(), vec![0.; 3]);
    }

    #[cfg(feature = "opencl")]
    #[test]
    fn test_to_cpu() {
        let buf = Buffer::from(&[2f32, 5., 1.]).to_cl();
        let buf = buf.to_cpu();

        assert_eq!(buf.as_slice(), &[2., 5., 1.]);
    }

    #[cfg(any(feature = "opencl", feature = "cuda"))]
    #[test]
    fn test_to_gpu() {
        use crate::buf;

        let buf = buf![2f32, 5., 1.].to_gpu();
        assert_eq!(buf.read(), vec![2., 5., 1.]);
    }

    #[cfg(feature = "cuda")]
    #[test]
    fn test_to_device_cu() {
        use crate::CUDA;

        let buf = Buffer::from(&[3f32, 1.4, 1., 2.]).to_dev::<CUDA>();

        assert_eq!(buf.read(), vec![3., 1.4, 1., 2.]);
    }

    #[cfg(feature = "opencl")]
    #[test]
    fn test_to_device_cl() {
        use crate::{buf, OpenCL};

        let buf = buf![3f32, 1.4, 1., 2.].to_dev::<OpenCL>();

        assert_eq!(buf.read(), vec![3., 1.4, 1., 2.]);
    }
}