|
81 | 81 | "| Dense | [`f.data[...] = glb_a`](#dense-global) | [`f.data_local[...] = loc_a`](#dense-local) | [`f.data[glb_idx] = loc_ext_a`](#dense-routed) (routed by global index), or [`f.data[region] = g.data[...]`](#dense-redistribute) (another distributed Devito object) |\n", |
82 | 82 | "| Sparse | [`src.data[...] = glb_a`](#sparse-global) | [`src.data_local[...] = loc_a`](#sparse-local) | [`src.data[:, glb_idx] = loc_ext_a`](#sparse-external) |\n", |
83 | 83 | "\n", |
| 84 | + "A dense right-hand side that is *read by window* rather than held in memory -- a memory mapped array, an HDF5 dataset, any object exposing a `shape` and slicing -- is assigned like a globally replicated one, [`f.data[...] = source`](#dense-windowed), and only the window each rank owns is read from it.\n", |
| 85 | + "\n", |
84 | 86 | "Both dense and sparse objects support *routed* (global-index) assignment: the difference is only which axis carries the global labels. For a dense `Function` the distributed axes are the spatial grid axes, so the labels are grid coordinates; for a sparse object the distributed axis is the sparse-point axis, so the labels are sparse-point numbers.\n", |
85 | 87 | "\n", |
86 | 88 | "### Indexing forms\n", |
|
129 | 131 | "assert np.all(f.data_local == glb_a[f.local_indices])" |
130 | 132 | ] |
131 | 133 | }, |
| 134 | + { |
| 135 | + "cell_type": "markdown", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "<a id=\"dense-windowed\"></a>\n", |
| 139 | + "\n", |
| 140 | + "## Dense data from a value read by window\n", |
| 141 | + "\n", |
| 142 | + "The right-hand side does not have to be in memory. Any value that has a `shape`\n", |
| 143 | + "and can be sliced -- a memory mapped array, an HDF5 dataset, a file reader --\n", |
| 144 | + "is assigned like a globally replicated array, and only the window each rank\n", |
| 145 | + "owns is ever read from it. The global array is therefore never materialized, on\n", |
| 146 | + "any rank, which is what makes a dataset larger than one node's memory usable as\n", |
| 147 | + "an input.\n", |
| 148 | + "\n", |
| 149 | + "The value is asked for exactly one window per rank, the same one `data_local`\n", |
| 150 | + "covers, so no communication and no full read take place." |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": null, |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [], |
| 158 | + "source": [ |
| 159 | + "%%px\n", |
| 160 | + "import numpy as np\n", |
| 161 | + "from devito import Function, Grid\n", |
| 162 | + "\n", |
| 163 | + "\n", |
| 164 | + "class Windowed:\n", |
| 165 | + " \"\"\"A value read by window: stands in for a memory mapped array, an HDF5\n", |
| 166 | + " dataset or a file reader.\"\"\"\n", |
| 167 | + "\n", |
| 168 | + " def __init__(self, array):\n", |
| 169 | + " self.array = array\n", |
| 170 | + " self.shape = array.shape\n", |
| 171 | + " self.reads = []\n", |
| 172 | + "\n", |
| 173 | + " def __getitem__(self, window):\n", |
| 174 | + " self.reads.append(window)\n", |
| 175 | + " return self.array[window]\n", |
| 176 | + "\n", |
| 177 | + "\n", |
| 178 | + "grid = Grid(shape=(4, 4), extent=(3.0, 3.0))\n", |
| 179 | + "f = Function(name=\"f_windowed\", grid=grid)\n", |
| 180 | + "\n", |
| 181 | + "glb_a = np.arange(np.prod(grid.shape), dtype=f.dtype).reshape(grid.shape)\n", |
| 182 | + "source = Windowed(glb_a)\n", |
| 183 | + "f.data[...] = source\n", |
| 184 | + "\n", |
| 185 | + "# One read, of exactly the window this rank owns\n", |
| 186 | + "assert len(source.reads) == 1\n", |
| 187 | + "assert glb_a[source.reads[0]].shape == f.data_local.shape\n", |
| 188 | + "assert np.all(f.data_local == glb_a[f.local_indices])" |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "markdown", |
| 193 | + "metadata": {}, |
| 194 | + "source": [ |
| 195 | + "A sub-region is assigned the same way, and only that part of the value is read:\n", |
| 196 | + "\n", |
| 197 | + "```python\n", |
| 198 | + "f.data[1:7, 1:7] = source # `source.shape == (6, 6)`\n", |
| 199 | + "```\n", |
| 200 | + "\n", |
| 201 | + "This is how a model or a property cube is loaded straight off its file:\n", |
| 202 | + "`initialize_function(f, source, nbl)` assigns through `data`, so it inherits the\n", |
| 203 | + "same behaviour, each rank reading only its own window and the boundary layers\n", |
| 204 | + "being filled from it afterwards." |
| 205 | + ] |
| 206 | + }, |
132 | 207 | { |
133 | 208 | "cell_type": "markdown", |
134 | 209 | "metadata": {}, |
|
0 commit comments