|
| 1 | +""" |
| 2 | + GrB_BinaryOp_new(op, fn, ztype, xtype, ytype) |
| 3 | +
|
| 4 | +Initializes a new GraphBLAS binary operator with a specified user-defined function and its types. |
| 5 | +
|
| 6 | +# Examples |
| 7 | +```jldoctest |
| 8 | +julia> using SuiteSparseGraphBLAS |
| 9 | +
|
| 10 | +julia> GrB_init(GrB_NONBLOCKING) |
| 11 | +GrB_SUCCESS::GrB_Info = 0 |
| 12 | +
|
| 13 | +julia> V = GrB_Vector{Float64}() |
| 14 | +GrB_Vector{Float64} |
| 15 | +
|
| 16 | +julia> GrB_Vector_new(V, GrB_FP64, 4) |
| 17 | +GrB_SUCCESS::GrB_Info = 0 |
| 18 | +
|
| 19 | +julia> I = [0, 0, 3, 3]; X = [2.1, 3.2, 4.5, 5.0]; n = 4; # two values at position 0 and 3 |
| 20 | +
|
| 21 | +julia> dup = GrB_BinaryOp() # dup is a binary operator which is applied when duplicate values for the same location are present in the vector |
| 22 | +GrB_BinaryOp |
| 23 | +
|
| 24 | +julia> function ADD(b, c) |
| 25 | + return b+c |
| 26 | + end |
| 27 | +ADD (generic function with 1 method) |
| 28 | +
|
| 29 | +julia> GrB_BinaryOp_new(dup, ADD, GrB_FP64, GrB_FP64, GrB_FP64) |
| 30 | +GrB_SUCCESS::GrB_Info = 0 |
| 31 | +
|
| 32 | +julia> GrB_Vector_build(V, I, X, n, dup) |
| 33 | +GrB_SUCCESS::GrB_Info = 0 |
| 34 | +
|
| 35 | +julia> @GxB_Vector_fprint(V, GxB_SHORT) # the value stored at position 0 and 3 will be the sum of the duplicate values |
| 36 | +
|
| 37 | +GraphBLAS vector: V |
| 38 | +nrows: 4 ncols: 1 max # entries: 2 |
| 39 | +format: standard CSC vlen: 4 nvec_nonempty: 1 nvec: 1 plen: 1 vdim: 1 |
| 40 | +hyper_ratio 0.0625 |
| 41 | +GraphBLAS type: double size: 8 |
| 42 | +number of entries: 2 |
| 43 | +column: 0 : 2 entries [0:1] |
| 44 | + row 0: double 5.3 |
| 45 | + row 3: double 9.5 |
| 46 | +
|
| 47 | +``` |
| 48 | +""" |
| 49 | +function GrB_BinaryOp_new( |
| 50 | + op::GrB_BinaryOp, |
| 51 | + fn::Function, |
| 52 | + ztype::GrB_Type{T}, |
| 53 | + xtype::GrB_Type{U}, |
| 54 | + ytype::GrB_Type{V}) where{T <: valid_types, U <: valid_types, V <: valid_types} |
| 55 | + |
| 56 | + op_ptr = pointer_from_objref(op) |
| 57 | + |
| 58 | + function binaryop_fn(z, x, y) |
| 59 | + unsafe_store!(z, fn(x, y)) |
| 60 | + return nothing |
| 61 | + end |
| 62 | + |
| 63 | + binaryop_fn_C = @cfunction($binaryop_fn, Cvoid, (Ptr{T}, Ref{U}, Ref{V})) |
| 64 | + |
| 65 | + return GrB_Info( |
| 66 | + ccall( |
| 67 | + dlsym(graphblas_lib, "GrB_BinaryOp_new"), |
| 68 | + Cint, |
| 69 | + (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}), |
| 70 | + op_ptr, binaryop_fn_C, ztype.p, xtype.p, ytype.p |
| 71 | + ) |
| 72 | + ) |
| 73 | +end |
| 74 | + |
| 75 | +function GrB_UnaryOp_new( |
| 76 | + op::GrB_UnaryOp, |
| 77 | + fn::Function, |
| 78 | + ztype::GrB_Type{T}, |
| 79 | + xtype::GrB_Type{U}) where{T <: valid_types, U <: valid_types} |
| 80 | + |
| 81 | + op_ptr = pointer_from_objref(op) |
| 82 | + |
| 83 | + function unaryop_fn(z, x) |
| 84 | + unsafe_store!(z, fn(x)) |
| 85 | + return nothing |
| 86 | + end |
| 87 | + |
| 88 | + unaryop_fn_C = @cfunction($unaryop_fn, Cvoid, (Ptr{T}, Ref{U})) |
| 89 | + |
| 90 | + return GrB_Info( |
| 91 | + ccall( |
| 92 | + dlsym(graphblas_lib, "GrB_UnaryOp_new"), |
| 93 | + Cint, |
| 94 | + (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}), |
| 95 | + op_ptr, unaryop_fn_C, ztype.p, xtype.p |
| 96 | + ) |
| 97 | + ) |
| 98 | +end |
0 commit comments