|
| 1 | +""" |
| 2 | +This file is part of the openPMD-api. |
| 3 | +
|
| 4 | +Copyright 2021 openPMD contributors |
| 5 | +Authors: Axel Huebl |
| 6 | +License: LGPLv3+ |
| 7 | +""" |
| 8 | +import math |
| 9 | +import numpy as np |
| 10 | +try: |
| 11 | + from dask.array import from_array |
| 12 | + found_dask = True |
| 13 | +except ImportError: |
| 14 | + found_dask = False |
| 15 | + |
| 16 | + |
| 17 | +class DaskRecordComponent: |
| 18 | + # shape, .ndim, .dtype and support numpy-style slicing |
| 19 | + def __init__(self, record_component): |
| 20 | + self.rc = record_component |
| 21 | + |
| 22 | + @property |
| 23 | + def shape(self): |
| 24 | + # fixme: https://github.com/openPMD/openPMD-api/issues/808 |
| 25 | + return tuple(self.rc.shape) |
| 26 | + |
| 27 | + @property |
| 28 | + def ndim(self): |
| 29 | + return self.rc.ndim |
| 30 | + |
| 31 | + @property |
| 32 | + def dtype(self): |
| 33 | + return self.rc.dtype |
| 34 | + |
| 35 | + def __getitem__(self, slices): |
| 36 | + """here we support what Record_Component implements: a tuple of slices, |
| 37 | + a slice or an index; we do not support fancy indexing |
| 38 | + """ |
| 39 | + # FIXME: implement handling of zero-slices in Record_Component |
| 40 | + # https://github.com/openPMD/openPMD-api/issues/957 |
| 41 | + all_zero = True |
| 42 | + for s in slices: |
| 43 | + if s != np.s_[0:0]: |
| 44 | + all_zero = False |
| 45 | + if all_zero: |
| 46 | + return np.array([], dtype=self.dtype) |
| 47 | + |
| 48 | + data = self.rc[slices] |
| 49 | + self.rc.series_flush() |
| 50 | + if not math.isclose(1.0, self.rc.unit_SI): |
| 51 | + data = np.multiply(data, self.rc.unit_SI) |
| 52 | + |
| 53 | + return data |
| 54 | + |
| 55 | + |
| 56 | +def record_component_to_daskarray(record_component): |
| 57 | + """ |
| 58 | + Load a RecordComponent into a Dask.array. |
| 59 | +
|
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + record_component : openpmd_api.Record_Component |
| 63 | + A record component class in openPMD-api. |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + dask.array |
| 68 | + A dask array. |
| 69 | +
|
| 70 | + Raises |
| 71 | + ------ |
| 72 | + ImportError |
| 73 | + Raises an exception if dask is not installed |
| 74 | +
|
| 75 | + See Also |
| 76 | + -------- |
| 77 | + openpmd_api.BaseRecordComponent.available_chunks : available chunks that |
| 78 | + are used internally to parallelize reading |
| 79 | + dask.array : the (potentially distributed) array object created here |
| 80 | + """ |
| 81 | + if not found_dask: |
| 82 | + raise ImportError("dask NOT found. Install dask for Dask DataFrame " |
| 83 | + "support.") |
| 84 | + |
| 85 | + # get optimal chunks |
| 86 | + chunks = record_component.available_chunks() |
| 87 | + |
| 88 | + # sort and prepare the chunks for Dask's array API |
| 89 | + # https://docs.dask.org/en/latest/array-chunks.html |
| 90 | + # https://docs.dask.org/en/latest/array-api.html?highlight=from_array#other-functions |
| 91 | + # sorted and unique |
| 92 | + offsets_per_dim = list(map(list, zip(*[chunk.offset for chunk in chunks]))) |
| 93 | + offsets_sorted_unique_per_dim = [sorted(set(o)) for o in offsets_per_dim] |
| 94 | + |
| 95 | + # print("offsets_sorted_unique_per_dim=", |
| 96 | + # list(offsets_sorted_unique_per_dim)) |
| 97 | + |
| 98 | + # case 1: PIConGPU static load balancing (works with Dask assumptions, |
| 99 | + # chunk option no. 3) |
| 100 | + # all chunks in the same column have the same column width although |
| 101 | + # individual columns have different widths |
| 102 | + # case 2: AMReX boxes |
| 103 | + # all chunks are multiple of a common block size, offsets are a multiple |
| 104 | + # of a common blocksize |
| 105 | + # problem: too limited description in Dask |
| 106 | + # https://github.com/dask/dask/issues/7475 |
| 107 | + # work-around: create smaller chunks (this incurs a read cost) by forcing |
| 108 | + # into case 1 |
| 109 | + # (this can lead to larger blocks than using the gcd of the |
| 110 | + # extents aka AMReX block size) |
| 111 | + common_chunk_widths_per_dim = list() |
| 112 | + for d, offsets_in_dim in enumerate(offsets_sorted_unique_per_dim): |
| 113 | + # print("d=", d, offsets_in_dim, record_component.shape[d]) |
| 114 | + offsets_in_dim_arr = np.array(offsets_in_dim) |
| 115 | + # note: this is in the right order of rows/columns, contrary to a |
| 116 | + # sorted extent list from chunks |
| 117 | + extents_in_dim = np.zeros_like(offsets_in_dim_arr) |
| 118 | + extents_in_dim[:-1] = offsets_in_dim_arr[1:] |
| 119 | + extents_in_dim[-1] = record_component.shape[d] |
| 120 | + if len(extents_in_dim) > 1: |
| 121 | + extents_in_dim[:-1] -= offsets_in_dim_arr[:-1] |
| 122 | + extents_in_dim[-1] -= offsets_in_dim_arr[-1] |
| 123 | + # print("extents_in_dim=", extents_in_dim) |
| 124 | + common_chunk_widths_per_dim.append(tuple(extents_in_dim)) |
| 125 | + |
| 126 | + common_chunk_widths_per_dim = tuple(common_chunk_widths_per_dim) |
| 127 | + # print("common_chunk_widths_per_dim=", common_chunk_widths_per_dim) |
| 128 | + |
| 129 | + da = from_array( |
| 130 | + DaskRecordComponent(record_component), |
| 131 | + chunks=common_chunk_widths_per_dim, |
| 132 | + # name=None, |
| 133 | + asarray=True, |
| 134 | + fancy=False, |
| 135 | + # getitem=None, |
| 136 | + # meta=None, |
| 137 | + # inline_array=False |
| 138 | + ) |
| 139 | + |
| 140 | + return da |
0 commit comments