| 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 <iomanip> |
| 13 |
#include <iostream> |
| 14 |
#include <math.h> |
| 15 |
#include <memory> |
| 16 |
#include <stdexcept> |
| 17 |
#include <string> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "PrintDataStep.h" |
| 21 |
#include "ReadSettings.h" |
| 22 |
|
| 23 |
void printUsage() |
| 24 |
{ |
| 25 |
std::cout << "Usage: heatRead config input output N M \n" |
| 26 |
<< " config: XML config file to use\n" |
| 27 |
<< " input: name of input data file/stream\n" |
| 28 |
<< " output: name of output data file/stream\n" |
| 29 |
<< " N: number of processes in X dimension\n" |
| 30 |
<< " M: number of processes in Y dimension\n\n"; |
| 31 |
} |
| 32 |
|
| 33 |
void Compute(const std::vector<double> &Tin, std::vector<double> &Tout, std::vector<double> &dT, |
| 34 |
bool firstStep) |
| 35 |
{ |
| 36 |
/* Compute dT and |
| 37 |
* copy Tin into Tout as it will be used for calculating dT in the |
| 38 |
* next step |
| 39 |
*/ |
| 40 |
if (firstStep) |
| 41 |
{ |
| 42 |
for (size_t i = 0; i < dT.size(); ++i) |
| 43 |
{ |
| 44 |
dT[i] = 0; |
| 45 |
Tout[i] = Tin[i]; |
| 46 |
} |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
for (size_t i = 0; i < dT.size(); ++i) |
| 51 |
{ |
| 52 |
dT[i] = Tout[i] - Tin[i]; |
| 53 |
Tout[i] = Tin[i]; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
int main(int argc, char *argv[]) |
| 59 |
{ |
| 60 |
int provided; |
| 61 |
std::string engineName = std::string(argv[argc - 1]); |
| 62 |
|
| 63 |
int threadSupportLevel = MPI_THREAD_SINGLE; |
| 64 |
if (engineName == "SST") |
| 65 |
{ |
| 66 |
threadSupportLevel = MPI_THREAD_MULTIPLE; |
| 67 |
} |
| 68 |
|
| 69 |
// MPI_THREAD_MULTIPLE is only required if you enable the SST MPI_DP |
| 70 |
MPI_Init_thread(&argc, &argv, threadSupportLevel, &provided); |
| 71 |
|
| 72 |
/* When writer and reader is launched together with a single mpirun command, |
| 73 |
the world comm spans all applications. We have to split and create the |
| 74 |
local 'world' communicator for the reader only. |
| 75 |
When writer and reader is launched separately, the mpiReaderComm |
| 76 |
communicator will just equal the MPI_COMM_WORLD. |
| 77 |
*/ |
| 78 |
|
| 79 |
int wrank, wnproc; |
| 80 |
MPI_Comm_rank(MPI_COMM_WORLD, &wrank); |
| 81 |
MPI_Comm_size(MPI_COMM_WORLD, &wnproc); |
| 82 |
|
| 83 |
const unsigned int color = 2; |
| 84 |
MPI_Comm mpiReaderComm; |
| 85 |
MPI_Comm_split(MPI_COMM_WORLD, color, wrank, &mpiReaderComm); |
| 86 |
|
| 87 |
int rank, nproc; |
| 88 |
MPI_Comm_rank(mpiReaderComm, &rank); |
| 89 |
MPI_Comm_size(mpiReaderComm, &nproc); |
| 90 |
|
| 91 |
try |
| 92 |
{ |
| 93 |
ReadSettings settings(argc, argv, rank, nproc); |
| 94 |
adios2::ADIOS ad(settings.configfile, mpiReaderComm); |
| 95 |
|
| 96 |
// Define method for engine creation |
| 97 |
// 1. Get method def from config file or define new one |
| 98 |
|
| 99 |
adios2::IO inIO = ad.DeclareIO("readerInput"); |
| 100 |
if (!inIO.InConfigFile()) |
| 101 |
{ |
| 102 |
// if not defined by user, we can change the default settings |
| 103 |
// BPFile is the default engine |
| 104 |
inIO.SetEngine("BPFile"); |
| 105 |
inIO.SetParameters({{"num_threads", "1"}}); |
| 106 |
|
| 107 |
// ISO-POSIX file output is the default transport (called "File") |
| 108 |
// Passing parameters to the transport |
| 109 |
inIO.AddTransport("File", {{"verbose", "4"}}); |
| 110 |
} |
| 111 |
|
| 112 |
adios2::IO outIO = ad.DeclareIO("readerOutput"); |
| 113 |
|
| 114 |
adios2::Engine reader = inIO.Open(settings.inputfile, adios2::Mode::Read, mpiReaderComm); |
| 115 |
|
| 116 |
std::vector<double> Tin; |
| 117 |
std::vector<double> Tout; |
| 118 |
std::vector<double> dT; |
| 119 |
adios2::Variable<double> vTin; |
| 120 |
adios2::Variable<double> vTout; |
| 121 |
adios2::Variable<double> vdT; |
| 122 |
adios2::Engine writer; |
| 123 |
bool firstStep = true; |
| 124 |
int step = 0; |
| 125 |
|
| 126 |
while (true) |
| 127 |
{ |
| 128 |
adios2::StepStatus status = reader.BeginStep(adios2::StepMode::Read); |
| 129 |
if (status != adios2::StepStatus::OK) |
| 130 |
{ |
| 131 |
break; |
| 132 |
} |
| 133 |
|
| 134 |
// Variable objects disappear between steps so we need this every |
| 135 |
// step |
| 136 |
vTin = inIO.InquireVariable<double>("T"); |
| 137 |
|
| 138 |
if (!vTin) |
| 139 |
{ |
| 140 |
std::cout << "Error: NO variable T found. Unable to proceed. " |
| 141 |
"Exiting. " |
| 142 |
<< std::endl; |
| 143 |
break; |
| 144 |
} |
| 145 |
|
| 146 |
if (firstStep) |
| 147 |
{ |
| 148 |
// Promise that we are not going to change the variable sizes |
| 149 |
// nor add new variables |
| 150 |
reader.LockReaderSelections(); |
| 151 |
|
| 152 |
unsigned int gndx = static_cast<unsigned int>(vTin.Shape()[0]); |
| 153 |
unsigned int gndy = static_cast<unsigned int>(vTin.Shape()[1]); |
| 154 |
|
| 155 |
if (rank == 0) |
| 156 |
{ |
| 157 |
std::cout << "gndx = " << gndx << std::endl; |
| 158 |
std::cout << "gndy = " << gndy << std::endl; |
| 159 |
} |
| 160 |
|
| 161 |
settings.DecomposeArray(gndx, gndy); |
| 162 |
Tin.resize(settings.readsize[0] * settings.readsize[1]); |
| 163 |
Tout.resize(settings.readsize[0] * settings.readsize[1]); |
| 164 |
dT.resize(settings.readsize[0] * settings.readsize[1]); |
| 165 |
|
| 166 |
/* Create output variables and open output stream */ |
| 167 |
vTout = outIO.DefineVariable<double>("T", {gndx, gndy}, settings.offset, |
| 168 |
settings.readsize); |
| 169 |
vdT = outIO.DefineVariable<double>("dT", {gndx, gndy}, settings.offset, |
| 170 |
settings.readsize); |
| 171 |
writer = outIO.Open(settings.outputfile, adios2::Mode::Write, mpiReaderComm); |
| 172 |
|
| 173 |
MPI_Barrier(mpiReaderComm); // sync processes just for stdout |
| 174 |
} |
| 175 |
|
| 176 |
if (!rank) |
| 177 |
{ |
| 178 |
std::cout << "Processing step " << step << std::endl; |
| 179 |
} |
| 180 |
|
| 181 |
// Create a 2D selection for the subset |
| 182 |
vTin.SetSelection(adios2::Box<adios2::Dims>(settings.offset, settings.readsize)); |
| 183 |
|
| 184 |
// Arrays are read by scheduling one or more of them |
| 185 |
// and performing the reads at once |
| 186 |
reader.Get<double>(vTin, Tin.data()); |
| 187 |
/*printDataStep(Tin.data(), settings.readsize.data(), |
| 188 |
settings.offset.data(), rank, step); */ |
| 189 |
reader.EndStep(); |
| 190 |
|
| 191 |
/* Compute dT from current T (Tin) and previous T (Tout) |
| 192 |
* and save Tin in Tout for output and for future computation |
| 193 |
*/ |
| 194 |
Compute(Tin, Tout, dT, firstStep); |
| 195 |
|
| 196 |
/* Output Tout and dT */ |
| 197 |
writer.BeginStep(); |
| 198 |
|
| 199 |
if (vTout) |
| 200 |
writer.Put<double>(vTout, Tout.data()); |
| 201 |
if (vdT) |
| 202 |
writer.Put<double>(vdT, dT.data()); |
| 203 |
writer.EndStep(); |
| 204 |
|
| 205 |
step++; |
| 206 |
firstStep = false; |
| 207 |
} |
| 208 |
reader.Close(); |
| 209 |
if (writer) |
| 210 |
writer.Close(); |
| 211 |
} |
| 212 |
catch (std::invalid_argument &e) // command-line argument errors |
| 213 |
{ |
| 214 |
std::cout << e.what() << std::endl; |
| 215 |
printUsage(); |
| 216 |
} |
| 217 |
|
| 218 |
MPI_Finalize(); |
| 219 |
return 0; |
| 220 |
} |