-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathvector.py
More file actions
333 lines (301 loc) · 12.5 KB
/
Copy pathvector.py
File metadata and controls
333 lines (301 loc) · 12.5 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from functools import partial
from .base import lib, ffi, NULL, GbContainer, GbDelayed
from .scalar import Scalar
from .ops import BinaryOp, find_opclass, find_return_type
from . import dtypes, binary, monoid, semiring
from .exceptions import check_status, is_error, NoValue
class Vector(GbContainer):
"""
GraphBLAS Sparse Vector
High-level wrapper around GrB_Vector type
"""
def __init__(self, gb_obj, dtype):
super().__init__(gb_obj, dtype)
def __del__(self):
check_status(lib.GrB_Vector_free(self.gb_obj))
def __repr__(self):
return f'<Vector {self.nvals}/{self.size}:{self.dtype.name}>'
def __eq__(self, other):
# Borrowed this recipe from LAGraph
if type(other) is not self.__class__:
return False
if self.dtype != other.dtype:
return False
if self.size != other.size:
return False
if self.nvals != other.nvals:
return False
# Use ewise_mult to compare equality via intersection
matches = Vector.new_from_type(bool, self.size)
matches << self.ewise_mult(other, binary.eq)
if matches.nvals != self.nvals:
return False
# Check if all results are True
result = Scalar.new_from_type(bool)
result << matches.reduce(monoid.land)
return result.value
def __len__(self):
return self.nvals
@property
def size(self):
n = ffi.new('GrB_Index*')
check_status(lib.GrB_Vector_size(n, self.gb_obj[0]))
return n[0]
@property
def shape(self):
return (self.size,)
@property
def nvals(self):
n = ffi.new('GrB_Index*')
check_status(lib.GrB_Vector_nvals(n, self.gb_obj[0]))
return n[0]
def clear(self):
check_status(lib.GrB_Vector_clear(self.gb_obj[0]))
def resize(self, size):
raise NotImplementedError('Not implemented in GraphBLAS 1.2')
check_status(lib.GrB_Vector_resize(self.gb_obj[0], size))
def to_values(self):
"""
GrB_Vector_extractTuples
Extract the indices and values as 2 generators
"""
indices = ffi.new('GrB_Index[]', self.nvals)
values = ffi.new(f'{self.dtype.c_type}[]', self.nvals)
n = ffi.new('GrB_Index*')
n[0] = self.nvals
func = getattr(lib, f'GrB_Vector_extractTuples_{self.dtype.name}')
check_status(func(
indices,
values,
n,
self.gb_obj[0]))
return tuple(indices), tuple(values)
def rebuild_from_values(self, indices, values, *, dup_op=NULL):
# TODO: add `size` option once .resize is available
self.clear()
if not isinstance(indices, (tuple, list)):
indices = tuple(indices)
if not isinstance(values, (tuple, list)):
values = tuple(values)
if len(indices) != len(values):
raise ValueError(f'`indices` and `values` have different lengths '
f'{len(indices)} != {len(values)}')
n = len(indices)
if n <= 0:
return
dup_orig = dup_op
if dup_op is NULL:
dup_op = binary.plus
if isinstance(dup_op, BinaryOp):
dup_op = dup_op[self.dtype]
indices = ffi.new('GrB_Index[]', indices)
values = ffi.new(f'{self.dtype.c_type}[]', values)
# Push values into w
func = getattr(lib, f'GrB_Vector_build_{self.dtype.name}')
check_status(func(
self.gb_obj[0],
indices,
values,
n,
dup_op))
# Check for duplicates when dup_op was not provided
if dup_orig is NULL and self.nvals < len(values):
raise ValueError('Duplicate indices found, must provide `dup_op` BinaryOp')
@classmethod
def new_from_type(cls, dtype, size=0):
"""
GrB_Vector_new
Create a new empty Vector from the given type and size
"""
new_vector = ffi.new('GrB_Vector*')
dtype = dtypes.lookup(dtype)
check_status(lib.GrB_Vector_new(new_vector, dtype.gb_type, size))
return cls(new_vector, dtype)
@classmethod
def new_from_existing(cls, vector):
"""
GrB_Vector_dup
Create a new Vector by duplicating an existing one
"""
new_vec = ffi.new('GrB_Vector*')
check_status(lib.GrB_Vector_dup(new_vec, vector.gb_obj[0]))
return cls(new_vec, vector.dtype)
@classmethod
def new_from_values(cls, indices, values, *, size=None, dup_op=NULL, dtype=None):
"""Create a new Vector from the given lists of indices and values. If
size is not provided, it is computed from the max index found.
"""
if not isinstance(indices, (tuple, list)):
indices = tuple(indices)
if not isinstance(values, (tuple, list)):
values = tuple(values)
if len(values) <= 0:
raise ValueError('No values provided. Unable to determine type.')
if dtype is None:
# Find dtype from any of the values (assumption is they are the same type)
dtype = type(values[0])
dtype = dtypes.lookup(dtype)
# Compute size if not provided
if size is None:
if not indices:
raise ValueError('No indices provided. Unable to infer size.')
size = max(indices) + 1
# Create the new vector
w = cls.new_from_type(dtype, size)
# Add the data
w.rebuild_from_values(indices, values, dup_op=dup_op)
return w
#########################################################
# Delayed methods
#
# These return a GbDelayed object which must be passed
# to update to trigger a call to GraphBLAS
#########################################################
def ewise_add(self, other, op=NULL):
"""
GrB_eWiseAdd_Vector
Result will contain the union of indices from both Vectors
"""
if not isinstance(other, Vector):
raise TypeError(f'Expected Vector, found {type(other)}')
if op is NULL:
op = binary.plus
opclass = find_opclass(op)
if opclass not in ('BinaryOp', 'Monoid', 'Semiring'):
raise TypeError(f'op must be BinaryOp, Monoid, or Semiring')
func = getattr(lib, f'GrB_eWiseAdd_Vector_{opclass}')
output_constructor = partial(Vector.new_from_type,
dtype=find_return_type(op, self.dtype, other.dtype),
size=self.size)
return GbDelayed(func,
[op, self.gb_obj[0], other.gb_obj[0]],
output_constructor=output_constructor)
def ewise_mult(self, other, op=NULL):
"""
GrB_eWiseMult_Vector
Result will contain the intersection of indices from both Vectors
"""
if not isinstance(other, Vector):
raise TypeError(f'Expected Vector, found {type(other)}')
if op is NULL:
op = binary.times
opclass = find_opclass(op)
if opclass not in ('BinaryOp', 'Monoid', 'Semiring'):
raise TypeError(f'op must be BinaryOp, Monoid, or Semiring')
func = getattr(lib, f'GrB_eWiseMult_Vector_{opclass}')
output_constructor = partial(Vector.new_from_type,
dtype=find_return_type(op, self.dtype, other.dtype),
size=self.size)
return GbDelayed(func,
[op, self.gb_obj[0], other.gb_obj[0]],
output_constructor=output_constructor)
def vxm(self, other, op=NULL):
"""
GrB_vxm
Vector-Matrix multiplication. Result is a Vector.
"""
from .matrix import Matrix
if not isinstance(other, Matrix):
raise TypeError(f'Expected Matrix, found {type(other)}')
if op is NULL:
op = semiring.plus_times
opclass = find_opclass(op)
if opclass != 'Semiring':
raise TypeError(f'op must be Semiring')
output_constructor = partial(Vector.new_from_type,
dtype=find_return_type(op, self.dtype, other.dtype),
size=other.ncols)
return GbDelayed(lib.GrB_vxm,
[op, self.gb_obj[0], other.gb_obj[0]],
bt=other.is_transposed,
output_constructor=output_constructor)
def apply(self, op, left=None, right=None):
"""
GrB_Vector_apply
Apply UnaryOp to each element of the calling Vector
A BinaryOp can also be applied if a scalar is passed in as `left` or `right`,
effectively converting a BinaryOp into a UnaryOp
"""
opclass = find_opclass(op)
if opclass == 'UnaryOp':
if left is not None or right is not None:
raise TypeError('Cannot provide `left` or `right` for a UnaryOp')
elif opclass == 'BinaryOp':
if left is None and right is None:
raise TypeError('Must provide either `left` or `right` for a BinaryOp')
elif left is not None and right is not None:
raise TypeError('Cannot provide both `left` and `right`')
else:
raise TypeError('apply only accepts UnaryOp or BinaryOp')
output_constructor = partial(Vector.new_from_type,
dtype=find_return_type(op, self.dtype),
size=self.size)
if opclass == 'UnaryOp':
return GbDelayed(lib.GrB_Vector_apply,
[op, self.gb_obj[0]],
output_constructor=output_constructor)
else:
raise NotImplementedError('apply with BinaryOp not available in GraphBLAS 1.2')
# TODO: fill this in once function is available
def reduce(self, op=NULL):
"""
GrB_Vector_reduce
Reduce all values into a scalar
"""
if op is NULL:
if self.dtype == bool:
op = monoid.lor
else:
op = monoid.plus
func = getattr(lib, f'GrB_Vector_reduce_{self.dtype.name}')
output_constructor = partial(Scalar.new_from_type,
dtype=find_return_type(op, self.dtype))
return GbDelayed(func,
[op, self.gb_obj[0]],
output_constructor=output_constructor)
##################################
# Extract and Assign index methods
##################################
def _extract_element(self, resolved_indexes):
index, _ = resolved_indexes.indices[0]
func = getattr(lib, f'GrB_Vector_extractElement_{self.dtype}')
result = ffi.new(f'{self.dtype.c_type}*')
err_code = func(result,
self.gb_obj[0],
index)
# Don't raise error for no value, simply return `None`
if is_error(err_code, NoValue):
return None, self.dtype
check_status(err_code)
return result[0], self.dtype
def _prep_for_extract(self, resolved_indexes):
index, isize = resolved_indexes.indices[0]
output_constructor = partial(Vector.new_from_type,
dtype=self.dtype,
size=isize)
return GbDelayed(lib.GrB_Vector_extract,
[self.gb_obj[0], index, isize],
output_constructor=output_constructor)
def _assign_element(self, resolved_indexes, value):
index, _ = resolved_indexes.indices[0]
func = getattr(lib, f'GrB_Vector_setElement_{self.dtype}')
check_status(func(
self.gb_obj[0],
ffi.cast(self.dtype.c_type, value),
index))
def _prep_for_assign(self, resolved_indexes, obj):
index, isize = resolved_indexes.indices[0]
if isinstance(obj, Scalar):
obj = obj.value
if isinstance(obj, (int, float, bool)):
dtype = self.dtype
func = getattr(lib, f'GrB_Vector_assign_{dtype.name}')
scalar = ffi.cast(dtype.c_type, obj)
delayed = GbDelayed(func,
[scalar, index, isize])
elif isinstance(obj, Vector):
delayed = GbDelayed(lib.GrB_Vector_assign,
[obj.gb_obj[0], index, isize])
else:
raise TypeError(f'Unexpected type for assignment value: {type(obj)}')
return delayed