| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <mpi.h> |
| 8 |
|
| 9 |
#include "adios2.h" |
| 10 |
|
| 11 |
#include <cstdint> |
| 12 |
#include <iostream> |
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#include "PrintData.h" |
| 16 |
|
| 17 |
int main(int argc, char *argv[]) |
| 18 |
{ |
| 19 |
MPI_Init(&argc, &argv); |
| 20 |
|
| 21 |
if (argc < 2) |
| 22 |
{ |
| 23 |
std::cout << "Not enough arguments: need an input file\n"; |
| 24 |
return 1; |
| 25 |
} |
| 26 |
const char *inputfile = argv[1]; |
| 27 |
|
| 28 |
/* World comm spans all applications started with the same aprun command |
| 29 |
on a Cray XK6. So we have to split and create the local |
| 30 |
'world' communicator for the reader only. |
| 31 |
In normal start-up, the communicator will just equal the MPI_COMM_WORLD. |
| 32 |
*/ |
| 33 |
|
| 34 |
int wrank, wnproc; |
| 35 |
MPI_Comm_rank(MPI_COMM_WORLD, &wrank); |
| 36 |
MPI_Comm_size(MPI_COMM_WORLD, &wnproc); |
| 37 |
|
| 38 |
const unsigned int color = 2; |
| 39 |
MPI_Comm mpiReaderComm; |
| 40 |
MPI_Comm_split(MPI_COMM_WORLD, color, wrank, &mpiReaderComm); |
| 41 |
|
| 42 |
int rank, nproc; |
| 43 |
MPI_Comm_rank(mpiReaderComm, &rank); |
| 44 |
MPI_Comm_size(mpiReaderComm, &nproc); |
| 45 |
|
| 46 |
adios2::ADIOS ad(mpiReaderComm); |
| 47 |
|
| 48 |
// Define method for engine creation |
| 49 |
// 1. Get method def from config file or define new one |
| 50 |
|
| 51 |
adios2::IO bpReaderIO = ad.DeclareIO("input"); |
| 52 |
if (!bpReaderIO.InConfigFile()) |
| 53 |
{ |
| 54 |
// if not defined by user, we can change the default settings |
| 55 |
// BPFile is the default engine |
| 56 |
bpReaderIO.SetEngine("BP"); |
| 57 |
bpReaderIO.SetParameters({{"num_threads", "2"}}); |
| 58 |
bpReaderIO.SetParameter("OpenAsFile", "true"); |
| 59 |
|
| 60 |
// ISO-POSIX file is the default transport |
| 61 |
// Passing parameters to the transport |
| 62 |
bpReaderIO.AddTransport("File", {{"verbose", "4"}}); |
| 63 |
} |
| 64 |
|
| 65 |
adios2::Engine bpReader = bpReaderIO.Open(inputfile, adios2::Mode::Read, mpiReaderComm); |
| 66 |
|
| 67 |
unsigned int gndx = 0; |
| 68 |
unsigned int gndy = 0; |
| 69 |
// bpReader->Read<unsigned int>("gndx", &gndx); |
| 70 |
// bpReader->Read<unsigned int>("gndy", &gndy); |
| 71 |
|
| 72 |
adios2::Variable<unsigned int> vgndx = bpReaderIO.InquireVariable<unsigned int>("gndx"); |
| 73 |
(void)vgndx; |
| 74 |
|
| 75 |
// gndx = vgndx.GetData()[0]; |
| 76 |
|
| 77 |
adios2::Variable<unsigned int> vgndy = bpReaderIO.InquireVariable<unsigned int>("gndy"); |
| 78 |
// gndy = vgndy.GetData()[0]; |
| 79 |
|
| 80 |
if (rank == 0) |
| 81 |
{ |
| 82 |
std::cout << "gndx = " << gndx << std::endl; |
| 83 |
std::cout << "gndy = " << gndy << std::endl; |
| 84 |
std::cout << "# of steps = " << vgndy.Steps() << std::endl; |
| 85 |
} |
| 86 |
|
| 87 |
// 1D decomposition of the columns, which is inefficient for reading! |
| 88 |
adios2::Dims readsize({gndx, gndy / nproc}); |
| 89 |
adios2::Dims offset({0LL, rank * readsize[1]}); |
| 90 |
if (rank == nproc - 1) |
| 91 |
{ |
| 92 |
// last process should read all the rest of columns |
| 93 |
readsize[1] = gndy - readsize[1] * (nproc - 1); |
| 94 |
} |
| 95 |
|
| 96 |
std::cout << "rank " << rank << " reads " << readsize[1] << " columns from offset " << offset[1] |
| 97 |
<< std::endl; |
| 98 |
|
| 99 |
adios2::Variable<double> vT = bpReaderIO.InquireVariable<double>("T"); |
| 100 |
|
| 101 |
double *T = new double[vT.Steps() * readsize[0] * readsize[1]]; |
| 102 |
|
| 103 |
// Create a 2D selection for the subset |
| 104 |
vT.SetSelection(adios2::Box<adios2::Dims>(offset, readsize)); |
| 105 |
vT.SetStepSelection(adios2::Box<std::size_t>(0, vT.Steps())); |
| 106 |
|
| 107 |
// Arrays are read by scheduling one or more of them |
| 108 |
// and performing the reads at once |
| 109 |
// bpReader->ScheduleRead<double>(*vT, T); |
| 110 |
// bpReader->PerformReads(adios2::ReadMode::Blocking); |
| 111 |
|
| 112 |
printData(T, readsize.data(), offset.data(), rank, vT.Steps()); |
| 113 |
bpReader.Close(); |
| 114 |
delete[] T; |
| 115 |
MPI_Finalize(); |
| 116 |
return 0; |
| 117 |
} |