This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Matrix product crash in a DLL

Tags: None
(comma "," separated)
mehrdadh
Registered Member
Posts
26
Karma
0

Matrix product crash in a DLL

Sun Jun 30, 2013 3:48 am
Hi,

This seemed too weird to me, so I thought I should ask if anybody knows why ?


I have a code which can be compiled with Visual Studio C++ 2010 as a DLL and it works in Excel VBA.


But if I compile the same exact code using gcc it causes a crash in Excel. This is basically the snippet of the code that causes the crash:


Code: Select all
   MatrixXd delta = MatrixXd::Zero(n-2, n);
   for (int i=0; i<n-2; i++)
   {
      delta(i,i)   = 1.0;
      delta(i,i+1) = -2.0;
      delta(i,i+2) = 1.0;
   }

   MatrixXd delta2 = delta.transpose()*delta;          // THIS LINE CAUSES THE CRASH




This code causes the VBA/Excel to crash. But, if I comment out the last line of this code, then DLL will not crash when called from VBA, and it works nicely. So the crash has something to do with matrix multiplication. I don't know why it works with Visual Studio 2010, but not with g++ 4.6.2 ?

This is the g++ command I use:

Code: Select all
g++  -DEIGEN_NO_DEBUG -DNDEBUG -s -I"C:/Eigen" -fomit-frame-pointer -fno-operator-names -O3 -mfpmath=sse -Wall -mtune=core2 -msse2 -o Silver.o -c SilverTest.cpp

g++ -shared -s -static-libgcc -o Silver.dll defFileTest.def Silver.o  -Wl,--add-stdcall-alias





Thanks,
Mehrdad
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Matrix product crash in a DLL

Sun Jun 30, 2013 7:24 am
We cannot help without the backtrace.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Matrix product crash in a DLL

Sun Jun 30, 2013 7:26 am
oh wait, again by gcc I guess you meant mingw, then change the -O3 to -O2. See: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=556
mehrdadh
Registered Member
Posts
26
Karma
0

Re: Matrix product crash in a DLL

Sun Jun 30, 2013 7:57 am
Followed instructions here:

http://eigen.tuxfamily.org/dox/TopicWro ... nment.html


and it seemed to fix it. Still would have preferred to make it work (since I used to compile the same code with g++ 4.6.2 as a DLL successfully for 'R', but now, it wasn't working for VBA).
mehrdadh
Registered Member
Posts
26
Karma
0

Re: Matrix product crash in a DLL

Sun Jun 30, 2013 8:00 am
Thanks gael, yes I meant mingw (and I would have gladly provided backtrace if I knew how - especially since it's a dll called from VBA)

Anyway, I actually tried that, and changed -O3 to -O2 but it was still crashing.

I even added -fno-ipa-cp-clone and it still crashed.

It was only adding -mstackrealign which worked without a crash.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Matrix product crash in a DLL

Sun Jun 30, 2013 2:09 pm
Are you saying that adding -mstackrealign fixed your issue? Makes sense now because matrix products are allocating some temporaries on the stack with alloca but since by default mingw does not keep an aligned stack, alloca failed to returned an aligned buffer. -mstackrealign is a good workaround, but that's also easy to fix upstream.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Matrix product crash in a DLL

Sun Jun 30, 2013 2:16 pm
hm, no that's strange because alloca is only used if __linux__ or _MSC_VER are defined. That should not be the case with mingw. Could you check whether EIGEN_ALLOCA is defined after including eigen, e.g.:

Code: Select all
#ifdef EIGEN_ALLOCA
#error EIGEN_ALLOCA is defined
#endif


thanks.
mehrdadh
Registered Member
Posts
26
Karma
0

Re: Matrix product crash in a DLL

Sun Jun 30, 2013 6:44 pm
hmmm.

I might have confused myself. The crash on Matrix product was happening in gcc 4.6.3.

Let me give more background. During the time I was looking for a solution, I installed mingw 4.6.2 (I guess the newest version), and now I can see that it will crash even without the matrix product (I guess some other lines that were OK are now causing a crash). I also noticed that using -mstackrealign my VBA dll runs ok. However, there is a caveat. Upon closing the Excel sheet, there is this error message from Excel "This application has requested the Runtime to terminate it in an unusual way", which I think has to do with the DLL being unloaded unsuccessfully.

So, I switched back to the older version of on my PC (gcc 4.6.3 that I had), and noticed that it is indeed the matrix product, alone that causes a crash, and using -mstackrealign (I actually used __attribute__((force_align_arg_pointer)) ), then the DLL works correctly, and when I close Excel that error message doesn't show up anymore and DLL gets unloaded successfully.


And I have not defined EIGEN_ALLOCA when compiling my code, neither have I defined it anywhere in my code. I tried that piece of code u gave in previous post, and I didn't get any error. So it's not defined.

I will dig in more I guess to see if I can get more information about it.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]