|
| 1 | +import numpy as np |
| 2 | +import cupy as cp |
| 3 | +from adios2 import FileReader |
| 4 | +import adios2.bindings as adios2 |
| 5 | + |
| 6 | +def write_array(fileName, nSteps, gpuArray, cpuArray): |
| 7 | + adios = adios2.ADIOS() |
| 8 | + ioWriter = adios.DeclareIO("cupyWriter") |
| 9 | + # define adios variables, the cpuArray is used for both variables to |
| 10 | + # define the type of the variables (float32 in this case) |
| 11 | + gpuVar = ioWriter.DefineVariable("gpuArray", cpuArray, gpuArray.shape, |
| 12 | + [0] * len(gpuArray.shape), gpuArray.shape) |
| 13 | + # optionally the memory space can be set to GPU |
| 14 | + gpuVar.SetMemorySpace(adios2.MemorySpace.GPU) |
| 15 | + cpuVar = ioWriter.DefineVariable("cpuArray", cpuArray, cpuArray.shape, |
| 16 | + [0] * len(cpuArray.shape), cpuArray.shape) |
| 17 | + |
| 18 | + # write both cpu and gpu arrays for each simulation step |
| 19 | + wStream = ioWriter.Open(fileName, adios2.Mode.Write) |
| 20 | + for step in range(nSteps): |
| 21 | + # write buffers |
| 22 | + wStream.BeginStep() |
| 23 | + wStream.Put(cpuVar, cpuArray) |
| 24 | + wStream.Put(gpuVar, gpuArray.data.ptr) |
| 25 | + wStream.EndStep() |
| 26 | + # update buffers |
| 27 | + gpuArray = gpuArray * 2 |
| 28 | + cpuArray = cpuArray + 1 |
| 29 | + wStream.Close() |
| 30 | + print("Write to file %s: %s data from GPU and %s data from CPU" % ( |
| 31 | + fileName, gpuArray.shape, cpuArray.shape)) |
| 32 | + |
| 33 | +def read_array(fileName, nSteps): |
| 34 | + adios = adios2.ADIOS() |
| 35 | + ioReader = adios.DeclareIO("cupyReader") |
| 36 | + rStream = ioReader.Open(fileName, adios2.Mode.Read) |
| 37 | + for step in range(nSteps): |
| 38 | + rStream.BeginStep() |
| 39 | + # prepare input buffers |
| 40 | + gpuVar = ioReader.InquireVariable("gpuArray") |
| 41 | + cpuVar = ioReader.InquireVariable("cpuArray") |
| 42 | + cpuBuffer = np.zeros(cpuVar.Shape(), dtype=np.float32) |
| 43 | + gpuShape = gpuVar.Shape(adios2.MemorySpace.GPU) |
| 44 | + gpuBuffer = cp.zeros(gpuShape, dtype=np.float32) |
| 45 | + gpuVar.SetSelection([(0, 0), gpuShape]) |
| 46 | + # populate data |
| 47 | + rStream.Get(gpuVar, gpuBuffer.data.ptr) |
| 48 | + rStream.Get(cpuVar, cpuBuffer) |
| 49 | + rStream.EndStep() |
| 50 | + print("Step %d: read GPU data\n %s" % (step, gpuBuffer)) |
| 51 | + print("Step %d: read CPU data\n %s" % (step, cpuBuffer)) |
| 52 | + rStream.Close() |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + # define simulation host data |
| 57 | + cpuArray = np.array([[0, 1.0, 2.0], [3.0, 4.0, 5.0]], dtype=np.float32) |
| 58 | + # copy the data on the device |
| 59 | + gpuArray = cp.asarray(cpuArray) |
| 60 | + print("Array allocation: ", gpuArray.device) |
| 61 | + |
| 62 | + mempool = cp.get_default_memory_pool() |
| 63 | + pinned_mempool = cp.get_default_pinned_memory_pool() |
| 64 | + print("Bytes required to store the gpu array", gpuArray.nbytes) |
| 65 | + print("Bytes allocated on the device memory pool", mempool.total_bytes()) |
| 66 | + print("Bytes used on the device memory pool", mempool.used_bytes()) |
| 67 | + print("Blocks allocated on the pinned memory pool (The allocated pinned" |
| 68 | + " memory is released just after the transfer is complete)", |
| 69 | + pinned_mempool.n_free_blocks()) |
| 70 | + |
| 71 | + nSteps = 2 |
| 72 | + write_array("StepsWriteReadCuPy.bp", nSteps, gpuArray, cpuArray) |
| 73 | + read_array("StepsWriteReadCuPy.bp", nSteps) |
0 commit comments