Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* heatTransfer.cpp
*
* Recreates heat_transfer.f90 (Fortran) in C++
*
* Created on: Feb 2017
* Author: Norbert Podhorszki
*
*/
#include <mpi.h>
#include <stdexcept>
#include <memory>
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
#include "HeatTransfer.h"
HeatTransfer::HeatTransfer( std::shared_ptr<Settings> settings )
: m_s{settings}
{
m_T1 = new double*[ m_s->ndx+2 ];
m_T1[0] = new double[ (m_s->ndx+2) * (m_s->ndy+2) ];
m_T2 = new double*[ m_s->ndx+2 ];
m_T2[0] = new double[ (m_s->ndx+2) * (m_s->ndy+2) ];
for (int i = 1; i < m_s->ndx+2; i++)
{
m_T1[i] = m_T1[i-1] + m_s->ndy+2;
m_T2[i] = m_T2[i-1] + m_s->ndy+2;
}
m_TCurrent = m_T1;
m_TNext = m_T2;
}
HeatTransfer::~HeatTransfer()
{
delete[] m_T1[0];
delete[] m_T1;
delete[] m_T2[0];
delete[] m_T2;
}
void HeatTransfer::init(bool init_with_rank)
{
if (init_with_rank)
{
for (int i = 0; i < m_s->ndx+2; i++)
for (int j = 0; j < m_s->ndy+2; j++)
m_T1[i][j] = m_s->rank;
}
else
{
const double hx = 2.0 * 4.0*atan(1.0)/m_s->ndx;
const double hy = 2.0 * 4.0*atan(1.0)/m_s->ndy;
double x, y;
for (int i = 0; i < m_s->ndx+2; i++)
{
x = 0.0 + hx*(i-1);
for (int j = 0; j < m_s->ndy+2; j++)
{
y = 0.0 + hy*(j-1);
m_T1[i][j] = cos(8*x) + cos(6*x) - cos(4*x) + cos(2*x) - cos(x) +
sin(8*y) - sin(6*y) + sin(4*y) - sin(2*y) + sin(y);
}
}
}
m_TCurrent = m_T1;
m_TNext = m_T2;
}
void HeatTransfer::printT(std::string message, MPI_Comm comm)
{
int rank, size;
int tag = 1;
int token;
MPI_Status status;
MPI_Comm_rank( comm, &rank );
MPI_Comm_size( comm, &size );
if( rank > 0)
{
MPI_Recv(&token, 1, MPI_INT, rank-1, tag, comm, &status);
}
std::cout << "Rank " << rank << " " << message << std::endl;
for (int i = 0; i < m_s->ndx+2; i++)
{
std::cout << " T[" << i << "][] = ";
for (int j = 0; j < m_s->ndy+2; j++)
{
std::cout << std::setw(6) << m_TCurrent[i][j];
}
std::cout << std::endl;
}
std::cout << std::flush << std::endl;
if( rank < size-1)
{
MPI_Send(&token, 1, MPI_INT, rank+1, tag, comm);
}
}
void HeatTransfer::switchCurrentNext()
{
double **tmp = m_TCurrent;
m_TCurrent = m_TNext;
m_TNext = tmp;
}
void HeatTransfer::iterate()
{
for( int i = 1; i <= m_s->ndx; ++i)
{
for( int j = 1; j <= m_s->ndy; ++j)
{
m_TNext[i][j] =
omega/4*(m_TCurrent[i-1][j] +
m_TCurrent[i+1][j] +
m_TCurrent[i][j-1] +
m_TCurrent[i][j+1]) +
(1.0-omega)*m_TCurrent[i][j];
}
}
switchCurrentNext();
}
void HeatTransfer::heatEdges()
{
// Heat the whole global edges
if( m_s->posx==0 )
for( int j = 0; j < m_s->ndy+2; ++j)
m_TCurrent[0][j]= edgetemp;
if( m_s->posx==m_s->npx-1 )
for( int j = 0; j < m_s->ndy+2; ++j)
m_TCurrent[m_s->ndx+1][j]= edgetemp;
if (m_s->posy==0)
for( int i = 0; i < m_s->ndx+2; ++i)
m_TCurrent[i][0]= edgetemp;
if (m_s->posy==m_s->npy-1)
for( int i = 0; i < m_s->ndx+2; ++i)
m_TCurrent[i][m_s->ndy+1]= edgetemp;
}
void HeatTransfer::exchange( MPI_Comm comm )
{
// Exchange ghost cells, in the order left-right-up-down
double * send_x = new double[m_s->ndx+2];
double * recv_x = new double[m_s->ndx+2];
// send to left + receive from right
int tag = 1;
MPI_Status status;
if( m_s->rank_left >= 0 )
{
std::cout << "Rank " << m_s->rank << " send left to rank " << m_s->rank_left;
for( int i = 0; i < m_s->ndx+2; ++i)
send_x[i] = m_TCurrent[i][1];
MPI_Send(send_x, m_s->ndx+2, MPI_REAL8, m_s->rank_left, tag, comm);
}
if( m_s->rank_right >= 0 )
{
std::cout << "Rank " << m_s->rank << " receive from right from rank " << m_s->rank_right;
MPI_Recv(recv_x, m_s->ndx+2, MPI_REAL8, m_s->rank_right, tag, comm, &status);
for( int i = 0; i < m_s->ndx+2; ++i)
m_TCurrent[i][m_s->ndy+1] = recv_x[i];
}
// send to right + receive from left
tag = 2;
if( m_s->rank_right >= 0 )
{
std::cout << "Rank " << m_s->rank << " send right to rank " << m_s->rank_right;
for( int i = 0; i < m_s->ndx+2; ++i)
send_x[i] = m_TCurrent[i][m_s->ndy];
MPI_Send(send_x, m_s->ndx+2, MPI_REAL8, m_s->rank_right, tag, comm);
}
if( m_s->rank_left >= 0 )
{
std::cout << "Rank " << m_s->rank << " receive from left from rank " << m_s->rank_left;
MPI_Recv(recv_x, m_s->ndx+2, MPI_REAL8, m_s->rank_left, tag, comm, &status);
for( int i = 0; i < m_s->ndx+2; ++i)
m_TCurrent[i][0] = recv_x[i];
}
// send down + receive from above
tag = 3;
if( m_s->rank_down >= 0 )
{
std::cout << "Rank " << m_s->rank << " send down to rank " << m_s->rank_down;
MPI_Send(m_TCurrent[m_s->ndx], m_s->ndy+2, MPI_REAL8, m_s->rank_down, tag, comm);
}
if ( m_s->rank_up >= 0 )
{
std::cout << "Rank " << m_s->rank << " receive from above from rank " << m_s->rank_up;
MPI_Recv(m_TCurrent[0], m_s->ndy+2, MPI_REAL8, m_s->rank_up, tag, comm, &status);
}
// send up + receive from below
tag = 4;
if( m_s->rank_up >= 0 )
{
std::cout << "Rank " << m_s->rank << " send up to rank " << m_s->rank_up;
MPI_Send(m_TCurrent[1], m_s->ndy+2, MPI_REAL8, m_s->rank_up, tag, comm);
}
if ( m_s->rank_down >= 0 )
{
std::cout << "Rank " << m_s->rank << " receive from below from rank " << m_s->rank_down;
MPI_Recv(m_TCurrent[m_s->ndx+1], m_s->ndy+2, MPI_REAL8, m_s->rank_down, tag, comm, &status);
}
delete[] send_x;
delete[] recv_x;
}