Skip to content

Commit 02839a9

Browse files
add Apply operation
1 parent 23f45de commit 02839a9

File tree

2 files changed

+162
-1
lines changed

2 files changed

+162
-1
lines changed

src/Operations/Apply.jl

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
"""
2+
GrB_apply(C, Mask, accum, op, A, desc)
3+
4+
Generic matrix/vector apply.
5+
"""
6+
function GrB_apply( # w<mask> = accum (w, op(u))
7+
C::X, # input/output vector for results
8+
Mask::T, # optional mask for w, unused if NULL
9+
accum::U, # optional accum for z=accum(w,t)
10+
op::GrB_UnaryOp, # operator to apply to the entries
11+
A::X, # first input: vector u
12+
desc::V # descriptor for w and mask
13+
) where {X <: Union{GrB_Vector, GrB_Matrix}, T <: Union{GrB_Vector, GrB_Matrix, GrB_NULL_Type}, U <: valid_accum_types, V <: valid_desc_types}
14+
15+
fn_name = "GrB_" * get_struct_name(C) * "_apply"
16+
17+
return GrB_Info(
18+
ccall(
19+
dlsym(graphblas_lib, fn_name),
20+
Cint,
21+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
22+
C.p, Mask.p, accum.p, op.p, A.p, desc.p
23+
)
24+
)
25+
end
26+
27+
"""
28+
GrB_Vector_apply(w, mask, accum, op, u, desc)
29+
30+
Compute the transformation of the values of the elements of a vector using a unary function.
31+
32+
# Examples
33+
```jldoctest
34+
julia> using SuiteSparseGraphBLAS
35+
36+
julia> GrB_init(GrB_NONBLOCKING)
37+
GrB_SUCCESS::GrB_Info = 0
38+
39+
julia> u = GrB_Vector{Int64}()
40+
GrB_Vector{Int64}
41+
42+
julia> GrB_Vector_new(u, GrB_INT64, 3)
43+
GrB_SUCCESS::GrB_Info = 0
44+
45+
julia> I = [0, 2]; X = [10, 20]; n = 2;
46+
47+
julia> GrB_Vector_build(u, I, X, n, GrB_FIRST_INT64)
48+
GrB_SUCCESS::GrB_Info = 0
49+
50+
julia> w = GrB_Vector{Int64}()
51+
GrB_Vector{Int64}
52+
53+
julia> GrB_Vector_new(w, GrB_INT64, 3)
54+
GrB_SUCCESS::GrB_Info = 0
55+
56+
julia> GrB_Vector_apply(w, GrB_NULL, GrB_NULL, GrB_AINV_INT64, u, GrB_NULL)
57+
GrB_SUCCESS::GrB_Info = 0
58+
59+
julia> @GxB_fprint(w, GxB_COMPLETE)
60+
61+
GraphBLAS vector: w
62+
nrows: 3 ncols: 1 max # entries: 2
63+
format: standard CSC vlen: 3 nvec_nonempty: 1 nvec: 1 plen: 1 vdim: 1
64+
hyper_ratio 0.0625
65+
GraphBLAS type: int64_t size: 8
66+
number of entries: 2
67+
column: 0 : 2 entries [0:1]
68+
row 0: int64 -10
69+
row 2: int64 -20
70+
71+
```
72+
"""
73+
function GrB_Vector_apply( # w<mask> = accum (w, op(u))
74+
w::GrB_Vector, # input/output vector for results
75+
mask::T, # optional mask for w, unused if NULL
76+
accum::U, # optional accum for z=accum(w,t)
77+
op::GrB_UnaryOp, # operator to apply to the entries
78+
u::GrB_Vector, # first input: vector u
79+
desc::V # descriptor for w and mask
80+
) where {T <: valid_vector_mask_types, U <: valid_accum_types, V <: valid_desc_types}
81+
82+
return GrB_Info(
83+
ccall(
84+
dlsym(graphblas_lib, "GrB_Vector_apply"),
85+
Cint,
86+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
87+
w.p, mask.p, accum.p, op.p, u.p, desc.p
88+
)
89+
)
90+
end
91+
92+
"""
93+
GrB_Matrix_apply(C, Mask, accum, op, A, desc)
94+
95+
Compute the transformation of the values of the elements of a matrix using a unary function.
96+
97+
# Examples
98+
```jldoctest
99+
julia> using SuiteSparseGraphBLAS
100+
101+
julia> GrB_init(GrB_NONBLOCKING)
102+
GrB_SUCCESS::GrB_Info = 0
103+
104+
julia> A = GrB_Matrix{Int64}()
105+
GrB_Matrix{Int64}
106+
107+
julia> GrB_Matrix_new(A, GrB_INT64, 2, 2)
108+
GrB_SUCCESS::GrB_Info = 0
109+
110+
julia> I = [0, 0, 1]; J = [0, 1, 1]; X = [10, 20, 30]; n = 3;
111+
112+
julia> GrB_Matrix_build(A, I, J, X, n, GrB_FIRST_INT64)
113+
GrB_SUCCESS::GrB_Info = 0
114+
115+
julia> B = GrB_Matrix{Int64}()
116+
GrB_Matrix{Int64}
117+
118+
julia> GrB_Matrix_new(B, GrB_INT64, 2, 2)
119+
GrB_SUCCESS::GrB_Info = 0
120+
121+
julia> GrB_Matrix_apply(B, GrB_NULL, GrB_NULL, GrB_AINV_INT64, A, GrB_NULL)
122+
GrB_SUCCESS::GrB_Info = 0
123+
124+
julia> @GxB_fprint(B, GxB_COMPLETE)
125+
126+
GraphBLAS matrix: B
127+
nrows: 2 ncols: 2 max # entries: 3
128+
format: standard CSR vlen: 2 nvec_nonempty: 2 nvec: 2 plen: 2 vdim: 2
129+
hyper_ratio 0.0625
130+
GraphBLAS type: int64_t size: 8
131+
number of entries: 3
132+
row: 0 : 2 entries [0:1]
133+
column 0: int64 -10
134+
column 1: int64 -20
135+
row: 1 : 1 entries [2:2]
136+
column 1: int64 -30
137+
138+
```
139+
"""
140+
function GrB_Matrix_apply( # C<Mask> = accum (C, op(A)) or op(A')
141+
C::GrB_Matrix, # input/output matrix for results
142+
Mask::T, # optional mask for C, unused if NULL
143+
accum::U, # optional accum for Z=accum(C,T)
144+
op::GrB_UnaryOp, # operator to apply to the entries
145+
A::GrB_Matrix, # first input: matrix A
146+
desc::V # descriptor for C, mask, and A
147+
) where {T <: valid_matrix_mask_types, U <: valid_accum_types, V <: valid_desc_types}
148+
149+
return GrB_Info(
150+
ccall(
151+
dlsym(graphblas_lib, "GrB_Matrix_apply"),
152+
Cint,
153+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
154+
C.p, Mask.p, accum.p, op.p, A.p, desc.p
155+
)
156+
)
157+
end

src/SuiteSparseGraphBLAS.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ include("Operations/Multiplication.jl")
146146
include("Operations/Element_wise_multiplication.jl")
147147
include("Operations/Element_wise_addition.jl")
148148
include("Operations/Extract.jl")
149+
include("Operations/Apply.jl")
149150

150151
export
151152
# Context Methods
@@ -186,7 +187,10 @@ GrB_eWiseAdd_Matrix_Semiring, GrB_eWiseAdd_Matrix_Monoid, GrB_eWiseAdd_Matrix_Bi
186187
GrB_eWiseAdd,
187188

188189
# Extract
189-
GrB_Vector_extract, GrB_Matrix_extract, GrB_Col_extract
190+
GrB_Vector_extract, GrB_Matrix_extract, GrB_Col_extract,
191+
192+
# Apply
193+
GrB_apply, GrB_Vector_apply, GrB_Matrix_apply
190194

191195
# Export global variables
192196

0 commit comments

Comments
 (0)