Interface Documentation
Version: invalid
policy.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 #if !defined(__FLECSI_PRIVATE__)
19 #error Do not include this file directly!
20 #endif
21 
22 #if !defined(FLECSI_ENABLE_LEGION)
23 #error FLECSI_ENABLE_LEGION not defined! This file depends on Legion!
24 #endif
25 
26 #include "flecsi/run/backend.hh"
27 #include "flecsi/topo/core.hh" // single_space
28 
29 #include <legion.h>
30 
31 #include <unordered_map>
32 
33 namespace flecsi {
34 namespace data {
35 
36 namespace leg {
37 inline auto &
38 run() {
39  return *Legion::Runtime::get_runtime();
40 }
41 inline auto
42 ctx() {
43  return Legion::Runtime::get_context();
44 }
45 
46 template<class T, void (Legion::Runtime::*D)(Legion::Context, T, bool)>
47 struct unique_handle {
48  unique_handle() = default;
49  unique_handle(T t) : h(t) {}
50  unique_handle(unique_handle && u) noexcept : h(std::exchange(u.h, {})) {}
51  ~unique_handle() {
52  if(*this) // it's not clear whether empty handles can be deleted
53  (run().*D)(ctx(), h, false);
54  }
55  unique_handle & operator=(unique_handle u) noexcept {
56  std::swap(h, u.h);
57  return *this;
58  }
59  explicit operator bool() {
60  return h.exists();
61  }
62  operator T() const {
63  return h;
64  }
65 
66 private:
67  T h;
68 };
69 
70 using unique_index_space =
72 // Legion seems to be buggy with destroying partitions:
73 using unique_index_partition = Legion::IndexPartition;
74 using unique_field_space =
76 using unique_logical_region = unique_handle<Legion::LogicalRegion,
77  &Legion::Runtime::destroy_logical_region>;
78 using unique_logical_partition = Legion::LogicalPartition;
79 
80 inline unique_index_space
81 index1(std::size_t n) {
82  return run().create_index_space(ctx(), Legion::Rect<1>(0, n - 1));
83 }
84 
85 struct region {
86  region(std::size_t n, const fields & fs)
87  : index_space(index1(n)),
88  field_space([&fs] { // TIP: IIFE (q.v.) allows statements here
89  auto & r = run();
90  const auto c = ctx();
91  unique_field_space ret = r.create_field_space(c);
92  Legion::FieldAllocator allocator = r.create_field_allocator(c, ret);
93  for(auto const & fi : fs)
94  allocator.allocate_field(fi->type_size, fi->fid);
95  return ret;
96  }()),
97  logical_region(
98  run().create_logical_region(ctx(), index_space, field_space)) {}
99 
100  unique_index_space index_space;
101  unique_field_space field_space;
102  unique_logical_region logical_region;
103 };
104 
105 struct partition {
106  // TODO: support create_partition_by_image_range case
107  template<class F>
108  partition(const region & reg,
109  std::size_t n,
110  F f,
111  disjointness dis = {},
112  completeness cpt = {})
113  : color_space(index1(n)),
114  index_partition(run().create_partition_by_domain(
115  ctx(),
116  reg.index_space,
117  [&] {
118  std::map<Legion::DomainPoint, Legion::Domain> ret;
119  for(std::size_t i = 0; i < n; ++i) {
120  // NB: reg.index_space is assumed to be one-dimensional.
121  const auto [b, e] = f(i);
122  ret.try_emplace(i, Legion::Rect<1>(b, e - 1));
123  }
124  return ret;
125  }(),
126  color_space,
127  true,
128  Legion::PartitionKind((dis + 2) % 3 + (cpt + 3) % 3 * 3))),
129  logical_partition(run().get_logical_partition(ctx(),
130  reg.logical_region,
131  index_partition)) {}
132 
133  std::size_t colors() const {
134  return run().get_index_space_domain(color_space).get_volume();
135  }
136 
137  unique_index_space color_space;
138  unique_index_partition index_partition;
139  unique_logical_partition logical_partition;
140 
141  template<topo::single_space>
142  const partition & get_partition() const {
143  return *this;
144  }
145 };
146 } // namespace leg
147 
148 using leg::region, leg::partition; // for backend-agnostic interface
149 
150 } // namespace data
151 } // namespace flecsi
Definition: policy.hh:105
Definition: policy.hh:47
Definition: policy.hh:85
size_t colors()
Definition: execution.hh:336
Definition: control.hh:31