tiledarray.cpp
Go to the documentation of this file.
1 #include <TiledArray/config.h>
3 
4 #ifdef TILEDARRAY_HAS_CUDA
7 #include <cutt.h>
8 #endif
9 
10 #ifdef TILEDARRAY_HAS_INTEL_MKL
11 #include <mkl_service.h>
12 #endif
13 
14 namespace TiledArray {
15 namespace {
16 
17 #ifdef TILEDARRAY_HAS_CUDA
18 inline void cuda_initialize() {
21  cudaEnv::instance();
22  //
23  cuBLASHandlePool::handle();
24  // initialize cuTT
25  cuttInitialize();
26 }
27 
29 inline void cuda_finalize() {
30  CudaSafeCall(cudaDeviceSynchronize());
31  cuttFinalize();
32  cublasDestroy(cuBLASHandlePool::handle());
33  delete &cuBLASHandlePool::handle();
34  cudaEnv::instance().reset(nullptr);
35 }
36 #endif
37 
38 inline bool& initialized_madworld_accessor() {
39  static bool flag = false;
40  return flag;
41 }
42 inline bool initialized_madworld() { return initialized_madworld_accessor(); }
43 inline bool& initialized_accessor() {
44  static bool flag = false;
45  return flag;
46 }
47 inline bool& finalized_accessor() {
48  static bool flag = false;
49  return flag;
50 }
51 #ifdef TILEDARRAY_HAS_INTEL_MKL
52 inline int& mklnumthreads_accessor() {
53  static int value = -1;
54  return value;
55 }
56 #endif
57 
58 } // namespace
59 } // namespace TiledArray
60 
63 bool TiledArray::initialized() { return initialized_accessor(); }
64 
66 bool TiledArray::finalized() { return finalized_accessor(); }
67 
75 
77 
80 TiledArray::World& TiledArray::initialize(int& argc, char**& argv,
81  const SafeMPI::Intracomm& comm,
82  bool quiet) {
83  if (initialized_madworld() && finalized())
84  throw Exception(
85  "TiledArray finalized MADWorld already, cannot re-initialize MADWorld "
86  "again");
87  if (!initialized()) {
88  if (!madness::initialized())
89  initialized_madworld_accessor() = true;
90  else { // if MADWorld initialized, we must assume that comm is its default
91  // World.
92  if (madness::World::is_default(comm))
93  throw Exception(
94  "MADWorld initialized before TiledArray::initialize(argc, argv, "
95  "comm), but not initialized with comm");
96  }
97  auto& default_world = initialized_madworld()
98  ? madness::initialize(argc, argv, comm, quiet)
99  : *madness::World::find_instance(comm);
100  TiledArray::set_default_world(default_world);
101 #ifdef TILEDARRAY_HAS_CUDA
102  TiledArray::cuda_initialize();
103 #endif
104 #ifdef TILEDARRAY_HAS_INTEL_MKL
105  // record number of MKL threads and set to 1
106  mklnumthreads_accessor() = mkl_get_max_threads();
107  mkl_set_num_threads(1);
108 #endif
109  madness::print_meminfo_disable();
110  initialized_accessor() = true;
111  return default_world;
112  } else
113  throw Exception("TiledArray already initialized");
114 }
115 
119 #ifdef TILEDARRAY_HAS_INTEL_MKL
120  // reset number of MKL threads
121  mkl_set_num_threads(mklnumthreads_accessor());
122 #endif
123 #ifdef TILEDARRAY_HAS_CUDA
124  TiledArray::cuda_finalize();
125 #endif
127  .gop.fence(); // TODO remove when madness::finalize() fences
128  if (initialized_madworld()) {
130  }
132  initialized_accessor() = false;
133  finalized_accessor() = true;
134 }
135 
137  std::abort();
138 }
139 
140 void TiledArray::ta_abort(const std::string &m) {
141  std::cerr << m << std::endl;
142  ta_abort();
143 }
void ta_abort()
Definition: tiledarray.cpp:136
void set_default_world(World &world)
Sets the default World to world .
Definition: madness.h:85
World & initialize(int &argc, char **&argv, const SafeMPI::Intracomm &comm, bool quiet=true)
Definition: tiledarray.cpp:80
World & get_default_world()
Definition: madness.h:90
void finalize()
Definition: tiledarray.cpp:118
void reset_default_world()
Definition: madness.h:93
bool initialized()
Definition: tiledarray.cpp:63
bool finalized()
Definition: tiledarray.cpp:66