Debian Codegraph @

examples/simulations/heatTransfer/write/HeatTransfer.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 <cstring>
8
9 #include <algorithm>
10 #include <iomanip>
11 #include <iostream>
12 #include <math.h>
13 #include <memory>
14 #include <stdexcept>
15 #include <string>
16
17 #include "HeatTransfer.h"
18
19 HeatTransfer::HeatTransfer(const Settings &settings)
20 : m_s(settings), m_T1Buf(new double[(m_s.ndx + 2) * (m_s.ndy + 2)]),
21 m_T2Buf(new double[(m_s.ndx + 2) * (m_s.ndy + 2)]), m_T1(new double *[m_s.ndx + 2]),
22 m_T2(new double *[m_s.ndx + 2])
23 {
24 m_T1[0] = m_T1Buf.get();
25 m_T2[0] = m_T2Buf.get();
26 for (size_t i = 1; i < m_s.ndx + 2; ++i)
27 {
28 m_T1[i] = m_T1[0] + i * (m_s.ndy + 2);
29 m_T2[i] = m_T2[0] + i * (m_s.ndy + 2);
30 }
31 m_TCurrent = m_T1.get();
32 m_TNext = m_T2.get();
33 }
34
35 void HeatTransfer::init(bool init_with_rank)
36 {
37 if (init_with_rank)
38 {
39 std::fill_n(m_T1Buf.get(), (m_s.ndx + 2) * (m_s.ndy + 2), static_cast<double>(m_s.rank));
40 }
41 else
42 {
43 const double hx = 2.0 * 4.0 * atan(1.0) / m_s.ndx;
44 const double hy = 2.0 * 4.0 * atan(1.0) / m_s.ndy;
45
46 double x, y;
47 for (unsigned int i = 0; i < m_s.ndx + 2; i++)
48 {
49 x = 0.0 + hx * (i - 1);
50 for (unsigned int j = 0; j < m_s.ndy + 2; j++)
51 {
52 y = 0.0 + hy * (j - 1);
53 m_T1[i][j] = cos(8 * x) + cos(6 * x) - cos(4 * x) + cos(2 * x) - cos(x) +
54 sin(8 * y) - sin(6 * y) + sin(4 * y) - sin(2 * y) + sin(y);
55 }
56 }
57 }
58 m_TCurrent = m_T1.get();
59 m_TNext = m_T2.get();
60 }
61
62 void HeatTransfer::printT(std::string message, MPI_Comm comm) const
63 {
64 int rank, size;
65 int tag = 1;
66 int token;
67 MPI_Status status;
68 MPI_Comm_rank(comm, &rank);
69 MPI_Comm_size(comm, &size);
70 if (rank > 0)
71 {
72 MPI_Recv(&token, 1, MPI_INT, rank - 1, tag, comm, &status);
73 }
74
75 std::cout << "Rank " << rank << " " << message << std::endl;
76 for (unsigned int i = 0; i < m_s.ndx + 2; i++)
77 {
78 std::cout << " T[" << i << "][] = ";
79 for (unsigned int j = 0; j < m_s.ndy + 2; j++)
80 {
81 std::cout << std::setw(6) << m_TCurrent[i][j];
82 }
83 std::cout << std::endl;
84 }
85 std::cout << std::flush << std::endl;
86
87 if (rank < size - 1)
88 {
89 MPI_Send(&token, 1, MPI_INT, rank + 1, tag, comm);
90 }
91 }
92
93 void HeatTransfer::iterate()
94 {
95 for (unsigned int i = 1; i <= m_s.ndx; ++i)
96 {
97 for (unsigned int j = 1; j <= m_s.ndy; ++j)
98 {
99 m_TNext[i][j] = omega / 4 *
100 (m_TCurrent[i - 1][j] + m_TCurrent[i + 1][j] +
101 m_TCurrent[i][j - 1] + m_TCurrent[i][j + 1]) +
102 (1.0 - omega) * m_TCurrent[i][j];
103 }
104 }
105 std::swap(m_TCurrent, m_TNext);
106 }
107
108 void HeatTransfer::heatEdges()
109 {
110 // Heat the whole global edges
111 if (m_s.posx == 0)
112 std::fill_n(m_TCurrent[0], m_s.ndy + 2, edgetemp);
113
114 if (m_s.posx == m_s.npx - 1)
115 std::fill_n(m_TCurrent[m_s.ndx + 1], m_s.ndy + 2, edgetemp);
116
117 if (m_s.posy == 0)
118 for (unsigned int i = 0; i < m_s.ndx + 2; ++i)
119 m_TCurrent[i][0] = edgetemp;
120
121 if (m_s.posy == m_s.npy - 1)
122 for (unsigned int i = 0; i < m_s.ndx + 2; ++i)
123 m_TCurrent[i][m_s.ndy + 1] = edgetemp;
124 }
125
126 void HeatTransfer::exchange(MPI_Comm comm)
127 {
128 // Build a custom MPI type for the column vector to allow strided access
129 MPI_Datatype tColumnVector;
130 MPI_Type_vector(m_s.ndx + 2, 1, m_s.ndy + 2, MPI_DOUBLE, &tColumnVector);
131 MPI_Type_commit(&tColumnVector);
132
133 // Exchange ghost cells, in the order left-right-up-down
134
135 // send to left + receive from right
136 int tag = 1;
137 MPI_Status status;
138 if (m_s.rank_left >= 0)
139 {
140 // std::cout << "Rank " << m_s.rank << " send left to rank "
141 // << m_s.rank_left << std::endl;
142 MPI_Send(m_TCurrent[0] + 1, 1, tColumnVector, m_s.rank_left, tag, comm);
143 }
144 if (m_s.rank_right >= 0)
145 {
146 // std::cout << "Rank " << m_s.rank << " receive from right from rank "
147 // << m_s.rank_right << std::endl;
148 MPI_Recv(m_TCurrent[0] + (m_s.ndy + 1), 1, tColumnVector, m_s.rank_right, tag, comm,
149 &status);
150 }
151
152 // send to right + receive from left
153 tag = 2;
154 if (m_s.rank_right >= 0)
155 {
156 // std::cout << "Rank " << m_s.rank << " send right to rank "
157 // << m_s.rank_right << std::endl;
158 MPI_Send(m_TCurrent[0] + m_s.ndy, 1, tColumnVector, m_s.rank_right, tag, comm);
159 }
160 if (m_s.rank_left >= 0)
161 {
162 // std::cout << "Rank " << m_s.rank << " receive from left from rank "
163 // << m_s.rank_left << std::endl;
164 MPI_Recv(m_TCurrent[0], 1, tColumnVector, m_s.rank_left, tag, comm, &status);
165 }
166
167 // Cleanup the custom column vector type
168 MPI_Type_free(&tColumnVector);
169
170 // send down + receive from above
171 tag = 3;
172 if (m_s.rank_down >= 0)
173 {
174 // std::cout << "Rank " << m_s.rank << " send down to rank "
175 // << m_s.rank_down << std::endl;
176 MPI_Send(m_TCurrent[m_s.ndx], m_s.ndy + 2, MPI_DOUBLE, m_s.rank_down, tag, comm);
177 }
178 if (m_s.rank_up >= 0)
179 {
180 // std::cout << "Rank " << m_s.rank << " receive from above from rank "
181 // << m_s.rank_up << std::endl;
182 MPI_Recv(m_TCurrent[0], m_s.ndy + 2, MPI_DOUBLE, m_s.rank_up, tag, comm, &status);
183 }
184
185 // send up + receive from below
186 tag = 4;
187 if (m_s.rank_up >= 0)
188 {
189 // std::cout << "Rank " << m_s.rank << " send up to rank " <<
190 // m_s.rank_up
191 // << std::endl;
192 MPI_Send(m_TCurrent[1], m_s.ndy + 2, MPI_DOUBLE, m_s.rank_up, tag, comm);
193 }
194 if (m_s.rank_down >= 0)
195 {
196 // std::cout << "Rank " << m_s.rank << " receive from below from rank "
197 // << m_s.rank_down << std::endl;
198 MPI_Recv(m_TCurrent[m_s.ndx + 1], m_s.ndy + 2, MPI_DOUBLE, m_s.rank_down, tag, comm,
199 &status);
200 }
201 }
202
203 /* Copies the internal ndx*ndy section of the ndx+2 * ndy+2 local array
204 * into a separate contiguous vector and returns it.
205 * @return A vector with ndx*ndy elements
206 */
207 std::vector<double> HeatTransfer::data_noghost() const
208 {
209 std::vector<double> d(m_s.ndx * m_s.ndy);
210 for (unsigned int i = 1; i <= m_s.ndx; ++i)
211 {
212 std::memcpy(&d[(i - 1) * m_s.ndy], m_TCurrent[i] + 1, m_s.ndy * sizeof(double));
213 }
214 return d;
215 }