domains.hh 8.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#ifndef DOMAINS_HH_
#define DOMAINS_HH_

#include "legion.h"
using namespace Legion;

namespace ProjectionIDs {
enum ProjectionIDs {
  IDENTITY,
  TO_1D_FROM_2D,
  TO_1D_FROM_2D_TRANSPOSED  
};
} // namespace



struct Data1D {
  Data1D(coord_t n_grid_cells_, coord_t n_parts_,
         const std::vector<unsigned>& field_ids_,
         const std::string& name_,
         Context ctx_, Runtime* runtime_) :
      name(name_),
      n_grid_cells(n_grid_cells_),
      domain(Point<1>(0), Point<1>(n_grid_cells_-1)),
      field_ids(field_ids_),
      n_parts(n_parts_),
      color_domain(Point<1>(0), Point<1>(n_parts_-1)),
      ctx(ctx_),
      runtime(runtime_)
  {
    
    index_space = runtime->create_index_space<1, coord_t>(ctx, domain);

    auto block = Point<1>(n_grid_cells / n_parts);
    index_partition = runtime->create_partition_by_blockify<1,coord_t>(ctx, index_space, block);
    runtime->attach_name(index_partition, (std::string("index_partition_")+name).c_str());

    field_space = runtime->create_field_space(ctx);
    {
      FieldAllocator allocator = runtime->create_field_allocator(ctx, field_space);
      for (auto fid : field_ids) allocator.allocate_field(sizeof(double), fid);
    }
    runtime->attach_name(field_space, (std::string("field_space_")+name).c_str());

    logical_region = runtime->create_logical_region(ctx, index_space, field_space);
    runtime->attach_name(logical_region, (std::string("logical_region_")+name).c_str());
    logical_partition = runtime->get_logical_partition(ctx, logical_region, index_partition);
    runtime->attach_name(logical_partition, (std::string("logical_partition_")+name).c_str());
  }

  ~Data1D() {
    runtime->destroy_logical_partition(ctx, logical_partition);
    runtime->destroy_logical_region(ctx, logical_region);
    runtime->destroy_field_space(ctx, field_space);
    runtime->destroy_index_partition(ctx, index_partition);
    runtime->destroy_index_space(ctx, index_space);
  }
  
  std::string name;
  
  coord_t n_grid_cells;
  const Rect<1> domain;
  IndexSpaceT<1,coord_t> index_space;
  IndexPartitionT<1, coord_t> index_partition;

  std::vector<unsigned> field_ids;
  FieldSpace field_space;

  LogicalRegion logical_region;
  LogicalPartition logical_partition;

  coord_t n_parts;
  const Rect<1> color_domain;

  const static unsigned projection_id = ProjectionIDs::IDENTITY;
  Context ctx;
  Runtime* runtime;
};



struct Data2D {
  Data2D(coord_t n_grid_cells_, coord_t n_second_, coord_t n_parts_,
         const std::string& name_,
         const std::vector<unsigned>& field_ids_,
         Context ctx_, Runtime* runtime_) :
      name(name_),
      n_grid_cells(n_grid_cells_),
      n_second(n_second_),
      domain(Point<2>(0,0), Point<2>(n_grid_cells_-1, n_second_-1)),
      field_ids(field_ids_),
      n_parts(n_parts_),
      color_domain(Point<2>(0,0), Point<2>(n_parts_-1,0)),
      ctx(ctx_),
      runtime(runtime_)
  {
    index_space = runtime->create_index_space<2,coord_t>(ctx, domain);

    auto block = Point<2>(n_grid_cells / n_parts, n_second);
    index_partition = runtime->create_partition_by_blockify<2,coord_t>(ctx, index_space, block);
    runtime->attach_name(index_partition, (std::string("index_partition_")+name).c_str());

    field_space = runtime->create_field_space(ctx);
    {
      FieldAllocator allocator = runtime->create_field_allocator(ctx, field_space);
      for (auto fid : field_ids) allocator.allocate_field(sizeof(double), fid);
    }
    runtime->attach_name(field_space, (std::string("field_space_")+name).c_str());

    logical_region = runtime->create_logical_region(ctx, index_space, field_space);
    runtime->attach_name(logical_region, (std::string("logical_region_")+name).c_str());
    logical_partition = runtime->get_logical_partition(ctx, logical_region, index_partition);
    runtime->attach_name(logical_partition, (std::string("logical_partition_")+name).c_str());
  }

  ~Data2D() {
    runtime->destroy_logical_partition(ctx, logical_partition);
    runtime->destroy_logical_region(ctx, logical_region);
    runtime->destroy_field_space(ctx, field_space);
    runtime->destroy_index_partition(ctx, index_partition);
    runtime->destroy_index_space(ctx, index_space);
  }
  
  std::string name;

  coord_t n_grid_cells, n_second;
  const Rect<2> domain;
  IndexSpaceT<2,coord_t> index_space;
  IndexPartitionT<2, coord_t> index_partition;

  std::vector<unsigned> field_ids;
  FieldSpace field_space;

  LogicalRegion logical_region;
  LogicalPartition logical_partition;

  coord_t n_parts;
  const Rect<2> color_domain;

  Context ctx;
  Runtime* runtime;

  class LocalProjectionFunction : public ProjectionFunctor{
   public:
    LocalProjectionFunction() {}
    LocalProjectionFunction(Runtime *rt) {}
    virtual ~LocalProjectionFunction() {}
   public:
    virtual LogicalRegion project(const Mappable *mappable, unsigned index,
            LogicalRegion upper_bound,
            const DomainPoint &point) override {
      assert(false);
    }
    virtual LogicalRegion project(const Mappable *mappable, unsigned index,
            LogicalPartition upper_bound,
            const DomainPoint &point) override {
      Point<2,coord_t> color; color[0] = point[0]; color[1] = 0;
      return runtime->get_logical_subregion_by_color(upper_bound, color);
    }
    virtual bool is_exclusive(void) const override { return true; }
    virtual unsigned get_depth(void) const override { return 0; }
  };

  const static unsigned projection_id = ProjectionIDs::TO_1D_FROM_2D;

};
  

struct Data2D_Transposed {
  Data2D_Transposed(coord_t n_grid_cells_, coord_t n_second_, coord_t n_parts_,
                  const std::string& name_,
                  const std::vector<unsigned>& field_ids_,
                  Context ctx_, Runtime* runtime_) :
      name(name_),
      n_grid_cells(n_grid_cells_),
      n_second(n_second_),
      domain(Point<2>(0,0), Point<2>(n_second_-1, n_grid_cells_-1)),
      field_ids(field_ids_),
      n_parts(n_parts_),
      color_domain(Point<2>(0,0), Point<2>(0,n_parts_-1)),
      ctx(ctx_),
      runtime(runtime_)
  {
    index_space = runtime->create_index_space<2,coord_t>(ctx, domain);

    auto block = Point<2>(n_second, n_grid_cells / n_parts);
    index_partition = runtime->create_partition_by_blockify<2,coord_t>(ctx, index_space, block);
    runtime->attach_name(index_partition, (std::string("index_partition_")+name).c_str());

    field_space = runtime->create_field_space(ctx);
    {
      FieldAllocator allocator = runtime->create_field_allocator(ctx, field_space);
      for (auto fid : field_ids) allocator.allocate_field(sizeof(double), fid);
    }
    runtime->attach_name(field_space, (std::string("field_space_")+name).c_str());

    logical_region = runtime->create_logical_region(ctx, index_space, field_space);
    runtime->attach_name(logical_region, (std::string("logical_region_")+name).c_str());
    logical_partition = runtime->get_logical_partition(ctx, logical_region, index_partition);
    runtime->attach_name(logical_partition, (std::string("logical_partition_")+name).c_str());
  }

  ~Data2D_Transposed() {
    runtime->destroy_logical_partition(ctx, logical_partition);
    runtime->destroy_logical_region(ctx, logical_region);
    runtime->destroy_field_space(ctx, field_space);
    runtime->destroy_index_partition(ctx, index_partition);
    runtime->destroy_index_space(ctx, index_space);
  }

  
  std::string name;

  coord_t n_grid_cells, n_second;
  const Rect<2> domain;
  IndexSpaceT<2,coord_t> index_space;
  IndexPartitionT<2, coord_t> index_partition;

  std::vector<unsigned> field_ids;
  FieldSpace field_space;

  LogicalRegion logical_region;
  LogicalPartition logical_partition;

  coord_t n_parts;
  const Rect<2> color_domain;

  static const unsigned projection_id = ProjectionIDs::TO_1D_FROM_2D_TRANSPOSED;

  Context ctx;
  Runtime* runtime;
  
  class LocalProjectionFunction : public ProjectionFunctor{
   public:
    LocalProjectionFunction() {}
    LocalProjectionFunction(Runtime *rt) {}
    virtual ~LocalProjectionFunction() {}
   public:
    virtual LogicalRegion project(const Mappable *mappable, unsigned index,
            LogicalRegion upper_bound,
            const DomainPoint &point) override {
      assert(false);
    }
    virtual LogicalRegion project(const Mappable *mappable, unsigned index,
            LogicalPartition upper_bound,
            const DomainPoint &point) override {
      Point<2,coord_t> color; color[0] = 0; color[1] = point[0];
      return runtime->get_logical_subregion_by_color(upper_bound, color);
    }
    virtual bool is_exclusive(void) const override { return true; }
    virtual unsigned get_depth(void) const override { return 0; }
  };
  
};
  

#endif