Skip to content

Commit

Permalink
Merge branch 'distexprs2' into 'master'
Browse files Browse the repository at this point in the history
Refactor Distributed Expressions Again to Track References and Find Leaks

See merge request StanfordLegion/legion!420
  • Loading branch information
rainmakereuab committed Dec 21, 2021
2 parents 088791e + bf5c956 commit 8077278
Show file tree
Hide file tree
Showing 19 changed files with 2,153 additions and 810 deletions.
222 changes: 221 additions & 1 deletion runtime/legion/garbage_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ namespace Legion {
mutation_effects.insert(ev);
}

/////////////////////////////////////////////////////////////
// ImplicitReferenceTracker
/////////////////////////////////////////////////////////////

//--------------------------------------------------------------------------
ImplicitReferenceTracker::~ImplicitReferenceTracker(void)
//--------------------------------------------------------------------------
{
for (std::vector<IndexSpaceExpression*>::const_iterator it =
live_expressions.begin(); it != live_expressions.end(); it++)
if ((*it)->remove_base_expression_reference(LIVE_EXPR_REF))
delete (*it);
}

/////////////////////////////////////////////////////////////
// DistributedCollectable
/////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -447,6 +461,172 @@ namespace Legion {
return do_deletion;
}

//--------------------------------------------------------------------------
bool DistributedCollectable::check_valid_and_increment(
ReferenceSource source, int cnt)
//--------------------------------------------------------------------------
{
AutoLock gc(gc_lock);
if (current_state != VALID_STATE)
return false;
#ifdef DEBUG_LEGION
assert(cnt >= 0);
#endif
#ifdef LEGION_GC
log_base_ref<true>(VALID_REF_KIND, did, local_space, source, cnt);
#endif
#ifndef DEBUG_LEGION_GC
int previous = __sync_fetch_and_add(&valid_references, cnt);
#ifdef DEBUG_LEGION
assert(previous >= 0);
#endif
if (previous == 0)
has_valid_references = true;
#else
valid_references++;
std::map<ReferenceSource,int>::iterator finder =
detailed_base_valid_references.find(source);
if (finder == detailed_base_valid_references.end())
detailed_base_valid_references[source] = cnt;
else
finder->second += cnt;
if (valid_references > cnt)
return true;
#ifdef DEBUG_LEGION
assert(!has_valid_references);
#endif
has_valid_references = true;
#endif
return true;
}

//--------------------------------------------------------------------------
bool DistributedCollectable::check_valid_and_increment(
DistributedID source, int cnt)
//--------------------------------------------------------------------------
{
AutoLock gc(gc_lock);
if (current_state != VALID_STATE)
return false;
#ifdef DEBUG_LEGION
assert(cnt >= 0);
#endif
#ifdef LEGION_GC
log_nested_ref<true>(VALID_REF_KIND, did, local_space, source, cnt);
#endif
#ifndef DEBUG_LEGION_GC
int previous = __sync_fetch_and_add(&valid_references, cnt);
#ifdef DEBUG_LEGION
assert(previous >= 0);
#endif
if (previous == 0)
has_valid_references = true;
#else
valid_references++;
source = LEGION_DISTRIBUTED_ID_FILTER(source);
std::map<DistributedID,int>::iterator finder =
detailed_nested_valid_references.find(source);
if (finder == detailed_nested_valid_references.end())
detailed_nested_valid_references[source] = cnt;
else
finder->second += cnt;
if (valid_references > cnt)
return true;
#ifdef DEBUG_LEGION
assert(!has_valid_references);
#endif
has_valid_references = true;
#endif
return true;
}

//--------------------------------------------------------------------------
bool DistributedCollectable::check_gc_and_increment(
ReferenceSource source, int cnt)
//--------------------------------------------------------------------------
{
AutoLock gc(gc_lock);
if ((current_state == INACTIVE_STATE) ||
(current_state == DELETED_STATE) ||
(current_state == PENDING_ACTIVE_STATE) ||
(current_state == PENDING_INACTIVE_STATE) ||
(current_state == PENDING_INACTIVE_INVALID_STATE))
return false;
#ifdef DEBUG_LEGION
assert(cnt >= 0);
#endif
#ifdef LEGION_GC
log_base_ref<true>(GC_REF_KIND, did, local_space, source, cnt);
#endif
#ifndef DEBUG_LEGION_GC
int previous = __sync_fetch_and_add(&gc_references, cnt);
#ifdef DEBUG_LEGION
assert(previous >= 0);
#endif
if (previous == 0)
has_gc_references = true;
#else
gc_references++;
std::map<ReferenceSource,int>::iterator finder =
detailed_base_gc_references.find(source);
if (finder == detailed_base_gc_references.end())
detailed_base_gc_references[source] = cnt;
else
finder->second += cnt;
if (gc_references > cnt)
return true;
#ifdef DEBUG_LEGION
assert(!has_gc_references);
#endif
has_gc_references = true;
#endif
return true;
}

//--------------------------------------------------------------------------
bool DistributedCollectable::check_gc_and_increment(
DistributedID source, int cnt)
//--------------------------------------------------------------------------
{
AutoLock gc(gc_lock);
if ((current_state == INACTIVE_STATE) ||
(current_state == DELETED_STATE) ||
(current_state == PENDING_ACTIVE_STATE) ||
(current_state == PENDING_INACTIVE_STATE) ||
(current_state == PENDING_INACTIVE_INVALID_STATE))
return false;
#ifdef DEBUG_LEGION
assert(cnt >= 0);
#endif
#ifdef LEGION_GC
log_nested_ref<true>(GC_REF_KIND, did, local_space, source, cnt);
#endif
#ifndef DEBUG_LEGION_GC
int previous = __sync_fetch_and_add(&gc_references, cnt);
#ifdef DEBUG_LEGION
assert(previous >= 0);
#endif
if (previous == 0)
has_gc_references = true;
#else
gc_references++;
source = LEGION_DISTRIBUTED_ID_FILTER(source);
std::map<DistributedID,int>::iterator finder =
detailed_nested_gc_references.find(source);
if (finder == detailed_nested_gc_references.end())
detailed_nested_gc_references[source] = cnt;
else
finder->second += cnt;
if (gc_references > cnt)
return true;
#ifdef DEBUG_LEGION
assert(!has_gc_references);
#endif
has_gc_references = true;
#endif
return true;
}

//--------------------------------------------------------------------------
bool DistributedCollectable::check_resource_and_increment(
ReferenceSource source, int cnt)
Expand Down Expand Up @@ -486,6 +666,46 @@ namespace Legion {
return true;
}

//--------------------------------------------------------------------------
bool DistributedCollectable::check_resource_and_increment(
DistributedID source, int cnt)
//--------------------------------------------------------------------------
{
AutoLock gc(gc_lock);
if (current_state == DELETED_STATE)
return false;
#ifdef DEBUG_LEGION
assert(cnt >= 0);
#endif
#ifdef LEGION_GC
log_nested_ref<true>(RESOURCE_REF_KIND, did, local_space, source, cnt);
#endif
#ifndef DEBUG_LEGION_GC
int previous = __sync_fetch_and_add(&resource_references, cnt);
#ifdef DEBUG_LEGION
assert(previous >= 0);
#endif
if (previous == 0)
has_resource_references = true;
#else
resource_references++;
source = LEGION_DISTRIBUTED_ID_FILTER(source);
std::map<DistributedID,int>::iterator finder =
detailed_nested_resource_references.find(source);
if (finder == detailed_nested_resource_references.end())
detailed_nested_resource_references[source] = cnt;
else
finder->second += cnt;
if (resource_references > cnt)
return true;
#ifdef DEBUG_LEGION
assert(!has_resource_references);
#endif
has_resource_references = true;
#endif
return true;
}

//--------------------------------------------------------------------------
void DistributedCollectable::add_resource_reference(void)
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -1630,7 +1850,7 @@ namespace Legion {
#ifdef DEBUG_LEGION
assert(count != 0);
assert(registered_with_runtime);
#endif
#endif
RtUserEvent done_event;
if (mutator != NULL)
{
Expand Down
83 changes: 34 additions & 49 deletions runtime/legion/garbage_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace Legion {
REMOTE_DID_REF = 7,
PENDING_COLLECTIVE_REF = 8,
MEMORY_MANAGER_REF = 9,
COMPOSITE_NODE_REF = 10,
INSTANCE_BUILDER_REF = 10,
FIELD_ALLOCATOR_REF = 11,
REMOTE_CREATE_REF = 12,
INSTANCE_MAPPER_REF = 13,
Expand All @@ -75,18 +75,17 @@ namespace Legion {
NEVER_GC_REF = 16,
CONTEXT_REF = 17,
RESTRICTED_REF = 18,
VERSION_STATE_TREE_REF = 19,
PHYSICAL_MANAGER_REF = 20,
META_TASK_REF = 19,
PHYSICAL_USER_REF = 20,
LOGICAL_VIEW_REF = 21,
REGION_TREE_REF = 22,
LAYOUT_DESC_REF = 23,
RUNTIME_REF = 24,
IS_EXPR_REF = 25,
LIVE_EXPR_REF = 25,
TRACE_REF = 26,
AGGREGATORE_REF = 27,
AGGREGATOR_REF = 27,
FIELD_STATE_REF = 28,
CANONICAL_REF = 29,
LAST_SOURCE_REF = 30,
LAST_SOURCE_REF = 29,
};

enum ReferenceKind {
Expand All @@ -107,7 +106,7 @@ namespace Legion {
"Remote Distributed ID Reference", \
"Pending Collective Reference", \
"Memory Manager Reference", \
"Composite Node Reference", \
"Instance Builder Reference", \
"Field Allocator Reference", \
"Remote Creation Reference", \
"Instance Mapper Reference", \
Expand All @@ -116,17 +115,16 @@ namespace Legion {
"Never GC Reference", \
"Context Reference", \
"Restricted Reference", \
"Version State Tree Reference", \
"Physical Manager Reference", \
"Meta-Task Reference", \
"Physical User Reference", \
"Logical View Reference", \
"Region Tree Reference", \
"Layout Description Reference", \
"Runtime Reference", \
"Index Space Expression Reference", \
"Live Index Space Expression Reference", \
"Physical Trace Reference", \
"Aggregator Reference", \
"Field State Reference", \
"Canonical Index Space Expression Reference", \
}

extern Realm::Logger log_garbage;
Expand Down Expand Up @@ -227,12 +225,24 @@ namespace Legion {
};

/**
* \class IgnoreReferenceMutator
* This will ignore any reference effects
* \class ImplicitReferenceTracker
* This class tracks implicit references that are held either by
* an application runtime API call or a meta-task. At the end of the
* runtime API call or meta-task the references are updated.
*/
class IgnoreReferenceMutator : public ReferenceMutator {
class ImplicitReferenceTracker {
public:
virtual void record_reference_mutation_effect(RtEvent event) { }
ImplicitReferenceTracker(void) { }
ImplicitReferenceTracker(const ImplicitReferenceTracker&) = delete;
~ImplicitReferenceTracker(void);
public:
ImplicitReferenceTracker& operator=(
const ImplicitReferenceTracker&) = delete;
public:
inline void record_live_expression(IndexSpaceExpression *expr)
{ live_expressions.emplace_back(expr); }
private:
std::vector<IndexSpaceExpression*> live_expressions;
};

/**
Expand Down Expand Up @@ -333,9 +343,16 @@ namespace Legion {
inline bool remove_base_resource_ref(ReferenceSource source, int cnt = 1);
inline bool remove_nested_resource_ref(DistributedID source, int cnt = 1);
public:
#ifdef DEBUG_LEGION
bool check_valid(void) const { return (current_state == VALID_STATE); }
#endif
// Atomic check and increment operations
inline bool check_valid_and_increment(ReferenceSource source,int cnt = 1);
bool check_valid_and_increment(ReferenceSource source,int cnt = 1);
bool check_valid_and_increment(DistributedID source, int cnt = 1);
bool check_gc_and_increment(ReferenceSource source, int cnt = 1);
bool check_gc_and_increment(DistributedID source, int cnt = 1);
bool check_resource_and_increment(ReferenceSource source ,int cnt = 1);
bool check_resource_and_increment(DistributedID source, int cnt = 1);
private:
void add_gc_reference(ReferenceMutator *mutator);
bool remove_gc_reference(ReferenceMutator *mutator);
Expand Down Expand Up @@ -864,38 +881,6 @@ namespace Legion {
#endif
}

//--------------------------------------------------------------------------
inline bool DistributedCollectable::check_valid_and_increment(
ReferenceSource source, int cnt /*=1*/)
//--------------------------------------------------------------------------
{
#ifdef DEBUG_LEGION
assert(cnt >= 0);
#endif
// Don't support this if we are debugging GC
#ifndef DEBUG_LEGION_GC
// Read the value in an unsafe way at first
int current_cnt = valid_references;
// Don't even both trying if the count is zero
while (current_cnt > 0)
{
const int next_cnt = current_cnt + cnt;
const int prev_cnt =
__sync_val_compare_and_swap(&valid_references, current_cnt, next_cnt);
if (prev_cnt == current_cnt)
{
#ifdef LEGION_GC
log_base_ref<true>(VALID_REF_KIND, did, local_space, source, cnt);
#endif
return true;
}
// Update the current count
current_cnt = prev_cnt;
}
#endif
return false;
}

}; // namespace Internal
}; // namespace Legion

Expand Down
Loading

0 comments on commit 8077278

Please sign in to comment.