4

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 = &amp;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 = &amp;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 = &amp;Common;
if(cc==NULL) {return 99;}
cholmod_l_start (cc); // works now


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

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

MArgument_setInteger(Res, 0);

return 0;

}

Albercoc
  • 988
  • 3
  • 13

1 Answers1

5

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.

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
  • 1
    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 – Albercoc May 11 '23 at 10:13
  • Ah, I see. Dunno. Good that I works! – Henrik Schumacher May 11 '23 at 11:42
  • 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. – Albercoc May 23 '23 at 07:58
  • For my case I found a work around avoiding LibraryLink. https://mathematica.stackexchange.com/questions/283946/rank-of-singular-large-sparse-matrices – Albercoc May 23 '23 at 07:59