forked from stanfordhpccenter/HTR-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.rg
952 lines (874 loc) · 35.4 KB
/
util.rg
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
import 'regent'
local Exports = {}
local C = terralib.includecstring([[
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
]])
-------------------------------------------------------------------------------
-- Arrays
-------------------------------------------------------------------------------
-- N, val -> array(val, val, ..., val)
function Exports.mkArrayConstant(N, val)
local vals = terralib.newlist()
for i = 1,N do
vals:insert(val)
end
return rexpr array([vals]) end
end
-------------------------------------------------------------------------------
-- Numeric
-------------------------------------------------------------------------------
-- A -> bool
function Exports.isPosInt(x)
return type(x) == 'number' and x == math.floor(x) and x > 0
end
-------------------------------------------------------------------------------
-- Tables
-------------------------------------------------------------------------------
-- table -> bool
function Exports.isEmpty(tbl)
if not tbl then
return true
end
for _,_ in pairs(tbl) do
return false
end
return true
end
-- map(K,V) -> K*
function Exports.keys(tbl)
local res = terralib.newlist()
for k,v in pairs(tbl) do
res:insert(k)
end
return res
end
-- T*, (T -> bool) -> bool
function Exports.all(list, pred)
assert(terralib.israwlist(list))
for _,x in ipairs(list) do
if not pred(x) then return false end
end
return true
end
-- T*, (T -> bool) -> bool
function Exports.any(list, pred)
assert(terralib.israwlist(list))
for _,x in ipairs(list) do
if pred(x) then return true end
end
return false
end
-- table -> table
function Exports.copyTable(tbl)
local cpy = {}
for k,v in pairs(tbl) do cpy[k] = v end
return cpy
end
-- table -> int
function Exports.tableSize(tbl)
local size = 0
for _,_ in pairs(tbl) do
size = size + 1
end
return size
end
-------------------------------------------------------------------------------
-- Lists
-------------------------------------------------------------------------------
local TerraList = getmetatable(terralib.newlist())
-- (T -> bool) -> bool
function TerraList:all(pred)
return Exports.all(self, pred)
end
-- (T -> bool) -> bool
function TerraList:any(pred)
return Exports.any(self, pred)
end
-- () -> T*
function TerraList:copy()
return self:map(function(x) return x end)
end
-- T -> int?
function TerraList:find(x)
for i,elem in ipairs(self) do
if elem == x then
return i
end
end
return nil
end
-- () -> set(T)
function TerraList:toSet()
local set = {}
for _,elem in ipairs(self) do
set[elem] = true
end
return set
end
-- string? -> string
function TerraList:join(sep)
sep = sep or ' '
local res = ''
for i,elem in ipairs(self) do
if i > 1 then
res = res..sep
end
res = res..tostring(elem)
end
return res
end
-- () -> T*
function TerraList:reverse()
local res = terralib.newlist()
for i = #self, 1, -1 do
res:insert(self[i])
end
return res
end
-- () -> T
function TerraList:pop()
local res = self[#self]
self[#self] = nil
return res
end
-- int, (() -> T) -> T*
function Exports.generate(n, generator)
local res = terralib.newlist()
for i = 1,n do
res:insert(generator())
end
return res
end
-- () -> T*
function TerraList:flatten(res)
res = res or terralib.newlist()
for _,e in ipairs(self) do
if terralib.israwlist(e) then
e:flatten(res)
else
res:insert(e)
end
end
return res
end
-- int, int -> T*
function Exports.range(first, last)
local res = terralib.newlist()
for i = first,last do
res:insert(i)
end
return res
end
-------------------------------------------------------------------------------
-- Sets
-------------------------------------------------------------------------------
-- set(T) -> T*
function Exports.setToList(set)
local list = terralib.newlist()
for e,_ in pairs(set) do
list:insert(e)
end
return list
end
-- T* -> set(T)
function Exports.listToSet(list)
local set = {}
for _,e in ipairs(list) do
set[e] = true
end
return set
end
-- set(T) -> T
function Exports.setPop(set)
local elem
for e,_ in pairs(set) do
elem = e
break
end
set[elem] = nil
return elem
end
-- set(T), T* -> set(T)
function Exports.setSubList(set, list)
local res = Exports.copyTable(set)
for _,e in ipairs(list) do
res[e] = nil
end
return res
end
-------------------------------------------------------------------------------
-- Strings
-------------------------------------------------------------------------------
-- string -> string*
function string:split(sep)
local fields = terralib.newlist()
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields:insert(c) end)
return fields
end
-- string -> bool
function string:startswith(subStr)
return self:sub(1, subStr:len()) == subStr
end
-- string -> bool
function string:endswith(subStr)
return self:sub(self:len() - subStr:len() + 1, self:len()) == subStr
end
-------------------------------------------------------------------------------
-- Terra type helpers
-------------------------------------------------------------------------------
-- {string,terralib.type} | {field:string,type:terralib.type} ->
-- string, terralib.type
function Exports.parseStructEntry(entry)
if terralib.israwlist(entry)
and #entry == 2
and type(entry[1]) == 'string'
and terralib.types.istype(entry[2]) then
return entry[1], entry[2]
elseif type(entry) == 'table'
and entry.field
and entry.type then
return entry.field, entry.type
else assert(false) end
end
-- map(terralib.type,string)
local cBaseType = {
[int] = 'int',
[int8] = 'int8_t',
[int16] = 'int16_t',
[int32] = 'int32_t',
[int64] = 'int64_t',
[uint] = 'unsigned',
[uint8] = 'uint8_t',
[uint16] = 'uint16_t',
[uint32] = 'uint32_t',
[uint64] = 'uint64_t',
[bool] = 'bool',
[float] = 'float',
[double] = 'double',
}
-- terralib.type, bool, string -> string, string
local function typeDecl(typ, cStyle, indent)
if typ:isarray() then
local decl, mods = typeDecl(typ.type, cStyle, indent)
decl = cStyle and decl or decl..'['..tostring(typ.N)..']'
mods = cStyle and '['..tostring(typ.N)..']'..mods or mods
return decl, mods
elseif typ:isstruct() then
if typ.name:startswith('anon') then
return Exports.prettyPrintStruct(typ, cStyle, indent), ''
elseif cStyle then
return ('struct '..typ.name), ''
else
return typ.name, ''
end
elseif typ:isprimitive() then
return (cStyle and cBaseType[typ] or tostring(typ)), ''
else assert(false) end
end
-- terralib.struct, bool?, string? -> string
function Exports.prettyPrintStruct(s, cStyle, indent)
indent = indent or ''
local lines = terralib.newlist()
local isUnion = false
local entries = s.entries
if #entries == 1 and terralib.israwlist(entries[1]) then
isUnion = true
entries = entries[1]
end
local name = s.name:startswith('anon') and '' or s.name
local open =
(cStyle and isUnion) and ('union '..name..' {') or
(cStyle and not isUnion) and ('struct '..name..' {') or
(not cStyle and isUnion) and ('struct '..name..' { union {') or
(not cStyle and not isUnion) and ('struct '..name..' {') or
assert(false)
lines:insert(open)
for _,e in ipairs(entries) do
local name, typ = Exports.parseStructEntry(e)
local s1 = cStyle and '' or (name..' : ')
local s3 = cStyle and (' '..name) or ''
local s2, s4 = typeDecl(typ, cStyle, indent..' ')
lines:insert(indent..' '..s1..s2..s3..s4..';')
end
local close =
(cStyle and isUnion) and '}' or
(cStyle and not isUnion) and '}' or
(not cStyle and isUnion) and '} }' or
(not cStyle and not isUnion) and '}' or
assert(false)
lines:insert(indent..close)
return lines:join('\n')
end
-- terralib.struct -> string*
function Exports.fieldNames(s)
return s.entries:map(function(e)
local name,typ = Exports.parseStructEntry(e)
return name
end)
end
-- string, terralib.struct, string*, map(string,terralib.type)?
-- -> terralib.struct
function Exports.deriveStruct(newName, origStruct, keptFlds, addedFlds)
addedFlds = addedFlds or {}
local origFlds = {}
for _,e in ipairs(origStruct.entries) do
local name,typ = Exports.parseStructEntry(e)
assert(not addedFlds[name])
origFlds[name] = typ
end
local newStruct = terralib.types.newstruct(newName)
for _,name in ipairs(keptFlds) do
local typ = assert(origFlds[name])
newStruct.entries:insert({name,typ})
end
for name,typ in pairs(addedFlds) do
newStruct.entries:insert({name,typ})
end
return newStruct
end
-------------------------------------------------------------------------------
-- Filesystem
-------------------------------------------------------------------------------
terra Exports.createDir(name : &int8)
var mode = 493 -- octal 0755 = rwxr-xr-x
var res = C.mkdir(name, mode)
if res < 0 then
var stderr = C.fdopen(2, 'w')
C.fprintf(stderr, 'Cannot create directory %s: ', name)
C.fflush(stderr)
C.perror('')
C.fflush(stderr)
C.exit(1)
end
end
terra Exports.openFile(name : &int8, mode : &int8) : &C.FILE
var file = C.fopen(name, mode)
if file == nil then
var stderr = C.fdopen(2, 'w')
C.fprintf(stderr, 'Cannot open file %s in mode "%s": ', name, mode)
C.fflush(stderr)
C.perror('')
C.fflush(stderr)
C.exit(1)
end
return file
end
-------------------------------------------------------------------------------
-- Graphs
-------------------------------------------------------------------------------
-- Graph(T) = map(T,T*)
-- Graph(T) -> set(T)
function Exports.getNodes(graph)
local nodes = {} -- set(T)
for src,tgts in pairs(graph) do
nodes[src] = true
for _,tgt in ipairs(tgts) do
nodes[tgt] = true
end
end
return nodes
end
-- Graph(T) -> T*
function Exports.revTopoSort(graph)
local unmarked = Exports.getNodes(graph) -- set(T)
local tempMarked = {} -- set(T)
local permMarked = {} -- set(T)
local res = terralib.newlist() -- T*
local function visit(src)
if permMarked[src] then
return true
end
if tempMarked[src] then
return false
end
tempMarked[src] = true
for _,tgt in ipairs(graph[src] or {}) do
visit(tgt)
end
permMarked[src] = true
res:insert(src)
return true
end
while not Exports.isEmpty(unmarked) do
if not visit(Exports.setPop(unmarked)) then
return nil
end
end
return res
end
-------------------------------------------------------------------------------
-- Regent metaprogramming
-------------------------------------------------------------------------------
-- regentlib.symbol, int, regentlib.rexpr, terralib.type -> regentlib.rquote
function Exports.emitRegionTagAttach(r, tag, value, typ)
return rquote
var info : typ[1]
info[0] = value
regentlib.c.legion_logical_region_attach_semantic_information(
__runtime(), __raw(r), tag, [&typ](info), [sizeof(typ)], false)
end
end
-- regentlib.symbol, string, terralib.type -> regentlib.rquote
function Exports.emitPartitionNameAttach(p, name)
return rquote
regentlib.c.legion_logical_partition_attach_name(
__runtime(), __raw(p), name, false)
end
end
-- intXd, intXd, terralib.struct -> regentlib.task
function Exports.mkPartitionByTile(r_istype, cs_istype, fs, name)
local partitionByTile
local p = regentlib.newsymbol(name)
-- if r_istype == int4d and cs_istype == int4d then
-- __demand(__inline)
-- task partitionByTile(r : region(ispace(int4d), fs),
-- cs : ispace(int4d),
-- halo : int4d,
-- offset : int4d)
-- var Nx = r.bounds.hi.x - 2*halo.x + 1; var ntx = cs.bounds.hi.x + 1
-- var Ny = r.bounds.hi.y - 2*halo.y + 1; var nty = cs.bounds.hi.y + 1
-- var Nz = r.bounds.hi.z - 2*halo.z + 1; var ntz = cs.bounds.hi.z + 1
-- var Nw = r.bounds.hi.w - 2*halo.w + 1; var ntw = cs.bounds.hi.w + 1
-- regentlib.assert(r.bounds.lo == int4d{0,0,0,0}, "Can only partition root region")
-- regentlib.assert(Nx % ntx == 0, "Uneven partitioning on x")
-- regentlib.assert(Ny % nty == 0, "Uneven partitioning on y")
-- regentlib.assert(Nz % ntz == 0, "Uneven partitioning on z")
-- regentlib.assert(Nw % ntw == 0, "Uneven partitioning on w")
-- regentlib.assert(-ntx <= offset.x and offset.x <= ntx, "offset.x too large")
-- regentlib.assert(-nty <= offset.y and offset.y <= nty, "offset.y too large")
-- regentlib.assert(-ntz <= offset.z and offset.z <= ntz, "offset.z too large")
-- regentlib.assert(-ntw <= offset.w and offset.w <= ntw, "offset.w too large")
-- var coloring = regentlib.c.legion_domain_point_coloring_create()
-- for c_real in cs do
-- var c = (c_real - offset + {ntx,nty,ntz,ntw}) % {ntx,nty,ntz,ntw}
-- var rect = rect4d{
-- lo = int4d{halo.x + (Nx/ntx)*(c.x),
-- halo.y + (Ny/nty)*(c.y),
-- halo.z + (Nz/ntz)*(c.z),
-- halo.w + (Nz/ntz)*(c.w)},
-- hi = int4d{halo.x + (Nx/ntx)*(c.x+1) - 1,
-- halo.y + (Ny/nty)*(c.y+1) - 1,
-- halo.z + (Nz/ntz)*(c.z+1) - 1,
-- halo.w + (Nw/ntw)*(c.w+1) - 1}}
-- if c.x == 0 then rect.lo.x -= halo.x end
-- if c.y == 0 then rect.lo.y -= halo.y end
-- if c.z == 0 then rect.lo.z -= halo.z end
-- if c.w == 0 then rect.lo.w -= halo.w end
-- if c.x == ntx-1 then rect.hi.x += halo.x end
-- if c.y == nty-1 then rect.hi.y += halo.y end
-- if c.z == ntz-1 then rect.hi.z += halo.z end
-- if c.w == ntw-1 then rect.hi.w += halo.w end
-- regentlib.c.legion_domain_point_coloring_color_domain(coloring, c_real, rect)
-- end
-- var [p] = partition(disjoint, r, coloring, cs)
-- regentlib.c.legion_domain_point_coloring_destroy(coloring)
-- return [p]
-- end
-- elseif r_istype == int3d and cs_istype == int3d then
if r_istype == int3d and cs_istype == int3d then
__demand(__inline)
task partitionByTile(r : region(ispace(int3d), fs),
cs : ispace(int3d),
halo : int3d,
offset : int3d)
var Nx = r.bounds.hi.x - 2*halo.x + 1; var ntx = cs.bounds.hi.x + 1
var Ny = r.bounds.hi.y - 2*halo.y + 1; var nty = cs.bounds.hi.y + 1
var Nz = r.bounds.hi.z - 2*halo.z + 1; var ntz = cs.bounds.hi.z + 1
regentlib.assert(r.bounds.lo == int3d{0,0,0}, "Can only partition root region")
regentlib.assert(Nx % ntx == 0, "Uneven partitioning on x")
regentlib.assert(Ny % nty == 0, "Uneven partitioning on y")
regentlib.assert(Nz % ntz == 0, "Uneven partitioning on z")
regentlib.assert(-ntx <= offset.x and offset.x <= ntx, "offset.x too large")
regentlib.assert(-nty <= offset.y and offset.y <= nty, "offset.y too large")
regentlib.assert(-ntz <= offset.z and offset.z <= ntz, "offset.z too large")
var coloring = regentlib.c.legion_domain_point_coloring_create()
for c_real in cs do
var c = (c_real - offset + {ntx,nty,ntz}) % {ntx,nty,ntz}
var rect = rect3d{
lo = int3d{halo.x + (Nx/ntx)*(c.x),
halo.y + (Ny/nty)*(c.y),
halo.z + (Nz/ntz)*(c.z)},
hi = int3d{halo.x + (Nx/ntx)*(c.x+1) - 1,
halo.y + (Ny/nty)*(c.y+1) - 1,
halo.z + (Nz/ntz)*(c.z+1) - 1}}
if c.x == 0 then rect.lo.x -= halo.x end
if c.y == 0 then rect.lo.y -= halo.y end
if c.z == 0 then rect.lo.z -= halo.z end
if c.x == ntx-1 then rect.hi.x += halo.x end
if c.y == nty-1 then rect.hi.y += halo.y end
if c.z == ntz-1 then rect.hi.z += halo.z end
regentlib.c.legion_domain_point_coloring_color_domain(coloring, c_real, rect)
end
var [p] = partition(disjoint, r, coloring, cs)
regentlib.c.legion_domain_point_coloring_destroy(coloring)
return [p]
end
elseif r_istype == int2d and cs_istype == int2d then
__demand(__inline)
task partitionByTile(r : region(ispace(int2d), fs),
cs : ispace(int2d),
halo : int2d,
offset : int2d)
var Nx = r.bounds.hi.x - 2*halo.x + 1; var ntx = cs.bounds.hi.x + 1
var Ny = r.bounds.hi.y - 2*halo.y + 1; var nty = cs.bounds.hi.y + 1
regentlib.assert(r.bounds.lo == int2d{0,0}, "Can only partition root region")
regentlib.assert(Nx % ntx == 0, "Uneven partitioning on x")
regentlib.assert(Ny % nty == 0, "Uneven partitioning on y")
regentlib.assert(-ntx <= offset.x and offset.x <= ntx, "offset.x too large")
regentlib.assert(-nty <= offset.y and offset.y <= nty, "offset.y too large")
var coloring = regentlib.c.legion_domain_point_coloring_create()
for c_real in cs do
var c = (c_real - offset + {ntx,nty}) % {ntx,nty}
var rect = rect2d{
lo = int2d{halo.x + (Nx/ntx)*(c.x),
halo.y + (Ny/nty)*(c.y)},
hi = int2d{halo.x + (Nx/ntx)*(c.x+1) - 1,
halo.y + (Ny/nty)*(c.y+1) - 1}}
if c.x == 0 then rect.lo.x -= halo.x end
if c.y == 0 then rect.lo.y -= halo.y end
if c.x == ntx-1 then rect.hi.x += halo.x end
if c.y == nty-1 then rect.hi.y += halo.y end
regentlib.c.legion_domain_point_coloring_color_domain(coloring, c_real, rect)
end
var [p] = partition(disjoint, r, coloring, cs)
regentlib.c.legion_domain_point_coloring_destroy(coloring)
return [p]
end
elseif r_istype == int1d and cs_istype == int3d then
__demand(__inline)
task partitionByTile(r : region(ispace(int1d), fs),
cs : ispace(int3d),
halo : int64,
offset : int3d)
var N = int64(r.bounds.hi - 2*halo + 1)
var ntx = cs.bounds.hi.x + 1
var nty = cs.bounds.hi.y + 1
var ntz = cs.bounds.hi.z + 1
regentlib.assert(int64(r.bounds.lo) == 0, "Can only partition root region")
regentlib.assert(N % (ntx*nty*ntz) == 0, "Uneven partitioning")
regentlib.assert(-ntx <= offset.x and offset.x <= ntx, "offset.x too large")
regentlib.assert(-nty <= offset.y and offset.y <= nty, "offset.y too large")
regentlib.assert(-ntz <= offset.z and offset.z <= ntz, "offset.z too large")
var coloring = regentlib.c.legion_domain_point_coloring_create()
for c_real in cs do
var c = (c_real - offset + {ntx,nty,ntz}) % {ntx,nty,ntz}
var rect = rect1d{
lo = halo + (N/ntx/nty/ntz)*(c.x*nty*ntz+c.y*ntz+c.z),
hi = halo + (N/ntx/nty/ntz)*(c.x*nty*ntz+c.y*ntz+c.z+1) - 1}
if c.x == 0 and
c.y == 0 and
c.z == 0 then rect.lo -= halo end
if c.x == ntx-1 and
c.y == nty-1 and
c.z == ntz-1 then rect.hi += halo end
regentlib.c.legion_domain_point_coloring_color_domain(coloring, c_real, rect)
end
var [p] = partition(disjoint, r, coloring, cs)
regentlib.c.legion_domain_point_coloring_destroy(coloring)
return [p]
end
else assert(false) end
return partitionByTile
end
-- intXd, terralib.struct -> regentlib.task
function Exports.mkPartitionIsInteriorOrGhost(r_istype, fs, name)
local partitionIsInteriorOrGhost
local p = regentlib.newsymbol(name)
if r_istype == int3d then
__demand(__inline)
task partitionIsInteriorOrGhost(r : region(ispace(int3d), fs), halo : int3d)
var Nx = r.bounds.hi.x - 2*halo.x + 1
var Ny = r.bounds.hi.y - 2*halo.y + 1
var Nz = r.bounds.hi.z - 2*halo.z + 1
regentlib.assert(r.bounds.lo == int3d{0,0,0}, "Can only partition root region")
var coloring = regentlib.c.legion_domain_point_coloring_create()
-- Interior points
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(0), rect3d{
lo = int3d{halo.x, halo.y, halo.z },
hi = int3d{halo.x+Nx-1, halo.y+Ny-1, halo.z+Nz-1 }})
-- xNeg faces
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(1), rect3d{
lo = int3d{ 0, halo.y, halo.z },
hi = int3d{ halo.x-1, halo.y+Ny-1, halo.z+Nz-1 }})
-- xPos faces
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(2), rect3d{
lo = int3d{ halo.x+Nx, halo.y, halo.z },
hi = int3d{ r.bounds.hi.x, halo.y+Ny-1, halo.z+Nz-1 }})
-- yNeg faces
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(3), rect3d{
lo = int3d{ halo.x, 0, halo.z },
hi = int3d{ halo.x+Nx-1, halo.y-1, halo.z+Nz-1 }})
-- yPos faces
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(4), rect3d{
lo = int3d{ halo.x, halo.y+Ny, halo.z },
hi = int3d{ halo.x+Nx-1, r.bounds.hi.y, halo.z+Nz-1 }})
-- zNeg faces
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(5), rect3d{
lo = int3d{ halo.x, halo.y, 0 },
hi = int3d{ halo.x+Nx-1, halo.y+Ny-1, halo.z-1 }})
-- zPos faces
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(6), rect3d{
lo = int3d{ halo.x, halo.y, halo.z+Nz },
hi = int3d{ halo.x+Nx-1, halo.y+Ny-1, r.bounds.hi.z }})
-- xNeg-yNeg edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(7), rect3d{
lo = int3d{ 0, 0, halo.z },
hi = int3d{ halo.x-1, halo.y-1, halo.z+Nz-1 }})
-- xNeg-zNeg edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(8), rect3d{
lo = int3d{ 0, halo.y, 0 },
hi = int3d{ halo.x-1, halo.y+Ny-1, halo.z-1 }})
-- xNeg-yPos edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(9), rect3d{
lo = int3d{ 0, halo.y+Ny, halo.z },
hi = int3d{ halo.x-1, r.bounds.hi.y, halo.z+Nz-1 }})
-- xNeg-yPos edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(10), rect3d{
lo = int3d{ 0, halo.y, halo.z+Nz },
hi = int3d{ halo.x-1, halo.y+Ny-1, r.bounds.hi.z }})
-- xPos-yNeg edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(11), rect3d{
lo = int3d{ halo.x+Nx, 0, halo.z },
hi = int3d{ r.bounds.hi.x, halo.y-1, halo.z+Nz-1 }})
-- xPos-zNeg edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(12), rect3d{
lo = int3d{ halo.x+Nx, halo.y, 0 },
hi = int3d{ r.bounds.hi.x, halo.y+Ny-1, halo.z-1 }})
-- xPos-yPos edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(13), rect3d{
lo = int3d{ halo.x+Nx, halo.y+Ny, halo.z },
hi = int3d{ r.bounds.hi.x, r.bounds.hi.y, halo.z+Nz-1 }})
-- xPos-yPos edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(14), rect3d{
lo = int3d{ halo.x+Nx, halo.y, halo.z+Nz },
hi = int3d{ r.bounds.hi.x, halo.y+Ny-1, r.bounds.hi.z }})
-- yNeg-zNeg edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(15), rect3d{
lo = int3d{ halo.x, 0, 0 },
hi = int3d{ halo.x+Nx-1, halo.y-1, halo.z-1 }})
-- yNeg-zPos edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(16), rect3d{
lo = int3d{ halo.x, 0, halo.z+Nz },
hi = int3d{ halo.x+Nx-1, halo.y-1, r.bounds.hi.z }})
-- yPos-zNeg edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(17), rect3d{
lo = int3d{ halo.x, halo.y+Ny, 0 },
hi = int3d{ halo.x+Nx-1, r.bounds.hi.y, halo.z-1 }})
-- yPos-zPos edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(18), rect3d{
lo = int3d{ halo.x, halo.y+Ny, halo.z+Nz },
hi = int3d{ halo.x+Nx-1, r.bounds.hi.y, r.bounds.hi.z }})
-- xNeg-yNeg-zNeg corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(19), rect3d{
lo = int3d{ 0, 0, 0 },
hi = int3d{ halo.x-1, halo.y-1, halo.z-1 }})
-- xNeg-yPos-zNeg corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(20), rect3d{
lo = int3d{ 0, halo.y+Ny, 0 },
hi = int3d{ halo.x-1, r.bounds.hi.y, halo.z-1 }})
-- xNeg-yNeg-zPos corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(21), rect3d{
lo = int3d{ 0, 0, halo.z+Nz },
hi = int3d{ halo.x-1, halo.y-1, r.bounds.hi.z }})
-- xNeg-yPos-zNeg corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(22), rect3d{
lo = int3d{ 0, halo.y+Ny, halo.z+Nz },
hi = int3d{ halo.x-1, r.bounds.hi.y, r.bounds.hi.z }})
-- xPos-yNeg-zNeg corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(23), rect3d{
lo = int3d{ halo.x+Nx, 0, 0 },
hi = int3d{ r.bounds.hi.x, halo.y-1, halo.z-1 }})
-- xPos-yPos-zNeg corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(24), rect3d{
lo = int3d{ halo.x+Nx, halo.y+Ny, 0 },
hi = int3d{ r.bounds.hi.x, r.bounds.hi.y, halo.z-1 }})
-- xPos-yNeg-zPos corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(25), rect3d{
lo = int3d{ halo.x+Nx, 0, halo.z+Nz },
hi = int3d{ r.bounds.hi.x, halo.y-1, r.bounds.hi.z }})
-- xPos-yPos-zNeg corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(26), rect3d{
lo = int3d{ halo.x+Nx, halo.y+Ny, halo.z+Nz },
hi = int3d{ r.bounds.hi.x, r.bounds.hi.y, r.bounds.hi.z }})
var [p] = partition(disjoint, r, coloring, ispace(int1d,27))
regentlib.c.legion_domain_point_coloring_destroy(coloring)
return [p]
end
elseif r_istype == int2d then
__demand(__inline)
task partitionIsInteriorOrGhost(r : region(ispace(int2d), fs), halo : int2d)
var Nx = r.bounds.hi.x - 2*halo.x + 1
var Ny = r.bounds.hi.y - 2*halo.y + 1
regentlib.assert(r.bounds.lo == int2d{0,0}, "Can only partition root region")
var coloring = regentlib.c.legion_domain_point_coloring_create()
-- Interior points
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(0), rect2d{
lo = int2d{halo.x, halo.y },
hi = int2d{halo.x+Nx-1, halo.y+Ny-1 }})
-- xNeg edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(1), rect2d{
lo = int2d{0, halo.y },
hi = int2d{halo.x-1, halo.y+Ny-1 }})
-- xPos edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(2), rect2d{
lo = int2d{halo.x+Nx, halo.y },
hi = int2d{r.bounds.hi.x, halo.y+Ny-1 }})
-- yNeg edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(3), rect2d{
lo = int2d{halo.x, 0 },
hi = int2d{halo.x+Nx-1, halo.y-1, }})
-- yPos edge
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(4), rect2d{
lo = int2d{halo.x, halo.y+Ny },
hi = int2d{halo.x+Nx-1, r.bounds.hi.y }})
-- xNeg-yNeg corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(5), rect2d{
lo = int2d{0, 0 },
hi = int2d{halo.x-1, halo.y-1 }})
-- xNeg-yPos corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(6), rect2d{
lo = int2d{0, halo.y+Ny },
hi = int2d{halo.x-1, r.bounds.hi.y }})
-- xPos-yNeg corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(7), rect2d{
lo = int2d{halo.x+Nx, 0 },
hi = int2d{r.bounds.hi.x, halo.y-1 }})
-- xPos-yPos corner
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(8), rect2d{
lo = int2d{halo.x+Nx, halo.y+Ny },
hi = int2d{r.bounds.hi.x, r.bounds.hi.y }})
var [p] = partition(disjoint, r, coloring, ispace(int1d,9))
regentlib.c.legion_domain_point_coloring_destroy(coloring)
return [p]
end
elseif r_istype == int1d then
__demand(__inline)
task partitionIsInteriorOrGhost(r : region(ispace(int1d), fs), halo : int64)
var N = int64(r.bounds.hi - 2*halo + 1)
regentlib.assert(r.bounds.lo == 0, "Can only partition root region")
var coloring = regentlib.c.legion_multi_domain_point_coloring_create()
-- Interior points
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(0), rect1d{ lo = halo, hi = halo+N-1 })
-- xNeg point
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(1), rect1d{ lo = 0, hi = halo-1 })
-- xPos point
regentlib.c.legion_domain_point_coloring_color_domain(coloring, int1d(2), rect1d{ lo = halo+N, hi = r.bounds.hi })
var [p] = partition(disjoint, r, coloring, ispace(int1d,3))
regentlib.c.legion_domain_point_coloring_destroy(coloring)
return [p]
end
else assert(false) end
return partitionIsInteriorOrGhost
end
-- string, intXd, intXd, terralib.struct, int -> regentlib.task
function Exports.mkExtractRelevantIspace(p_type, r_istype, cs_istype, fs, ind)
local extractRelevantIspace
if p_type == "cross_product" and r_istype == int3d and cs_istype == int3d and ind == nil then
__demand(__inline)
task extractRelevantIspace(r : region(ispace(int3d), fs),
p1 : partition(aliased, r, ispace(int1d)),
p2 : partition(disjoint, r, ispace(int3d)),
pr : cross_product(p1, p2),
aux : region(ispace(int3d), bool))
var cs1 = p1.colors
var cs2 = p2.colors
var isvalidAux = (aux.ispace.bounds.lo.x == cs2.bounds.lo.x) and
(aux.ispace.bounds.lo.y == cs2.bounds.lo.y) and
(aux.ispace.bounds.lo.z == cs2.bounds.lo.z) and
(aux.ispace.bounds.hi.x == cs2.bounds.hi.x) and
(aux.ispace.bounds.hi.y == cs2.bounds.hi.y) and
(aux.ispace.bounds.hi.z == cs2.bounds.hi.z)
regentlib.assert(isvalidAux, "extractRelevantIspace: aux region must have the same index space as p2")
var coloring = regentlib.c.legion_multi_domain_point_coloring_create()
for c1 in cs1 do
for c2 in cs2 do
var p = pr[c1][c2]
if p.volume ~= 0 then
regentlib.c.legion_multi_domain_point_coloring_color_domain(coloring, c1, rect3d{lo=c2, hi=c2})
end
end
end
var p_aux = partition(aliased, aux, coloring, cs1)
regentlib.c.legion_multi_domain_point_coloring_destroy(coloring)
return p_aux
end
elseif p_type == "cross_product" and r_istype == int3d and cs_istype == int3d and ind ~= nil then
__demand(__inline)
task extractRelevantIspace(r : region(ispace(int3d), fs),
p1 : partition(disjoint, r, ispace(int3d)),
p2 : partition(disjoint, r, ispace(int1d)),
pr : cross_product(p1, p2))
var cs = p1.colors
var aux = region(cs, bool)
var coloring = regentlib.c.legion_multi_domain_point_coloring_create()
for c in cs do
var p = pr[c][ind]
if p.volume ~= 0 then
regentlib.c.legion_multi_domain_point_coloring_color_domain(coloring, int1d(0), rect3d{lo=c, hi=c})
end
end
var p_aux = partition(disjoint, aux, coloring, ispace(int1d, 1))
regentlib.c.legion_multi_domain_point_coloring_destroy(coloring)
return p_aux[0].ispace
end
elseif p_type == "partition" and r_istype == int3d and cs_istype == int3d then
assert(ind == nil)
__demand(__inline)
task extractRelevantIspace(r : region(ispace(int3d), fs),
p : partition(disjoint, r, ispace(int3d)))
var cs = p1.colors
var aux = region(cs, bool)
var coloring = regentlib.c.legion_multi_domain_point_coloring_create()
for c in cs do
var p = p[c]
if p.volume ~= 0 then
regentlib.c.legion_multi_domain_point_coloring_color_domain(coloring, int1d(0), rect3d{lo=c, hi=c})
end
end
var p_aux = partition(disjoint, aux, coloring, ispace(int1d, 1))
regentlib.c.legion_multi_domain_point_coloring_destroy(coloring)
return p_aux[0].ispace
end
else assert(false) end
return extractRelevantIspace
end
-- int, string, regentlib.rexpr, regentlib.rexpr -> regentlib.rquote
function Exports.emitArrayReduce(dims, op, lhs, rhs)
-- We decompose each array-type reduction into a sequence of primitive
-- reductions over the array's elements, to make sure the code generator can
-- emit them as atomic operations if needed.
return rquote
var tmp = [rhs];
@ESCAPE for i = 0,dims-1 do
if op == '+' then @EMIT lhs[i] += tmp[i] @TIME
elseif op == '-' then @EMIT lhs[i] -= tmp[i] @TIME
elseif op == '*' then @EMIT lhs[i] *= tmp[i] @TIME
elseif op == '/' then @EMIT lhs[i] /= tmp[i] @TIME
elseif op == 'max' then @EMIT lhs[i] max= tmp[i] @TIME
elseif op == 'min' then @EMIT lhs[i] min= tmp[i] @TIME
else assert(false) end
end @EPACSE
end
end
-------------------------------------------------------------------------------
-- Error handling
-------------------------------------------------------------------------------
-- regentlib.rexpr, string, regentlib.rexpr* -> regentlib.rquote
function Exports.emitAssert(cond, format, ...)
local args = terralib.newlist{...}
return rquote
if not cond then
var stderr = C.fdopen(2, 'w')
C.fprintf(stderr, format, [args])
C.fflush(stderr)
C.exit(1)
end
end
end
-------------------------------------------------------------------------------
return Exports