-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicRoutines.cpp
234 lines (200 loc) · 6.27 KB
/
BasicRoutines.cpp
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
226
227
228
229
230
231
232
233
234
/**
* @file BasicRoutines.cpp
* @author Jiri Jaros and Vojtech Nikl\n
* Faculty of Information Technology\n
* Brno University of Technology\n
* jarosjir@fit.vutbr.cz
*
* @brief The implementation file with basic routines
*
* @version 2017
* @date 02 March 2015, 14:09 (created)\n
* 28 March 2017, 12:02 (revised)
*
* @detail
* This implementation file with basic routines and simulation parameters
*/
#include <getopt.h>
#include <string>
#include <cmath>
#include <mpi.h>
#include <omp.h>
#include "BasicRoutines.h"
#include "MaterialProperties.h"
/**
* Print parameters
*/
void TParameters::PrintParameters() const
{
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (batchMode)
{
printf("%d;%d;%ld;%ld;%ld;%2.6f;%s;", size, nThreads, edgeSize, nIterations,
diskWriteIntensity, airFlowRate,
materialFileName.c_str());
}
else
{
printf(".......... Parameters of the simulation ...........\n");
printf("Domain size : %ldx%ld\n", edgeSize, edgeSize);
printf("Number of iterations : %ld\n", nIterations);
printf("Number of MPI processes : %d\n", size);
printf("Number of OpenMP threads: %d\n", nThreads);
printf("Disk write intensity : %ld\n", diskWriteIntensity);
printf("Air flow rate : %2.6f\n", airFlowRate);
printf("Input file name : %s \n", materialFileName.c_str());
printf("Output file name : %s \n", outputFileName.c_str());
printf("Mode : %d \n", mode);
printf("...................................................\n\n");
}
} // end of PrintParameters
//------------------------------------------------------------------------------
/**
* Parsing arguments from command line
* @param [in] argc
* @param [in] argv
* @param [out] parameters - returns filled struct
*/
void ParseCommandline(int argc, char *argv[], TParameters ¶meters)
{
int c;
bool n_flag = false;
bool w_flag = false;
bool i_flag = false;
bool m_flag = false;
bool t_flag = false;
while ((c = getopt (argc, argv, "n:w:a:dvi:o:bm:pt:")) != -1)
{
switch (c)
{
case 'n':
parameters.nIterations = atol(optarg);
n_flag = true;
break;
case 'w':
parameters.diskWriteIntensity = atol(optarg);
w_flag = true;
break;
case 'a':
parameters.airFlowRate = atof(optarg);
break;
case 'd':
parameters.debugFlag = true;
break;
case 'm':
parameters.mode = atol(optarg);
m_flag = true;
break;
case 'v':
parameters.verificationFlag = true;
break;
case 'i':
i_flag = true;
parameters.materialFileName.assign(optarg);
break;
case 'o':
parameters.outputFileName.assign(optarg);
break;
case 'b':
parameters.batchMode = true;
break;
case 'p':
parameters.useParallelIO = true;
break;
case 't':
t_flag = true;
parameters.nThreads = atol(optarg);
omp_set_num_threads(parameters.nThreads);
break;
default:
fprintf(stderr,"Wrong parameter!\n");
PrintUsageAndExit();
}
} // while
if (!(n_flag && i_flag && m_flag) ||
!(parameters.mode >= 0 && parameters.mode <= 2))
{
PrintUsageAndExit();
}
// implicit I/O intensity is 50
if (!w_flag)
{
parameters.diskWriteIntensity = 50;
}
// implicit number of OpenMP threads is 1
if (!t_flag)
{
parameters.nThreads = 1;
omp_set_num_threads(parameters.nThreads);
}
} // end of ParseCommandline
//------------------------------------------------------------------------------
/**
* Verify the difference between two corresponding gridpoints in the sequential
* and parallel version. Print possible problems
* @param [in] tempsSeq
* @param [in] tempsPar
* @param [in] parameters
* @param [in] epsilon
* @return true if no problem found
*/
bool VerifyResults(const float *seqResult,
const float *parResult,
const TParameters parameters,
const float epsilon)
{
for (size_t i = 0; i < parameters.edgeSize * parameters.edgeSize; i++)
{
if (fabs(parResult[i] - seqResult[i]) > epsilon)
{
printf("Error found at position -> difference: ");
printf("[%ld, %ld] -> %e\n", i / parameters.edgeSize, i % parameters.edgeSize,
parResult[i] - seqResult[i]);
return false;
}
} // for
return true;
} //end of VerifyResults
//------------------------------------------------------------------------------
/**
* Print usage and exit.
*/
void PrintUsageAndExit()
{
fprintf(stderr,"Usage:\n");
fprintf(stderr,"Mandatory arguments:\n");
fprintf(stderr," -m [0-1] mode 0 - run sequential version\n");
fprintf(stderr," mode 1 - run parallel version\n");
fprintf(stderr," -n number of iterations\n");
fprintf(stderr," -i material hdf5 file\n");
fprintf(stderr,"Optional arguments:\n");
fprintf(stderr," -t number of OpenMP threads (default 1)\n");
fprintf(stderr," -o output hdf5 file\n");
fprintf(stderr," -w disk write intensity (how often)\n");
fprintf(stderr," -a air flow rate (values within <0.5, 0.0001> make sense\n");
fprintf(stderr," -d set debug mode (compare results from seq and par version and write them to cout)\n");
fprintf(stderr," -v verification mode (compare results of seq and par version)\n");
fprintf(stderr," -p parallel I/O mode\n");
fprintf(stderr," -b batch mode - output data in CSV format\n");
exit(EXIT_FAILURE);
} // end of print usage
//------------------------------------------------------------------------------
/**
* Print array content
* @param [in] data
* @param [im] size
*/
void PrintArray(const float *data, const size_t edgeSize)
{
for (size_t i = 0; i < edgeSize; i++)
{
printf("[Row %ld]: ", i);
for (size_t j = 0; j < edgeSize; j++)
{
printf("%e, ", data[i * edgeSize + j]);
}
printf("\n");
}
} // end of PrintArray
//------------------------------------------------------------------------------