GMLC-TDC / HELICS

Hierarchical Engine for Large-scale Infrastructure Co-Simulation (HELICS)
https://docs.helics.org/en/latest/
BSD 3-Clause "New" or "Revised" License
127 stars 40 forks source link

MEX compiled helicsGetVersion hangs #18

Closed kdheepak closed 6 years ago

kdheepak commented 6 years ago

Minimal Working Example below:

// test.cpp
#include "helics.h";
#include <stdio.h>

int main() {

    char *result = 0;
    result = (char *)helicsGetVersion();
    printf("%s\n", result);
    return 0;

}
clang test.cpp -I../path/to/HELICS-src/src/shared_api_library -L../path/to/helics_install/lib -lhelicsSharedLib
# (Optional) install_name_tool -change @rpath/libhelicsSharedLib.dylib ../path/to/helics_install/lib/libhelicsSharedLib.dylib
./a.out
0.3.0 (10-16-17)

Minimal Example of Mex not working below:

//test.cpp
#include "mex.h"
#include "helics.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

    char *result = 0;
    result = (char *)helicsGetVersion()

}
mex -I../path/to/HELICS-src/src/shared_api_library -L../path/to/helics_install/lib test.cpp helicsSharedLib
matlab
>>> test()
% Hang indefinitely
kdheepak commented 6 years ago

I was able to replace test.cpp with a fibonacci example, which worked fine. This leads me to believe there's an issue with mex HELICS interface. To clarify, the above mex file is hand written and is not generated using SWIG. But I'm getting the same issue with SWIG (MATLAB hangs) because SWIG essentially generates a similar (larger) CPP file.

phlptp commented 6 years ago

It would be useful to know where the hang occurs. I think we need to determine if it is something with the function call or in the initialization of one of the static variables.

kdheepak commented 6 years ago

I tried adding the following to the mexFunction but it did not print anything, not on the command line nor in the matlab window.

    std::cout << "In mexFunction";

Any suggestions on how to figure out where the hang happens? It looks like the hang is not in the function based on this.

phlptp commented 6 years ago

That is what I was afraid of. That probably means it is blocking in one of the init statics somewhere or it is failing to load the library in some way.

kdheepak commented 6 years ago

There's a mex debugger that I'm going to attempt, I'll post on progress.

kdheepak commented 6 years ago

Mex output

mex -v -g -I../../src/helics/shared_api_library test.cpp libhelicsSharedLib.dylib
Verbose mode is on.
No MEX options file identified; looking for an implicit selection.
... Looking for compiler 'Xcode Clang++' ...
... Looking for environment variable 'DEVELOPER_DIR' ...No.
... Executing command 'xcode-select -print-path' ...Yes ('/Applications/Xcode.app/Contents/Developer').
... Looking for folder '/Applications/Xcode.app/Contents/Developer' ...Yes.
... Executing command 'which xcrun' ...Yes ('/usr/bin/xcrun').
... Looking for folder '/usr/bin' ...Yes.
... Executing command 'defaults read com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense' ...No.
... Executing command 'defaults read /Library/Preferences/com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense' ...Yes ('9.0').
... Executing command '
agreed=9.0
 if echo $agreed | grep -E '[\.\"]' >/dev/null; then
 lhs=`expr "$agreed" : '\([0-9]*\)[\.].*'`
  rhs=`expr "$agreed" : '[0-9]*[\.]\(.*\)$'`
 if echo $rhs | grep -E '[\."]' >/dev/null; then
 rhs=`expr "$rhs" : '\([0-9]*\)[\.].*'`
 fi
 if [ $lhs -gt 4 ] || ( [ $lhs -eq 4 ] && [ $rhs -ge 3 ] ); then
 echo $agreed
 else
 exit 1
 fi
 fi' ...Yes ('9.0').
... Executing command 'xcrun -sdk macosx --show-sdk-path' ...Yes ('/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk').
... Executing command 'xcrun -sdk macosx --show-sdk-version' ...Yes ('10.13').
Found installed compiler 'Xcode Clang++'.
Options file details
-------------------------------------------------------------------
        Compiler location: /Applications/Xcode.app/Contents/Developer
        Options file: /Applications/MATLAB_R2017a.app/bin/maci64/mexopts/clang++_maci64.xml
        CMDLINE200 : /usr/bin/xcrun -sdk macosx10.13 clang++ -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.9 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -framework Cocoa -bundle  -stdlib=libc++ -g -Wl,-exported_symbols_list,"/Applications/MATLAB_R2017a.app/extern/lib/maci64/mexFunction.map" -Wl,-exported_symbols_list,"/Applications/MATLAB_R2017a.app/extern/lib/maci64/c_exportsmexfileversion.map" test.o cpp_mexapi_version.o   libhelicsSharedLib.dylib  -L"/Applications/MATLAB_R2017a.app/bin/maci64" -lmx -lmex -lmat -o test.mexmaci64
        CC : /usr/bin/xcrun -sdk macosx10.13 clang
        CXX : /usr/bin/xcrun -sdk macosx10.13 clang++
        DEFINES : -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE
        MATLABMEX : -DMATLAB_MEX_FILE
        MACOSX_DEPLOYMENT_TARGET : 10.9
        CFLAGS : -fno-common -arch x86_64 -mmacosx-version-min=10.9 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk
        CXXFLAGS : -fno-common -arch x86_64 -mmacosx-version-min=10.9 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fobjc-arc -std=c++11 -stdlib=libc++
        INCLUDE : -I../../src/helics/shared_api_library  -I"/Applications/MATLAB_R2017a.app/extern/include" -I"/Applications/MATLAB_R2017a.app/simulink/include"
        CXXOPTIMFLAGS : -O2 -fwrapv -DNDEBUG
        CXXDEBUGFLAGS : -g
        LD : /usr/bin/xcrun -sdk macosx10.13 clang
        LDXX : /usr/bin/xcrun -sdk macosx10.13 clang++
        LDFLAGS : -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.9 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -framework Cocoa -bundle  -stdlib=libc++
        LDBUNDLE : -bundle
        LINKEXPORT : -Wl,-exported_symbols_list,"/Applications/MATLAB_R2017a.app/extern/lib/maci64/mexFunction.map"
        LINKEXPORTVER : -Wl,-exported_symbols_list,"/Applications/MATLAB_R2017a.app/extern/lib/maci64/c_exportsmexfileversion.map"
        LINKLIBS : libhelicsSharedLib.dylib  -L"/Applications/MATLAB_R2017a.app/bin/maci64" -lmx -lmex -lmat
        LDOPTIMFLAGS : -O
        LDDEBUGFLAGS : -g
        OBJEXT : .o
        LDEXT : .mexmaci64
        SETENV : CC="/usr/bin/xcrun -sdk macosx10.13 clang"
                CXX="/usr/bin/xcrun -sdk macosx10.13 clang++"
                CFLAGS="-fno-common -arch x86_64 -mmacosx-version-min=10.9 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE "
                CXXFLAGS="-fno-common -arch x86_64 -mmacosx-version-min=10.9 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE "
                COPTIMFLAGS="-O2 -fwrapv -DNDEBUG"
                CXXOPTIMFLAGS="-O2 -fwrapv -DNDEBUG"
                CDEBUGFLAGS="-g"
                CXXDEBUGFLAGS="-g"
                LD="/usr/bin/xcrun -sdk macosx10.13 clang"
                LDXX="/usr/bin/xcrun -sdk macosx10.13 clang++"
                LDFLAGS="-Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.9 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -framework Cocoa -bundle  -stdlib=libc++ libhelicsSharedLib.dylib  -L"/Applications/MATLAB_R2017a.app/bin/maci64" -lmx -lmex -lmat -Wl,-exported_symbols_list,"/Applications/MATLAB_R2017a.app/extern/lib/maci64/mexFunction.map""
                LDDEBUGFLAGS="-g"
        DEVELOPER_DIR_CHECK :
        XCODE_DIR : /Applications/Xcode.app/Contents/Developer
        XCRUN_DIR : /usr/bin
        XCODE_AGREED_VERSION : 9.0
        ISYSROOT : /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk
        SDKVER : 10.13
        MATLABROOT : /Applications/MATLAB_R2017a.app
        ARCH : maci64
        SRC : /Users/$USER/GitRepos/GMLC-TDC/HELICS-src/swig/matlab/test.cpp;/Applications/MATLAB_R2017a.app/extern/version/cpp_mexapi_version.cpp
        OBJ : test.o;cpp_mexapi_version.o
        OBJS : test.o cpp_mexapi_version.o
        SRCROOT : /Users/$USER/GitRepos/GMLC-TDC/HELICS-src/swig/matlab/test
        DEF : /var/folders/wk/lcf0vgd90bx0vq1873tn04knk_djr3/T/mex_16594524319308_79818/test.def
        EXP : test.exp
        LIB : test.lib
        EXE : test.mexmaci64
        ILK : test.ilk
        MANIFEST : test.mexmaci64.manifest
        TEMPNAME : test
        EXEDIR :
        EXENAME : test
        OPTIM : -g
        LINKOPTIM : -g
        CMDLINE100_0 : /usr/bin/xcrun -sdk macosx10.13 clang++ -c -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE  -I../../src/helics/shared_api_library  -I"/Applications/MATLAB_R2017a.app/extern/include" -I"/Applications/MATLAB_R2017a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.9 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -g /Users/$USER/GitRepos/GMLC-TDC/HELICS-src/swig/matlab/test.cpp -o test.o
        CMDLINE100_1 : /usr/bin/xcrun -sdk macosx10.13 clang++ -c -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE  -I../../src/helics/shared_api_library  -I"/Applications/MATLAB_R2017a.app/extern/include" -I"/Applications/MATLAB_R2017a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.9 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -g /Applications/MATLAB_R2017a.app/extern/version/cpp_mexapi_version.cpp -o cpp_mexapi_version.o
-------------------------------------------------------------------
Building with 'Xcode Clang++'.
/usr/bin/xcrun -sdk macosx10.13 clang++ -c -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE  -I../../src/helics/shared_api_library  -I"/Applications/MATLAB_R2017a.app/extern/include" -I"/Applications/MATLAB_R2017a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.9 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -g /Users/$USER/GitRepos/GMLC-TDC/HELICS-src/swig/matlab/test.cpp -o test.o
/usr/bin/xcrun -sdk macosx10.13 clang++ -c -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -DMATLAB_MEX_FILE  -I../../src/helics/shared_api_library  -I"/Applications/MATLAB_R2017a.app/extern/include" -I"/Applications/MATLAB_R2017a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.9 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -g /Applications/MATLAB_R2017a.app/extern/version/cpp_mexapi_version.cpp -o cpp_mexapi_version.o
/usr/bin/xcrun -sdk macosx10.13 clang++ -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.9 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -framework Cocoa -bundle  -stdlib=libc++ -g -Wl,-exported_symbols_list,"/Applications/MATLAB_R2017a.app/extern/lib/maci64/mexFunction.map" -Wl,-exported_symbols_list,"/Applications/MATLAB_R2017a.app/extern/lib/maci64/c_exportsmexfileversion.map" test.o cpp_mexapi_version.o   libhelicsSharedLib.dylib  -L"/Applications/MATLAB_R2017a.app/bin/maci64" -lmx -lmex -lmat -o test.mexmaci64
MEX completed successfully.
kdheepak commented 6 years ago

Using the debugger did not get me very far. However, I'm able to get the shared library loaded using FFI (loadLibrary) in MATLAB and I'm able to call the helicsGetVersion function (using calllib) successfully.

screen shot 2017-12-06 at 10 40 44

kdheepak commented 6 years ago

I think I figured out how to prevent the hang, but I'm unable to recompile the SWIG extension at the moment. Some progress though, hopefully I'll be able to close this issue shortly. The hang does not occur if I load the shared library before calling the mex compiled library.

kdheepak commented 6 years ago

I'm getting this error that I haven't gotten before.

Building with 'Xcode Clang++'.
In file included from /Users/$USER/GitRepos/GMLC-TDC/HELICS-src/swig/helics_wrap.cxx:1212:
../../helics_install/include/shared_api_library/api-data.h:16:10: fatal error: '../core/flag-definitions.h' file not found
#include "../core/flag-definitions.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
kdheepak commented 6 years ago

The helics install directory would need to be updated. I'm now able to build using source header files, which have the correct relative directory links to other header files.

kdheepak commented 6 years ago

Example of pireceiver.m and pisender.m

screen shot 2017-12-06 at 15 00 52