diff --git a/examples/10_streaming_read.cpp b/examples/10_streaming_read.cpp index 99da2c5a5c..12392b4e70 100644 --- a/examples/10_streaming_read.cpp +++ b/examples/10_streaming_read.cpp @@ -11,7 +11,6 @@ using namespace openPMD; int main() { #if openPMD_HAVE_ADIOS2 - using position_t = double; auto backends = openPMD::getFileExtensions(); if (std::find(backends.begin(), backends.end(), "sst") == backends.end()) { @@ -40,7 +39,7 @@ int main() std::cout << "Current iteration: " << iteration.iterationIndex << std::endl; Record electronPositions = iteration.particles["e"]["position"]; - std::array, 3> loadedChunks; + std::array loadedChunks; std::array extents; std::array const dimensions{{"x", "y", "z"}}; @@ -48,7 +47,7 @@ int main() { std::string const &dim = dimensions[i]; RecordComponent rc = electronPositions[dim]; - loadedChunks[i] = rc.loadChunk( + loadedChunks[i] = rc.loadChunkVariant( Offset(rc.getDimensionality(), 0), rc.getExtent()); extents[i] = rc.getExtent(); } @@ -64,10 +63,14 @@ int main() Extent const &extent = extents[i]; std::cout << "\ndim: " << dim << "\n" << std::endl; auto chunk = loadedChunks[i]; - for (size_t j = 0; j < extent[0]; ++j) - { - std::cout << chunk.get()[j] << ", "; - } + std::visit( + [&extent](auto &shared_ptr) { + for (size_t j = 0; j < extent[0]; ++j) + { + std::cout << shared_ptr.get()[j] << ", "; + } + }, + chunk); std::cout << "\n----------\n" << std::endl; } } diff --git a/examples/2_read_serial.cpp b/examples/2_read_serial.cpp index 8fb3ccb190..1ab1ac7d3a 100644 --- a/examples/2_read_serial.cpp +++ b/examples/2_read_serial.cpp @@ -75,20 +75,25 @@ int main() Offset chunk_offset = {1, 1, 1}; Extent chunk_extent = {2, 2, 1}; - auto chunk_data = E_x.loadChunk(chunk_offset, chunk_extent); + // Loading without explicit datatype here + auto chunk_data = E_x.loadChunkVariant(chunk_offset, chunk_extent); cout << "Queued the loading of a single chunk from disk, " "ready to execute\n"; series.flush(); cout << "Chunk has been read from disk\n" << "Read chunk contains:\n"; - for (size_t row = 0; row < chunk_extent[0]; ++row) - { - for (size_t col = 0; col < chunk_extent[1]; ++col) - cout << "\t" << '(' << row + chunk_offset[0] << '|' - << col + chunk_offset[1] << '|' << 1 << ")\t" - << chunk_data.get()[row * chunk_extent[1] + col]; - cout << '\n'; - } + std::visit( + [&chunk_offset, &chunk_extent](auto &shared_ptr) { + for (size_t row = 0; row < chunk_extent[0]; ++row) + { + for (size_t col = 0; col < chunk_extent[1]; ++col) + cout << "\t" << '(' << row + chunk_offset[0] << '|' + << col + chunk_offset[1] << '|' << 1 << ")\t" + << shared_ptr.get()[row * chunk_extent[1] + col]; + cout << '\n'; + } + }, + chunk_data); auto all_data = E_x.loadChunk(); diff --git a/examples/4_read_parallel.cpp b/examples/4_read_parallel.cpp index 477177cec6..8bd12627d9 100644 --- a/examples/4_read_parallel.cpp +++ b/examples/4_read_parallel.cpp @@ -49,7 +49,8 @@ int main(int argc, char *argv[]) Offset chunk_offset = {static_cast(mpi_rank) + 1, 1, 1}; Extent chunk_extent = {2, 2, 1}; - auto chunk_data = E_x.loadChunk(chunk_offset, chunk_extent); + // If you know the datatype, use `loadChunk(...)` instead. + auto chunk_data = E_x.loadChunkVariant(chunk_offset, chunk_extent); if (0 == mpi_rank) cout << "Queued the loading of a single chunk per MPI rank from " @@ -72,9 +73,20 @@ int main(int argc, char *argv[]) for (size_t row = 0; row < chunk_extent[0]; ++row) { for (size_t col = 0; col < chunk_extent[1]; ++col) + { cout << "\t" << '(' << row + chunk_offset[0] << '|' - << col + chunk_offset[1] << '|' << 1 << ")\t" - << chunk_data.get()[row * chunk_extent[1] + col]; + << col + chunk_offset[1] << '|' << 1 << ")\t"; + /* + * For hot loops, the std::visit(...) call should be moved + * further up. + */ + std::visit( + [row, col, &chunk_extent](auto &shared_ptr) { + cout << shared_ptr + .get()[row * chunk_extent[1] + col]; + }, + chunk_data); + } cout << std::endl; } }