Legion Runtime
Loading...
Searching...
No Matches
physical_region.h
Go to the documentation of this file.
1/* Copyright 2026 Stanford University, NVIDIA Corporation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef __LEGION_PHYSICAL_REGION_H__
17#define __LEGION_PHYSICAL_REGION_H__
18
24#include "legion/api/types.h"
25#include "legion/api/geometry.h"
26#include "legion/api/buffers.h"
27
28namespace Legion {
29
39 public:
40 PhysicalRegion(void);
42 PhysicalRegion(PhysicalRegion&& rhs) noexcept;
43 ~PhysicalRegion(void);
44 private:
45 Internal::PhysicalRegionImpl* impl;
46 protected:
47 FRIEND_ALL_RUNTIME_CLASSES
48 explicit PhysicalRegion(Internal::PhysicalRegionImpl* impl);
49 public:
50 PhysicalRegion& operator=(const PhysicalRegion& rhs);
51 PhysicalRegion& operator=(PhysicalRegion&& rhs) noexcept;
52 inline bool exists(void) const { return (impl != nullptr); }
53 inline bool operator==(const PhysicalRegion& reg) const
54 {
55 return (impl == reg.impl);
56 }
57 inline bool operator!=(const PhysicalRegion& reg) const
58 {
59 return (impl != reg.impl);
60 }
61 inline bool operator<(const PhysicalRegion& reg) const
62 {
63 return (impl < reg.impl);
64 }
65 std::size_t hash(void) const;
66 public:
70 bool is_mapped(void) const;
79 bool silence_warnings = false, const char* warning_string = nullptr);
86 bool is_valid(void) const;
94 PrivilegeMode get_privilege(void) const;
99 std::set<Memory>& memories, bool silence_warnings = false,
100 const char* warning_string = nullptr) const;
104 void get_fields(std::vector<FieldID>& fields) const;
105 public:
106 template<int DIM, typename COORD_T>
107 DomainT<DIM, COORD_T> get_bounds(void) const;
108 // We'll also allow this to implicitly cast to a realm index space
109 // so that users can easily iterate over the points
110 template<int DIM, typename COORD_T>
111 operator DomainT<DIM, COORD_T>(void) const;
112 // They can implicitly cast to a rectangle if there is no
113 // sparsity map, runtime will check for this
114 template<int DIM, typename COORD_T>
115 operator Rect<DIM, COORD_T>(void) const;
116 protected:
117 // These methods can only be accessed by accessor classes
118 template<PrivilegeMode, typename, int, typename, typename, bool>
119 friend class FieldAccessor;
120 template<typename, bool, int, typename, typename, bool>
121 friend class ReductionAccessor;
122 template<typename, int, typename, typename, bool, bool, int>
123 friend class MultiRegionAccessor;
124 template<typename, int, typename, typename, bool>
125 friend class PaddingAccessor;
126 template<typename, int, typename, typename>
127 friend class UnsafeFieldAccessor;
128 template<typename, PrivilegeMode>
130 template<typename>
131 friend class ArraySyntax::AffineRefHelper;
132 friend class PieceIterator;
133 template<PrivilegeMode, typename, int, typename>
134 friend class SpanIterator;
135 template<typename, int, typename>
136 friend class UnsafeSpanIterator;
137 Realm::RegionInstance get_instance_info(
138 PrivilegeMode mode, FieldID fid, size_t field_size, void* realm_is,
139 TypeTag type_tag, const char* warning_string, bool silence_warnings,
140 bool generic_accessor, bool check_field_size,
141 ReductionOpID redop = 0) const;
142 Realm::RegionInstance get_instance_info(
143 PrivilegeMode mode, const std::vector<PhysicalRegion>& other_regions,
144 FieldID fid, size_t field_size, void* realm_is, TypeTag type_tag,
145 const char* warning_string, bool silence_warnings,
146 bool generic_accessor, bool check_field_size, bool need_bounds,
147 ReductionOpID redop = 0) const;
148 Realm::RegionInstance get_padding_info(
149 FieldID fid, size_t field_size, Domain* inner, Domain& outer,
150 const char* warning_string, bool silence_warnings,
151 bool generic_accessor, bool check_field_size) const;
152 void report_incompatible_accessor(
153 const char* accessor_kind, Realm::RegionInstance instance,
154 FieldID fid) const;
155 void report_incompatible_multi_accessor(
156 unsigned index, FieldID fid, Realm::RegionInstance inst1,
157 Realm::RegionInstance inst2) const;
158 void report_colocation_violation(
159 const char* accessor_kind, FieldID fid, Realm::RegionInstance inst1,
160 Realm::RegionInstance inst2, const PhysicalRegion& other,
161 bool reduction = false) const;
162 static void empty_colocation_regions(
163 const char* accessor_kind, FieldID fid, bool reduction = false);
164 static void fail_bounds_check(
165 DomainPoint p, FieldID fid, PrivilegeMode mode, bool multi = false);
166 static void fail_bounds_check(
167 Domain d, FieldID fid, PrivilegeMode mode, bool multi = false);
168 static void fail_privilege_check(
169 DomainPoint p, FieldID fid, PrivilegeMode mode);
170 static void fail_privilege_check(Domain d, FieldID fid, PrivilegeMode mode);
171 static void fail_padding_check(DomainPoint p, FieldID fid);
172 static void fail_nondense_rect(void);
173 static void fail_rect_piece(void);
174 protected:
175 void get_bounds(void* realm_is, TypeTag type_tag) const;
176 };
177
186 public:
187 ExternalResources(void);
189 ExternalResources(ExternalResources&& rhs) noexcept;
190 ~ExternalResources(void);
191 private:
192 Internal::ExternalResourcesImpl* impl;
193 protected:
194 FRIEND_ALL_RUNTIME_CLASSES
195 explicit ExternalResources(Internal::ExternalResourcesImpl* impl);
196 public:
197 ExternalResources& operator=(const ExternalResources& rhs);
198 ExternalResources& operator=(ExternalResources&& rhs) noexcept;
199 inline bool exists(void) const { return (impl != nullptr); }
200 inline bool operator==(const ExternalResources& reg) const
201 {
202 return (impl == reg.impl);
203 }
204 inline bool operator<(const ExternalResources& reg) const
205 {
206 return (impl < reg.impl);
207 }
208 public:
209 size_t size(void) const;
210 PhysicalRegion operator[](unsigned index) const;
211 };
212
237 public:
238 PieceIterator(void);
239 PieceIterator(const PieceIterator& rhs);
240 PieceIterator(PieceIterator&& rhs) noexcept;
242 const PhysicalRegion& region, FieldID fid, bool privilege_only = true,
243 bool silence_warnings = false, const char* warning_string = nullptr);
244 ~PieceIterator(void);
245 public:
246 PieceIterator& operator=(const PieceIterator& rhs);
247 PieceIterator& operator=(PieceIterator&& rhs) noexcept;
248 public:
249 inline bool valid(void) const;
250 bool step(void);
251 public:
252 inline operator bool(void) const;
253 inline bool operator()(void) const;
254 inline const Domain& operator*(void) const;
255 inline const Domain* operator->(void) const;
256 inline PieceIterator& operator++(void);
257 inline PieceIterator operator++(int /*postfix*/);
258 public:
259 bool operator<(const PieceIterator& rhs) const;
260 bool operator==(const PieceIterator& rhs) const;
261 bool operator!=(const PieceIterator& rhs) const;
262 private:
263 Internal::PieceIteratorImpl* impl;
264 int index;
265 protected:
266 Domain current_piece;
267 };
268
274 template<int DIM, typename COORD_T = coord_t>
276 private:
277 static_assert(DIM > 0, "DIM must be positive");
278 static_assert(DIM <= LEGION_MAX_DIM, "DIM must be <= LEGION_MAX_DIM");
279 static_assert(std::is_integral<COORD_T>::value, "must be integral type");
280 public:
281 PieceIteratorT(void);
282 PieceIteratorT(const PieceIteratorT& rhs);
283 PieceIteratorT(PieceIteratorT&& rhs) noexcept;
285 const PhysicalRegion& region, FieldID fid, bool privilege_only,
286 bool silence_warnings = false, const char* warning_string = nullptr);
287 public:
288 PieceIteratorT<DIM, COORD_T>& operator=(const PieceIteratorT& rhs);
289 PieceIteratorT<DIM, COORD_T>& operator=(PieceIteratorT&& rhs) noexcept;
290 public:
291 inline bool step(void);
292 inline const Rect<DIM, COORD_T>& operator*(void) const;
293 inline const Rect<DIM, COORD_T>* operator->(void) const;
294 inline PieceIteratorT<DIM, COORD_T>& operator++(void);
295 inline PieceIteratorT<DIM, COORD_T> operator++(int /*postfix*/);
296 protected:
297 Rect<DIM, COORD_T> current_rect;
298 };
299
308 template<typename FT, PrivilegeMode PM = LEGION_READ_WRITE>
309 class Span {
310 public:
311 class iterator {
312 public:
313 // explicitly set iterator traits
314 typedef std::random_access_iterator_tag iterator_category;
315 typedef FT value_type;
316 typedef std::ptrdiff_t difference_type;
317 typedef FT* pointer;
318 typedef FT& reference;
319
320 iterator(void) : ptr(nullptr), stride(0) { }
321 private:
322 iterator(uint8_t* p, size_t s) : ptr(p), stride(s) { }
323 public:
324 inline iterator& operator=(const iterator& rhs)
325 {
326 ptr = rhs.ptr;
327 stride = rhs.stride;
328 return *this;
329 }
330 inline iterator& operator+=(int rhs)
331 {
332 ptr += stride;
333 return *this;
334 }
335 inline iterator& operator-=(int rhs)
336 {
337 ptr -= stride;
338 return *this;
339 }
340 inline FT& operator*(void) const
341 {
342 FT* result = nullptr;
343 static_assert(sizeof(result) == sizeof(ptr));
344 memcpy(&result, &ptr, sizeof(result));
345 return *result;
346 }
347 inline FT* operator->(void) const
348 {
349 FT* result = nullptr;
350 static_assert(sizeof(result) == sizeof(ptr));
351 memcpy(&result, &ptr, sizeof(result));
352 return result;
353 }
354 inline FT& operator[](int rhs) const
355 {
356 FT* result = nullptr;
357 uint8_t* ptr2 = ptr + rhs * stride;
358 static_assert(sizeof(result) == sizeof(ptr2));
359 memcpy(&result, &ptr2, sizeof(result));
360 return *result;
361 }
362 public:
363 inline iterator& operator++(void)
364 {
365 ptr += stride;
366 return *this;
367 }
368 inline iterator& operator--(void)
369 {
370 ptr -= stride;
371 return *this;
372 }
373 inline iterator operator++(int)
374 {
375 iterator it(ptr, stride);
376 ptr += stride;
377 return it;
378 }
379 inline iterator operator--(int)
380 {
381 iterator it(ptr, stride);
382 ptr -= stride;
383 return it;
384 }
385 inline iterator operator+(int rhs) const
386 {
387 return iterator(ptr + stride * rhs, stride);
388 }
389 inline iterator operator-(int rhs) const
390 {
391 return iterator(ptr - stride * rhs, stride);
392 }
393 public:
394 inline bool operator==(const iterator& rhs) const
395 {
396 return (ptr == rhs.ptr);
397 }
398 inline bool operator!=(const iterator& rhs) const
399 {
400 return (ptr != rhs.ptr);
401 }
402 inline bool operator<(const iterator& rhs) const
403 {
404 return (ptr < rhs.ptr);
405 }
406 inline bool operator>(const iterator& rhs) const
407 {
408 return (ptr > rhs.ptr);
409 }
410 inline bool operator<=(const iterator& rhs) const
411 {
412 return (ptr <= rhs.ptr);
413 }
414 inline bool operator>=(const iterator& rhs) const
415 {
416 return (ptr >= rhs.ptr);
417 }
418 private:
419 uint8_t* ptr;
420 size_t stride;
421 };
423 public:
424 // explicitly set iterator traits
425 typedef std::random_access_iterator_tag iterator_category;
426 typedef FT value_type;
427 typedef std::ptrdiff_t difference_type;
428 typedef FT* pointer;
429 typedef FT& reference;
430
431 reverse_iterator(void) : ptr(nullptr), stride(0) { }
432 private:
433 reverse_iterator(uint8_t* p, size_t s) : ptr(p), stride(s) { }
434 public:
435 inline reverse_iterator& operator=(const reverse_iterator& rhs)
436 {
437 ptr = rhs.ptr;
438 stride = rhs.stride;
439 return *this;
440 }
441 inline reverse_iterator& operator+=(int rhs)
442 {
443 ptr -= stride;
444 return *this;
445 }
446 inline reverse_iterator& operator-=(int rhs)
447 {
448 ptr += stride;
449 return *this;
450 }
451 inline FT& operator*(void) const
452 {
453 FT* result = nullptr;
454 static_assert(sizeof(result) == sizeof(ptr));
455 memcpy(&result, &ptr, sizeof(result));
456 return *result;
457 }
458 inline FT* operator->(void) const
459 {
460 FT* result = nullptr;
461 static_assert(sizeof(result) == sizeof(ptr));
462 memcpy(&result, &ptr, sizeof(result));
463 return result;
464 }
465 inline FT& operator[](int rhs) const
466 {
467 FT* result = nullptr;
468 uint8_t* ptr2 = ptr - rhs * stride;
469 static_assert(sizeof(result) == sizeof(ptr2));
470 memcpy(&result, &ptr2, sizeof(result));
471 return *result;
472 }
473 public:
474 inline reverse_iterator& operator++(void)
475 {
476 ptr -= stride;
477 return *this;
478 }
479 inline reverse_iterator& operator--(void)
480 {
481 ptr += stride;
482 return *this;
483 }
484 inline reverse_iterator operator++(int)
485 {
486 reverse_iterator it(ptr, stride);
487 ptr -= stride;
488 return it;
489 }
490 inline reverse_iterator operator--(int)
491 {
492 reverse_iterator it(ptr, stride);
493 ptr += stride;
494 return it;
495 }
496 inline reverse_iterator operator+(int rhs) const
497 {
498 return reverse_iterator(ptr - stride * rhs, stride);
499 }
500 inline reverse_iterator operator-(int rhs) const
501 {
502 return reverse_iterator(ptr + stride * rhs, stride);
503 }
504 public:
505 inline bool operator==(const reverse_iterator& rhs) const
506 {
507 return (ptr == rhs.ptr);
508 }
509 inline bool operator!=(const reverse_iterator& rhs) const
510 {
511 return (ptr != rhs.ptr);
512 }
513 inline bool operator<(const reverse_iterator& rhs) const
514 {
515 return (ptr > rhs.ptr);
516 }
517 inline bool operator>(const reverse_iterator& rhs) const
518 {
519 return (ptr < rhs.ptr);
520 }
521 inline bool operator<=(const reverse_iterator& rhs) const
522 {
523 return (ptr >= rhs.ptr);
524 }
525 inline bool operator>=(const reverse_iterator& rhs) const
526 {
527 return (ptr <= rhs.ptr);
528 }
529 private:
530 uint8_t* ptr;
531 size_t stride;
532 };
533 public:
534 Span(void) : base(nullptr), extent(0), stride(0) { }
535 Span(FT* b, size_t e, size_t s = sizeof(FT))
536 : base(nullptr), extent(e), stride(s)
537 {
538 static_assert(sizeof(base) == sizeof(b));
539 memcpy(&base, &b, sizeof(base));
540 }
541 public:
542 inline iterator begin(void) const { return iterator(base, stride); }
543 inline iterator end(void) const
544 {
545 return iterator(base + extent * stride, stride);
546 }
547 inline reverse_iterator rbegin(void) const
548 {
549 return reverse_iterator(base + (extent - 1) * stride, stride);
550 }
551 inline reverse_iterator rend(void) const
552 {
553 return reverse_iterator(base - stride, stride);
554 }
555 public:
556 inline FT& front(void) const
557 {
558 FT* result = nullptr;
559 static_assert(sizeof(result) == sizeof(base));
560 memcpy(&result, &base, sizeof(result));
561 return *result;
562 }
563 inline FT& back(void) const
564 {
565 FT* result = nullptr;
566 uint8_t* ptr = base + (extent - 1) * stride;
567 static_assert(sizeof(result) == sizeof(ptr));
568 memcpy(&result, &ptr, sizeof(result));
569 return *result;
570 }
571 inline FT& operator[](int index) const
572 {
573 FT* result = nullptr;
574 uint8_t* ptr = base + index * stride;
575 static_assert(sizeof(result) == sizeof(ptr));
576 memcpy(&result, &ptr, sizeof(result));
577 return *result;
578 }
579 inline FT* data(void) const
580 {
581 FT* result = nullptr;
582 static_assert(sizeof(result) == sizeof(base));
583 memcpy(&result, &base, sizeof(result));
584 return result;
585 }
586 inline uintptr_t get_base(void) const { return uintptr_t(base); }
587 public:
588 inline size_t size(void) const { return extent; }
589 inline size_t step(void) const { return stride; }
590 inline bool empty(void) const { return (extent == 0); }
591 private:
592 uint8_t* base;
593 size_t extent; // number of elements
594 size_t stride; // byte stride
595 };
596
606 template<PrivilegeMode PM, typename FT, int DIM, typename COORD_T = coord_t>
608 private:
609 static_assert(DIM > 0, "DIM must be positive");
610 static_assert(DIM <= LEGION_MAX_DIM, "DIM must be <= LEGION_MAX_DIM");
611 static_assert(std::is_integral<COORD_T>::value, "must be integral type");
612 public:
613 SpanIterator(void) { }
615 const PhysicalRegion& region, FieldID fid,
616 // The actual field size in case it is different from the
617 // one being used in FT and we still want to check it
618 size_t actual_field_size = sizeof(FT),
619#ifdef LEGION_DEBUG
620 bool check_field_size = true,
621#else
622 bool check_field_size = false,
623#endif
624 // Iterate only the spans that we have privileges on
625 bool privileges_only = true, bool silence_warnings = false,
626 const char* warning_string = nullptr);
627 public:
628 inline bool valid(void) const;
629 inline bool step(void);
630 public:
631 inline operator bool(void) const;
632 inline bool operator()(void) const;
633 inline const Span<FT, PM>& operator*(void) const;
634 inline const Span<FT, PM>* operator->(void) const;
635 inline SpanIterator<PM, FT, DIM, COORD_T>& operator++(void);
636 inline SpanIterator<PM, FT, DIM, COORD_T> operator++(int);
637 private:
638 PieceIteratorT<DIM, COORD_T> piece_iterator;
639 Realm::MultiAffineAccessor<FT, DIM, COORD_T> accessor;
640 Span<FT, PM> current;
641 Point<DIM, COORD_T> partial_step_point;
642 int dim_order[DIM];
643 int partial_step_dim;
644 bool partial_piece;
645 };
646
647} // namespace Legion
648
649#include "legion/api/physical_region.inl"
650
651#endif // __LEGION_PHYSICAL_REGION_H__
Definition geometry.h:159
Definition geometry.h:34
Definition physical_region.h:185
Definition accessors.h:76
Definition data.h:189
Definition accessors.h:651
Definition physical_region.h:38
void get_memories(std::set< Memory > &memories, bool silence_warnings=false, const char *warning_string=nullptr) const
LogicalRegion get_logical_region(void) const
PrivilegeMode get_privilege(void) const
bool is_valid(void) const
bool is_mapped(void) const
void wait_until_valid(bool silence_warnings=false, const char *warning_string=nullptr)
void get_fields(std::vector< FieldID > &fields) const
Definition physical_region.h:236
Definition physical_region.h:275
Definition accessors.h:384
Definition physical_region.h:311
Definition physical_region.h:422
Definition physical_region.h:309
Definition physical_region.h:607
Definition types.h:268
Definition types.h:343
Definition accessors.h:29