Interface Documentation
Version: invalid
context.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #include <flecsi-config.h>
19 
20 #if !defined(__FLECSI_PRIVATE__)
21 #error Do not include this file directly!
22 #endif
23 
24 #include "flecsi/run/context.hh"
25 //#include "flecsi/exec/launch.hh"
26 //#include "flecsi/exec/processor.hh"
27 #include "flecsi/run/types.hh"
28 #include "flecsi/util/common.hh"
29 
30 #if !defined(FLECSI_ENABLE_LEGION)
31 #error FLECSI_ENABLE_LEGION not defined! This file depends on Legion!
32 #endif
33 
34 #include <legion.h>
35 
36 #if !defined(FLECSI_ENABLE_MPI)
37 #error FLECSI_ENABLE_MPI not defined! This file depends on MPI!
38 #endif
39 
40 #include <mpi.h>
41 
42 #include <functional>
43 #include <map>
44 #include <string_view>
45 #include <unordered_map>
46 
47 namespace flecsi::run {
48 
49 const size_t FLECSI_TOP_LEVEL_TASK_ID = 0;
50 const size_t FLECSI_MAPPER_FORCE_RANK_MATCH = 0x00001000;
51 const size_t FLECSI_MAPPER_COMPACTED_STORAGE = 0x00002000;
52 const size_t FLECSI_MAPPER_SUBRANK_LAUNCH = 0x00003000;
53 const size_t FLECSI_MAPPER_EXCLUSIVE_LR = 0x00004000;
54 
55 namespace leg {
56 template<class R = void>
57 using task = R(const Legion::Task *,
58  const std::vector<Legion::PhysicalRegion> &,
59  Legion::Context,
60  Legion::Runtime *);
61 }
62 
63 struct context_t : context {
64 
65  /*
66  Friend declarations. Some parts of this interface are intentionally private
67  to avoid inadvertent corruption of initialization logic.
68  */
69 
70  friend leg::task<> top_level_task;
71 
72  //--------------------------------------------------------------------------//
73  // Runtime.
74  //--------------------------------------------------------------------------//
75 
76  /*
77  Documentation for this interface is in the top-level context type.
78  */
79 
80  int initialize(int argc, char ** argv, bool dependent);
81 
82  /*
83  Documentation for this interface is in the top-level context type.
84  */
85 
86  void finalize();
87 
88  /*
89  Documentation for this interface is in the top-level context type.
90  */
91 
92  int start(const std::function<int()> &);
93 
94  /*
95  Documentation for this interface is in the top-level context type.
96  */
97 
98  void clear() {} // clear
99 
100  /*
101  Documentation for this interface is in the top-level context type.
102  */
103 
104  size_t process() const {
105  return context::process_;
106  } // process
107 
108  /*
109  Documentation for this interface is in the top-level context type.
110  */
111 
112  size_t processes() const {
113  return context::processes_;
114  } // processes
115 
116  /*
117  Documentation for this interface is in the top-level context type.
118  */
119 
120  size_t threads_per_process() const {
121  return context::threads_per_process_;
122  } // threads_per_process
123 
124  /*
125  Documentation for this interface is in the top-level context type.
126  */
127 
128  size_t threads() const {
129  return context::threads_;
130  } // threads
131 
132  /*
133  Documentation for this interface is in the top-level context type.
134  */
135 
136  static size_t task_depth() {
137  return Legion::Runtime::get_runtime()
138  ->get_current_task(Legion::Runtime::get_context())
139  ->get_depth();
140  } // task_depth
141 
142  /*
143  Documentation for this interface is in the top-level context type.
144  */
145 
146  static size_t color() {
147  flog_assert(
148  task_depth() > 0, "this method can only be called from within a task");
149  return Legion::Runtime::get_runtime()
150  ->get_current_task(Legion::Runtime::get_context())
151  ->index_point.point_data[0];
152  } // color
153 
154  /*
155  Documentation for this interface is in the top-level context type.
156  */
157 
158  static size_t colors() {
159  flog_assert(
160  task_depth() > 0, "this method can only be called from within a task");
161  return Legion::Runtime::get_runtime()
162  ->get_current_task(Legion::Runtime::get_context())
163  ->index_domain.get_volume();
164  } // colors
165 
166  //--------------------------------------------------------------------------//
167  // MPI interoperability.
168  //--------------------------------------------------------------------------//
169 
175  void set_mpi_task(std::function<void()> mpi_task) {
176  {
177  log::devel_guard guard(context_tag);
178  flog_devel(info) << "In set_mpi_task" << std::endl;
179  }
180 
181  mpi_task_ = std::move(mpi_task);
182  }
183 
188  void set_all_processes(const LegionRuntime::Arrays::Rect<1> & all_processes) {
189  all_processes_ = all_processes;
190  } // all_processes
191 
196  const LegionRuntime::Arrays::Rect<1> & all_processes() const {
197  return all_processes_;
198  } // all_processes
199 
207  void handoff_to_mpi(Legion::Context & ctx, Legion::Runtime * runtime);
208 
218  Legion::FutureMap wait_on_mpi(Legion::Context & ctx,
219  Legion::Runtime * runtime);
220 
228  void connect_with_mpi(Legion::Context & ctx, Legion::Runtime * runtime);
229 
230 private:
235  void handoff_to_legion() {
236  {
237  log::devel_guard guard(context_tag);
238  flog_devel(info) << "In handoff_to_legion" << std::endl;
239  }
240 
241  MPI_Barrier(MPI_COMM_WORLD);
242  handshake_.mpi_handoff_to_legion();
243  } // handoff_to_legion
244 
249  void wait_on_legion() {
250  {
251  log::devel_guard guard(context_tag);
252  flog_devel(info) << "In wait_on_legion" << std::endl;
253  }
254 
255  handshake_.mpi_wait_on_legion();
256  MPI_Barrier(MPI_COMM_WORLD);
257  } // wait_on_legion
258 
259  // When GCC fixes bug #83258, these can be lambdas in the public functions:
264  static void mpi_handoff() {
265  instance().handshake_.legion_handoff_to_mpi();
266  }
267 
272  static void mpi_wait() {
273  instance().handshake_.legion_wait_on_mpi();
274  }
275 
282  bool invoke_mpi_task() {
283  const bool ret(mpi_task_);
284  if(ret) {
285  mpi_task_();
286  mpi_task_ = nullptr;
287  }
288  return ret;
289  } // invoke_mpi_task
290 
291  /*--------------------------------------------------------------------------*
292  Runtime data.
293  *--------------------------------------------------------------------------*/
294 
295  const std::function<int()> * top_level_action_ = nullptr;
296 
297  /*--------------------------------------------------------------------------*
298  Interoperability data members.
299  *--------------------------------------------------------------------------*/
300 
301  std::function<void()> mpi_task_;
302  Legion::MPILegionHandshake handshake_;
303  LegionRuntime::Arrays::Rect<1> all_processes_;
304 };
305 
306 } // namespace flecsi::run
size_t threads_per_process()
Definition: execution.hh:303
void set_all_processes(const LegionRuntime::Arrays::Rect< 1 > &all_processes)
Definition: context.hh:188
#define flog_assert(test, message)
Definition: flog.hh:411
void set_mpi_task(std::function< void()> mpi_task)
Definition: context.hh:175
int start(const std::function< int()> &action)
Definition: execution.hh:69
Definition: flog.hh:82
Definition: backend.hh:41
size_t processes()
Definition: execution.hh:294
int initialize(int argc, char **argv, bool dependent=true)
Definition: execution.hh:55
size_t process()
Definition: execution.hh:285
void finalize()
Definition: execution.hh:80
const LegionRuntime::Arrays::Rect< 1 > & all_processes() const
Definition: context.hh:196
Definition: context.hh:64
size_t color()
Definition: execution.hh:326
size_t colors()
Definition: execution.hh:336
Definition: context.hh:63
size_t threads()
Definition: execution.hh:316