mwa_hyperbeam 0.10.4

Primary beam code for the Murchison Widefield Array (MWA) radio telescope.
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Convenience code for interfacing with GPUs.

use std::{
    ffi::{c_void, CStr},
    panic::Location,
};

use thiserror::Error;

#[cfg(feature = "cuda")]
use cuda_runtime_sys::{
    cudaDeviceSynchronize as gpuDeviceSynchronize, cudaError::cudaSuccess as gpuSuccess,
    cudaFree as gpuFree, cudaGetErrorString as gpuGetErrorString,
    cudaGetLastError as gpuGetLastError, cudaMalloc as gpuMalloc, cudaMemcpy as gpuMemcpy,
    cudaMemcpyKind::cudaMemcpyDeviceToHost as gpuMemcpyDeviceToHost,
    cudaMemcpyKind::cudaMemcpyHostToDevice as gpuMemcpyHostToDevice,
};
#[cfg(feature = "hip")]
use hip_sys::hiprt::{
    hipDeviceSynchronize as gpuDeviceSynchronize, hipError_t::hipSuccess as gpuSuccess,
    hipFree as gpuFree, hipGetErrorString as gpuGetErrorString, hipGetLastError as gpuGetLastError,
    hipMalloc as gpuMalloc, hipMemcpy as gpuMemcpy,
    hipMemcpyKind::hipMemcpyDeviceToHost as gpuMemcpyDeviceToHost,
    hipMemcpyKind::hipMemcpyHostToDevice as gpuMemcpyHostToDevice,
};

// Set a compile-time variable type (this makes things a lot cleaner than having
// a #[cfg(...)] on many struct members).
cfg_if::cfg_if! {
    if #[cfg(feature = "gpu-single")] {
        pub type GpuFloat = f32;
        pub type GpuComplex = num_complex::Complex32;
    } else {
        pub type GpuFloat = f64;
        pub type GpuComplex = num_complex::Complex64;
    }
}

/// A Rust-managed pointer to GPU device memory. When this is dropped,
/// [`gpuFree`] is called on the pointer.
#[derive(Debug)]
pub struct DevicePointer<T> {
    ptr: *mut T,

    /// The number of elements of `T` allocated against the pointer.
    ///
    /// Yeah, I know a pointer isn't an array.
    num_elements: usize,
}

impl<T> DevicePointer<T> {
    /// Allocate a number of bytes on the device.
    ///
    /// # Safety
    ///
    /// This function interfaces directly with the CUDA/HIP API. Rust errors
    /// attempt to catch problems but there are no guarantees.
    #[track_caller]
    pub unsafe fn malloc(size: usize) -> Result<DevicePointer<T>, GpuError> {
        if size == 0 {
            Ok(Self::default())
        } else {
            let mut d_ptr = std::ptr::null_mut();
            gpuMalloc(&mut d_ptr, size);
            check_for_errors(GpuCall::Malloc)?;
            Ok(Self {
                ptr: d_ptr.cast(),
                num_elements: size / std::mem::size_of::<T>(),
            })
        }
    }

    /// Get the number of elements of `T` that have been allocated on the
    /// device. The number of bytes allocated is `num_elements *
    /// std::mem::size_of::<T>()`.
    pub fn get_num_elements(&self) -> usize {
        self.num_elements
    }

    /// Copy a slice of data to the device. Any type is allowed, and the
    /// returned pointer is to the device memory.
    ///
    /// # Safety
    ///
    /// This function interfaces directly with the CUDA/HIP API. Rust errors
    /// attempt to catch problems but there are no guarantees.
    #[track_caller]
    pub unsafe fn copy_to_device(v: &[T]) -> Result<DevicePointer<T>, GpuError> {
        let size = std::mem::size_of_val(v);
        let d_ptr = Self::malloc(size)?;
        gpuMemcpy(
            d_ptr.get_mut() as *mut c_void,
            v.as_ptr().cast(),
            size,
            gpuMemcpyHostToDevice,
        );
        check_for_errors(GpuCall::CopyToDevice)?;
        Ok(d_ptr)
    }

    /// Copy a slice of data from the device. The amount of data copied depends
    /// on the length of `v`, so if in doubt, the size of the device allocation
    /// should be checked with `DevicePointer::get_num_elements`.
    ///
    /// # Safety
    ///
    /// This function interfaces directly with the CUDA/HIP API. Rust errors
    /// attempt to catch problems but there are no guarantees.
    #[track_caller]
    pub unsafe fn copy_from_device(&self, v: &mut [T]) -> Result<(), GpuError> {
        let size = std::mem::size_of_val(v);
        gpuMemcpy(
            v.as_mut_ptr().cast(),
            self.ptr.cast(),
            size,
            gpuMemcpyDeviceToHost,
        );
        check_for_errors(GpuCall::CopyFromDevice)
    }

    /// Overwrite the device memory allocated against this [`DevicePointer`]
    /// with new memory. The amount of memory `v` must match what is allocated
    /// on against this [`DevicePointer`].
    ///
    /// # Safety
    ///
    /// This function interfaces directly with the CUDA/HIP API. Rust errors attempt
    /// to catch problems but there are no guarantees.
    #[track_caller]
    pub unsafe fn overwrite(&mut self, v: &[T]) -> Result<(), GpuError> {
        if v.len() != self.num_elements {
            return Err(GpuError::SizeMismatch);
        }
        let size = std::mem::size_of_val(v);
        gpuMemcpy(
            self.get_mut() as *mut c_void,
            v.as_ptr().cast(),
            size,
            gpuMemcpyHostToDevice,
        );
        check_for_errors(GpuCall::CopyToDevice)
    }

    /// Get a const pointer to the device memory.
    pub fn get(&self) -> *const T {
        self.ptr as *const T
    }

    /// Get a mutable pointer to the device memory.
    pub fn get_mut(&self) -> *mut T {
        self.ptr
    }
}

impl<T> Drop for DevicePointer<T> {
    fn drop(&mut self) {
        unsafe {
            gpuFree(self.ptr.cast());
        }
    }
}

impl<T> Default for DevicePointer<T> {
    fn default() -> Self {
        Self {
            ptr: std::ptr::null_mut(),
            num_elements: 0,
        }
    }
}

#[derive(Error, Debug)]
pub enum GpuError {
    #[error("When overwriting, the new amount of memory did not equal the old amount")]
    SizeMismatch,

    #[cfg(feature = "cuda")]
    #[error("{file}:{line}: cudaMemcpy to device failed: {msg}")]
    CopyToDevice {
        msg: Box<str>,
        file: &'static str,
        line: u32,
    },

    #[cfg(feature = "hip")]
    #[error("{file}:{line}: hipMemcpy to device failed: {msg}")]
    CopyToDevice {
        msg: Box<str>,
        file: &'static str,
        line: u32,
    },

    #[cfg(feature = "cuda")]
    #[error("{file}:{line}: cudaMemcpy from device failed: {msg}")]
    CopyFromDevice {
        msg: Box<str>,
        file: &'static str,
        line: u32,
    },

    #[cfg(feature = "hip")]
    #[error("{file}:{line}: hipMemcpy from device failed: {msg}")]
    CopyFromDevice {
        msg: Box<str>,
        file: &'static str,
        line: u32,
    },

    #[cfg(feature = "cuda")]
    #[error("{file}:{line}: cudaMalloc error: {msg}")]
    Malloc {
        msg: Box<str>,
        file: &'static str,
        line: u32,
    },

    #[cfg(feature = "hip")]
    #[error("{file}:{line}: hipMalloc error: {msg}")]
    Malloc {
        msg: Box<str>,
        file: &'static str,
        line: u32,
    },

    #[cfg(feature = "cuda")]
    #[error("{file}:{line}: CUDA kernel error: {msg}")]
    Kernel {
        msg: Box<str>,
        file: &'static str,
        line: u32,
    },

    #[cfg(feature = "hip")]
    #[error("{file}:{line}: HIP kernel error: {msg}")]
    Kernel {
        msg: Box<str>,
        file: &'static str,
        line: u32,
    },
}

#[derive(Clone, Copy)]
pub enum GpuCall {
    Malloc,
    CopyToDevice,
    CopyFromDevice,
}

/// Run [`gpuGetLastError`] and [`gpuDeviceSynchronize`]. If either of these
/// calls return an error, it is converted to a Rust error and returned from
/// this function. The single argument describes what the just-performed
/// operation was and makes the returned error a helpful one.
///
/// # Safety
///
/// This function interfaces directly with the CUDA/HIP API. Rust errors attempt
/// to catch problems but there are no guarantees.
#[track_caller]
unsafe fn check_for_errors(gpu_call: GpuCall) -> Result<(), GpuError> {
    // Only do a device sync if we're in debug mode, for performance.
    let debug_mode = matches!(std::env::var("DEBUG").as_deref(), Ok("true"));
    if debug_mode {
        let code = gpuDeviceSynchronize();
        if code != gpuSuccess {
            let c_str = CStr::from_ptr(gpuGetErrorString(code));
            let msg = c_str.to_str();
            #[cfg(feature = "cuda")]
            let msg = msg.unwrap_or("<cannot read CUDA error string>");
            #[cfg(feature = "hip")]
            let msg = msg.unwrap_or("<cannot read HIP error string>");
            let location = Location::caller();
            return Err(match gpu_call {
                GpuCall::Malloc => GpuError::Malloc {
                    msg: msg.into(),
                    file: location.file(),
                    line: location.line(),
                },

                GpuCall::CopyToDevice => GpuError::CopyToDevice {
                    msg: msg.into(),
                    file: location.file(),
                    line: location.line(),
                },

                GpuCall::CopyFromDevice => GpuError::CopyFromDevice {
                    msg: msg.into(),
                    file: location.file(),
                    line: location.line(),
                },
            });
        }
    }

    let code = gpuGetLastError();
    if code != gpuSuccess {
        let c_str = CStr::from_ptr(gpuGetErrorString(code));
        let msg = c_str.to_str();
        #[cfg(feature = "cuda")]
        let msg = msg.unwrap_or("<cannot read CUDA error string>");
        #[cfg(feature = "hip")]
        let msg = msg.unwrap_or("<cannot read HIP error string>");
        let location = Location::caller();
        return Err(match gpu_call {
            GpuCall::Malloc => GpuError::Malloc {
                msg: msg.into(),
                file: location.file(),
                line: location.line(),
            },
            GpuCall::CopyToDevice => GpuError::CopyToDevice {
                msg: msg.into(),
                file: location.file(),
                line: location.line(),
            },
            GpuCall::CopyFromDevice => GpuError::CopyFromDevice {
                msg: msg.into(),
                file: location.file(),
                line: location.line(),
            },
        });
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use ndarray::Array1;
    use serial_test::serial;

    #[test]
    fn copy_to_and_from_device_succeeds() {
        unsafe {
            let heap = vec![0_u32; 100];
            let result = DevicePointer::copy_to_device(&heap);
            assert!(result.is_ok(), "Couldn't copy data to device memory");
            let d_ptr = result.unwrap();
            let mut heap2 = vec![1_u32; d_ptr.get_num_elements()];
            let result = d_ptr.copy_from_device(&mut heap2);
            assert!(result.is_ok(), "Couldn't copy data from device memory");
            result.unwrap();
            assert_abs_diff_eq!(Array1::from(heap), Array1::from(heap2));

            const LEN: usize = 100;
            let stack = [0_u32; LEN];
            let result = DevicePointer::copy_to_device(&stack);
            assert!(result.is_ok(), "Couldn't copy data to device memory");
            let d_ptr = result.unwrap();
            let mut stack2 = [1_u32; LEN];
            let result = d_ptr.copy_from_device(&mut stack2);
            assert!(result.is_ok(), "Couldn't copy data from device memory");
            result.unwrap();
            assert_abs_diff_eq!(Array1::from(stack.to_vec()), Array1::from(stack2.to_vec()));

            // Verify the copy_from_behaviour behaviour that the number of
            // elements copied depends on the host's array length.
            let mut stack3 = [1_u32; 100];
            let result = d_ptr.copy_from_device(&mut stack3[..50]);
            assert!(result.is_ok(), "Couldn't copy data from device memory");
            result.unwrap();
            assert_abs_diff_eq!(Array1::from(stack3[..50].to_vec()), Array1::zeros(50));
            assert_abs_diff_eq!(Array1::from(stack3[50..].to_vec()), Array1::ones(50));
        }
    }

    #[test]
    #[serial]
    fn gpu_malloc_huge_fails() {
        let size = 1024_usize.pow(4); // 1 TB;
        let result: Result<DevicePointer<u8>, GpuError> = unsafe { DevicePointer::malloc(size) };
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        #[cfg(feature = "cuda")]
        assert!(err.ends_with("cudaMalloc error: out of memory"), "{err}");
        #[cfg(feature = "hip")]
        assert!(
            err.contains("hipMalloc error"),
            "Error string wasn't expected; got: {err}"
        );
    }

    #[test]
    fn copy_from_non_existent_pointer_fails() {
        let d_ptr: DevicePointer<u8> = DevicePointer {
            ptr: std::ptr::null_mut::<u8>(),
            num_elements: 1,
        };
        let mut dest = [0; 100];
        let result = unsafe { d_ptr.copy_from_device(&mut dest) };
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        #[cfg(feature = "cuda")]
        assert!(
            err.ends_with("cudaMemcpy from device failed: invalid argument"),
            "Error string wasn't expected; got: {err}"
        );
        #[cfg(feature = "hip")]
        assert!(
            err.contains("hipMemcpy from device failed"),
            "Error string wasn't expected; got: {err}"
        );
    }
}