-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_caching.py
185 lines (170 loc) · 8.23 KB
/
_caching.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from graphblas import Scalar, dtypes, op
from graphblas.core import operator
NONNEGATIVE_DTYPES = {dtypes.BOOL, dtypes.UINT8, dtypes.UINT16, dtypes.UINT32, dtypes.UINT64}
def get_reduce_to_vector(key, opname, methodname):
op_ = op.from_string(opname)
op_, opclass = operator.find_opclass(op_)
keybase = key[:-1]
if key[-1] == "-":
def get_reduction(G, mask=None):
cache = G._cache
if mask is not None:
if key in cache:
return cache[key].dup(mask=mask)
if cache.get("has_self_edges") is False and f"{keybase}+" in cache:
cache[key] = cache[f"{keybase}+"]
return cache[key].dup(mask=mask)
if "offdiag" in cache:
return getattr(cache["offdiag"], methodname)(op_).new(mask=mask, name=key)
if (
"L-" in cache
and "U-" in cache
and opclass in {"BinaryOp", "Monoid"}
and G.get_property("has_self_edges")
):
return op_(
getattr(cache["L-"], methodname)(op_).new(mask=mask)
| getattr(cache["U-"], methodname)(op_).new(mask=mask)
).new(name=key)
if not G.get_property("has_self_edges"):
return G.get_property(f"{keybase}+", mask=mask)
return getattr(G.get_property("offdiag"), methodname)(op_).new(mask=mask, name=key)
if key not in cache:
if cache.get("has_self_edges") is False and f"{keybase}+" in cache:
cache[key] = cache[f"{keybase}+"]
elif "offdiag" in cache:
cache[key] = getattr(cache["offdiag"], methodname)(op_).new(name=key)
elif (
"L-" in cache
and "U-" in cache
and opclass in {"BinaryOp", "Monoid"}
and G.get_property("has_self_edges")
):
cache[key] = op_(
getattr(cache["L-"], methodname)(op_)
| getattr(cache["U-"], methodname)(op_)
).new(name=key)
elif not G.get_property("has_self_edges"):
cache[key] = G.get_property(f"{keybase}+")
else:
cache[key] = getattr(G.get_property("offdiag"), methodname)(op_).new(name=key)
if (
"has_self_edges" not in cache
and f"{keybase}+" in cache
and cache[key].nvals != cache[f"{keybase}+"].nvals
):
cache["has_self_edges"] = True
elif cache.get("has_self_edges") is False:
cache[f"{keybase}+"] = cache[key]
return cache[key]
else:
def get_reduction(G, mask=None):
A = G._A
cache = G._cache
if mask is not None:
if key in cache:
return cache[key].dup(mask=mask)
if cache.get("has_self_edges") is False and f"{keybase}-" in cache:
cache[key] = cache[f"{keybase}-"]
return cache[key].dup(mask=mask)
if methodname == "reduce_columnwise" and "AT" in cache:
return cache["AT"].reduce_rowwise(op_).new(mask=mask, name=key)
return getattr(A, methodname)(op_).new(mask=mask, name=key)
if key not in cache:
if cache.get("has_self_edges") is False and f"{keybase}-" in cache:
cache[key] = cache[f"{keybase}-"]
elif methodname == "reduce_columnwise" and "AT" in cache:
cache[key] = cache["AT"].reduce_rowwise(op_).new(name=key)
else:
cache[key] = getattr(A, methodname)(op_).new(name=key)
if (
"has_self_edges" not in cache
and f"{keybase}-" in cache
and cache[key].nvals != cache[f"{keybase}-"].nvals
):
cache["has_self_edges"] = True
elif cache.get("has_self_edges") is False:
cache[f"{keybase}-"] = cache[key]
return cache[key]
return get_reduction
def get_reduce_to_scalar(key, opname):
op_ = op.from_string(opname)
op_, opclass = operator.find_opclass(op_)
keybase = key[:-1]
if key[-1] == "-":
def get_reduction(G, mask=None):
cache = G._cache
if key not in cache:
if cache.get("has_self_edges") is False and f"{keybase}+" in cache:
cache[key] = cache[f"{keybase}+"]
elif f"{opname}_rowwise-" in cache:
cache[key] = cache[f"{opname}_rowwise-"].reduce(op_).new(name=key)
elif f"{opname}_columnwise-" in cache:
cache[key] = cache[f"{opname}_columnwise-"].reduce(op_).new(name=key)
elif cache.get("has_self_edges") is False and f"{opname}_rowwise+" in cache:
cache[key] = cache[f"{opname}_rowwise+"].reduce(op_).new(name=key)
elif cache.get("has_self_edges") is False and f"{opname}_columnwise+" in cache:
cache[key] = cache[f"{opname}_columnwise+"].reduce(op_).new(name=key)
elif "offdiag" in cache:
cache[key] = cache["offdiag"].reduce_scalar(op_).new(name=key)
elif (
"L-" in cache
and "U-" in cache
and opclass in {"BinaryOp", "Monoid"}
and G.get_property("has_self_edges")
):
return op_(
cache["L-"].reduce(op_)._as_vector() | cache["U-"].reduce(op_)._as_vector()
)[0].new(name=key)
elif not G.get_property("has_self_edges"):
cache[key] = G.get_property(f"{keybase}+")
else:
cache[key] = G.get_property("offdiag").reduce_scalar(op_).new(name=key)
if (
"has_self_edges" not in cache
and f"{keybase}+" in cache
and cache[key] != cache[f"{keybase}+"]
):
cache["has_self_edges"] = True
elif cache.get("has_self_edges") is False:
cache[f"{keybase}+"] = cache[key]
return cache[key]
elif key[-1] == "+":
def get_reduction(G, mask=None):
A = G._A
cache = G._cache
if key not in cache:
if cache.get("has_self_edges") is False and f"{keybase}-" in cache:
cache[key] = cache[f"{keybase}-"]
elif f"{opname}_rowwise+" in cache:
cache[key] = cache[f"{opname}_rowwise+"].reduce(op_).new(name=key)
elif f"{opname}_columnwise+" in cache:
cache[key] = cache[f"{opname}_columnwise+"].reduce(op_).new(name=key)
elif cache.get("has_self_edges") is False and f"{opname}_rowwise-" in cache:
cache[key] = cache[f"{opname}_rowwise-"].reduce(op_).new(name=key)
elif cache.get("has_self_edges") is False and f"{opname}_columnwise-" in cache:
cache[key] = cache[f"{opname}_columnwise-"].reduce(op_).new(name=key)
else:
cache[key] = A.reduce_scalar(op_).new(name=key)
if (
"has_self_edges" not in cache
and f"{keybase}-" in cache
and cache[key] != cache[f"{keybase}-"]
):
cache["has_self_edges"] = True
elif cache.get("has_self_edges") is False:
cache[f"{keybase}-"] = cache[key]
return cache[key]
elif key.endswith("_diagonal"):
def get_reduction(G, mask=None):
A = G._A
cache = G._cache
if key not in cache:
if not G.get_property("has_self_edges"):
cache[key] = Scalar(op_[A.dtype].return_type, name=key)
else:
cache[key] = G.get_property("diag").reduce(op_).new(name=key)
return cache[key]
else: # pragma: no cover (sanity)
raise RuntimeError
return get_reduction