Skip to content

Commit

Permalink
fixed bug with non-threadsafe append in parallel implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dyumanaditya committed Mar 28, 2024
1 parent d246415 commit 294f9ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions pyreason/scripts/interpretation/interpretation_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,10 @@ def _ground_node_rule(rule, interpretations_node, interpretations_edge, nodes, n
# We return a list of tuples which specify the target nodes/edges that have made the rule body true
applicable_rules = numba.typed.List.empty_list(node_applicable_rule_type)

# Create pre-allocated data structure so that parallel code does not need to use "append" to be threadsafe
# One array for each node, then condense into a single list later
applicable_rules_threadsafe = numba.typed.List([numba.typed.List.empty_list(node_applicable_rule_type) for _ in nodes])

# Return empty list if rule is not node rule and if we are not inferring edges
if rule_type != 'node' and rule_edges[0] == '':
return applicable_rules
Expand Down Expand Up @@ -963,7 +967,12 @@ def _ground_node_rule(rule, interpretations_node, interpretations_edge, nodes, n
edges_to_be_added[1].append(target)

# node/edge, annotations, qualified nodes, qualified edges, edges to be added
applicable_rules.append((target_node, annotations, qualified_nodes, qualified_edges, edges_to_be_added))
applicable_rules_threadsafe[piter] = numba.typed.List([(target_node, annotations, qualified_nodes, qualified_edges, edges_to_be_added)])

# Merge all threadsafe rules into one single array
for applicable_rule in applicable_rules_threadsafe:
if len(applicable_rule) > 0:
applicable_rules.append(applicable_rule[0])

return applicable_rules

Expand All @@ -980,6 +989,10 @@ def _ground_edge_rule(rule, interpretations_node, interpretations_edge, nodes, e
# We return a list of tuples which specify the target nodes/edges that have made the rule body true
applicable_rules = numba.typed.List.empty_list(edge_applicable_rule_type)

# Create pre-allocated data structure so that parallel code does not need to use "append" to be threadsafe
# One array for each node, then condense into a single list later
applicable_rules_threadsafe = numba.typed.List([numba.typed.List.empty_list(edge_applicable_rule_type) for _ in edges])

# Return empty list if rule is not node rule
if rule_type != 'edge':
return applicable_rules
Expand Down Expand Up @@ -1191,7 +1204,12 @@ def _ground_edge_rule(rule, interpretations_node, interpretations_edge, nodes, e
edges_to_be_added[1].append(target)

# node/edge, annotations, qualified nodes, qualified edges, edges to be added
applicable_rules.append((target_edge, annotations, qualified_nodes, qualified_edges, edges_to_be_added))
applicable_rules_threadsafe[piter] = numba.typed.List([(target_edge, annotations, qualified_nodes, qualified_edges, edges_to_be_added)])

# Merge all threadsafe rules into one single array
for applicable_rule in applicable_rules_threadsafe:
if len(applicable_rule) > 0:
applicable_rules.append(applicable_rule[0])

return applicable_rules

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='pyreason',
version='2.2.0',
version='2.2.1',
author='Dyuman Aditya',
author_email='[email protected]',
description='An explainable inference software supporting annotated, real valued, graph based and temporal logic',
Expand Down

0 comments on commit 294f9ab

Please sign in to comment.