| 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 <iostream> |
| 12 |
#include <memory> |
| 13 |
#include <stdexcept> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
#include "../write/HeatTransfer.h" |
| 17 |
#include "../write/Settings.h" |
| 18 |
#include "InlineIO.h" |
| 19 |
|
| 20 |
void printUsage() |
| 21 |
{ |
| 22 |
std::cout << "Usage: heatTransfer config output N M nx ny steps " |
| 23 |
"iterations\n" |
| 24 |
<< " config: XML config file to use\n" |
| 25 |
<< " output: name of output data file/stream\n" |
| 26 |
<< " N: number of processes in X dimension\n" |
| 27 |
<< " M: number of processes in Y dimension\n" |
| 28 |
<< " nx: local array size in X dimension per processor\n" |
| 29 |
<< " ny: local array size in Y dimension per processor\n" |
| 30 |
<< " steps: the total number of steps to output\n" |
| 31 |
<< " iterations: one step consist of this many iterations\n\n"; |
| 32 |
} |
| 33 |
|
| 34 |
void Compute(const double *Tin, std::vector<double> &Tout, std::vector<double> &dT, 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 |
std::vector<double> Tout; |
| 59 |
std::vector<double> dT; |
| 60 |
adios2::Variable<double> vTout; |
| 61 |
adios2::Variable<double> vdT; |
| 62 |
adios2::IO outIO; |
| 63 |
adios2::ADIOS ad; |
| 64 |
adios2::Engine writer; |
| 65 |
|
| 66 |
void writeOutput() |
| 67 |
{ |
| 68 |
writer.BeginStep(); |
| 69 |
if (vTout) |
| 70 |
writer.Put<double>(vTout, Tout.data()); |
| 71 |
if (vdT) |
| 72 |
writer.Put<double>(vdT, dT.data()); |
| 73 |
writer.EndStep(); |
| 74 |
} |
| 75 |
|
| 76 |
void setupOutputIO(const Settings &s, MPI_Comm comm) |
| 77 |
{ |
| 78 |
ad = adios2::ADIOS(s.configfile, comm); |
| 79 |
outIO = ad.DeclareIO("readerOutput"); |
| 80 |
Tout.resize(s.ndx * s.ndy); |
| 81 |
dT.resize(s.ndx * s.ndy); |
| 82 |
|
| 83 |
/* Create output variables and open output stream */ |
| 84 |
// For inline engine, there's no exchange of data between processes, |
| 85 |
// so the shape of variables to be written out for validation |
| 86 |
// is the same as the writer |
| 87 |
vTout = outIO.DefineVariable<double>("T", {s.gndx, s.gndy}, {s.offsx, s.offsy}, {s.ndx, s.ndy}); |
| 88 |
vdT = outIO.DefineVariable<double>("dT", {s.gndx, s.gndy}, {s.offsx, s.offsy}, {s.ndx, s.ndy}); |
| 89 |
writer = outIO.Open(s.outputfile, adios2::Mode::Write, comm); |
| 90 |
} |
| 91 |
|
| 92 |
int main(int argc, char *argv[]) |
| 93 |
{ |
| 94 |
MPI_Init(&argc, &argv); |
| 95 |
|
| 96 |
/* When writer and reader is launched together with a single mpirun command, |
| 97 |
the world comm spans all applications. We have to split and create the |
| 98 |
local 'world' communicator mpiHeatTransferComm for the writer only. |
| 99 |
When writer and reader is launched separately, the mpiHeatTransferComm |
| 100 |
communicator will just equal the MPI_COMM_WORLD. |
| 101 |
*/ |
| 102 |
|
| 103 |
int wrank, wnproc; |
| 104 |
MPI_Comm_rank(MPI_COMM_WORLD, &wrank); |
| 105 |
MPI_Comm_size(MPI_COMM_WORLD, &wnproc); |
| 106 |
|
| 107 |
const unsigned int color = 1; |
| 108 |
MPI_Comm mpiHeatTransferComm; |
| 109 |
MPI_Comm_split(MPI_COMM_WORLD, color, wrank, &mpiHeatTransferComm); |
| 110 |
|
| 111 |
int rank, nproc; |
| 112 |
MPI_Comm_rank(mpiHeatTransferComm, &rank); |
| 113 |
MPI_Comm_size(mpiHeatTransferComm, &nproc); |
| 114 |
|
| 115 |
try |
| 116 |
{ |
| 117 |
double timeStart = MPI_Wtime(); |
| 118 |
Settings settings(argc, argv, rank, nproc); |
| 119 |
HeatTransfer ht(settings); |
| 120 |
InlineIO io(settings, mpiHeatTransferComm); |
| 121 |
setupOutputIO(settings, mpiHeatTransferComm); |
| 122 |
|
| 123 |
ht.init(false); |
| 124 |
// ht.printT("Initialized T:", mpiHeatTransferComm); |
| 125 |
ht.heatEdges(); |
| 126 |
ht.exchange(mpiHeatTransferComm); |
| 127 |
// ht.printT("Heated T:", mpiHeatTransferComm); |
| 128 |
|
| 129 |
io.write(ht); |
| 130 |
const double *Tin = io.read(true); |
| 131 |
Compute(Tin, Tout, dT, true); |
| 132 |
writeOutput(); |
| 133 |
|
| 134 |
for (unsigned int t = 1; t <= settings.steps; ++t) |
| 135 |
{ |
| 136 |
if (rank == 0) |
| 137 |
std::cout << "Step " << t << ":\n"; |
| 138 |
for (unsigned int iter = 1; iter <= settings.iterations; ++iter) |
| 139 |
{ |
| 140 |
ht.iterate(); |
| 141 |
ht.exchange(mpiHeatTransferComm); |
| 142 |
ht.heatEdges(); |
| 143 |
} |
| 144 |
|
| 145 |
io.write(ht); |
| 146 |
Tin = io.read(false); |
| 147 |
Compute(Tin, Tout, dT, false); |
| 148 |
writeOutput(); |
| 149 |
} |
| 150 |
MPI_Barrier(mpiHeatTransferComm); |
| 151 |
|
| 152 |
double timeEnd = MPI_Wtime(); |
| 153 |
if (rank == 0) |
| 154 |
std::cout << "Total runtime = " << timeEnd - timeStart << "s\n"; |
| 155 |
|
| 156 |
writer.Close(); |
| 157 |
} |
| 158 |
catch (std::invalid_argument &e) // command-line argument errors |
| 159 |
{ |
| 160 |
std::cout << e.what() << std::endl; |
| 161 |
printUsage(); |
| 162 |
} |
| 163 |
catch (std::ios_base::failure &e) // I/O failure (e.g. file not found) |
| 164 |
{ |
| 165 |
std::cout << "I/O base exception caught\n"; |
| 166 |
std::cout << e.what() << std::endl; |
| 167 |
} |
| 168 |
catch (std::exception &e) // All other exceptions |
| 169 |
{ |
| 170 |
std::cout << "Exception caught\n"; |
| 171 |
std::cout << e.what() << std::endl; |
| 172 |
} |
| 173 |
|
| 174 |
MPI_Finalize(); |
| 175 |
return 0; |
| 176 |
} |