-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
39 lines (30 loc) · 902 Bytes
/
utils.py
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
import torch
import torch.multiprocessing as mp
class TrafficLight:
"""used by chief to allow workers to run or not"""
def __init__(self, val=True):
self.val = mp.Value("b", False)
self.lock = mp.Lock()
def get(self):
with self.lock:
return self.val.value
def switch(self):
with self.lock:
self.val.value = (not self.val.value)
class Counter:
"""enable the chief to access worker's total number of updates"""
def __init__(self, val=True):
self.val = mp.Value("i", 0)
self.lock = mp.Lock()
def get(self):
# used by chief
with self.lock:
return self.val.value
def increment(self):
# used by workers
with self.lock:
self.val.value += 1
def reset(self):
# used by chief
with self.lock:
self.val.value = 0