Debian Codegraph @

examples/simulations/heatTransfer/write/main.cpp

raw
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 <iostream>
10 #include <memory>
11 #include <stdexcept>
12 #include <string>
13
14 #include "HeatTransfer.h"
15 #include "IO.h"
16 #include "Settings.h"
17
18 void printUsage()
19 {
20 std::cout << "Usage: heatTransfer config output N M nx ny steps "
21 "iterations\n"
22 << " config: XML config file to use\n"
23 << " output: name of output data file/stream\n"
24 << " N: number of processes in X dimension\n"
25 << " M: number of processes in Y dimension\n"
26 << " nx: local array size in X dimension per processor\n"
27 << " ny: local array size in Y dimension per processor\n"
28 << " steps: the total number of steps to output\n"
29 << " iterations: one step consist of this many iterations\n\n";
30 }
31
32 int main(int argc, char *argv[])
33 {
34 int provided;
35
36 std::string engineName = std::string(argv[argc - 1]);
37
38 int threadSupportLevel = MPI_THREAD_SINGLE;
39 if (engineName == "SST")
40 {
41 threadSupportLevel = MPI_THREAD_MULTIPLE;
42 }
43
44 // MPI_THREAD_MULTIPLE is only required if you enable the SST MPI_DP
45 MPI_Init_thread(&argc, &argv, threadSupportLevel, &provided);
46
47 /* When writer and reader is launched together with a single mpirun command,
48 the world comm spans all applications. We have to split and create the
49 local 'world' communicator mpiHeatTransferComm for the writer only.
50 When writer and reader is launched separately, the mpiHeatTransferComm
51 communicator will just equal the MPI_COMM_WORLD.
52 */
53
54 int wrank, wnproc;
55 MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
56 MPI_Comm_size(MPI_COMM_WORLD, &wnproc);
57
58 const unsigned int color = 1;
59 MPI_Comm mpiHeatTransferComm;
60 MPI_Comm_split(MPI_COMM_WORLD, color, wrank, &mpiHeatTransferComm);
61
62 int rank, nproc;
63 MPI_Comm_rank(mpiHeatTransferComm, &rank);
64 MPI_Comm_size(mpiHeatTransferComm, &nproc);
65
66 try
67 {
68 double timeStart = MPI_Wtime();
69 Settings settings(argc, argv, rank, nproc);
70 HeatTransfer ht(settings);
71 IO io(settings, mpiHeatTransferComm);
72
73 ht.init(false);
74 // ht.printT("Initialized T:", mpiHeatTransferComm);
75 ht.heatEdges();
76 ht.exchange(mpiHeatTransferComm);
77 // ht.printT("Heated T:", mpiHeatTransferComm);
78
79 io.write(0, ht, settings, mpiHeatTransferComm);
80
81 for (unsigned int t = 1; t <= settings.steps; ++t)
82 {
83 if (rank == 0)
84 std::cout << "Step " << t << ":\n";
85 for (unsigned int iter = 1; iter <= settings.iterations; ++iter)
86 {
87 ht.iterate();
88 ht.exchange(mpiHeatTransferComm);
89 ht.heatEdges();
90 }
91
92 io.write(t, ht, settings, mpiHeatTransferComm);
93 }
94 MPI_Barrier(mpiHeatTransferComm);
95
96 double timeEnd = MPI_Wtime();
97 if (rank == 0)
98 std::cout << "Total runtime = " << timeEnd - timeStart << "s\n";
99 }
100 catch (std::invalid_argument &e) // command-line argument errors
101 {
102 std::cout << e.what() << std::endl;
103 printUsage();
104 }
105 catch (std::ios_base::failure &e) // I/O failure (e.g. file not found)
106 {
107 std::cout << "I/O base exception caught\n";
108 std::cout << e.what() << std::endl;
109 }
110 catch (std::exception &e) // All other exceptions
111 {
112 std::cout << "Exception caught\n";
113 std::cout << e.what() << std::endl;
114 }
115
116 MPI_Finalize();
117 return 0;
118 }