Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust the lock location #111

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions graphlearn_torch/python/sampler/neighbor_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(self,
self._neg_sampler = None
self._inducer = None
self._sampler_lock = threading.Lock()
self.is_sampler_initialized = False

if seed is not None:
pywrap.RandomSeedManager.getInstance().setSeed(seed)
Expand All @@ -87,8 +88,8 @@ def subgraph_op(self):
return self._subgraph_op

def lazy_init_sampler(self):
if self._sampler is None:
with self._sampler_lock:
if not self.is_sampler_initialized:
with self._sampler_lock:
if self._sampler is None:
if self._g_cls == 'homo':
if self.device.type == 'cuda':
Expand All @@ -97,6 +98,7 @@ def lazy_init_sampler(self):
self._sampler = pywrap.CPURandomSampler(self.graph.graph_handler)
else:
self._sampler = pywrap.CPUWeightedSampler(self.graph.graph_handler)
self.is_sampler_initialized = True

else: # hetero
self._sampler = {}
Expand All @@ -107,18 +109,20 @@ def lazy_init_sampler(self):
self._sampler[etype] = pywrap.CPURandomSampler(g.graph_handler)
else:
self._sampler[etype] = pywrap.CPUWeightedSampler(g.graph_handler)
self.is_sampler_initialized = True


def lazy_init_neg_sampler(self):
if self._neg_sampler is None and self.with_neg:
with self._sampler_lock:
if not self.is_sampler_initialized and self.with_neg:
with self._sampler_lock:
if self._neg_sampler is None:
if self._g_cls == 'homo':
self._neg_sampler = RandomNegativeSampler(
graph=self.graph,
mode=self.device.type.upper(),
edge_dir=self.edge_dir
)
self.is_sampler_initialized = True
else: # hetero
self._neg_sampler = {}
for etype, g in self.graph.items():
Expand All @@ -127,10 +131,11 @@ def lazy_init_neg_sampler(self):
mode=self.device.type.upper(),
edge_dir=self.edge_dir
)
self.is_sampler_initialized = True

def lazy_init_subgraph_op(self):
if self._subgraph_op is None:
with self._sampler_lock:
with self._sampler_lock:
if self._subgraph_op is None:
if self.device.type == 'cuda':
self._subgraph_op = pywrap.CUDASubGraphOp(self.graph.graph_handler)
Expand Down
Loading