4
$\begingroup$

I am trying to use third-party libraries in LibraryLink, but the kernel crashes when I call their functions.

In particular, I want to make available some functions of SuiteSparse to manipulate SparseArrays in MMA. The library gets built, and the function loaded, but calling it crashes the kernel.

Here a minimal example:

Needs["CCompilerDriver`"]

CreateLibrary[
  {NotebookDirectory[] <> "/libTest.c"}, "libTest",
  "TargetDirectory" -> NotebookDirectory[],
  "ShellCommandFunction" -> Function[Print[Style[#, Blue]]],
  "ShellOutputFunction" -> Function[Print[Style[#, Red]]],
  "CompileOptions" -> "-lcholmod -lspqr -lsuitesparseconfig"
  ]

fun = LibraryFunctionLoad[lib, "function", {}, Integer];
fun[] (* <-CRASHES KERNEL*)

And the library code (libTest.c)

#include "WolframLibrary.h"
#include "WolframSparseLibrary.h"
#include <stdint.h>
#include <stdio.h>

#include "SuiteSparseQR_C.h"
#include "SuiteSparse_config.h"


DLLEXPORT mint WolframLibrary_getVersion( ) {
    return WolframLibraryVersion;
}

DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData) {
    return LIBRARY_NO_ERROR;
}

DLLEXPORT void WolframLibrary_uninitialize(WolframLibraryData libData) {
    return;
}


DLLEXPORT int function(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res)
{
    cholmod_common Common, *cc;

    // start CHOLMOD
    cc = &Common;
    cholmod_l_start (cc); // CRASHES KERNEL

    MArgument_setInteger(Res, 0);

    return 0;
}

I am out of ideas. The kernel just crashes without any output. Any comment would be extremely useful just to have something to look at.

Important to say that those lines run if compiled and executed outside LibraryLink

standalone.c:

#include "SuiteSparseQR_C.h"
#include "SuiteSparse_config.h"
int main()
{
    cholmod_common Common, *cc;

    // start CHOLMOD
    cc = &Common;
    cholmod_l_start (cc); // CRASHES KERNEL

    return 0;
}

gcc standalone.c -lcholmod -lspqr -lsuitesparseconfig -o standalone

./standalone

EDIT

It run through by explicitly adding cholmod.so to the first argument of CreateLibrary (see comment). But trying to run the QR factorization I get the same crash. Hopefully I only need to add other libraries, but I had no success so far. This is the new code that crashes:

DLLEXPORT int function(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res)
{
    cholmod_common Common, *cc;
    cholmod_sparse *A;

    // start CHOLMOD
    cc = &Common;
    if(cc==NULL) {return 99;}
    cholmod_l_start (cc); // works now


    // load A
    int mtype ;
    FILE* fp; fp = fopen("/home/albercoc/Programs/SuiteSparse-7.0.1/SPQR/Matrix/b1_ss.mtx", "r");     
    A = (cholmod_sparse *) cholmod_l_read_matrix (fp, 1, &mtype, cc) ;

    // Factorization
    SuiteSparseQR_C_factorize (SPQR_ORDERING_DEFAULT, SPQR_DEFAULT_TOL, A, cc) ; // CRASHES

    MArgument_setInteger(Res, 0);

    return 0;
}
$\endgroup$

1 Answer 1

5
$\begingroup$

The fact that it compiles cleanly only tells you that the header files were detected correctly during compilation. But dynamic linking at runtime is a completely different beast. Maybe some library is not found at runtime? I frequently had problems with dynamic linking under macos. In the end I adopted the habit of always adding

"LibraryDirectories" -> {<<paths to all libraries that you want to link>>}

to the call to CreateLibrary. Please try that. Maybe it helps.

$\endgroup$
4
  • 1
    $\begingroup$ Thanks! It worked but only adding the library in the first argument as CreateLibrary[{NotebookDirectory[] <> "/libTest.c", "/usr/local/lib/libcholmod.so"} , .... Is this behavior expected? Departing from the minimal example and trying to use SuiteSparseQR_C_factorize I get the same crash. I have tried to add all libraries I can imagine: libspqr.so,libsuitesparseconfig.so, libumfpack.so, libgraphblas.so, libcolamd.so, libcxsparse.so $\endgroup$
    – Albercoc
    Commented May 11, 2023 at 10:13
  • $\begingroup$ Ah, I see. Dunno. Good that I works! $\endgroup$ Commented May 11, 2023 at 11:42
  • $\begingroup$ I am confident that the problem is related to libraries. This was a good hint. It didn't fully solve my specific case but it can be useful to others. $\endgroup$
    – Albercoc
    Commented May 23, 2023 at 7:58
  • $\begingroup$ For my case I found a work around avoiding LibraryLink. mathematica.stackexchange.com/questions/283946/… $\endgroup$
    – Albercoc
    Commented May 23, 2023 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.