TiledArray  0.7.0
madness.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef TILEDARRAY_MADNESS_H__INCLUDED
21 #define TILEDARRAY_MADNESS_H__INCLUDED
22 
23 // This needs to be defined before world/worldreduce.h and world/worlddc.h
24 #ifndef WORLD_INSTANTIATE_STATIC_TEMPLATES
25 #define WORLD_INSTANTIATE_STATIC_TEMPLATES
26 #endif // WORLD_INSTANTIATE_STATIC_TEMPLATES
27 
28 #include <memory>
29 #pragma GCC diagnostic push
30 #pragma GCC system_header
31 #include <madness/world/MADworld.h>
32 #include <madness/tensor/cblas.h>
33 #pragma GCC diagnostic pop
34 #include <TiledArray/error.h>
35 
36 namespace TiledArray {
37 // Import some MADNESS classes into TiledArray for convenience.
38  using madness::World;
39  using madness::Future;
40 
41  // it is useful to specify the implicit execution context for the TiledArray
42  // DSL on a per-scope basis ... this assumes that only 1 thread (usually, main)
43  // parses TiledArray DSL
44  namespace detail {
45  struct default_world {
46  static World& get() {
47  if (!world()) {
48  TA_USER_ASSERT(madness::initialized(),
49  "TiledArray::detail::default_world::get() called "
50  "before madness::initialize()");
51  world() = &madness::World::get_default();
52  }
53  return *world();
54  }
55  static void set(World* w) {
56  world() = w;
57  }
59  static World* query() {
60  return world();
61  }
62  private:
63  static World*& world() {
64  static World* world_ = nullptr;
65  return world_;
66  }
67  };
68  } // namespace detail
69 
71 
82  static void set_default_world(World& world) {
83  return detail::default_world::set(&world);
84  }
87  static World& get_default_world() {
89  }
92  static void reset_default_world() {
93  return detail::default_world::set(nullptr);
94  }
95 
96  namespace {
97  auto world_resetter = [](World* w) { set_default_world(*w); };
98  } // namespace detail
99 
116  static std::unique_ptr<World, decltype(world_resetter)>
117  push_default_world(World& world) {
118  World* current_world = detail::default_world::query();
119  set_default_world(world);
120  return std::unique_ptr<World, decltype(world_resetter)>(
121  current_world, world_resetter);
122  }
123 
127 
129  inline World& initialize(int& argc, char**& argv, const SafeMPI::Intracomm& comm) {
130  auto& default_world = madness::initialize(argc, argv, comm);
131  TiledArray::set_default_world(default_world);
132  return default_world;
133  }
134 
135  inline World& initialize(int& argc, char**& argv) {
136  return TiledArray::initialize(argc, argv, SafeMPI::COMM_WORLD);
137  }
138 
139  inline World& initialize(int& argc, char**& argv, const MPI_Comm& comm) {
140  return TiledArray::initialize(argc, argv, SafeMPI::Intracomm(comm));
141  }
142 
143  inline void finalize() {
145  TiledArray::reset_default_world();
146  }
147 
149 
150 } // namespace TiledArray
151 
152 #endif // TILEDARRAY_MADNESS_H__INCLUDED
static void set(World *w)
Definition: madness.h:55
World & initialize(int &argc, char **&argv, const SafeMPI::Intracomm &comm)
Definition: madness.h:129
void finalize()
Definition: madness.h:143
#define TA_USER_ASSERT(a, m)
Definition: error.h:123
World & initialize(int &argc, char **&argv, const MPI_Comm &comm)
Definition: madness.h:139