Bug introduced in 9.0 or earlier and fixed in 10.3
Update
As Todd Gayley commented under halirutan's answer, this is a bug.
I'm trying link one of Lapack's function to Mathematica through LibraryLink, and the Library function works great at the first execution but crashes at the second time.
Here is the code (modified from here)
MMASrc="
#include <WolframLibrary.h>
#include <WolframCompileLibrary.h>
DLLEXPORT mint WolframLibrary_getVersion(){
return WolframLibraryVersion;
}
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData){
return 0;
}
EXTERN_C void zspsv_(char* UPLO, const mint* N, mint* NRHS, mcomplex* A, mint* IPIV, mcomplex* B, mint* LDB, mint* INFO);
EXTERN_C DLLEXPORT int lpkSolveAXBS(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res){
MTensor ta=MArgument_getMTensor(Args[0]);
MTensor tb=MArgument_getMTensor(Args[1]);
mint N=*MTensor_getDimensionsMacro(tb);
mcomplex* A=MTensor_getComplexDataMacro(ta);
mcomplex* B=MTensor_getComplexDataMacro(tb);
char U='U';
mint IPIV[N];
mint NRHS=1;
mint LDA=N,LDB=N;
mint INFO;
mint i,j;
mcomplex AP[N*(N+1)/2];
for(j=0;j<N;j++)
for(i=0;i<=j;i++)
AP[i + (j+1)*j/2] = A[i*N+j];
zspsv_(&U,&N,&NRHS,AP,IPIV,B,&LDB,&INFO);
MArgument_setMTensor(Res,tb);
return LIBRARY_NO_ERROR;
}
";
Needs["CCompilerDriver`"];
CreateLibrary[MMASrc,"lpk","Debug"->True,"TargetDirectory"->"/tmp",
"CompileOptions"->"-llapack"];
A={{1. +0. I,0. +0. I,0. -6.94908*^-13 I,0. -6.94908*^-13 I},
{0. +0. I,1. +0.0352595 I,0. -4.51893*^-11 I,0. -4.51893*^-11 I},
{0. -6.94908*^-13 I,0. -4.51893*^-11 I,1. +0.0376938 I,0. +0. I},
{0. -6.94908*^-13 I,0. -4.51893*^-11 I,0. +0. I,1. +0.0378932 I}};
B={1. +0. I,0. +0. I,0. +6.94908*^-13 I,0. +6.94908*^-13 I};
lpkSolve=LibraryFunctionLoad["/tmp/lpk.dylib","lpkSolveAXBS",
{{Complex,2},{Complex,1}},{Complex,1}]
(*LibraryFunction[<>,lpkSolveAXBS,{{Complex,2},{Complex,1}},{Complex,1}]*)
Abs[LinearSolve[A,B]-lpkSolve[A,B]]//Max
(*2.02047*10^-28*)
Table[LinearSolve[A,B],{40000}];//AbsoluteTiming
(*{0.470767,Null}*)
Table[lpkSolve[A,B],{40000}];//AbsoluteTiming
(*{0.086038,Null}*)
Table[lpkSolve[A,B],{40000}];//AbsoluteTiming
In executing In[10], I hear a beep sound and then the kernel crashes and there is no output. Why the kernel crashes at the second time of invoking the LibraryLink function? How to fix that?
lpkSolveinstead ofLinearSolveinIn[10], but I see both cause the kernel to crash. I'll correct it so that it wouldn't cause confusion. – xslittlegrass Jul 25 '13 at 06:49Transpose@Inverse@Transpose[M] === Inverse[M], but will be important in other cases and could easily lead to crashes if not properly observed (especially for non-square matrices). By the way, you may be interested to learn thatLinearAlgebra\LAPACK`GESVandLinearAlgebra`LAPACK`POSVexist. Unfortunately there's noSPSV`. – Oleksandr R. Jul 26 '13 at 17:30LinearAlgebraLAPACKGESVandLinearAlgebraLAPACKPOSVexist, I never knew them before. Could you give some reference to how to use them? I tried to google them but find nothing interesting. Thanks very much. – xslittlegrass Jul 27 '13 at 06:18