def run_positions_test(self, grid_size, illum_size, data_size, ntime, npol, nchan, polmajor, dtype=numpy.complex64): TEST_OFFSET = 1 gridshape = (ntime, nchan, npol, grid_size, grid_size) ishape = (ntime, nchan, npol, data_size) illum_shape = (ntime, nchan, npol, data_size, illum_size, illum_size) # Create grid and illumination pattern grid = bifrost.zeros(shape=gridshape, dtype=numpy.complex64) illum = self._create_illum(illum_size, data_size, ntime, npol, nchan) # Create data data = self._create_data(data_size, ntime, nchan, npol, dtype=dtype) # Create the locations locs = self._create_locs(data_size, ntime, nchan, npol, illum_size, grid_size - illum_size) # Grid using a naive method gridnaive = self.naive_romein(gridshape, illum, data, locs[0, :] + TEST_OFFSET, locs[1, :] + TEST_OFFSET, locs[2, :] + TEST_OFFSET, ntime, npol, nchan, data_size) # Transpose for non pol-major kernels if not polmajor: data = data.transpose((0, 1, 3, 2)).copy() locs = locs.transpose((0, 1, 2, 4, 3)).copy() illum = illum.transpose((0, 1, 3, 2, 4, 5)).copy() grid = grid.copy(space='cuda') data = data.copy(space='cuda') illum = illum.copy(space='cuda') locs = locs.copy(space='cuda') self.romein.init(locs, illum, grid_size, polmajor=polmajor) # Offset locs = locs.copy(space='system') locs += TEST_OFFSET locs = locs.copy(space='cuda') self.romein.set_positions(locs) self.romein.execute(data, grid) grid = grid.copy(space="system") # Compare the two methods numpy.testing.assert_allclose(grid, gridnaive, 1e-4, 1e-5)
def _create_illum(self, illum_size, data_size, ntime, npol, nchan, dtype=numpy.complex64): illum_shape = (ntime, nchan, npol, data_size, illum_size, illum_size) illum = bifrost.zeros(shape=illum_shape, dtype=dtype) illum += 1 return illum
def naive_romein(self, grid_shape, illum, data, xlocs, ylocs, zlocs, ntime, npol, nchan, ndata, dtype=numpy.complex64): # Unpack the ci4 data to ci8, if needed if data.dtype == ci4: data_unpacked = bifrost.ndarray(shape=data.shape, dtype='ci8') unpack(data, data_unpacked) data = data_unpacked # Create the output grid grid = bifrost.zeros(shape=grid_shape, dtype=dtype) # Combine axes xlocs_flat = xlocs.reshape(-1, ndata) ylocs_flat = ylocs.reshape(-1, ndata) data_flat = data.reshape(-1, ndata) grid_flat = grid.reshape(-1, grid.shape[-2], grid.shape[-1]) illum_flat = illum.reshape(-1, ndata, illum.shape[-2], illum.shape[-1]) # Excruciatingly slow, but it's just for testing purposes... # Could probably use a blas based function for simplicity. for tcp in range(data_flat.shape[0]): for d in range(ndata): datapoint = data_flat[tcp, d] if data.dtype != dtype: try: datapoint = dtype(datapoint[0] + 1j * datapoint[1]) except IndexError: datapoint = dtype(datapoint) #if(d==128): # print(datapoint) x_s = xlocs_flat[tcp, d] y_s = ylocs_flat[tcp, d] for y in range(y_s, y_s + illum_flat.shape[-2]): for x in range(x_s, x_s + illum_flat.shape[-1]): illump = illum_flat[tcp, d, y - y_s, x - x_s] grid_flat[tcp, y, x] += datapoint * illump return grid
def run_contiguous_slice_copy(self, space='system'): a = np.random.rand(2,3,4,5) a = a.astype(np.float64) b = a.transpose(0,3,2,1)[:,1:,:,:].copy() c = bf.zeros(a.shape, dtype=a.dtype, space='system') c[...] = a c = c.copy(space=space) d = c.transpose(0,3,2,1)[:,1:,:,:].copy(space='system') # Use ctypes to directly access the memory b_data = ctypes.cast(b.ctypes.data, ctypes.POINTER(ctypes.c_double)) b_data = np.array([b_data[i] for i in range(b.size)]) d_data = ctypes.cast(d.ctypes.data, ctypes.POINTER(ctypes.c_double)) d_data = np.array([d_data[i] for i in range(d.size)]) np.testing.assert_equal(d_data, b_data)
def _create_data(self, data_size, ntime, nchan, npol, dtype=numpy.complex64): ishape = (ntime, nchan, npol, data_size) data = bifrost.zeros(shape=ishape, dtype=numpy.complex64) data.real[...] = numpy.random.normal(0, 1.0, size=ishape) data.imag[...] = numpy.random.normal(0, 1.0, size=ishape) data *= 7 if dtype not in (numpy.complex64, 'cf32'): data_quantized = bifrost.ndarray(shape=data.shape, dtype=dtype) quantize(data, data_quantized) data = data_quantized return data