MEX File Compilation
Closed this issue · 6 comments
I'd like to use the C++ version, but I'm having some trouble going through the process.
On a supercomputer shell, I'm running into the issue of not having gcc 6.3 installed (apparently the only one supported for MEX functions by MATLAB). The MEX file compiles with gcc 4, but produces an error when I try to use it. The MEX file doesn't compile with gcc 6.4 or 7. Alternatively, I'm considering trying to compile it from an Ubuntu shell, but will have the same issue of needing gcc 6.3.
There are some instructions on installing 6.3: https://www.mathworks.com/matlabcentral/answers/454659-how-can-i-change-my-current-gcc-g-version-to-a-supported-one
I think I'll give this a try, but if you have other suggestions please let me know.
Also, once the MEX file is compiled, I'm assuming it can only be used on Linux 64 instances of MATLAB. Is that correct?
I was able to get it working thanks to bkasu's recent post here: https://www.techilatechnologies.com/forum/viewtopic.php?f=9&t=971
The main differences for me are that instead of (4), I set the preferred compiler using the mex function (see https://www.mathworks.com/matlabcentral/answers/454659-how-can-i-change-my-current-gcc-g-version-to-a-supported-one
mex -glnxa64 -I./Eigen -I./libigl/include -I/usr/include -I. -v GCC='<path to gcc-6.3 executable>' HierarchicalSmoothMatlab.cpp libhsmooth.a
I basically didn't do (5) or (6)
And I dropped the "sudo" from the commands I used since this isn't allowed on our supercomputing system. This is also why I set the preferred compiler using the mex function instead of (4).
I end up getting a crash when I try to run the MEX file.
Here is my workspace just prior to entering the smoothing function:
Here is the code I run:
method = 'Cpp';
disp(['method = ',method])
switch method
case 'Cpp'
scriptfolder = 'Cpp';
case 'Matlab'
scriptfolder = 'Matlab';
end
addpath(fullfile('Src',scriptfolder))
% use example data
datafolder = {'examples','ex2'};
% load the data
filenames = {'SharedVertexList.txt','SharedTriList.txt',...
'FaceLabels.txt','NodeType.txt'};
nheaders = [0 0 0 0];
filepaths = fullfile(datafolder{:},filenames);
xdat = readmatrix(filepaths{1},'NumHeaderLines',nheaders(1));
xdat = xdat';
tri = readmatrix(filepaths{2},'NumHeaderLines',nheaders(2));
tri = 1+tri;
fl = readmatrix(filepaths{3});
f = find( any( fl > 0, 2 ) );
tri = tri( f, : );
fl = fl( f, : );
ntype = readmatrix(filepaths{4});
ntype = ntype';
disp('start smoothing')
tic
switch method
case 'Cpp'
xsmooth = HierarchicalSmoothMatlab(tri,xdat,fl,ntype);
case 'Matlab'
xsmooth = HierarchicalSmooth( xdat, tri, fl, ntype );
end
toc
writematrix(xsmooth.', 'xsmooth.txt', 'Delimiter', ' ') %updated to be n rows x 3 columns
Here is the output from the crash:
MATLAB: ./Eigen/Eigen/src/Core/DenseCoeffsBase.h:425: Eigen::DenseCoeffsBase<Derived, 1>::Scalar& Eigen::DenseCoeffsBase<Derived, 1>::operator()(Eigen::Index) [with Derived = Eigen::Array<int, -1, 1>; Eigen::DenseCoeffsBase<Derived, 1>::Scalar = int; Eigen::Index = long int]: Assertion `index >= 0 && index < size()' failed.
--------------------------------------------------------------------------------
abort() detected at Wed Apr 29 15:44:37 2020 -0600
--------------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled - No sandbox or build area path
Crash Mode : continue (default)
Default Encoding : US-ASCII
Deployed : false
GNU C Library : 2.17 stable
Graphics Driver : Unknown software
Graphics card 1 : 0x102b ( 0x102b ) 0x534 Version 0.0.0.0 (0-0-0)
Java Version : Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
MATLAB Architecture : glnxa64
MATLAB Entitlement ID : 789934
MATLAB Root : /zapps7/matlab/r2019b
MATLAB Version : 9.7.0.1296695 (R2019b) Update 4
OpenGL : software
Operating System : "Red Hat Enterprise Linux Server release 7.6 (Maipo)"
Process ID : 177123
Processor ID : x86 Family 6 Model 63 Stepping 2, GenuineIntel
Session Key : 2ce33ac3-621a-4b56-8f2a-36e26b653cd4
Static TLS mitigation : Disabled: Unnecessary
Window System : No active display
Fault Count: 1
Abnormal termination:
abort()
Register State (from fault):
RAX = 0000000000000000 RBX = 00007f40086d6000
RCX = ffffffffffffffff RDX = 0000000000000006
RSP = 00007f4062926808 RBP = 00007f4081c5a020
RSI = 000000000002b449 RDI = 000000000002b3e3
R8 = 0000000000000001 R9 = 0000000000000160
R10 = 0000000000000008 R11 = 0000000000000202
R12 = 00007f3fe0445a32 R13 = 00007f3fe04480c0
R14 = 000000000000000b R15 = 00007f400e6d0e80
RIP = 00007f4081b062c7 EFL = 0000000000000202
CS = 0033 FS = 0000 GS = 0000
Stack Trace (from fault):
[ 0] 0x00007f4081b062c7 /lib64/libc.so.6+00221895 gsignal+00000055
[ 1] 0x00007f4081b079b8 /lib64/libc.so.6+00227768 abort+00000328
[ 2] 0x00007f4081aff0e6 /lib64/libc.so.6+00192742
[ 3] 0x00007f4081aff192 /lib64/libc.so.6+00192914
[ 4] 0x00007f3fe042c1c4 /lustre/scratch/usr/.../reconstruction/HierarchicalSmooth-master/Src/Cpp/HierarchicalSmoothMatlab.mexa64+00037316
[ 5] 0x00007f3fe042551f /lustre/scratch/usr/.../reconstruction/HierarchicalSmooth-master/Src/Cpp/HierarchicalSmoothMatlab.mexa64+00009503 mexFunction+00001327
[ 6] 0x00007f406ab00de3 /zapps7/matlab/r2019b/bin/glnxa64/libmex.so+00548323
[ 7] 0x00007f406ab00ee5 /zapps7/matlab/r2019b/bin/glnxa64/libmex.so+00548581
[ 8] 0x00007f406ab01317 /zapps7/matlab/r2019b/bin/glnxa64/libmex.so+00549655
[ 9] 0x00007f406ab01f93 /zapps7/matlab/r2019b/bin/glnxa64/libmex.so+00552851
[ 10] 0x00007f406aaed7ac /zapps7/matlab/r2019b/bin/glnxa64/libmex.so+00468908
[ 11] 0x00007f406b9d515f /zapps7/matlab/r2019b/bin/glnxa64/libmwm_dispatcher.so+01073503 _ZN8Mfh_file20dispatch_file_commonEMS_FviPP11mxArray_tagiS2_EiS2_iS2_+00000207
[ 12] 0x00007f406b9d6c5e /zapps7/matlab/r2019b/bin/glnxa64/libmwm_dispatcher.so+01080414
[ 13] 0x00007f406b9d71a1 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_dispatcher.so+01081761 _ZN8Mfh_file8dispatchEiPSt10unique_ptrI11mxArray_tagN6matrix6detail17mxDestroy_deleterEEiPPS1_+00000033
[ 14] 0x00007f4068fb3323 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+14025507
[ 15] 0x00007f4068fb80d6 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+14045398
[ 16] 0x00007f40690beebe /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+15122110
[ 17] 0x00007f40690b2211 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+15069713
[ 18] 0x00007f406901ee34 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+14466612
[ 19] 0x00007f406904544d /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+14623821
[ 20] 0x00007f40687c483b /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05707835
[ 21] 0x00007f40687c6a74 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05716596
[ 22] 0x00007f40687c381d /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05703709
[ 23] 0x00007f40687b0cf1 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05627121
[ 24] 0x00007f40687b0f29 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05627689
[ 25] 0x00007f40687c3026 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05701670
[ 26] 0x00007f40687c3126 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05701926
[ 27] 0x00007f40688fcc49 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+06986825
[ 28] 0x00007f4068900383 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+07000963
[ 29] 0x00007f4068e6f0f1 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+12697841
[ 30] 0x00007f4068fa08a1 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+13949089
[ 31] 0x00007f4068fa199d /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+13953437
[ 32] 0x00007f406b9d515f /zapps7/matlab/r2019b/bin/glnxa64/libmwm_dispatcher.so+01073503 _ZN8Mfh_file20dispatch_file_commonEMS_FviPP11mxArray_tagiS2_EiS2_iS2_+00000207
[ 33] 0x00007f406b9d6c5e /zapps7/matlab/r2019b/bin/glnxa64/libmwm_dispatcher.so+01080414
[ 34] 0x00007f406b9d71a1 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_dispatcher.so+01081761 _ZN8Mfh_file8dispatchEiPSt10unique_ptrI11mxArray_tagN6matrix6detail17mxDestroy_deleterEEiPPS1_+00000033
[ 35] 0x00007f4068fb3323 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+14025507
[ 36] 0x00007f4068fb80d6 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+14045398
[ 37] 0x00007f40690beebe /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+15122110
[ 38] 0x00007f40690b236c /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+15070060
[ 39] 0x00007f406901ee34 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+14466612
[ 40] 0x00007f406904547d /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+14623869
[ 41] 0x00007f40687c483b /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05707835
[ 42] 0x00007f40687c6a74 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05716596
[ 43] 0x00007f40687c381d /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05703709
[ 44] 0x00007f40687b0cf1 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05627121
[ 45] 0x00007f40687b0f29 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05627689
[ 46] 0x00007f40687c3026 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05701670
[ 47] 0x00007f40687c3126 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+05701926
[ 48] 0x00007f40688fcc49 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+06986825
[ 49] 0x00007f4068900383 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+07000963
[ 50] 0x00007f4068e6f0f1 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+12697841
[ 51] 0x00007f4068e19693 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+12347027
[ 52] 0x00007f4068e1dd2f /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+12365103
[ 53] 0x00007f4068e210e2 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+12378338
[ 54] 0x00007f4068ebdf8f /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+13021071
[ 55] 0x00007f4068ebe279 /zapps7/matlab/r2019b/bin/glnxa64/libmwm_lxe.so+13021817
[ 56] 0x00007f406ab904c4 /zapps7/matlab/r2019b/bin/glnxa64/libmwbridge.so+00341188 _Z8mnParserv+00000596
[ 57] 0x00007f406bb205b5 /zapps7/matlab/r2019b/bin/glnxa64/libmwmcr.so+01017269
[ 58] 0x00007f408319742b /zapps7/matlab/r2019b/bin/glnxa64/libmwmvm.so+03097643 _ZN14cmddistributor15PackagedTaskIIP10invokeFuncIN7mwboost8functionIFvvEEEEENS2_10shared_ptrINS2_13unique_futureIDTclfp_EEEEEERKT_+00000059
[ 59] 0x00007f4083197518 /zapps7/matlab/r2019b/bin/glnxa64/libmwmvm.so+03097880 _ZNSt17_Function_handlerIFN7mwboost3anyEvEZN14cmddistributor15PackagedTaskIIP10createFuncINS0_8functionIFvvEEEEESt8functionIS2_ET_EUlvE_E9_M_invokeERKSt9_Any_data+00000024
[ 60] 0x00007f406bc7089c /zapps7/matlab/r2019b/bin/glnxa64/libmwiqm.so+00751772 _ZN7mwboost6detail8function21function_obj_invoker0ISt8functionIFNS_3anyEvEES4_E6invokeERNS1_15function_bufferE+00000028
[ 61] 0x00007f406bc70557 /zapps7/matlab/r2019b/bin/glnxa64/libmwiqm.so+00750935 _ZN3iqm18PackagedTaskPlugin7executeEP15inWorkSpace_tag+00000439
[ 62] 0x00007f406bb0f015 /zapps7/matlab/r2019b/bin/glnxa64/libmwmcr.so+00946197
[ 63] 0x00007f406bc556a0 /zapps7/matlab/r2019b/bin/glnxa64/libmwiqm.so+00640672
[ 64] 0x00007f406bc39e01 /zapps7/matlab/r2019b/bin/glnxa64/libmwiqm.so+00527873
[ 65] 0x00007f406bc3aa7f /zapps7/matlab/r2019b/bin/glnxa64/libmwiqm.so+00531071
[ 66] 0x00007f406baf6575 /zapps7/matlab/r2019b/bin/glnxa64/libmwmcr.so+00845173
[ 67] 0x00007f406baf6b93 /zapps7/matlab/r2019b/bin/glnxa64/libmwmcr.so+00846739
[ 68] 0x00007f406baf7404 /zapps7/matlab/r2019b/bin/glnxa64/libmwmcr.so+00848900
[ 69] 0x00007f408149bbdd /zapps7/matlab/r2019b/bin/glnxa64/libmwboost_thread.so.1.65.1+00080861
[ 70] 0x00007f408243cdd5 /lib64/libpthread.so.0+00032213
[ 71] 0x00007f4081bce02d /lib64/libc.so.6+01040429 clone+00000109
[ 72] 0x0000000000000000 <unknown-module>+00000000
This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.
Hi @sgbaird ,
Sorry for getting to this only now; I'm glad you were able to figure out the gcc compiler issue. Thanks for posting your solution here! This will definitely help others with the same problem.
I think you're almost there; I would try again after transposing the array ntype
to a column vector instead of a row vector, as you seem to have done. I think Eigen is complaining because it expects an Nx1 array for ntype
. You can also confirm this by looking at the nodetype
typedef in Types.h
. I know this is a departure from the original Matlab code, but I haven't bothered tweaking that in a long time! Let me know if it works...
@sgbaird and to answer your question, yes I think a compiled MEX binary is not probably portable to MATLAB on other platforms, although I haven't actually tried to do this.
Hi @siddharth-maddali
That seemed to do it! Thank you. This was my output:
WARNING: 437 of 346730 nodes not smoothed. Query VolumeSolver::Status for more information.
Elapsed time is 5.308397 seconds.
Feels pretty good to have it run so fast!
Good to know!
The fact that it's missing a few nodes means that it tried and couldn't resolve the topology into the hierarchy that it builds. This usually happens when your mesh has too many quad points clumped together in one part, instead of being separated by triple line segments. The code just doesn't smooth this part, and warns the user about it.