def test_alignment_of_different_layouts(): offset = 1 byte_offset = 8 for tries in range( 16 ): # try a few times, since we might get lucky and get randomly a correct alignment arr = create_numpy_array_with_layout((3, 4, 5), layout=(0, 1, 2), alignment=True, byte_offset=byte_offset) assert is_aligned(arr[offset, ...], 8 * 4, byte_offset) arr = create_numpy_array_with_layout((3, 4, 5), layout=(2, 1, 0), alignment=True, byte_offset=byte_offset) assert is_aligned(arr[..., offset], 8 * 4, byte_offset) arr = create_numpy_array_with_layout((3, 4, 5), layout=(2, 0, 1), alignment=True, byte_offset=byte_offset) assert is_aligned(arr[:, 0, :], 8 * 4, byte_offset)
def add_ghost_layers(arr, index_dimensions=0, ghost_layers=1, layout=None): dimensions = len(arr.shape) spatial_dimensions = dimensions - index_dimensions new_shape = [e + 2 * ghost_layers for e in arr.shape[:spatial_dimensions] ] + list(arr.shape[spatial_dimensions:]) if layout is None: layout = get_layout_of_array(arr) result = create_numpy_array_with_layout(new_shape, layout) result.fill(0.0) indexing = [ slice(ghost_layers, -ghost_layers, None), ] * spatial_dimensions indexing += [slice(None, None, None)] * index_dimensions result[tuple(indexing)] = arr return result
def create_pdf_array(size, num_directions, ghost_layers=1, layout='fzyx'): """Creates an empty numpy array for a pdf field with the specified memory layout. Examples: >>> create_pdf_array((3, 4, 5), 9, layout='zyxf', ghost_layers=0).shape (3, 4, 5, 9) >>> create_pdf_array((3, 4, 5), 9, layout='zyxf', ghost_layers=0).strides (72, 216, 864, 8) >>> create_pdf_array((3, 4), 9, layout='zyxf', ghost_layers=1).shape (5, 6, 9) >>> create_pdf_array((3, 4), 9, layout='zyxf', ghost_layers=1).strides (72, 360, 8) """ size_with_gl = [s + 2 * ghost_layers for s in size] dim = len(size) if isinstance(layout, str): layout = layout_string_to_tuple(layout, dim + 1) return create_numpy_array_with_layout(size_with_gl + [num_directions], layout)
def _generate_fields(dt=np.uint64, num_directions=1, layout='numpy'): field_sizes = FIELD_SIZES if num_directions > 1: field_sizes = [s + (num_directions, ) for s in field_sizes] fields = [] for size in field_sizes: field_layout = layout_string_to_tuple(layout, len(size)) src_arr = create_numpy_array_with_layout(size, field_layout, dtype=dt) array_data = np.reshape(np.arange(1, int(np.prod(size) + 1)), size) # Use flat iterator to input data into the array src_arr.flat = add_ghost_layers( array_data, index_dimensions=1 if num_directions > 1 else 0).astype(dt).flat dst_arr = np.zeros(src_arr.shape, dtype=dt) buffer_arr = np.zeros(np.prod(src_arr.shape), dtype=dt) fields.append((src_arr, dst_arr, buffer_arr)) return fields
def _generate_fields(dt=np.uint8, stencil_directions=1, layout='numpy'): pytest.importorskip('pycuda') field_sizes = FIELD_SIZES if stencil_directions > 1: field_sizes = [s + (stencil_directions, ) for s in field_sizes] fields = [] for size in field_sizes: field_layout = layout_string_to_tuple(layout, len(size)) src_arr = create_numpy_array_with_layout(size, field_layout).astype(dt) array_data = np.reshape(np.arange(1, int(np.prod(size) + 1)), size) # Use flat iterator to input data into the array src_arr.flat = add_ghost_layers( array_data, index_dimensions=1 if stencil_directions > 1 else 0).astype(dt).flat gpu_src_arr = gpuarray.to_gpu(src_arr) gpu_dst_arr = gpuarray.empty_like(gpu_src_arr) size = int(np.prod(src_arr.shape)) gpu_buffer_arr = gpuarray.zeros(size, dtype=dt) fields.append((src_arr, gpu_src_arr, gpu_dst_arr, gpu_buffer_arr)) return fields
def add_array(self, name, values_per_cell=1, dtype=np.float64, latex_name=None, ghost_layers=None, layout=None, cpu=True, gpu=None, alignment=False, field_type=FieldType.GENERIC): if ghost_layers is None: ghost_layers = self.default_ghost_layers if layout is None: layout = self.default_layout if gpu is None: gpu = self.default_target in self._GPU_LIKE_TARGETS kwargs = { 'shape': tuple(s + 2 * ghost_layers for s in self._domainSize), 'dtype': dtype, } if not hasattr(values_per_cell, '__len__'): values_per_cell = (values_per_cell, ) if len(values_per_cell) == 1 and values_per_cell[0] == 1: values_per_cell = () self._field_information[name] = { 'ghost_layers': ghost_layers, 'values_per_cell': values_per_cell, 'layout': layout, 'dtype': dtype, 'alignment': alignment, 'field_type': field_type, } index_dimensions = len(values_per_cell) kwargs['shape'] = kwargs['shape'] + values_per_cell if index_dimensions > 0: layout_tuple = layout_string_to_tuple(layout, self.dim + index_dimensions) else: layout_tuple = spatial_layout_string_to_tuple(layout, self.dim) # cpu_arr is always created - since there is no create_pycuda_array_with_layout() byte_offset = ghost_layers * np.dtype(dtype).itemsize cpu_arr = create_numpy_array_with_layout(layout=layout_tuple, alignment=alignment, byte_offset=byte_offset, **kwargs) if alignment and gpu: raise NotImplementedError("Alignment for GPU fields not supported") if cpu: if name in self.cpu_arrays: raise ValueError("CPU Field with this name already exists") self.cpu_arrays[name] = cpu_arr if gpu: if name in self.gpu_arrays: raise ValueError("GPU Field with this name already exists") self.gpu_arrays[name] = self.array_handler.to_gpu(cpu_arr) assert all(f.name != name for f in self.fields.values() ), "Symbolic field with this name already exists" self.fields[name] = Field.create_from_numpy_array( name, cpu_arr, index_dimensions=index_dimensions, field_type=field_type) self.fields[name].latex_name = latex_name return self.fields[name]