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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// 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/.

//! GPU code to implement the MWA analytic beam.

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

// Include Rust bindings to the GPU code, depending on the precision used.
#[cfg(feature = "gpu-single")]
include!("single.rs");
#[cfg(not(feature = "gpu-single"))]
include!("double.rs");

#[cfg(test)]
mod tests;

use std::{
    collections::hash_map::DefaultHasher,
    convert::TryInto,
    ffi::CStr,
    hash::{Hash, Hasher},
};

use marlu::{AzEl, Jones};
use ndarray::prelude::*;

use super::{delay_ints_to_floats, reorder_to_rts, AnalyticBeam, AnalyticBeamError};
use crate::gpu::{DevicePointer, GpuError, GpuFloat};

/// A GPU beam object ready to calculate beam responses.
pub struct AnalyticBeamGpu {
    analytic_type: super::AnalyticType,
    dipole_height: GpuFloat,
    bowties_per_row: u8,

    d_delays: DevicePointer<GpuFloat>,
    d_amps: DevicePointer<GpuFloat>,

    /// The number of unique tiles according to the delays and amps.
    pub(super) num_unique_tiles: i32,

    /// This is used to access de-duplicated Jones matrices.
    tile_map: Vec<i32>,

    /// The device pointer to the `tile_map` (same as the host's memory
    /// equivalent above).
    d_tile_map: DevicePointer<i32>,
}

impl AnalyticBeamGpu {
    /// Prepare a GPU-capable device for beam-response computations given the
    /// frequencies, delays and amps to be used. The resulting object takes
    /// directions and computes the beam responses on the device.
    ///
    /// This function is intentionally kept private. Use
    /// [`AnalyticBeam::gpu_prepare`] to create a `AnalyticBeamGpu`.
    pub(super) unsafe fn new(
        analytic_beam: &AnalyticBeam,
        delays_array: ArrayView2<u32>,
        amps_array: ArrayView2<f64>,
    ) -> Result<AnalyticBeamGpu, AnalyticBeamError> {
        let num_bowties =
            usize::from(analytic_beam.bowties_per_row * analytic_beam.bowties_per_row);
        if delays_array.len_of(Axis(1)) != num_bowties {
            return Err(AnalyticBeamError::IncorrectDelaysArrayColLength {
                rows: delays_array.len_of(Axis(0)),
                num_delays: delays_array.len_of(Axis(1)),
                expected: num_bowties,
            });
        }
        if amps_array.len_of(Axis(1)) != num_bowties
            && amps_array.len_of(Axis(1)) != num_bowties * 2
        {
            return Err(AnalyticBeamError::IncorrectAmpsLength {
                got: amps_array.len_of(Axis(1)),
                expected1: num_bowties,
                expected2: num_bowties * 2,
            });
        }

        // Determine the unique tiles according to the gains and delays. Unlike
        // FEE, all frequencies give different results, so there's no need to
        // consider them.
        let mut unique_tiles = vec![];
        let mut tile_map = vec![];
        let mut i_tile = 0;
        let mut unique_delays = vec![];
        let mut unique_amps = vec![];
        for (delays, amps) in delays_array.outer_iter().zip(amps_array.outer_iter()) {
            let mut unique_tile_hasher = DefaultHasher::new();
            delays.hash(&mut unique_tile_hasher);
            // We can't hash f64 values, but we can hash their bits.
            for amp in amps {
                amp.to_bits().hash(&mut unique_tile_hasher);
            }
            let unique_tile_hash = unique_tile_hasher.finish();

            let (amps, delays) = fix_amps_ndarray(amps, delays);
            let (amps, delays) = if matches!(analytic_beam.beam_type, super::AnalyticType::Rts) {
                reorder_to_rts(&amps, &delays)
            } else {
                (amps.to_vec(), delay_ints_to_floats(&delays))
            };

            let this_tile_index = if let Some((index, _)) = unique_tiles
                .iter()
                .enumerate()
                .find(|(_, t)| **t == unique_tile_hash)
            {
                index.try_into().expect("smaller than i32::MAX")
            } else {
                unique_tiles.push(unique_tile_hash);
                unique_delays.extend(delays.iter().copied().map(|d| d as GpuFloat));
                unique_amps.extend(amps.iter().map(|&f| f as GpuFloat));
                i_tile += 1;
                i_tile - 1
            };
            tile_map.push(this_tile_index);
        }

        let d_tile_map = DevicePointer::copy_to_device(&tile_map)?;
        Ok(AnalyticBeamGpu {
            analytic_type: analytic_beam.beam_type,
            dipole_height: analytic_beam.dipole_height as GpuFloat,
            bowties_per_row: analytic_beam.bowties_per_row,
            d_delays: DevicePointer::copy_to_device(&unique_delays)?,
            d_amps: DevicePointer::copy_to_device(&unique_amps)?,
            num_unique_tiles: unique_tiles
                .len()
                .try_into()
                .expect("smaller than i32::MAX"),
            tile_map,
            d_tile_map,
        })
    }

    /// Given directions, calculate beam-response Jones matrices on the device
    /// and return a pointer to them.
    ///
    /// Note that this function needs to allocate two vectors for azimuths and
    /// zenith angles from the supplied `azels`.
    pub fn calc_jones_device(
        &self,
        azels: &[AzEl],
        freqs_hz: &[u32],
        latitude_rad: f64,
        norm_to_zenith: bool,
    ) -> Result<DevicePointer<Jones<GpuFloat>>, AnalyticBeamError> {
        unsafe {
            // Allocate a buffer on the device for results.
            let d_results = DevicePointer::malloc(
                self.num_unique_tiles as usize
                    * freqs_hz.len()
                    * azels.len()
                    * std::mem::size_of::<Jones<GpuFloat>>(),
            )?;

            // Also copy the directions to the device.
            let (azs, zas): (Vec<GpuFloat>, Vec<GpuFloat>) = azels
                .iter()
                .map(|&azel| (azel.az as GpuFloat, azel.za() as GpuFloat))
                .unzip();
            let d_azs = DevicePointer::copy_to_device(&azs)?;
            let d_zas = DevicePointer::copy_to_device(&zas)?;
            let d_freqs = DevicePointer::copy_to_device(freqs_hz)?;

            self.calc_jones_device_pair_inner(
                d_azs.get(),
                d_zas.get(),
                azels.len().try_into().expect("much fewer than i32::MAX"),
                d_freqs.get(),
                freqs_hz.len().try_into().expect("much fewer than i32::MAX"),
                latitude_rad as GpuFloat,
                norm_to_zenith,
                d_results.get_mut() as *mut std::ffi::c_void,
            )?;
            Ok(d_results)
        }
    }

    /// Given directions, calculate beam-response Jones matrices on the device
    /// and return a pointer to them.
    pub fn calc_jones_device_pair(
        &self,
        az_rad: &[GpuFloat],
        za_rad: &[GpuFloat],
        freqs_hz: &[u32],
        latitude_rad: GpuFloat,
        norm_to_zenith: bool,
    ) -> Result<DevicePointer<Jones<GpuFloat>>, AnalyticBeamError> {
        unsafe {
            // Allocate a buffer on the device for results.
            let d_results = DevicePointer::malloc(
                self.num_unique_tiles as usize
                    * freqs_hz.len()
                    * az_rad.len()
                    * std::mem::size_of::<Jones<GpuFloat>>(),
            )?;

            // Also copy the directions to the device.
            let d_azs = DevicePointer::copy_to_device(az_rad)?;
            let d_zas = DevicePointer::copy_to_device(za_rad)?;
            let d_freqs = DevicePointer::copy_to_device(freqs_hz)?;

            self.calc_jones_device_pair_inner(
                d_azs.get(),
                d_zas.get(),
                az_rad.len().try_into().expect("much fewer than i32::MAX"),
                d_freqs.get(),
                freqs_hz.len().try_into().expect("much fewer than i32::MAX"),
                latitude_rad,
                norm_to_zenith,
                d_results.get_mut() as *mut std::ffi::c_void,
            )?;
            Ok(d_results)
        }
    }

    /// Given directions, calculate beam-response Jones matrices
    /// into the supplied pre-allocated device pointer. This buffer
    /// should have a shape of (`num_unique_tiles`, `num_freqs`,
    /// `az_rad_length`). The number of unique tiles can be accessed with
    /// [`AnalyticBeamGpu::get_num_unique_tiles`]. `d_latitude_rad` is
    /// populated with the array latitude, if the caller wants the parallactic-
    /// angle correction to be applied. If the pointer is null, then no
    /// correction is applied.
    ///
    /// # Safety
    ///
    /// If `d_results` is too small (correct size described above), then
    /// undefined behaviour looms.
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn calc_jones_device_pair_inner(
        &self,
        d_az_rad: *const GpuFloat,
        d_za_rad: *const GpuFloat,
        num_directions: i32,
        d_freqs_hz: *const u32,
        num_freqs: i32,
        latitude_rad: GpuFloat,
        norm_to_zenith: bool,
        d_results: *mut std::ffi::c_void,
    ) -> Result<(), AnalyticBeamError> {
        // Don't do anything if there aren't any directions.
        if num_directions == 0 {
            return Ok(());
        }

        // The return value is a pointer to a CUDA/HIP error string. If it's
        // null then everything is fine.
        let error_message_ptr = gpu_analytic_calc_jones(
            match self.analytic_type {
                super::AnalyticType::MwaPb => ANALYTIC_TYPE_MWA_PB,
                super::AnalyticType::Rts => ANALYTIC_TYPE_RTS,
            },
            self.dipole_height,
            d_az_rad,
            d_za_rad,
            num_directions,
            d_freqs_hz,
            num_freqs,
            self.d_delays.get(),
            self.d_amps.get(),
            self.num_unique_tiles,
            latitude_rad,
            norm_to_zenith as _,
            self.bowties_per_row,
            d_results,
        );
        if error_message_ptr.is_null() {
            Ok(())
        } else {
            let error_message = CStr::from_ptr(error_message_ptr)
                .to_str()
                .unwrap_or("<cannot read GPU error string>");
            let our_error_str =
                format!("analytic.h:analytic_calc_jones_gpu failed with: {error_message}");
            Err(AnalyticBeamError::Gpu(GpuError::Kernel {
                msg: our_error_str.into(),
                file: file!(),
                line: line!(),
            }))
        }
    }

    /// Given directions, calculate beam-response Jones matrices on the device,
    /// copy them to the host, and free the device memory. The returned array
    /// is "expanded"; tile and frequency de-duplication is undone to give
    /// an array with the same number of tiles as was specified when this
    /// [`AnalyticBeamGpu`] was created and frequencies specified to this
    /// function.
    ///
    /// Note that this function needs to allocate two vectors for azimuths and
    /// zenith angles from the supplied `azels`.
    pub fn calc_jones(
        &self,
        azels: &[AzEl],
        freqs_hz: &[u32],
        latitude_rad: f64,
        norm_to_zenith: bool,
    ) -> Result<Array3<Jones<GpuFloat>>, AnalyticBeamError> {
        let mut results = Array3::from_elem(
            (self.tile_map.len(), freqs_hz.len(), azels.len()),
            Jones::default(),
        );

        let (azs, zas): (Vec<GpuFloat>, Vec<GpuFloat>) = azels
            .iter()
            .map(|&azel| (azel.az as GpuFloat, azel.za() as GpuFloat))
            .unzip();
        self.calc_jones_pair_inner(
            &azs,
            &zas,
            freqs_hz,
            latitude_rad as GpuFloat,
            norm_to_zenith,
            results.view_mut(),
        )?;
        Ok(results)
    }

    /// Given directions, calculate beam-response Jones matrices on the device,
    /// copy them to the host, and free the device memory. The returned array
    /// is "expanded"; tile and frequency de-duplication is undone to give
    /// an array with the same number of tiles as was specified when this
    /// [`AnalyticBeamGpu`] was created and frequencies specified to this
    /// function.
    pub fn calc_jones_pair(
        &self,
        az_rad: &[GpuFloat],
        za_rad: &[GpuFloat],
        freqs_hz: &[u32],
        latitude_rad: GpuFloat,
        norm_to_zenith: bool,
    ) -> Result<Array3<Jones<GpuFloat>>, AnalyticBeamError> {
        let mut results = Array3::from_elem(
            (self.tile_map.len(), freqs_hz.len(), az_rad.len()),
            Jones::default(),
        );

        self.calc_jones_pair_inner(
            az_rad,
            za_rad,
            freqs_hz,
            latitude_rad,
            norm_to_zenith,
            results.view_mut(),
        )?;
        Ok(results)
    }

    /// Given directions, calculate beam-response Jones matrices on the device,
    /// copy them to the host, and free the device memory. This function is
    /// the same as [`AnalyticBeamGpu::calc_jones_pair`], but the results are
    /// stored in a pre-allocated array. This array should have a shape of
    /// (`total_num_tiles`, `total_num_freqs`, `az_rad_length`). The first
    /// dimension can be accessed with `AnalyticBeamGpu::get_total_num_tiles`.
    pub fn calc_jones_pair_inner(
        &self,
        az_rad: &[GpuFloat],
        za_rad: &[GpuFloat],
        freqs_hz: &[u32],
        latitude_rad: GpuFloat,
        norm_to_zenith: bool,
        mut results: ArrayViewMut3<Jones<GpuFloat>>,
    ) -> Result<(), AnalyticBeamError> {
        // Allocate an array matching the deduplicated device memory.
        let mut dedup_results: Array3<Jones<GpuFloat>> = Array3::from_elem(
            (self.num_unique_tiles as usize, freqs_hz.len(), az_rad.len()),
            Jones::default(),
        );
        // Calculate the beam responses. and copy them to the host.
        let device_ptr =
            self.calc_jones_device_pair(az_rad, za_rad, freqs_hz, latitude_rad, norm_to_zenith)?;
        unsafe {
            device_ptr.copy_from_device(dedup_results.as_slice_mut().expect("is contiguous"))?;
        }
        // Free the device memory.
        drop(device_ptr);

        // Expand the results according to the map.
        results
            .outer_iter_mut()
            .zip(self.tile_map.iter())
            .for_each(|(mut jones_row, &i_row)| {
                let i_row: usize = i_row.try_into().expect("is a positive int");
                jones_row.assign(&dedup_results.slice(s![i_row, .., ..]));
            });
        Ok(())
    }

    /// Get the number of tiles that this [`AnalyticBeamGpu`] applies to.
    pub fn get_total_num_tiles(&self) -> usize {
        self.tile_map.len()
    }

    /// Get a pointer to the tile map associated with this
    /// [`AnalyticBeamGpu`]. This is necessary to access de-duplicated beam
    /// Jones matrices.
    pub fn get_tile_map(&self) -> *const i32 {
        self.tile_map.as_ptr()
    }

    /// Get a pointer to the device tile map associated with this
    /// [`AnalyticBeamGpu`]. This is necessary to access de-duplicated beam
    /// Jones matrices on the device.
    pub fn get_device_tile_map(&self) -> *const i32 {
        self.d_tile_map.get()
    }

    /// Get the number of de-duplicated tiles associated with this
    /// [`AnalyticBeamGpu`].
    pub fn get_num_unique_tiles(&self) -> i32 {
        self.num_unique_tiles
    }
}

/// Ensure that any delays of 32 have an amplitude (dipole gain) of 0. The
/// results are bad otherwise! Also ensure that we have 32 dipole gains (amps)
/// here. Also return a Rust array of delays for convenience.
pub(super) fn fix_amps_ndarray(
    amps: ArrayView1<f64>,
    delays: ArrayView1<u32>,
) -> (Vec<f64>, Vec<u32>) {
    // The lengths of `amps` and `delays` should be checked before calling this
    // functions; the asserts are a last resort guard.
    assert!(amps.len() == delays.len() || amps.len() == delays.len() * 2);

    let mut fixed_amps = vec![0.0; delays.len()];
    fixed_amps
        .iter_mut()
        .zip(amps.iter())
        .zip(delays.iter().cycle())
        .for_each(|((out_amp, &in_amp), &delay)| {
            if delay == 32 {
                *out_amp = 0.0;
            } else {
                *out_amp = in_amp;
            }
        });
    if amps.len() == delays.len() * 2 {
        fixed_amps
            .iter_mut()
            .zip(amps.iter().skip(delays.len()))
            .for_each(|(fixed, &amp)| {
                *fixed = fixed.min(amp);
            });
    }

    // So that we don't have to do .as_slice().unwrap() on our ndarrays outside
    // of this function, return a Rust array of delays here.
    let mut delays_a = vec![0; delays.len()];
    delays_a.iter_mut().zip(delays).for_each(|(da, d)| *da = *d);

    (fixed_amps, delays_a)
}