Skip to content

Commit 7a86353

Browse files
add extract operations
1 parent 7db99bb commit 7a86353

File tree

2 files changed

+257
-1
lines changed

2 files changed

+257
-1
lines changed

src/Operations/Extract.jl

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
"""
2+
GrB_Vector_extract(w, mask, accum, u, I, ni, desc)
3+
4+
Extract a sub-vector from a larger vector as specified by a set of indices.
5+
The result is a vector whose size is equal to the number of indices.
6+
7+
# Examples
8+
```jldoctest
9+
julia> using SuiteSparseGraphBLAS
10+
11+
julia> GrB_init(GrB_NONBLOCKING)
12+
GrB_SUCCESS::GrB_Info = 0
13+
14+
julia> V = GrB_Vector{Int64}()
15+
GrB_Vector{Int64}
16+
17+
julia> GrB_Vector_new(V, GrB_INT64, 5)
18+
GrB_SUCCESS::GrB_Info = 0
19+
20+
julia> I = [1, 2, 4]; X = [15, 32, 84]; n = 3;
21+
22+
julia> GrB_Vector_build(V, I, X, n, GrB_FIRST_INT64)
23+
GrB_SUCCESS::GrB_Info = 0
24+
25+
julia> @GxB_Vector_fprint(V, GxB_COMPLETE)
26+
27+
GraphBLAS vector: V
28+
nrows: 5 ncols: 1 max # entries: 3
29+
format: standard CSC vlen: 5 nvec_nonempty: 1 nvec: 1 plen: 1 vdim: 1
30+
hyper_ratio 0.0625
31+
GraphBLAS type: int64_t size: 8
32+
number of entries: 3
33+
column: 0 : 3 entries [0:2]
34+
row 1: int64 15
35+
row 2: int64 32
36+
row 4: int64 84
37+
38+
39+
julia> W = GrB_Vector{Int64}()
40+
GrB_Vector{Int64}
41+
42+
julia> GrB_Vector_new(W, GrB_INT64, 2)
43+
GrB_SUCCESS::GrB_Info = 0
44+
45+
julia> GrB_Vector_extract(W, GrB_NULL, GrB_NULL, V, [1, 4], 2, GrB_NULL)
46+
GrB_SUCCESS::GrB_Info = 0
47+
48+
julia> GrB_Vector_extractTuples(W)[2]
49+
2-element Array{Int64,1}:
50+
15
51+
84
52+
```
53+
"""
54+
function GrB_Vector_extract( # w<mask> = accum (w, u(I))
55+
w::GrB_Vector, # input/output vector for results
56+
mask, # optional mask for w, unused if NULL
57+
accum, # optional accum for z=accum(w,t)
58+
u::GrB_Vector, # first input: vector u
59+
I::Vector{T}, # row indices
60+
ni::T, # number of row indices
61+
desc # descriptor for w and mask
62+
) where T <: GrB_Index
63+
64+
mask_ptr = mask == GrB_NULL ? C_NULL : mask.p
65+
accum_ptr = accum == GrB_NULL ? C_NULL : accum.p
66+
desc_ptr = desc == GrB_NULL ? C_NULL : desc.p
67+
68+
return GrB_Info(
69+
ccall(
70+
dlsym(graphblas_lib, "GrB_Vector_extract"),
71+
Cint,
72+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cintmax_t}, Cintmax_t, Ptr{Cvoid}),
73+
w.p, mask_ptr, accum_ptr, u.p, pointer(I), ni, desc_ptr
74+
)
75+
)
76+
end
77+
78+
"""
79+
GrB_Matrix_extract(C, Mask, accum, A, I, ni, J, nj, desc)
80+
81+
Extract a sub-matrix from a larger matrix as specified by a set of row indices and a set of column indices.
82+
The result is a matrix whose size is equal to size of the sets of indices.
83+
84+
# Examples
85+
```jldoctest
86+
julia> using SuiteSparseGraphBLAS
87+
88+
julia> GrB_init(GrB_NONBLOCKING)
89+
GrB_SUCCESS::GrB_Info = 0
90+
91+
julia> MAT = GrB_Matrix{Int8}()
92+
GrB_Matrix{Int8}
93+
94+
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
95+
GrB_SUCCESS::GrB_Info = 0
96+
97+
julia> I = [1, 2, 2, 2, 3]; J = [1, 2, 1, 3, 3]; X = Int8[2, 3, 4, 5, 6]; n = 5;
98+
99+
100+
julia> GrB_Matrix_build(MAT, I, J, X, n, GrB_FIRST_INT8)
101+
GrB_SUCCESS::GrB_Info = 0
102+
103+
julia> @GxB_Matrix_fprint(MAT, GxB_COMPLETE)
104+
105+
GraphBLAS matrix: MAT
106+
nrows: 4 ncols: 4 max # entries: 5
107+
format: standard CSR vlen: 4 nvec_nonempty: 3 nvec: 4 plen: 4 vdim: 4
108+
hyper_ratio 0.0625
109+
GraphBLAS type: int8_t size: 1
110+
number of entries: 5
111+
row: 1 : 1 entries [0:0]
112+
column 1: int8 2
113+
row: 2 : 3 entries [1:3]
114+
column 1: int8 4
115+
column 2: int8 3
116+
column 3: int8 5
117+
row: 3 : 1 entries [4:4]
118+
column 3: int8 6
119+
120+
121+
julia> OUT = GrB_Matrix{Int8}()
122+
GrB_Matrix{Int8}
123+
124+
julia> GrB_Matrix_new(OUT, GrB_INT8, 2, 2)
125+
GrB_SUCCESS::GrB_Info = 0
126+
127+
julia> GrB_Matrix_extract(OUT, GrB_NULL, GrB_NULL, MAT, [1, 3], 2, [1, 3], 2, GrB_NULL)
128+
GrB_SUCCESS::GrB_Info = 0
129+
130+
julia> GrB_Matrix_extractTuples(OUT)[3]
131+
2-element Array{Int8,1}:
132+
2
133+
6
134+
```
135+
"""
136+
function GrB_Matrix_extract( # C<Mask> = accum (C, A(I,J))
137+
C::GrB_Matrix, # input/output matrix for results
138+
Mask, # optional mask for C, unused if NULL
139+
accum, # optional accum for Z=accum(C,T)
140+
A::GrB_Matrix, # first input: matrix A
141+
I::Vector{T}, # row indices
142+
ni::T, # number of row indices
143+
J::Vector{T}, # column indices
144+
nj::T, # number of column indices
145+
desc # descriptor for C, Mask, and A
146+
) where T <: GrB_Index
147+
148+
Mask_ptr = Mask == GrB_NULL ? C_NULL : Mask.p
149+
accum_ptr = accum == GrB_NULL ? C_NULL : accum.p
150+
desc_ptr = desc == GrB_NULL ? C_NULL : desc.p
151+
152+
return GrB_Info(
153+
ccall(
154+
dlsym(graphblas_lib, "GrB_Matrix_extract"),
155+
Cint,
156+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cintmax_t}, Cintmax_t, Ptr{Cintmax_t}, Cintmax_t, Ptr{Cvoid}),
157+
C.p, Mask_ptr, accum_ptr, A.p, pointer(I), ni, pointer(J), nj, desc_ptr
158+
)
159+
)
160+
end
161+
162+
"""
163+
GrB_Col_extract(w, mask, accum, A, I, ni, j, desc)
164+
165+
Extract from one column of a matrix into a vector. With the transpose descriptor for the source matrix,
166+
elements of an arbitrary row of the matrix can be extracted with this function as well.
167+
168+
# Examples
169+
```jldoctest
170+
julia> using SuiteSparseGraphBLAS
171+
172+
julia> GrB_init(GrB_NONBLOCKING)
173+
GrB_SUCCESS::GrB_Info = 0
174+
175+
julia> MAT = GrB_Matrix{Int8}()
176+
GrB_Matrix{Int8}
177+
178+
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
179+
GrB_SUCCESS::GrB_Info = 0
180+
181+
julia> I = [1, 2, 2, 2, 3]; J = [1, 2, 1, 3, 3]; X = Int8[23, 34, 43, 57, 61]; n = 5;
182+
183+
julia> GrB_Matrix_build(MAT, I, J, X, n, GrB_FIRST_INT8)
184+
GrB_SUCCESS::GrB_Info = 0
185+
186+
julia> @GxB_Matrix_fprint(MAT, GxB_COMPLETE)
187+
188+
GraphBLAS matrix: MAT
189+
nrows: 4 ncols: 4 max # entries: 5
190+
format: standard CSR vlen: 4 nvec_nonempty: 3 nvec: 4 plen: 4 vdim: 4
191+
hyper_ratio 0.0625
192+
GraphBLAS type: int8_t size: 1
193+
number of entries: 5
194+
row: 1 : 1 entries [0:0]
195+
column 1: int8 23
196+
row: 2 : 3 entries [1:3]
197+
column 1: int8 43
198+
column 2: int8 34
199+
column 3: int8 57
200+
row: 3 : 1 entries [4:4]
201+
column 3: int8 61
202+
203+
204+
julia> desc = GrB_Descriptor()
205+
GrB_Descriptor
206+
207+
julia> GrB_Descriptor_new(desc)
208+
GrB_SUCCESS::GrB_Info = 0
209+
210+
julia> GrB_Descriptor_set(desc, GrB_INP0, GrB_TRAN) # descriptor to transpose first input
211+
GrB_SUCCESS::GrB_Info = 0
212+
213+
julia> out = GrB_Vector{Int8}()
214+
GrB_Vector{Int8}
215+
216+
julia> GrB_Vector_new(out, GrB_INT8, 3)
217+
GrB_SUCCESS::GrB_Info = 0
218+
219+
julia> GrB_Col_extract(out, GrB_NULL, GrB_NULL, MAT, [1, 2, 3], 3, 2, desc) # extract elements of row 2
220+
GrB_SUCCESS::GrB_Info = 0
221+
222+
julia> GrB_Vector_extractTuples(out)[2]
223+
3-element Array{Int8,1}:
224+
43
225+
34
226+
57
227+
```
228+
"""
229+
function GrB_Col_extract( # w<mask> = accum (w, A(I,j))
230+
w::GrB_Vector, # input/output matrix for results
231+
mask, # optional mask for w, unused if NULL
232+
accum, # optional accum for z=accum(w,t)
233+
A::GrB_Matrix, # first input: matrix A
234+
I::Vector{T}, # row indices
235+
ni::T, # number of row indices
236+
j::T, # column index
237+
desc # descriptor for w, mask, and A
238+
) where T <: GrB_Index
239+
240+
mask_ptr = mask == GrB_NULL ? C_NULL : mask.p
241+
accum_ptr = accum == GrB_NULL ? C_NULL : accum.p
242+
desc_ptr = desc == GrB_NULL ? C_NULL : desc.p
243+
244+
return GrB_Info(
245+
ccall(
246+
dlsym(graphblas_lib, "GrB_Col_extract"),
247+
Cint,
248+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cintmax_t}, Cintmax_t, Cintmax_t, Ptr{Cvoid}),
249+
w.p, mask_ptr, accum_ptr, A.p, pointer(I), ni, j, desc_ptr
250+
)
251+
)
252+
end

src/SuiteSparseGraphBLAS.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ include("Object_Methods/Descriptor_Methods.jl")
140140
include("Object_Methods/Print_Objects.jl")
141141
include("Operations/Multiplication.jl")
142142
include("Operations/Element_wise_multiplication.jl")
143+
include("Operations/Extract.jl")
143144

144145
export
145146
# Context Methods
@@ -172,7 +173,10 @@ GrB_mxm, GrB_vxm, GrB_mxv,
172173
# Element wise multiplication
173174
GrB_eWiseMult_Vector_Semiring, GrB_eWiseMult_Vector_Monoid, GrB_eWiseMult_Vector_BinaryOp,
174175
GrB_eWiseMult_Matrix_Semiring, GrB_eWiseMult_Matrix_Monoid, GrB_eWiseMult_Matrix_BinaryOp,
175-
GrB_eWiseMult
176+
GrB_eWiseMult,
177+
178+
# Extract
179+
GrB_Vector_extract, GrB_Matrix_extract, GrB_Col_extract
176180

177181
# Export global variables
178182

0 commit comments

Comments
 (0)