domains.hh 9.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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_,
Ethan Coon's avatar
Ethan Coon committed
19
         const std::vector<std::string>& field_names_,
20
21
22
         const std::string& name_,
         Context ctx_, Runtime* runtime_) :
      name(name_),
Ethan Coon's avatar
Ethan Coon committed
23
      field_names(field_names_),
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
      n_grid_cells(n_grid_cells_),
      domain(Point<1>(0), Point<1>(n_grid_cells_-1)),
      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);
Ethan Coon's avatar
Ethan Coon committed
40
41
      for (auto fname : field_names)
        field_ids[fname] = allocator.allocate_field(sizeof(double), AUTO_GENERATE_ID);
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    }
    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);
  }
Ethan Coon's avatar
Ethan Coon committed
58
59

  // name the container
60
  std::string name;
Ethan Coon's avatar
Ethan Coon committed
61
62
63
  std::vector<std::string> field_names;

  // index space and partition
64
65
66
  coord_t n_grid_cells;
  const Rect<1> domain;
  IndexSpaceT<1,coord_t> index_space;
Ethan Coon's avatar
Ethan Coon committed
67
68
69

  coord_t n_parts;
  const Rect<1> color_domain;
70
71
  IndexPartitionT<1, coord_t> index_partition;

Ethan Coon's avatar
Ethan Coon committed
72
73
  // field space
  std::map<std::string, unsigned> field_ids;
74
75
  FieldSpace field_space;

Ethan Coon's avatar
Ethan Coon committed
76
  // cross product of index and field
77
78
79
  LogicalRegion logical_region;
  LogicalPartition logical_partition;

Ethan Coon's avatar
Ethan Coon committed
80
  // projection from this color_space into the 1D default color_space
81
82
83
84
85
86
87
88
89
  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_,
Ethan Coon's avatar
Ethan Coon committed
90
         const std::vector<std::string>& field_names_,
91
92
         Context ctx_, Runtime* runtime_) :
      name(name_),
Ethan Coon's avatar
Ethan Coon committed
93
      field_names(field_names_),
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
      n_grid_cells(n_grid_cells_),
      n_second(n_second_),
      domain(Point<2>(0,0), Point<2>(n_grid_cells_-1, n_second_-1)),
      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);
Ethan Coon's avatar
Ethan Coon committed
111
112
      for (auto fname : field_names_)
        field_ids[fname] = allocator.allocate_field(sizeof(double), AUTO_GENERATE_ID);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    }
    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;
Ethan Coon's avatar
Ethan Coon committed
131
  std::vector<std::string> field_names;
132
133
134
135
136
137

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

Ethan Coon's avatar
Ethan Coon committed
138
  std::map<std::string, unsigned> field_ids;
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
  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_,
Ethan Coon's avatar
Ethan Coon committed
178
179
180
                    const std::string& name_,
                    const std::vector<std::string>& field_names_,
                    Context ctx_, Runtime* runtime_) :
181
      name(name_),
Ethan Coon's avatar
Ethan Coon committed
182
      field_names(field_names_),
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
      n_grid_cells(n_grid_cells_),
      n_second(n_second_),
      domain(Point<2>(0,0), Point<2>(n_second_-1, n_grid_cells_-1)),
      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);
Ethan Coon's avatar
Ethan Coon committed
200
201
202
203
204
205
206
207
208
      for (auto fname : field_names_)
        field_ids[fname] = allocator.allocate_field(sizeof(double), AUTO_GENERATE_ID);

      std::cout << "Field IDs (" << name_ << "):";
      for (auto fid : field_ids) {
        std::cout << fid.first << "," << fid.second << ";";
      }
      std::cout << std::endl;
        
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    }
    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;
Ethan Coon's avatar
Ethan Coon committed
228
  std::vector<std::string> field_names;
229
230
231
232
233
234

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

Ethan Coon's avatar
Ethan Coon committed
235
  std::map<std::string,unsigned> field_ids;
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  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