chenxm1986 / cktso

Pursuing the best performance of linear solver in circuit simulation
20 stars 3 forks source link

always return -8 from factorization #7

Closed mzy2240 closed 1 year ago

mzy2240 commented 1 year ago

I'm trying to directly use the dll from Julia, however the factorization function always return -8, while others seem work fine (return 0). Here is my script:

using CEnum
using SparseArrays

const _libcktso = joinpath(dirname(@__FILE__),"..","cktso","win10_x64","cktso_l.dll")

mutable struct __cktso_l_dummy end

const ICktSo_L = Ptr{__cktso_l_dummy}

function CKTSO_L_CreateSolver(inst, iparm, oparm)
    ccall((:CKTSO_L_CreateSolver, _libcktso), Cint, (Ptr{ICktSo_L}, Ptr{Ptr{Cint}}, Ptr{Ptr{Clonglong}}), inst, iparm, oparm)
end

function CKTSO_L_DestroySolver(inst)
    ccall((:CKTSO_L_DestroySolver, _libcktso), Cint, (ICktSo_L,), inst)
end

function CKTSO_L_Analyze(inst, is_complex, n, ap, ai, ax, threads)
    ccall((:CKTSO_L_Analyze, _libcktso), Cint, (ICktSo_L, Bool, Clonglong, Ptr{Clonglong}, Ptr{Clonglong}, Ptr{Cdouble}, Cint), inst, is_complex, n, ap, ai, ax, threads)
end

function CKTSO_L_Factorize(inst, ax, fast)
    ccall((:CKTSO_L_Factorize, _libcktso), Cint, (ICktSo_L, Ptr{Cdouble}, Bool), inst, ax, fast)
end

function CKTSO_L_CleanUpGarbage(inst)
    ccall((:CKTSO_L_CleanUpGarbage, _libcktso), Cint, (ICktSo_L,), inst)
end

function CKTSO_L_Determinant(inst, mantissa, exponent)
    ccall((:CKTSO_L_Determinant, _libcktso), Cint, (ICktSo_L, Ptr{Cdouble}, Ptr{Cdouble}), inst, mantissa, exponent)
end

a = Ref{ICktSo_L}(0)
b = Cint[]
c = Clonglong[]
solver = CKTSO_L_CreateSolver(a, b, c)

A = sprand(100, 100, 0.01)
# make sure the diagonal is 1
for i in 1:100
    A[i, i] = 1
end

ap = Clonglong.(A.colptr) .- 1;  # -1 because the indices in Julia are 1-based
ai = Clonglong.(A.rowval) .- 1;  #
ax = Cdouble.(A.nzval);

CKTSO_L_Analyze(a[], Bool(false), Clonglong(0),  pointer_from_objref(ap), pointer_from_objref(ai), pointer_from_objref(ax), Cint(0)) # return 0

res = CKTSO_L_Factorize(a[], pointer_from_objref(ax), Bool(false))  # return -8

display(res)
mzy2240 commented 1 year ago

Create, destroy, and analyze seem work fine.

chenxm1986 commented 1 year ago

It looks that you passed a zero dimension when calling CKTSO_L_Analyze (CKTSO_L_Analyze(a[], Bool(false), Clonglong(0), ...). Zero dimension means an empty matrix and the following functions cannot continue.

mzy2240 commented 1 year ago

you mean the third argument? I thought 0 means it will destroy previous matrix instance and only keep this current one

chenxm1986 commented 1 year ago

The 3rd argument of CKTSO_L_Analyze is the only parameter for the solver to know the matrix dimension. 0 will only free the previous matrix. Nonzero will first free the previous matrix and create the new matrix.

mzy2240 commented 1 year ago

Can we have a function to directly accept input option array as one of the argument, instead of only exposing the pointer? I managed to directly call the module from Julia, all the functions work as expected except retrieving the input and output options. It would be much easier if the input option could be passed into the function as a normal argument. I am wrapping it in Julia so those pointer are quite difficult to operate.

chenxm1986 commented 1 year ago

I do not use Julia so I do not know what the problem is. The input and output options are pointers to pointers, which are similar to the handle. If the handle can work, the input and output options should also work. Could you elaborate your problem?

mzy2240 commented 1 year ago

The biggest headache is, in Julia I have to instantiate an object first to obtain the pointer (and of course the pointer to pointer). According to the header file, it is eventually pointing to a Cint, which means I could only obtain a Cint after dereferencing. Can we just have a method like CreateSolverWithOption so that we could directly put the option array as one of the argument? Like how we pass ax, ai. It will be much easier for many developers who are working on language with limited access to pointers.

chenxm1986 commented 1 year ago

Thank you for providing the information. I will consider this.