-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_reduction.h
500 lines (428 loc) · 17.7 KB
/
image_reduction.h
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* Copyright 2017 Stanford University, NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef image_reduction_h
#define image_reduction_h
//#define TRACE_TASKS
//tracing for debug
#if 0
#define __TRACE {char hostname[512];gethostname(hostname,sizeof(hostname));char buffer[1024];sprintf(buffer, "%s %s:%d %s pid %d\n",hostname,__FILE__,__LINE__,__FUNCTION__,getpid());std::cout<<buffer;fflush(stdout);}
#else
#define __TRACE
#endif
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#include <iostream>
#include <sstream>
#include <mutex>
#include <unistd.h>
#include <assert.h>
#include "legion/legion_c_util.h"
#include "KDTree.hpp"
namespace Legion {
namespace Visualization {
class ImageReduction {
private:
typedef struct {
ImageDescriptor imageDescriptor;
bool isAssociative;
int compositeTaskID;
GLenum depthFunction;
GLenum blendFunctionSource;
GLenum blendFunctionDestination;
LogicalRegion image;
} ScreenSpaceArguments;
typedef struct {
ImageDescriptor imageDescriptor;
GLenum depthFunction;
GLenum blendFunctionSource;
GLenum blendFunctionDestination;
GLenum blendEquation;
/* camera */
bool cameraIsOrthographic;
GLfloat cameraData[image_region_dimensions];
} CompositeArguments;
typedef struct {
ImageDescriptor imageDescriptor;
int t;
} DisplayArguments;
public:
enum FieldIDs {
FID_FIELD_R = 0,
FID_FIELD_G,
FID_FIELD_B,
FID_FIELD_A,
FID_FIELD_Z,
FID_FIELD_USERDATA,
FID_FIELD_COLOR, // used in coloring logical region
FID_FIELD_EXTENT // used in extent logical region
};
typedef float PixelField;
static const int numPixelFields = 6;//rgbazu
typedef float SimulationBoundsCoordinate;
typedef size_t Stride[ImageReduction::numPixelFields][image_region_dimensions];
/**
* Initialize the image reduction framework.
* Be sure to call this before starting the Legion runtime.
*
* @param mapperID dynamically generated mapper ID (see runtime->generate_dynamic_mapper_id)
*/
static void preinitializeBeforeRuntimeStarts();
/**
* Preregister an array of simulation bounds in 3D.
* This is optional, required if you plan to use noncommutative reductions.
* Be sure to call this *before* starting the Legion runtime.
*
* @param bounds array of 6xGLfloatxnumNodes
* @param numBounds number of simulation elements
*/
static void preregisterSimulationBounds(SimulationBoundsCoordinate *bounds, int numBounds);
ImageReduction(){}
/**
* Construct an image reduction framework based on ImageDescriptor.
*
* @param imageDescriptor defines dimensions of current image
* @param ctx Legion context
* @param runtime Legion runtime
*/
ImageReduction(ImageDescriptor imageDescriptor,
Context ctx,
HighLevelRuntime *runtime,
MapperID mapperID);
/**
* Construct an image reduction framework based on an existing partition.
*
* @param partition defines an existing partition with one node per subregion
* @param imageDescriptor defines dimensions of current image
* @param ctx Legion context
* @param runtime Legion runtime
*/
ImageReduction(LogicalRegion region,
LogicalPartition partition,
IndexSpace launch_space,
legion_field_id_t pFields[],
int numPFields,
ImageDescriptor imageDescriptor,
Context context,
HighLevelRuntime *runtime,
MapperID mapperID);
/**
* Destroy an instance of an image reduction framework.
*/
virtual ~ImageReduction();
/**
* Perform a tree reduction.
* Be sure to call either set_blend_func or set_depth_func first.
*/
FutureMap reduceImagesOrthographic(Context context, float cameraDirection[image_region_dimensions]);
FutureMap reduceImagesPerspective(Context context, float cameraLocation[image_region_dimensions]);
/**
* Move reduced image result to a display.
*
* @param t integer timestep
*/
Future display(int t, Context context);
/**
* Provide the camera view matrix, typically from gluLookAt
*
* @param view a 4x4 homogeneous view matrix, typically from gluLookAt
*/
void set_view_matrix(GLfloat view[]) {
memcpy(mGlViewTransform, view, sizeof(mGlViewTransform));
}
/**
* Define blend source and destination functions to use in subsequent reductions
* (see glBlendFunc).
*
* @param sfactor source blend factor
* @param dfactor destination blend factor
*/
void set_blend_func(GLenum sfactor, GLenum dfactor) {
mGlBlendFunctionSource = sfactor;
mGlBlendFunctionDestination = dfactor;
}
/**
* Specify the constant color to use with certain blend functions (see glBlendFunc)
* @param red constant color
* @param green constant color
* @param blue constant color
* @param alpha constant alpha
*/
void set_blend_color(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
mGlConstantColor[FID_FIELD_R] = red;
mGlConstantColor[FID_FIELD_G] = green;
mGlConstantColor[FID_FIELD_B] = blue;
mGlConstantColor[FID_FIELD_A] = alpha;
}
/**
* Specify the use with blending (this is not common, see glBlendEquation)
* @param mode must be one of GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT, GL_MIN, GL_MAX
*/
void set_blend_equation(GLenum mode) {
assert(mode == GL_FUNC_ADD || mode == GL_FUNC_SUBTRACT || mode == GL_FUNC_REVERSE_SUBTRACT || mode == GL_MIN || mode == GL_MAX);
mGlBlendEquation = mode;
}
/**
* Define a depth operator to use in subsequent reductions.
* For definition of depth factors (see glDepthFunc).
* @param func depth comparison factor
*/
void set_depth_func(GLenum func){ mDepthFunction = func; }
/**
* Utility function to provide descriptive output for messages.
*
* @param task Legion task pointer
*/
static std::string describe_task(const Task *task) {
char hostname[128] = { 0 };
if(hostname[0] == '\0') gethostname(hostname, sizeof(hostname));
std::ostringstream output;
output << hostname << " ";
output << task->get_task_name() << " "
<< task->task_id << " "
<< task->get_unique_id()
<< " pid " << getpid()
<< " " << hostname
<< " (" << task->index_point.point_data[0]
<< ", " << task->index_point.point_data[1]
<< ", " << task->index_point.point_data[2]
<< ")"
;
return output.str();
}
/**
* obtain the compositeImage domain, useful for index launches
*/
Domain compositeImageDomain() const {
return mCompositeImageDomain;
}
/*
* obtain the compositeImage partition
*/
LogicalPartition compositeImagePartition() const {
return mCompositeImagePartition;
}
/*
* obtain the compositeImage color space
*/
Domain compositeImageColorSpace() const {
return mCompositeImageColorSpace;
}
/**
* obtain the renderImage domain, useful for index launches
*/
Domain renderImageDomain() const {
return mRenderImageDomain;
}
/**
* obtain the renderImage partition
*/
LogicalPartition renderImagePartition() const {
return mRenderImagePartition;
}
/*
* obtain the renderImage color space
*/
Domain renderImageColorSpace() const {
return mRenderImageColorSpace;
}
/**
* obtain the source image index space
**/
IndexSpace sourceIndexSpace() const {
return mSourceIndexSpace;
}
/**
* obtain the source image logical region
*/
LogicalRegion sourceImage() const {
return mSourceImage;
}
/**
* obtain the source image fields
**/
void sourceImageFields(Context context, legion_field_id_t imageFields[]) const {
std::vector<FieldID> fields;
mRuntime->get_field_space_fields(context, mSourceImageFields, fields);
for(unsigned i = 0; i < fields.size(); ++i) {
imageFields[i] = fields[i];
}
}
/**
* obtain the image descriptor, pass this to the mapper
*/
ImageDescriptor imageDescriptor() const {
return mImageDescriptor;
}
/**
* Launcher utility over compositor domain.
*/
Legion::FutureMap launch_task_composite_domain(
unsigned taskID,
Runtime* runtime,
Context context,
void* argsBuffer,
int totalArgLen,
bool blocking);
static void display_task(const Task *task,
const std::vector<PhysicalRegion> ®ions,
Context ctx, Runtime *runtime);
static int numTreeLevels(ImageDescriptor imageDescriptor);
static int numTreeLevels(int numImageLayers);
static void initial_task(const Task *task,
const std::vector<PhysicalRegion> ®ions,
Context ctx, Runtime *runtime);
static KDNode<image_region_dimensions, long long int>* findFragmentInKDTree(PhysicalRegion fragment);
static bool flipRegions(PhysicalRegion fragment0, PhysicalRegion fragment1, bool cameraIsOrthographic, float cameraData[image_region_dimensions]);
static void composite_task(const Task *task,
const std::vector<PhysicalRegion> ®ions,
Context ctx, Runtime *runtime);
void initializeRenderNodes(HighLevelRuntime* runtime,
Context context,
unsigned taskID,
char* args,
int totalArgLen);
static FieldSpace imageFields(Context context);
protected:
class CompositeProjectionFunctor : public ProjectionFunctor {
public:
CompositeProjectionFunctor(int offset, int multiplier, int numBounds, int id) {
mOffset = offset;
mMultiplier = multiplier;
mNumBounds = numBounds;
mID = id;
}
virtual LogicalRegion project(const Mappable *mappable, unsigned index,
LogicalPartition upperBound,
const DomainPoint &point) {
int launchDomainLayer = point[2];
DomainPoint remappedPoint = point;
int remappedLayer = launchDomainLayer * mMultiplier + mOffset;
// handle non-power of 2 simulation size
if(mNumBounds == 0 || remappedLayer < mNumBounds) {
remappedPoint[2] = remappedLayer;
}
// printf("[CompositeProjectionFunctor0] point (%d, %d, %d) -> p (%d, %d, %d)\n",
// (int)point[0], (int)point[1], (int)point[2],
// (int)remappedPoint[0], (int)remappedPoint[1], (int)remappedPoint[2]
// );
LogicalRegion result = Legion::Runtime::get_runtime()->get_logical_subregion_by_color(upperBound, remappedPoint);
return result;
}
virtual LogicalRegion project(LogicalPartition upper_bound, const DomainPoint &point, const Domain &launch_domain) {
int launchDomainLayer = point[2];
DomainPoint remappedPoint = point;
int remappedLayer = launchDomainLayer * mMultiplier + mOffset;
// handle non-power of 2 simulation size
if(mNumBounds == 0 || remappedLayer < mNumBounds) {
remappedPoint[2] = remappedLayer;
}
// printf("[CompositeProjectionFunctor1] point (%d, %d, %d) -> p (%d, %d, %d)\n",
// (int)point[0], (int)point[1], (int)point[2],
// (int)remappedPoint[0], (int)remappedPoint[1], (int)remappedPoint[2]
// );
LogicalRegion result = Legion::Runtime::get_runtime()->get_logical_subregion_by_color(upper_bound, remappedPoint);
return result;
}
virtual LogicalRegion project(const Mappable *mappable, unsigned index, LogicalRegion upper_bound, const DomainPoint &point) {
assert(false);
LogicalRegion result;
return result;
}
virtual LogicalRegion project(LogicalRegion upper_bound, const DomainPoint &point, const Domain &launch_domain) {
assert(false);
LogicalRegion result;
return result;
}
int id() const{ return mID; }
std::string to_string() const {
char buffer[256];
sprintf(buffer, "CompositeProjectionFunctor id %d offset %d multiplier %d numNodes %d", mID, mOffset, mMultiplier, mNumBounds);
return std::string(buffer);
}
virtual bool is_exclusive(void) const{ return true; }
virtual unsigned get_depth(void) const{ return 0; }
virtual bool is_functional(void) const { return true; }
private:
int mOffset;
int mMultiplier;
int mNumBounds;
int mID;
};
static void createProjectionFunctors(Runtime* runtime, int numImageLayers);
void initializeNodes(HighLevelRuntime* runtime, Context context);
void initializeViewMatrix();
void createTreeDomains(int numTreeLevels, Runtime* runtime, ImageDescriptor mImageDescriptor);
void createImageRegion(IndexSpace& indexSpace, LogicalRegion ®ion, Domain &domain, FieldSpace& fields, legion_field_id_t fieldID[], Context context);
void createImagePartition(legion_field_id_t fieldID[], Context context);
void partitionImageByDepth(LogicalRegion image, Domain &domain, LogicalPartition &partition, Context context);
void partitionImageByImageDescriptor(LogicalRegion image, Context ctx, HighLevelRuntime* runtime, ImageDescriptor imageDescriptor);
void partitionImageByKDTree(LogicalRegion image, LogicalPartition sourcePartition, Context ctx, HighLevelRuntime* runtime, ImageDescriptor imageDescriptor);
void addCompositeArgumentsToArgmap(CompositeArguments *&argsPtr, int taskZ, ArgumentMap &argMap, int layer0, int layer1);
void addRegionRequirementToCompositeLauncher(IndexTaskLauncher &launcher, int projectionFunctorID, PrivilegeMode privilege, CoherenceProperty coherence);
static void buildKDTrees(ImageDescriptor imageDescriptor, Context ctx, HighLevelRuntime *runtime);
static void registerTasks();
static void addImageFieldsToRequirement(RegionRequirement &req);
// static void createImageRegionFieldPointer(LegionRuntime::Accessor::RegionAccessor<LegionRuntime::Accessor::AccessorType::Generic, PixelField> &acc,
// int fieldID,
// PixelField *&field,
// Rect<image_region_dimensions> imageBounds,
// PhysicalRegion region,
// ByteOffset offset[image_region_dimensions]);
static int subtreeHeight(ImageDescriptor imageDescriptor);
static FutureMap launchTreeReduction(ImageDescriptor imageDescriptor, int treeLevel,
GLenum depthFunc, GLenum blendFuncSource, GLenum blendFuncDestination, GLenum blendEquation,
int compositeTaskID, LogicalPartition sourceFragmentPartition, LogicalRegion image,
Runtime* runtime, Context context,
int maxTreeLevel,
bool cameraIsOrthographic, float cameraData[image_region_dimensions]);
ImageDescriptor mImageDescriptor;
Runtime *mRuntime;
IndexSpace mSourceIndexSpace;
LogicalRegion mSourceImage;
FieldSpace mSourceImageFields;
Domain mSourceImageDomain;
Domain mCompositeImageDomain;
Domain mCompositeImageColorSpace;
LogicalPartition mCompositeImagePartition;
Domain mRenderImageDomain;
Domain mRenderImageColorSpace;
LogicalPartition mRenderImagePartition;
Domain mDisplayDomain;
Domain mSourceFragmentDomain;
GLenum mDepthFunction;
int mAccessorFunctorID;
MapperID mMapperID;
public:
static int mNodeID;
static std::vector<CompositeProjectionFunctor*> *mCompositeProjectionFunctor;
static std::vector<Domain> *mHierarchicalTreeDomain;
static const int numMatrixElements4x4 = 16;
static GLfloat mGlViewTransform[numMatrixElements4x4];
static PixelField mGlConstantColor[numPixelFields];
static GLenum mGlBlendEquation;
static GLenum mGlBlendFunctionSource;
static GLenum mGlBlendFunctionDestination;
static TaskID mCompositeTaskID;
static TaskID mDisplayTaskID;
static KDTree<image_region_dimensions, long long int>* mSimulationKDTree;
static KDTree<image_region_dimensions, long long int>* mImageKDTree;
};
}
}
#endif /* image_reduction_h */