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;
}