-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·83 lines (70 loc) · 2.09 KB
/
main.cpp
File metadata and controls
executable file
·83 lines (70 loc) · 2.09 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
#include <bits/stdc++.h>
#include "mpi.h"
#include "coordinator.h"
#include "node.h"
#include "client.h"
#include <chrono>
#include <fstream>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#else
#include <unistd.h>
#include <fstream>
#include <sstream>
#endif
using namespace std;
inline void log_performance(const string& operation, double duration, size_t memory_usage = 0) {
ofstream log_file("performance_log.txt", ios_base::app);
if (!log_file.is_open()) {
cerr << "Error opening log file!" << endl;
return;
}
log_file << "Operation: " << operation << ", Duration: " << duration << " seconds";
log_file << ", Memory Usage: " << memory_usage << " KB";
log_file << "\n";
log_file.close();
}
inline size_t get_memory_usage() {
#ifdef _WIN32
// Windows-specific memory usage measurement
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
return pmc.PrivateUsage / 1024; // Convert bytes to KB
#else
// Linux-specific memory usage measurement
ifstream status_file("/proc/self/status");
string line;
size_t memory_usage = 0;
while (getline(status_file, line)) {
if (line.substr(0, 6) == "VmRSS:") {
istringstream iss(line);
string key;
iss >> key >> memory_usage; // Read the memory usage in KB
break;
}
}
return memory_usage;
#endif
}
int main(int argc, char **argv) {
auto start_time = chrono::high_resolution_clock::now();
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if(rank == 0) {
coordinator(size-2);
} else if (rank == 1 || rank == 2) {
node(rank);
} else {
client(rank);
}
auto end_time = chrono::high_resolution_clock::now();
chrono::duration<double> duration = end_time - start_time;
size_t memory_usage = get_memory_usage();
log_performance("Total Execution", duration.count(), memory_usage);
MPI_Finalize();
return 0;
}