-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathMain.cpp
324 lines (292 loc) · 9.11 KB
/
Main.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
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
316
317
318
319
320
321
322
323
324
/*******************************************************************************
GPU OPTIMIZED MONTE CARLO (GOMC) 2.75
Copyright (C) 2022 GOMC Group
A copy of the MIT License can be found in License.txt
along with this program, also can be found at
<https://opensource.org/licenses/MIT>.
********************************************************************************/
#include "GOMC_Config.h" //For version number
#include "Simulation.h"
#if GOMC_LIB_MPI
#include <mpi.h>
#include "ParallelTemperingPreprocessor.h"
#endif
#ifdef GOMC_CUDA
#include <cuda_runtime_api.h>
#include "cuda.h"
#endif
#ifdef _OPENMP
#include <unordered_map>
#endif
#include <ctime>
#include <iostream>
// find and include appropriate files for getHostname
#ifdef _WIN32
#include <Winsock2.h>
#define HOSTNAME
#elif defined(__linux__) || defined(__apple__) || defined(__FreeBSD__)
#include <sys/sysinfo.h>
#include <sys/utsname.h>
#include <unistd.h>
#define HOSTNAME
#endif
namespace {
std::ostream &PrintTime(std::ostream &stream);
std::ostream &PrintHostname(std::ostream &stream);
std::ostream &PrintVersion(std::ostream &stream);
void PrintSimulationHeader();
void PrintSimulationFooter();
void PrintDebugMode();
bool CheckAndPrintEnsemble();
uint ReadNum(char *argv);
} // namespace
void PrintHardwareInfo();
#ifdef GOMC_CUDA
void PrintGPUHardwareInfo();
#endif
int main(int argc, char *argv[]) {
#ifdef RECORD_DEBUG
#ifdef GOMC_CUDA
remove("gpu.debug");
#else
remove("cpu.debug");
#endif
#endif
#if GOMC_LIB_MPI
ParallelTemperingPreprocessor pt(argc, argv);
MultiSim *multisim = pt.checkIfValidRank() ? new MultiSim(pt) : NULL;
#endif
#ifndef NDEBUG
PrintDebugMode();
#endif
PrintSimulationHeader();
PrintHardwareInfo();
#ifdef GOMC_CUDA
PrintGPUHardwareInfo();
#endif
// Only run if valid ensemble was detected.
if (CheckAndPrintEnsemble()) {
// FOLLOWING LINES ADDED TO OBTAIN INPUT PARAMETER FILE
std::string inputFileString;
std::fstream inputFileReader;
uint numThreads;
// CHECK IF ARGS/FILE PROVIDED IN CMD LINE
if (argc < 2) {
std::cout << "Error: Input parameter file (*.dat or *.conf) not "
"specified on command line!\n";
exit(EXIT_FAILURE);
} else {
if (argc == 2) {
// FIRST PARAMETER WILL BE FILE NAME
inputFileString = argv[1];
numThreads = 1;
} else {
// SECOND PARAMETER WILL BE FILE NAME
inputFileString = argv[2];
if (argv[1][0] == '+' && argv[1][1] == 'p') {
numThreads = ReadNum(argv[1]);
} else {
std::cout << "Error: Undefined command to set number of threads!\n";
std::cout << "Use +p# command to set number of threads.\n";
exit(EXIT_FAILURE);
}
}
}
// SET NUMBER OF THREADS
#ifdef _OPENMP
omp_set_num_threads(numThreads);
printf("%-40s %-d \n", "Info: Number of threads", numThreads);
#else
printf("%-40s %-d \n", "Info: Number of threads", 1);
#endif
#if defined _OPENMP && _OPENMP < 201511
printf("Warning: OpenMP version < 4.5. GOMC will not run optimally!\n");
#endif
// Print the OpenMP version if recognized or instead the OpenMP date code.
#ifdef _OPENMP
std::unordered_map<int, std::string> omp_map{
{200505, "2.5"}, {200805, "3.0"}, {201107, "3.1"}, {201307, "4.0"},
{201511, "4.5"}, {201611, "5.0 Preview 1"}, {201811, "5.0"},
{202011, "5.1"}, {202111, "5.2"}, {202411, "6.0"}};
auto match = omp_map.find(_OPENMP);
if (match == omp_map.end())
printf("%-40s %u\n", "Info: Compiled with OpenMP Version", _OPENMP);
else
printf("%-40s %s\n", "Info: Compiled with OpenMP Version",
match->second.c_str());
#endif
// OPEN FILE
inputFileReader.open(inputFileString.c_str(), std::ios::in | std::ios::out);
// CHECK IF FILE IS OPENED...IF NOT OPENED EXCEPTION REASON FIRED
if (!inputFileReader.is_open()) {
std::cout << "Error: Cannot open/find " << inputFileString
<< " in the directory provided!\n";
exit(EXIT_FAILURE);
}
// CLOSE FILE TO NOW PASS TO SIMULATION
inputFileReader.close();
// ONCE FILE FOUND PASS STRING TO SIMULATION CLASS TO READ AND
// HANDLE PDB|PSF FILE
#if GOMC_LIB_MPI
if (multisim != NULL) {
Simulation sim(inputFileString.c_str(), multisim);
sim.RunSimulation();
PrintSimulationFooter();
delete multisim;
MPI_Finalize();
} else {
MPI_Finalize();
}
#else
Simulation sim(inputFileString.c_str());
sim.RunSimulation();
PrintSimulationFooter();
#endif
}
return 0;
}
namespace {
void PrintSimulationHeader() {
std::cout << PrintVersion << '\n'
<< "Info: Start Time: " << PrintTime
#ifdef HOSTNAME
<< "Info: Host Name: " << PrintHostname
#endif
<< "\n";
}
bool CheckAndPrintEnsemble() {
bool healthy = true;
std::cout << "Info: GOMC COMPILED TO RUN ";
#if ENSEMBLE == NVT
std::cout << "CANONICAL (NVT)";
#elif ENSEMBLE == GEMC
std::cout << "GIBBS";
#elif ENSEMBLE == GCMC
std::cout << "GRAND CANONICAL";
#elif ENSEMBLE == NPT
std::cout << "ISOBARIC-ISOTHERMAL";
#else
std::cerr << "CRITICAL ERROR! Preprocessor value ENSEMBLE is "
<< "invalid or undefined." << std::endl
<< "Code will exit.";
healthy = false;
#endif
std::cout << " ENSEMBLE." << std::endl;
return healthy;
}
void PrintDebugMode() {
std::cout << "###############################################################"
"#################\n";
std::cout << "########################## RUNNING GOMC IN DEBUG MODE "
"##########################\n";
std::cout << "###############################################################"
"#################\n";
}
void PrintSimulationFooter() {
std::cout << PrintVersion << '\n'
<< "Info: Completed at: " << PrintTime
<< "Info: On hostname: " << PrintHostname << '\n';
}
std::ostream &PrintVersion(std::ostream &stream) {
stream << "Info: GOMC Version " << GOMC_VERSION_MAJOR << '.'
<< GOMC_VERSION_MINOR;
return stream;
}
std::ostream &PrintTime(std::ostream &stream) {
time_t timer;
time(&timer);
stream << asctime(localtime(&timer));
return stream;
}
std::ostream &PrintHostname(std::ostream &stream) {
#ifdef HOSTNAME
#ifdef _WIN32
// setup WINSOCK
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 0), &wsaData);
#endif
const int maxNameLength = 80;
char hostname[maxNameLength];
gethostname(hostname, maxNameLength);
// gethostname does not guarantee null termination
hostname[maxNameLength - 1] = '\0';
stream << hostname;
#ifdef _WIN32
// teardown WINSOCK
WSACleanup();
#endif
#else
stream << "Info: Hostname Unavailable";
#endif
return stream;
}
uint ReadNum(char *argv) {
uint thread = 0;
for (uint i = 2; argv[i] != 0; i++) {
thread = thread * 10 + (argv[i] - '0');
}
return thread;
}
} // namespace
void PrintHardwareInfo() {
#ifdef __linux__
struct sysinfo mem;
const double megabyte = 1024 * 1024;
struct utsname name;
uname(&name);
printf("CPU information:\n");
std::cout << std::setprecision(1) << std::fixed;
std::cout << "Info: Total number of CPUs: " << get_nprocs() << std::endl;
std::cout << "Info: Total number of CPUs available: "
<< sysconf(_SC_NPROCESSORS_ONLN) << std::endl;
std::cout << "Info: Model name:" << std::flush;
if (system("awk -F: '/model name/ {print $2;exit}' /proc/cpuinfo") == -1)
std::cout << "Error: Couldn't retrieve CPU information" << std::endl;
std::cout << "Info: System name: " << name.sysname << std::endl;
std::cout << "Info: Release: " << name.release << std::endl;
std::cout << "Info: Version: " << name.version << std::endl;
std::cout << "Info: Kernel Architecture: " << name.machine << std::endl;
if (sysinfo(&mem) == 0) {
std::cout << "Info: Total Ram: " << mem.totalram / megabyte << "MB"
<< std::endl;
std::cout << "Info: Used Ram: "
<< mem.totalram / megabyte - mem.freeram / megabyte << "MB"
<< std::endl;
}
char *pathname = get_current_dir_name();
std::cout << "Info: Working in the current directory: " << pathname;
std::cout << std::endl;
free(pathname);
#endif
}
#ifdef GOMC_CUDA
void PrintGPUHardwareInfo() {
int nDevices;
int fast = 0;
int fastIndex = 0;
cudaGetDeviceCount(&nDevices);
if (nDevices == 0) {
printf("There are no available device(s) that support CUDA\n");
exit(EXIT_FAILURE);
}
if (nDevices <= 4) {
printf("GPU information:\n");
for (int i = 0; i < nDevices; i++) {
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, i);
printf("Info: Device Number: %d\n", i);
printf("Info: Device name: %s\n", prop.name);
printf("Info: Memory Clock Rate (KHz): %d\n", prop.memoryClockRate);
printf("Info: Memory Bus Width (bits): %d\n", prop.memoryBusWidth);
printf("Info: Peak Memory Bandwidth (GB/s): %f\n",
2.0 * prop.memoryClockRate * (prop.memoryBusWidth / 8) / 1.0e6);
if (prop.memoryClockRate > fast) {
fast = prop.memoryClockRate;
fastIndex = i;
}
}
}
cudaSetDevice(fastIndex);
printf("Info: Using Device Number: %d\n", fastIndex);
}
#endif