Skip to content

Commit

Permalink
refactor code to match Legion style
Browse files Browse the repository at this point in the history
  • Loading branch information
eddy16112 committed Oct 6, 2017
1 parent f1ffdbc commit 48c8f8d
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 139 deletions.
81 changes: 28 additions & 53 deletions examples/attach_2darray_c_fortran_layout/attach_2darray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
using namespace Legion;

template<typename FT, int N, typename T = coord_t>
using AccessorRO = FieldAccessor<READ_ONLY,FT,N,T,Realm::AffineAccessor<FT,N,T> >;
using AccessorRO = FieldAccessor<READ_ONLY,FT,N,T,
Realm::AffineAccessor<FT,N,T> >;
template<typename FT, int N, typename T = coord_t>
using AccessorWD = FieldAccessor<WRITE_DISCARD,FT,N,T,Realm::AffineAccessor<FT,N,T> >;
using AccessorWD = FieldAccessor<WRITE_DISCARD,FT,N,T,
Realm::AffineAccessor<FT,N,T> >;

enum TaskIDs {
TOP_LEVEL_TASK_ID,
Expand All @@ -47,7 +49,6 @@ void top_level_task(const Task *task,
Context ctx, Runtime *runtime)
{
int num_elements = 10;
//double x[10], y[10];
// See if we have any command line arguments to parse
{
const InputArgs &command_args = Runtime::get_input_args();
Expand All @@ -64,7 +65,6 @@ void top_level_task(const Task *task,
Point<2> lo(0, 0);
Point<2> hi(num_elements-1, num_elements-1);
const Rect<2> elem_rect(lo, hi);
//Rect<1> elem_rect(0,num_elements-1);
IndexSpace is = runtime->create_index_space(ctx, elem_rect);
FieldSpace input_fs = runtime->create_field_space(ctx);
{
Expand All @@ -77,9 +77,9 @@ void top_level_task(const Task *task,
}
LogicalRegion input_lr = runtime->create_logical_region(ctx, is, input_fs);

/* init array */
int i;
double val = 0.0;

xy_t *xy_ptr = (xy_t*)malloc(sizeof(xy_t)*(num_elements*num_elements));
double *a_ptr = (double*)malloc(sizeof(double)*(num_elements*num_elements));
double *b_ptr = (double*)malloc(sizeof(double)*(num_elements*num_elements));
Expand All @@ -92,56 +92,42 @@ void top_level_task(const Task *task,
val += 1.0;
}

/* attach array */
std::map<FieldID, size_t> offset_x;
offset_x[FID_X] = 0;
printf("Attach AOS array in fortran layout, fid %d, ptr %p\n", FID_X, xy_ptr);
PhysicalRegion pr_x = runtime->attach_array_aos(ctx, input_lr, input_lr, xy_ptr, sizeof(xy_t), offset_x, 0);
printf("Attach AOS array in fortran layout, fid %d, ptr %p\n",
FID_X, xy_ptr);
PhysicalRegion pr_x = runtime->attach_array_aos(ctx, input_lr, input_lr,
xy_ptr, sizeof(xy_t),
offset_x, 0);

std::map<FieldID, size_t> offset_y;
offset_y[FID_Y] = sizeof(double);
printf("Attach AOS array in c layout, fid %d, ptr %p\n", FID_Y, ((unsigned char*)(xy_ptr))+sizeof(double));
PhysicalRegion pr_y = runtime->attach_array_aos(ctx, input_lr, input_lr, xy_ptr, sizeof(xy_t), offset_y, 1);
printf("Attach AOS array in c layout, fid %d, ptr %p\n",
FID_Y, ((unsigned char*)(xy_ptr))+sizeof(double));
PhysicalRegion pr_y = runtime->attach_array_aos(ctx, input_lr, input_lr,
xy_ptr, sizeof(xy_t),
offset_y, 1);

std::map<FieldID,void*> field_pointer_map_a;
field_pointer_map_a[FID_A] = a_ptr;
printf("Attach SOA array in fortran layout, fid %d, ptr %p\n", FID_A, a_ptr);
PhysicalRegion pr_a = runtime->attach_array_soa(ctx, input_lr, input_lr, field_pointer_map_a, 0);
printf("Attach SOA array in fortran layout, fid %d, ptr %p\n",
FID_A, a_ptr);
PhysicalRegion pr_a = runtime->attach_array_soa(ctx, input_lr, input_lr,
field_pointer_map_a, 0);

std::map<FieldID,void*> field_pointer_map_b;
field_pointer_map_b[FID_B] = b_ptr;
printf("Attach SOA array in c layout, fid %d, ptr %p\n", FID_B, b_ptr);
PhysicalRegion pr_b = runtime->attach_array_soa(ctx, input_lr, input_lr, field_pointer_map_b, 1);

// Instead of using an inline mapping to initialize the fields for
// daxpy, in this case we will launch two separate tasks for initializing
// each of the fields in parallel. To launch the sub-tasks for performing
// the initialization we again use the launcher objects that were
// introduced earlier. The only difference now is that instead of passing
// arguments by value, we now want to specify the logical regions
// that the tasks may access as their arguments. We again make use of
// the RegionRequirement struct to name the logical regions and fields
// for which the task should have privileges. In this case we launch
// a task that asks for WRITE_DISCARD privileges on the 'X' field.
//
// An important property of the Legion programming model is that sub-tasks
// are only allowed to request privileges which are a subset of a
// parent task's privileges. When a task creates a logical region it
// is granted full read-write privileges for that logical region. It
// can then pass them down to sub-tasks. In this example the top-level
// task has full privileges on all the fields of input_lr and output_lr.
// In this call it passing read-write privileges down to the sub-task
// on input_lr on field 'X'. Legion will enforce the property that the
// sub-task only accesses the 'X' field of input_lr. This property of
// Legion is crucial for the implementation of Legion's hierarchical
// scheduling algorithm which is described in detail in our two papers.
TaskLauncher read_launcher(READ_FIELD_TASK_ID, TaskArgument(&num_elements, sizeof(num_elements)));
PhysicalRegion pr_b = runtime->attach_array_soa(ctx, input_lr, input_lr,
field_pointer_map_b, 1);

TaskLauncher read_launcher(READ_FIELD_TASK_ID,
TaskArgument(&num_elements,
sizeof(num_elements)));
read_launcher.add_region_requirement(
RegionRequirement(input_lr, READ_ONLY, EXCLUSIVE, input_lr));
read_launcher.add_field(0/*idx*/, FID_X);
// Note that when we launch this task we don't record the future.
// This is because we're going to let Legion be responsible for
// computing the data dependences between how different tasks access
// logical regions.
Future fx = runtime->execute_task(ctx, read_launcher);

read_launcher.region_requirements[0].privilege_fields.clear();
Expand All @@ -163,17 +149,7 @@ void top_level_task(const Task *task,
fy.wait();
fa.wait();
fb.wait();
// Notice that we never once blocked waiting on the result of any sub-task
// in the execution of the top-level task. We don't even block before
// destroying any of our resources. This works because Legion understands
// the data being accessed by all of these operations and defers all of
// their executions until they are safe to perform. Legion is still smart
// enough to know that the top-level task is not finished until all of
// the sub operations that have been performed are completed. However,
// from the programmer's perspective, all of these operations can be
// done without ever blocking and thereby exposing as much task-level
// parallelism to the Legion runtime as possible. We'll discuss the
// implications of Legion's deferred execution model in a later example.

runtime->detach_array(ctx, pr_x);
runtime->detach_array(ctx, pr_y);
runtime->detach_array(ctx, pr_a);
Expand All @@ -200,8 +176,7 @@ void read_field_task(const Task *task,
assert(regions.size() == 1);
assert(task->regions.size() == 1);
assert(task->regions[0].privilege_fields.size() == 1);
// This is a field polymorphic function so figure out
// which field we are responsible for initializing.

FieldID fid = *(task->regions[0].privilege_fields.begin());
const int num_elements = *((const int*)task->args);
int i = 0;
Expand Down
35 changes: 24 additions & 11 deletions examples/attach_array_daxpy/attach_array_daxpy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
using namespace Legion;

template<typename FT, int N, typename T = coord_t>
using AccessorRO = FieldAccessor<READ_ONLY,FT,N,T,Realm::AffineAccessor<FT,N,T> >;
using AccessorRO = FieldAccessor<READ_ONLY,FT,N,T,
Realm::AffineAccessor<FT,N,T> >;
template<typename FT, int N, typename T = coord_t>
using AccessorWD = FieldAccessor<WRITE_DISCARD,FT,N,T,Realm::AffineAccessor<FT,N,T> >;
using AccessorWD = FieldAccessor<WRITE_DISCARD,FT,N,T,
Realm::AffineAccessor<FT,N,T> >;

enum TaskIDs {
TOP_LEVEL_TASK_ID,
Expand Down Expand Up @@ -115,7 +117,8 @@ void top_level_task(const Task *task,
double *z_ptr = NULL;
double *xyz_ptr = NULL;

if (soa_flag == 0) { // SOA
if (soa_flag == 0)
{ // SOA
double *y_ptr = (double*)malloc(sizeof(double)*(num_elements));
double *x_ptr = (double*)malloc(sizeof(double)*(num_elements));
double *z_ptr = (double*)malloc(sizeof(double)*(num_elements));
Expand All @@ -127,27 +130,36 @@ void top_level_task(const Task *task,
std::map<FieldID,void*> field_pointer_map_xy;
field_pointer_map_xy[FID_X] = x_ptr;
field_pointer_map_xy[FID_Y] = y_ptr;
printf("Attach SOA array fid %d, ptr %p, fid %d, ptr %p\n", FID_X, x_ptr, FID_Y, y_ptr);
xy_pr = runtime->attach_array_soa(ctx, input_lr, input_lr, field_pointer_map_xy, 0);
printf("Attach SOA array fid %d, ptr %p, fid %d, ptr %p\n",
FID_X, x_ptr, FID_Y, y_ptr);
xy_pr = runtime->attach_array_soa(ctx, input_lr, input_lr,
field_pointer_map_xy, 0);

std::map<FieldID,void*> field_pointer_map_z;
field_pointer_map_z[FID_Z] = z_ptr;
printf("Attach SOA array fid %d, ptr %p\n", FID_Z, z_ptr);
z_pr = runtime->attach_array_soa(ctx, output_lr, output_lr, field_pointer_map_z, 0);
z_pr = runtime->attach_array_soa(ctx, output_lr, output_lr,
field_pointer_map_z, 0);

} else { // AOS
}
else
{ // AOS
daxpy_t *xyz_ptr = (daxpy_t*)malloc(sizeof(daxpy_t)*(num_elements));

std::map<FieldID, size_t> offset_input;
offset_input[FID_X] = 0;
offset_input[FID_Y] = sizeof(double);

xy_pr = runtime->attach_array_aos(ctx, input_lr, input_lr, xyz_ptr, sizeof(daxpy_t), offset_input, 0);
xy_pr = runtime->attach_array_aos(ctx, input_lr, input_lr,
xyz_ptr, sizeof(daxpy_t),
offset_input, 0);

std::map<FieldID, size_t> offset_output;
offset_output[FID_Z] = 2*sizeof(double);

z_pr = runtime->attach_array_aos(ctx, output_lr, output_lr, xyz_ptr, sizeof(daxpy_t), offset_output, 0);
z_pr = runtime->attach_array_aos(ctx, output_lr, output_lr,
xyz_ptr, sizeof(daxpy_t),
offset_output, 0);
printf("Attach AOS array ptr %p\n", xyz_ptr);
}

Expand Down Expand Up @@ -351,7 +363,8 @@ void daxpy_task(const Task *task,
Rect<1> rect = runtime->get_index_space_domain(ctx,
task->regions[0].region.get_index_space());
printf("Running daxpy computation with alpha %.8g for point %d, xptr %p, y_ptr %p, z_ptr %p...\n",
alpha, point, acc_x.ptr(rect.lo), acc_y.ptr(rect.lo), acc_z.ptr(rect.lo));
alpha, point,
acc_x.ptr(rect.lo), acc_y.ptr(rect.lo), acc_z.ptr(rect.lo));
for (PointInRectIterator<1> pir(rect); pir(); pir++)
acc_z[*pir] = alpha * acc_x[*pir] + acc_y[*pir];
}
Expand All @@ -372,7 +385,7 @@ void check_task(const Task *task,
Rect<1> rect = runtime->get_index_space_domain(ctx,
task->regions[0].region.get_index_space());
printf("Checking results... xptr %p, y_ptr %p, z_ptr %p...\n",
acc_x.ptr(rect.lo), acc_y.ptr(rect.lo), acc_z.ptr(rect.lo));
acc_x.ptr(rect.lo), acc_y.ptr(rect.lo), acc_z.ptr(rect.lo));
bool all_passed = true;
for (PointInRectIterator<1> pir(rect); pir(); pir++)
{
Expand Down
7 changes: 4 additions & 3 deletions runtime/legion/legion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5866,7 +5866,7 @@ namespace Legion {
PhysicalRegion Runtime::attach_array_soa(Context ctx,
LogicalRegion handle,
LogicalRegion parent,
const std::map<FieldID,void*> &field_pointer_map,
const std::map<FieldID,void*> &field_pointer_map,
int c_f_layout_flag)
//--------------------------------------------------------------------------
{
Expand Down Expand Up @@ -5896,12 +5896,13 @@ namespace Legion {
LogicalRegion parent,
const void* array_ptr,
size_t stride,
const std::map<FieldID, size_t> &field_offset,
const std::map<FieldID, size_t> &field_offset,
int c_f_layout_flag)
//--------------------------------------------------------------------------
{
unsigned char* base_ptr = (unsigned char*)array_ptr;
std::map<FieldID, size_t>::const_iterator it_offset = field_offset.begin();
std::map<FieldID, size_t>::const_iterator it_offset
= field_offset.begin();
std::map<FieldID,void*> field_pointer_map;
while(it_offset != field_offset.end())
{
Expand Down
6 changes: 3 additions & 3 deletions runtime/legion/legion.h
Original file line number Diff line number Diff line change
Expand Up @@ -5185,8 +5185,8 @@ namespace Legion {
void detach_file(Context ctx, PhysicalRegion region);

PhysicalRegion attach_array_soa(Context ctx,
LogicalRegion handle, LogicalRegion parent,
const std::map<FieldID,void*> &field_pointer_map,
LogicalRegion handle, LogicalRegion parent,
const std::map<FieldID,void*> &field_pointer_map,
int c_f_layout_flag);

void detach_array(Context ctx, PhysicalRegion region);
Expand All @@ -5196,7 +5196,7 @@ namespace Legion {
LogicalRegion parent,
const void* array_ptr,
size_t stride,
const std::map<FieldID, size_t> &field_offset,
const std::map<FieldID, size_t> &field_offset,
int c_f_layout_flag);
public:
//------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion runtime/legion/legion.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3459,7 +3459,8 @@ namespace Legion {
}

//--------------------------------------------------------------------------
inline void AttachLauncher::attach_array(const std::map<FieldID,void*> &field_pointer_map,
inline void AttachLauncher::
attach_array(const std::map<FieldID,void*> &field_pointer_map,
int layoutflag)
//--------------------------------------------------------------------------
{
Expand Down
18 changes: 12 additions & 6 deletions runtime/legion/legion_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14041,14 +14041,17 @@ namespace Legion {
std::vector<void*> field_pointers(field_pointers_map.size());
unsigned idx = 0;
for (std::map<FieldID,void*>::const_iterator it =
field_pointers_map.begin(); it != field_pointers_map.end(); it++, idx++)
field_pointers_map.begin();
it != field_pointers_map.end();
it++, idx++)
{
field_ids[idx] = it->first;
field_pointers[idx] = it->second;
}
// Now ask the low-level runtime to create the instance
result = node->create_array_instance(resource, field_ids, sizes, field_pointers,
layout_flag, aos_base_ptr, aos_stride);
result = node->create_array_instance(resource, field_ids, sizes,
field_pointers, layout_flag,
aos_base_ptr, aos_stride);
constraints.specialized_constraint =
SpecializedConstraint(NORMAL_SPECIALIZE);
break;
Expand All @@ -14060,14 +14063,17 @@ namespace Legion {
std::vector<void*> field_pointers(field_pointers_map.size());
unsigned idx = 0;
for (std::map<FieldID,void*>::const_iterator it =
field_pointers_map.begin(); it != field_pointers_map.end(); it++, idx++)
field_pointers_map.begin();
it != field_pointers_map.end();
it++, idx++)
{
field_ids[idx] = it->first;
field_pointers[idx] = it->second;
}
// Now ask the low-level runtime to create the instance
result = node->create_array_instance(resource, field_ids, sizes, field_pointers,
layout_flag, aos_base_ptr, aos_stride);
result = node->create_array_instance(resource, field_ids, sizes,
field_pointers, layout_flag,
aos_base_ptr, aos_stride);
constraints.specialized_constraint =
SpecializedConstraint(NORMAL_SPECIALIZE);
break;
Expand Down
6 changes: 4 additions & 2 deletions runtime/legion/region_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,8 @@ namespace Legion {
const std::vector<Realm::FieldID> &field_ids,
const std::vector<size_t> &field_sizes,
const std::vector<void*> &field_pointers,
int layout_flag, unsigned char* aos_base_ptr, size_t aos_stride) = 0;
int layout_flag, unsigned char* aos_base_ptr,
size_t aos_stride) = 0;
public:
virtual void get_launch_space_domain(Domain &launch_domain) = 0;
virtual void validate_slicing(const std::vector<IndexSpace> &slice_spaces,
Expand Down Expand Up @@ -1181,7 +1182,8 @@ namespace Legion {
const std::vector<Realm::FieldID> &field_ids,
const std::vector<size_t> &field_sizes,
const std::vector<void*> &field_pointers,
int layout_flag, unsigned char* aos_base_ptr, size_t aos_stride);
int layout_flag, unsigned char* aos_base_ptr,
size_t aos_stride);
public:
virtual void get_launch_space_domain(Domain &launch_domain);
virtual void validate_slicing(const std::vector<IndexSpace> &slice_spaces,
Expand Down
Loading

0 comments on commit 48c8f8d

Please sign in to comment.