-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
50 lines (32 loc) · 943 Bytes
/
utils.h
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
#ifndef HPC2022_UTILS_H
#define HPC2022_UTILS_H
#include <mpi.h>
#include <sys/time.h>
#include <cmath>
/**
* Get the current time for logging function
*/
void get_current_time_formatted(char *buffer, long *millisec) {
struct tm *tm_info;
struct timeval tv{};
gettimeofday(&tv, nullptr);
*millisec = lrint((double) tv.tv_usec / 1000.0); // Round to nearest millisec
if (*millisec >= 1000) { // Allow for rounding up to the nearest second
*millisec -= 1000;
tv.tv_sec++;
}
tm_info = localtime(&tv.tv_sec);
strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
}
/**
* Logging function for prints
*/
void logtime() {
int process_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
char buffer[26];
long millisec;
get_current_time_formatted(buffer, &millisec);
printf("[rank %d at %s.%03ld] ", process_rank, buffer, millisec);
}
#endif //HPC2022_UTILS_H