Closed fcdimitr closed 3 years ago
Hi @fcdimitr you need to wrap the values in a struct here you go -
using SparseArrays
struct foo
x::Float64
end
function GxB_Matrix_import_CSC(Aj)
A = GrB_Matrix{Float64}()
A_ptr = pointer_from_objref(A)
type = GrB_FP64
nr = convert(UInt64, size(Aj, 1))
nc = convert(UInt64, size(Aj, 2))
nv = convert(UInt64, SparseArrays.nnz(Aj))
ne = convert(Int64, -1)
Ap = Ref(pointer(ZeroBasedIndices(Aj.colptr.-1)))
Ai = Ref(pointer(ZeroBasedIndices(Aj.rowval.-1 )))
X = Ref(pointer(foo.(Aj.nzval)))
desc = GrB_NULL
g = GrB_Info(
ccall(
dlsym(graphblas_lib, "GxB_Matrix_import_CSC"),
Cint,
# ----- INPUT SPECIFICATION
(Ptr{Cvoid}, # GrB_Matrix *A
Ptr{Cvoid}, # GrB_Type type
UInt64, # GrB_Index nrows
UInt64, # GrB_Index ncols
UInt64, # GrB_Index nvals
Int64, # int64_t nonempty
Ptr{Cvoid}, # GrB_Index **Ap
Ptr{Cvoid}, # GrB_Index **Ai
Ptr{Cvoid}, # void **Ax
Ptr{Cvoid}), # (descriptor, unused)
# ----- INPUT PARAMS
A_ptr, # GrB_Matrix *A
type.p, # GrB_Type type
nr, # GrB_Index nrows
nc, # GrB_Index ncols
nv, # GrB_Index nvals
ne, # int64_t nonempty
Ap, # GrB_Index **Ap
Ai, # GrB_Index **Ai
X, # void **Ax
desc.p # (descriptor, unused)
)
)
g != GrB_SUCCESS && return g
return A
end
Since the C function expects a pointer to a pointer, this will work.
julia> using GraphBLASInterface, SuiteSparseGraphBLAS
julia> GrB_init(GrB_NONBLOCKING)
GrB_SUCCESS::GrB_Info = 0
julia> using SparseArrays
julia> Aj = sprand(Int64,10,10,0.1);
julia> f = GxB_Matrix_import_CSC(Aj);
julia> @GxB_fprint(f, GxB_COMPLETE)
10x10 GraphBLAS double matrix, sparse by col:
f, 12 entries
(6,0) -6.51981090010754e+18
(2,1) 2.38012960675141e+18
(2,2) 7.15625156581955e+18
(6,2) -8.4509572234965e+17
(0,5) -5.14419131400837e+18
(7,6) -8.76743228265509e+18
(6,7) -4.87354216758125e+18
(8,7) -5.25682341711608e+18
(5,8) -8.96788900640334e+18
(9,8) 6.97693687580616e+18
(4,9) -1.12098026882762e+18
(8,9) -9.16149892069805e+18
julia> Aj
10×10 SparseMatrixCSC{Int64,Int64} with 12 stored entries:
[7 , 1] = -6519810900107535143
[3 , 2] = 2380129606751411208
[3 , 3] = 7156251565819545931
[7 , 3] = -845095722349649729
[1 , 6] = -5144191314008365360
[8 , 7] = -8767432282655090949
[7 , 8] = -4873542167581250904
[9 , 8] = -5256823417116077675
[6 , 9] = -8967889006403336730
[10, 9] = 6976936875806161528
[5 , 10] = -1120980268827621848
[9 , 10] = -9161498920698047516
If you want, you can check out this fork https://github.com/cvdlab/SuiteSparseGraphBLAS.jl, they've built an interface on top of this library.
Thank you very much @abhinavmehndiratta, both for the solution, and for the link to the updated fork. I am closing the issue.
I am trying to extend your wrapper to provide additional functionality, specifically, to be able to pass CSC matrices seamlessly between Julia and C (without going through the
extractTuples
andbuild
functions).The
GxB_Matrix_import_CSC
(andexport
respectively) allows us to work with pointers to the CSC vectors. See https://github.com/DrTimothyAldenDavis/GraphBLAS/blob/stable/Doc/GraphBLAS_UserGuide.pdf page 98, for example.My initial try is the following:
I am working with Double pointers in this example. When running the following code
I get a segmentation violation:
Any ideas what I am doing wrong?