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_EXTERNAL_MADNESS_H__INCLUDED
21 #define TILEDARRAY_EXTERNAL_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 
30 #include <TiledArray/config.h>
31 
32 TILEDARRAY_PRAGMA_GCC(diagnostic push)
33 TILEDARRAY_PRAGMA_GCC(system_header)
34 
35 #include <madness/world/MADworld.h>
36 #include <madness/world/worldmem.h>
37 
38 TILEDARRAY_PRAGMA_GCC(diagnostic pop)
39 
40 #include <TiledArray/error.h>
41 
42 namespace TiledArray {
43 // Import some MADNESS classes into TiledArray for convenience.
44 using madness::Future;
45 using madness::World;
46 
47 // it is useful to specify the implicit execution context for the TiledArray
48 // DSL on a per-scope basis ... this assumes that only 1 thread (usually, main)
49 // parses TiledArray DSL
50 namespace detail {
51 struct default_world {
52  static World& get() {
53  if (!world()) {
55  "TiledArray::detail::default_world::get() called "
56  "before madness::initialize()");
57  world() = &madness::World::get_default();
58  }
59  return *world();
60  }
61  static void set(World* w) { world() = w; }
63  static World* query() { return world(); }
64 
65  private:
66  static World*& world() {
67  static World* world_ = nullptr;
68  return world_;
69  }
70 };
71 } // namespace detail
72 
74 
85 inline void set_default_world(World& world) {
86  return detail::default_world::set(&world);
87 }
90 inline World& get_default_world() { return detail::default_world::get(); }
93 inline void reset_default_world() {
94  return detail::default_world::set(nullptr);
95 }
96 
97 namespace {
98 auto world_resetter = [](World* w) { set_default_world(*w); };
99 } // namespace
100 
117 inline std::unique_ptr<World, decltype(world_resetter)> push_default_world(
118  World& world) {
119  World* current_world = detail::default_world::query();
120  set_default_world(world);
121  return std::unique_ptr<World, decltype(world_resetter)>(current_world,
122  world_resetter);
123 }
124 
125 } // namespace TiledArray
126 
127 #endif // TILEDARRAY_EXTERNAL_MADNESS_H__INCLUDED
std::unique_ptr< World, decltype(world_resetter)> push_default_world(World &world)
Definition: madness.h:117
void set_default_world(World &world)
Sets the default World to world .
Definition: madness.h:85
static void set(World *w)
Definition: madness.h:61
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
World & get_default_world()
Definition: madness.h:90
void reset_default_world()
Definition: madness.h:93
bool initialized()
Definition: tiledarray.cpp:63