-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtiledarray.cpp
More file actions
269 lines (235 loc) · 8.63 KB
/
tiledarray.cpp
File metadata and controls
269 lines (235 loc) · 8.63 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
#include <TiledArray/config.h>
#include <TiledArray/initialize.h>
#include <TiledArray/util/threads.h>
#include <TiledArray/math/linalg/basic.h>
#include <madness/world/safempi.h>
#ifdef TILEDARRAY_HAS_DEVICE
#include <TiledArray/device/blas.h>
#include <TiledArray/external/device.h>
#include <librett.h>
#endif
#if TILEDARRAY_HAS_TTG
#include <ttg.h>
#endif
#include <cerrno>
#include <cstdlib>
namespace TiledArray {
namespace {
#ifdef TILEDARRAY_HAS_DEVICE
/// initialize cuda environment
inline void device_initialize() {
/// initialize deviceEnv
deviceEnv::instance();
#if defined(TILEDARRAY_HAS_DEVICE)
BLASQueuePool::initialize();
#endif
// initialize LibreTT
librettInitialize();
}
/// finalize cuda environment
inline void device_finalize() {
DeviceSafeCall(device::deviceSynchronize());
librettFinalize();
#if defined(TILEDARRAY_HAS_DEVICE)
BLASQueuePool::finalize();
#endif
// although TA::deviceEnv is a singleton, must explicitly delete it so
// that the device runtime is not finalized before the deviceEnv dtor is
// called
deviceEnv::instance().reset(nullptr);
}
#endif // TILEDARRAY_HAS_DEVICE
inline bool& initialized_madworld_accessor() {
static bool flag = false;
return flag;
}
inline bool initialized_madworld() { return initialized_madworld_accessor(); }
inline bool& initialized_accessor() {
static bool flag = false;
return flag;
}
inline bool& finalized_accessor() {
static bool flag = false;
return flag;
}
inline bool& initialized_mpi_accessor() {
static bool flag = false;
return flag;
}
#if defined(TILEDARRAY_HAS_TTG) || defined(HAVE_PARSEC)
inline parsec_context_t*& initialized_parsec_ctx_accessor() {
static parsec_context_t* ctx = nullptr;
return ctx;
}
#endif
inline bool& quiet_accessor() {
static bool quiet = false;
return quiet;
}
} // namespace
} // namespace TiledArray
bool TiledArray::initialized() { return initialized_accessor(); }
bool TiledArray::finalized() { return finalized_accessor(); }
bool TiledArray::initialized_to_be_quiet() { return quiet_accessor(); }
TiledArray::World& TiledArray::initialize(int& argc, char**& argv,
const SafeMPI::Intracomm& comm,
bool quiet) {
if (initialized_madworld() && finalized())
throw Exception(
"TiledArray finalized MADWorld already, cannot re-initialize MADWorld "
"again");
if (!initialized()) {
if (!madness::initialized()) {
initialized_madworld_accessor() = true;
} else { // if MADWorld initialized, we must assume that comm is its
// default World.
if (!madness::World::is_default(comm))
throw Exception(
"MADWorld initialized before TiledArray::initialize(argc, argv, "
"comm), but not initialized with comm");
}
// if need to use TTG, and MADNESS uses PaRSEC *also*
// must initialize PaRSEC here so that both MADNESS and TTG use
// the same context
#if defined(TILEDARRAY_HAS_TTG) && defined(HAVE_PARSEC)
if (!ttg::initialized()) {
// preconditions:
// - MADWorld is NOT initialized, or initialized and used PaRSEC context
// using same communicator as comm
if (madness::initialized()) {
auto parsec_comm = reinterpret_cast<MPI_Comm>(
madness::ParsecRuntime::context()->comm_ctx);
int parsec_comm_equiv_comm = 0;
MADNESS_MPI_TEST(MPI_Comm_compare(comm.Get_mpi_comm(), parsec_comm,
&parsec_comm_equiv_comm));
if (!parsec_comm_equiv_comm)
throw std::runtime_error(
"TiledArray::initialize(): existing MADWorld's PaRSEC context "
"must use same communicator as given to TA::initialize()");
} else {
// if MPI is not initialized yet, initialize it here, and remember we
// did that
if (!SafeMPI::Is_initialized()) {
SafeMPI::Init_thread(argc, argv, MPI_THREAD_MULTIPLE);
initialized_mpi_accessor() = true;
}
// create a PaRSEC context using the given communicator
const auto nthreads = madness::ThreadPool::default_nthread();
const int num_parsec_threads = nthreads + 1;
auto* ctx = parsec_init(num_parsec_threads, &argc, &argv);
parsec_remote_dep_set_ctx(ctx, (intptr_t)comm.Get_mpi_comm());
// tell MADWorld to use this context
madness::ParsecRuntime::initialize_with_existing_context(ctx);
// remember the context and free it in finalize()
initialized_parsec_ctx_accessor() = ctx;
}
}
#endif
// initialize MADWorld, if not yet initialized
auto& default_world = initialized_madworld()
? madness::initialize(argc, argv, comm, quiet)
: *madness::World::find_instance(comm);
TiledArray::set_default_world(default_world);
#ifdef TILEDARRAY_HAS_DEVICE
TiledArray::device_initialize();
#endif
TiledArray::max_threads = TiledArray::get_num_threads();
TiledArray::set_num_threads(1);
madness::print_meminfo_disable();
initialized_accessor() = true;
quiet_accessor() = quiet;
// if have TTG, initialize it also
#if defined(TILEDARRAY_HAS_TTG)
// use same PaRSEC context as used by MADWorld if it uses PaRSEC
#if defined(HAVE_PARSEC)
ttg::initialize(argc, argv, -1, madness::ParsecRuntime::context());
#else // defined(HAVE_PARSEC)
ttg::initialize(argc, argv, -1);
#endif // defined(HAVE_PARSEC)
#endif // defined(TILEDARRAY_HAS_TTG)
// check if user specified linear algebra backend + params
auto* linalg_backend_cstr = std::getenv("TA_LINALG_BACKEND");
if (linalg_backend_cstr) {
using namespace std::literals::string_literals;
if ("scalapack"s == linalg_backend_cstr) {
TiledArray::set_linalg_backend(
TiledArray::LinearAlgebraBackend::ScaLAPACK);
} else if ("ttg"s == linalg_backend_cstr) {
TiledArray::set_linalg_backend(TiledArray::LinearAlgebraBackend::TTG);
} else if ("lapack"s == linalg_backend_cstr) {
TiledArray::set_linalg_backend(
TiledArray::LinearAlgebraBackend::LAPACK);
} else
TA_EXCEPTION(
"TiledArray::initialize: invalid value of environment variable "
"TA_LINALG_BACKEND, valid values are \"scalapack\", \"ttg\", and "
"\"lapack\"");
}
const char* linalg_distributed_minsize_cstr =
std::getenv("TA_LINALG_DISTRIBUTED_MINSIZE");
if (linalg_distributed_minsize_cstr) {
char* end;
const auto linalg_distributed_minsize =
std::strtoul(linalg_distributed_minsize_cstr, &end, 10);
if (errno == ERANGE)
TA_EXCEPTION(
"TiledArray::initialize: invalid value of environment variable "
"TA_LINALG_DISTRIBUTED_MINSIZE");
TiledArray::set_linalg_crossover_to_distributed(
linalg_distributed_minsize);
}
return default_world;
} else
throw Exception("TiledArray already initialized");
}
void TiledArray::finalize() {
// finalize in the reverse order of initialize
#if TILEDARRAY_HAS_TTG
ttg::finalize();
#endif
// reset number of threads
TiledArray::set_num_threads(TiledArray::max_threads);
TiledArray::get_default_world().gop.fence(); // this should ensure no pending
// tasks using cuda allocators
#ifdef TILEDARRAY_HAS_DEVICE
TiledArray::device_finalize();
#endif
if (initialized_madworld()) {
madness::finalize();
}
TiledArray::reset_default_world();
#if defined(TILEDARRAY_HAS_TTG) && defined(HAVE_PARSEC)
if (initialized_parsec_ctx_accessor()) {
parsec_context_wait(initialized_parsec_ctx_accessor());
parsec_fini(&initialized_parsec_ctx_accessor());
initialized_parsec_ctx_accessor() = nullptr;
}
if (initialized_mpi_accessor()) {
SafeMPI::Finalize();
}
#endif // defined(TILEDARRAY_HAS_TTG) && defined(HAVE_PARSEC)
initialized_accessor() = false;
finalized_accessor() = true;
}
TiledArray::detail::Finalizer::~Finalizer() noexcept {
static std::mutex mtx;
std::scoped_lock lock(mtx);
if (TiledArray::initialized()) {
TiledArray::finalize();
}
}
TiledArray::detail::Finalizer TiledArray::scoped_finalizer() { return {}; }
void TiledArray::ta_abort() { SafeMPI::COMM_WORLD.Abort(); }
void TiledArray::ta_abort(const std::string& m) {
std::cerr << m << std::endl;
ta_abort();
}
void TiledArray::taskq_wait_busy() {
madness::threadpool_wait_policy(madness::WaitPolicy::Busy);
}
void TiledArray::taskq_wait_yield() {
madness::threadpool_wait_policy(madness::WaitPolicy::Yield);
}
void TiledArray::taskq_wait_usleep(int us) {
madness::threadpool_wait_policy(madness::WaitPolicy::Sleep, us);
}