forked from ammarhakim/gkyl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgkyl.cxx
215 lines (187 loc) · 5.58 KB
/
gkyl.cxx
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
// Gkyl ------------------------------------------------------------------------
//
// Top-level entry point into Gkyl
// _______ ___
// + 6 @ |||| # P ||| +
//------------------------------------------------------------------------------
// gkyl includes
#include <gkylconfig.h>
#include <gkylgittip.h>
#include <Gkyl.h>
#ifdef HAVE_MPI_H
# include <mpi.h>
#endif
// std includes
#include <algorithm>
#include <cfenv>
#include <iostream>
#include <list>
#include <stdexcept>
#include <string>
#include <unistd.h>
// Compiler specific includes
#if defined(__GNUC__) || defined(__GNUG__)
#if defined(__arm__) || defined(__arm64__)
// nothing for arm chips
#else
#include <xmmintrin.h>
#endif
#endif
#if defined(__clang__)
#if defined(__APPLE__)
#if defined(__arm__) || defined(__arm64__)
// nothing for Apple m1 chip
#else
#include <fenv.h>
#endif
#endif
#endif
// Compiler specifics defines
#if defined(__clang__)
# if defined(__APPLE__)
# define GKYL_COMPILER_ID "Apple Clang"
# else
# define GKYL_COMPILER_ID "Clang"
# endif
#elif defined(__ICC)
# define GKYL_COMPILER_ID "Intel"
#elif defined(__PGI)
# define GKYL_COMPILER_ID "PGI"
#elif (__GNUC__) || defined(__GNUG__)
# define GKYL_COMPILER_ID "GCC"
#else
# define GKYL_COMPILER_ID "UNKNOWN"
#endif
// Finish simulation
int finish(int err) {
int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Finalize();
return err;
}
// show usage
void showUsage() {
std::cout << "This is the Gkeyll code. See gkeyll.rtfd.io for details." << std::endl;
std::cout << "Type 'gkyl man' for help." << std::endl;
std::cout << std::endl;
std::cout << "gkyl [OPTIONS] app/tool [APP-OPTIONS]" << std::endl;
std::cout << "Available options are" << std::endl;
std::cout << " -e chunk Execute string 'chunk'" << std::endl;
std::cout << " -t Show list of registered tools" << std::endl;
std::cout << " -v Show version information" << std::endl;
std::cout << " -g Run on NVIDIA GPU (if available and built with CUDA)" << std::endl;
std::cout << std::endl;
std::cout << "Most app input files take the following commands:" << std::endl;
std::cout << " run Run simulation. Default if nothing specified" << std::endl;
std::cout << " init Only initialize simulation but do not run it" << std::endl;
std::cout << " restart Restart simulation from last saved restart frame" << std::endl;
std::cout << std::endl;
std::cout << "Individual tools may take other options and commands. See their specific help." << std::endl;
}
// show version information
void showVersion() {
std::cout << "Changeset: " << GKYL_GIT_CHANGESET << std::endl;
std::cout << "Built on: " << __DATE__ << " " << __TIME__ << std::endl;
std::cout << "Built with: " << GKYL_COMPILER_ID << " compiler and";
#ifdef HAVE_MPI_H
std::cout << " MPI";
#endif
#ifdef HAVE_ADIOS2_C_H
std::cout << " Adios";
#endif
#ifdef USING_SQLITE3
std::cout << " Sqlite3";
#endif
#ifdef HAVE_ZMQ_H
std::cout << " ZeroMQ";
#endif
#ifdef HAVE_CUDA_H
std::cout << " Cuda";
#endif
#ifdef HAVE_GKYLZERO_H
std::cout << " gkylzero";
#endif
std::cout << std::endl;
}
// show tool list
void showToolList(const std::vector<std::pair<std::string, std::string>>& toolList) {
size_t maxSz = 0;
for (auto t : toolList) maxSz = std::max(maxSz, t.first.length());
std::cout << "Following tools are available. Query tool help for more information." << std::endl;
for (auto t : toolList)
std::cout << " " << t.first << std::string(maxSz-t.first.length() + 2, ' ') << t.second << std::endl;
}
int
main(int argc, char **argv) {
// This prevents denormalized floats from occuring in
// code. Otherwise, the code become horribly slow in some rare (but
// not impossible to reproduce) situations.
#if defined(__GNUC__) || defined(__GNUG__)
#if defined(__arm__) || defined(__arm64__)
// nothing for arm chips
#else
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
#endif
#endif
#if defined(__clang__)
#if defined(__APPLE__)
#if defined(__arm__) || defined(__arm64__)
// nothing for Apple m1 chip
#else
fesetenv(FE_DFL_DISABLE_SSE_DENORMS_ENV);
#endif
#endif
#endif
MPI_Init(&argc, &argv);
bool optShowToolList = false;
std::string luaExpr;
// parse options: I am not using any heavy-duty tools as basic
// requirements are very simple. Sadly, even "sophisticated" tools
// do not support very special need of gkeyll in which app or tool
// can have complex command parsers of their own
int c;
while ((c = getopt(argc, argv, "+hvte:g")) != -1)
switch (c)
{
case 'h':
showUsage();
return finish(0);
break;
case 'v':
showVersion();
return finish(0);
case 't':
optShowToolList = true;
break;
case 'e':
luaExpr.append(optarg);
break;
case 'g':
luaExpr.append("GKYL_USE_GPU = true");
break;
case '?':
return finish(0);
}
std::list<std::string> argRest;
// collect remaining options into a list
for (; optind < argc; ++optind)
argRest.push_back(argv[optind]);
std::string inpFile;
if (argRest.size() > 0) {
inpFile = argRest.front(); // first remaining argument is input file
argRest.pop_front();
}
int status = 0;
// create top-level object
try {
Gkyl app(luaExpr, inpFile, argRest);
// we need to handle tool-list show here as list of tools is
// constructed in app
if (optShowToolList) showToolList(app.getToolList());
status = app.run();
}
catch (const std::runtime_error& e) {
status = 1;
std::cout << e.what() << std::endl;
}
return finish(status);
}