-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
316 lines (272 loc) · 8.4 KB
/
Copy pathUtils.cpp
File metadata and controls
316 lines (272 loc) · 8.4 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/************************************************************************
* Copyright © 2020 The Multiphysics Modeling and Computation (M2C) Lab
* <kevin.wgy@gmail.com> <kevinw3@vt.edu>
************************************************************************/
#include <iostream>
#include <Utils.h>
#include <time.h>
#include <version.h>
#include <stdio.h>
#include <cstring>
#include <cmath> //floor
#include <unistd.h> //sleep
using std::vector;
using std::cout;
using std::endl;
extern MPI_Comm a2c_comm;
//--------------------------------------------------
// MPI Rank 0 will print to stdout
void print(const char format[],...)
{
int rank;
MPI_Comm_rank(a2c_comm, &rank);
if(!rank) {
va_list Argp;
va_start(Argp, format);
vprintf(format, Argp);
va_end(Argp);
}
MPI_Barrier(a2c_comm);
return;
}
//--------------------------------------------------
// MPI Rank 0 will print to stdout
void print(MPI_Comm& comm, const char format[],...)
{
int rank;
MPI_Comm_rank(comm, &rank);
if(!rank) {
va_list Argp;
va_start(Argp, format);
vprintf(format, Argp);
va_end(Argp);
}
MPI_Barrier(comm);
return;
}
//--------------------------------------------------
// MPI Rank 0 will print to stdout in red color
void print_error(const char format[],...)
{
int rank;
MPI_Comm_rank(a2c_comm, &rank);
if(!rank) {
char format_colored[strlen(format)+40] = "";
strcat(format_colored, "\033[0;31m");
strcat(format_colored, format);
strcat(format_colored, "\033[0m");
va_list Argp;
va_start(Argp, format);
vprintf(format_colored, Argp);
va_end(Argp);
}
MPI_Barrier(a2c_comm);
return;
}
//--------------------------------------------------
// MPI Rank 0 will print to stdout in red color
void print_error(MPI_Comm& comm, const char format[],...)
{
int rank;
MPI_Comm_rank(comm, &rank);
if(!rank) {
char format_colored[strlen(format)+40] = "";
strcat(format_colored, "\033[0;31m");
strcat(format_colored, format);
strcat(format_colored, "\033[0m");
va_list Argp;
va_start(Argp, format);
vprintf(format_colored, Argp);
va_end(Argp);
}
MPI_Barrier(comm);
return;
}
//--------------------------------------------------
// MPI Rank 0 will print to stdout in purple color
void print_warning(const char format[],...)
{
int rank;
MPI_Comm_rank(a2c_comm, &rank);
if(!rank) {
char format_colored[strlen(format)+40] = "";
strcat(format_colored, "\033[0;35m");
strcat(format_colored, format);
strcat(format_colored, "\033[0m");
va_list Argp;
va_start(Argp, format);
vprintf(format_colored, Argp);
va_end(Argp);
}
MPI_Barrier(a2c_comm);
return;
}
//--------------------------------------------------
// MPI Rank 0 will print to stdout in purple color
void print_warning(MPI_Comm& comm, const char format[],...)
{
int rank;
MPI_Comm_rank(comm, &rank);
if(!rank) {
char format_colored[strlen(format)+40] = "";
strcat(format_colored, "\033[0;35m");
strcat(format_colored, format);
strcat(format_colored, "\033[0m");
va_list Argp;
va_start(Argp, format);
vprintf(format_colored, Argp);
va_end(Argp);
}
MPI_Barrier(comm);
return;
}
//--------------------------------------------------
// MPI Rank i will print to stdout
/*
void print(int i, const char format[],...)
{
int rank;
MPI_Comm_rank(a2c_comm,&rank);
if(rank == i) {
va_list Argp;
va_start(Argp, format);
vprintf(format, Argp);
va_end(Argp);
}
MPI_Barrier(a2c_comm);
return;
}
*/
//--------------------------------------------------
// MPI Rank i will print to stdout
void print(MPI_Comm& comm, int i, const char format[],...)
{
int rank;
MPI_Comm_rank(comm, &rank);
if(rank == i) {
va_list Argp;
va_start(Argp, format);
vprintf(format, Argp);
va_end(Argp);
}
MPI_Barrier(comm);
return;
}
//--------------------------------------------------
// MPI Rank 0 will print to a file
void print(FILE* fd, const char format[],...)
{
int rank;
MPI_Comm_rank(a2c_comm,&rank);
if(!rank) {
va_list Argp;
va_start(Argp, format);
vfprintf(fd, format, Argp);
va_end(Argp);
}
//MPI_Barrier(a2c_comm); (This slows down the code!)
return;
}
//--------------------------------------------------
// MPI Rank 0 will print to a file
void print(MPI_Comm& comm, FILE* fd, const char format[],...)
{
int rank;
MPI_Comm_rank(comm, &rank);
if(!rank) {
va_list Argp;
va_start(Argp, format);
vfprintf(fd, format, Argp);
va_end(Argp);
}
//MPI_Barrier(comm); (This slows down the code)
return;
}
//--------------------------------------------------
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const string getCurrentDateTime()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d.%X ", &tstruct);
if(strlen(tzname[1]) != 0)
strcat(buf, tzname[1]); //daylight saving time
else
strcat(buf, tzname[0]); //standard time
return buf;
}
//--------------------------------------------------
//--------------------------------------------------
// Print logo
void printHeader(int argc, char *argv[])
{
int size, rank;
MPI_Comm_size(a2c_comm, &size);
MPI_Comm_rank(a2c_comm, &rank);
if(!rank) {
cout << endl;
cout << "\033[0;38;5;162m .----------------. .----------------. .----------------. \033[0m" << endl;
cout << "\033[0;38;5;162m | .--------------. || .--------------. || .--------------. | \033[0m" << endl;
cout << "\033[0;38;5;162m | | __ | || | _____ | || | ______ | | \033[0m" << endl;
cout << "\033[0;38;5;162m | | / \\ | || | / ___ `. | || | .' ___ | | | \033[0m" << endl;
cout << "\033[0;38;5;162m | | / /\\ \\ | || | |_/___) | | || | / .' \\_| | | \033[0m" << endl;
cout << "\033[0;38;5;162m | | / ____ \\ | || | .'____.' | || | | | | | \033[0m" << endl;
cout << "\033[0;38;5;162m | | _/ / \\ \\_ | || | / /____ | || | \\ `.___.'\\ | | \033[0m" << endl;
cout << "\033[0;38;5;162m | ||____| |____|| || | |_______| | || | `._____.' | | \033[0m" << endl;
cout << "\033[0;38;5;162m | | | || | | || | | | \033[0m" << endl;
cout << "\033[0;38;5;162m | '--------------' || '--------------' || '--------------' | \033[0m" << endl;
cout << "\033[0;38;5;162m '----------------' '----------------' '----------------' \033[0m" << endl;
cout << endl;
cout << "Revision: " << GIT_REV << " | " << "Branch: " << GIT_BRANCH << " | " << "Tag: " << GIT_TAG << endl;
cout << "Computation started at: " << getCurrentDateTime() << endl;
cout << "Using " << size << " processor cores (including concurrent programs)." << endl;
cout << "Command:";
for(int i=0; i<argc; i++)
cout << " " << argv[i];
cout << endl;
cout << endl;
cout << "\033[0;32m==========================================\033[0m" << endl;
cout << "\033[0;32m START \033[0m" << endl;
cout << "\033[0;32m==========================================\033[0m" << endl;
cout << endl;
cout.flush();
} else
sleep(0.1); //wait until the message gets printed to the screen
//MPI_Barrier(a2c_comm); //not friendly to concurrent programs
//
return;
}
//--------------------------------------------------
// Terminate program properly
void exit_mpi()
{
// MPI_Finalize(); //This would not work if there are concurrent programs (other than DMD)
exit(-1);
}
//--------------------------------------------------
bool isTimeToWrite(double time, double dt, int time_step, double frequency_dt, int frequency,
double last_snapshot_time, bool force_write)
{
//! If "force_write", just avoid duplication
if(force_write)
return (time > last_snapshot_time + 0.1*dt);
//! Check frequency_dt. If it is not specified, use frequency
if(frequency_dt > 0) {
if(floor(time/frequency_dt) != floor((time-dt)/frequency_dt))
return true;
else
return false;
}
else { //!< use frequency
if((frequency > 0) && (time_step % frequency == 0))
return true;
else
return false;
}
}
//--------------------------------------------------
//--------------------------------------------------