mcdougallab / matlabneuroninterface

Interface for connecting NEURON and MATLAB
BSD 3-Clause "New" or "Revised" License
5 stars 1 forks source link

Enable matlabneuroninterface on Mac #18

Open AljenU opened 1 year ago

AljenU commented 1 year ago

See #17 for more info on what needs to be done, will be similar for Mac as for Linux.

ramcdougal commented 1 year ago

If I'm not mistaken, it looks like MATLAB doesn't directly support M1/M2 macs yet? Is this right @vijayiyer05 ? @tkuenzmw? If so, is there a timeline on that? (Edit: I see there's an "open beta", but the end date on that is before CNS, so I'd rather not depend on it.)

If MATLAB uses Rosetta 2, we may need to use the intel version of NEURON on M1 macs, whereas obviously normally we'd use the arm64 version... this probably requires some explanation. Also, ideally, we can come up with a way for this to coexist with an arm64 NEURON, so users who want to use Python and MATLAB aren't forced to use the intel version of NEURON which would force them to use an intel-compatible Python.

ramcdougal commented 1 year ago

As part of the process, we'll want to make sure examples/example_mod.m works in all platforms. The original version assumes a Windows .dll file extension.

vijayiyer05 commented 1 year ago

For the purpose of the active project cycle, I think using the M1 open beta would be a good approach, and simply capture how well it does or doesn't work.

vijayiyer05 commented 1 year ago

(Edit: I see there's an "open beta", but the end date on that is before CNS, so I'd rather not depend on it.)

Looks like you caught this too :)

I'd still be inclined towards keeping some focus now (i.e., trying this beta) on the longer-term goal of Mac support & getting in feedback to the MathWorks end asap, versus solely focusing on what's needed for the event.

Can you remind if the event is planned as hands-on or more of a demo? I'd recommend the latter at this stage, which would make it less mission critical to have Mac support on a short timeline.

AljenU commented 1 year ago

@kian-ohara see the note in https://github.com/mcdougallab/matlabneuroninterface/issues/17, similar for Mac.

Additionally, for this issue:

AljenU commented 1 year ago

With #72 , this issue could be closed, expect that setting the DYLD_LIBRARY_PATH on the outside, does not propagate to inside Matlab, contrary to the Matlab documentation. That needs to be figured out, before we can say this issue is ready.

Additionally, only intel Macs are supported so far. Might want to create a follow up issue on ARM Macs, but Matlab itself is also not entirely ready there.

AljenU commented 1 year ago

The #72 has been updated, at least the DYLD_LIBRARY_PATH is now ok on the inside as well. (Based on https://nl.mathworks.com/matlabcentral/answers/1969999-dyld_library_path-from-shell-not-passed-to-matlab-on-mac)

However, even with a correct DYLD_LIBRARY_PATH, the interface library fails to run, with the same totally uninformative error message. Whereas setting some dl environment variables to get more dl loading print statements, it looks like dl should be able to find all necessary dependencies.

Thus, this is as far as we can get for now. I can provide @tkuenzmw or @vijayiyer05 with more details on what we do know, then maybe they can pass that on to someone that can take a next step.

AljenU commented 1 year ago

FYI, the uninformative error message is like

>>> example_run
Unable to load interface library: 'PATH_REDACTED/matlabneuroninterface/neuron/neuronInterface.so'.
Reason: The specified module could not be found.
Ensure the C++ dependent libraries for the interface library are added to run-time path.

Error in neuron.Neuron

Error in example_run (line 6)
n = neuron.Neuron();

Related documentation
vijayiyer05 commented 1 year ago

@AljenU You could assign this issue to me, to indicate a pending action to seek some input from my employer. Looks like I need to be added to the repo as a collaborator.

edovanveen commented 1 year ago

In order to have a common version for Windows, Linux and Mac, I set the version to Neuron 9.0a (from the Neuron release page at https://github.com/neuronsimulator/nrn/releases). @ramcdougal is this a newer or an older version than the previous version of nrn-nightly you sent us? Are there any known issues with 9.0a?

Now I get crashes on Windows, even for something simple like:

n = neuron.Neuron();
axon = n.Section("axon");
axon.length = 1;
Abnormal termination:
Access violation

Stack Trace (from fault):
[  0] 0x000000006f5e019f     Z:\Git\matlabneuron\neuron\neuronInterface.dll+00393631 cppGetPassThrough+00382143
[  1] 0x000000006f591df1     Z:\Git\matlabneuron\neuron\neuronInterface.dll+00073201 cppGetPassThrough+00061713
[  2] 0x00007fffac456133 C:\Program Files\MATLAB\R2023b\bin\win64\cpp_library_service_core.dll+00024883 cpp_library_service_core::DynamicLoadedFunction::invoke+00000499
[  3] 0x00007fff67277c8c C:\Program Files\MATLAB\R2023b\bin\win64\cppcli.dll+00818316 mwboost::archive::codecvt_null<wchar_t>::do_out+00129884
[  4] 0x00007fff8dae31c0 C:\Program Files\MATLAB\R2023b\bin\win64\mcos_impl.dll+02961856 mcos::heterogeneous::mcosIsHeterogeneousArray+00049440
[  5] 0x00007fff8dae3cf3 C:\Program Files\MATLAB\R2023b\bin\win64\mcos_impl.dll+02964723 mcos::heterogeneous::mcosIsHeterogeneousArray+00052307
[  6] 0x00007fff8dae3f8f C:\Program Files\MATLAB\R2023b\bin\win64\mcos_impl.dll+02965391 mcos::heterogeneous::mcosIsHeterogeneousArray+00052975
[  7] 0x00007fff8dae7c7c C:\Program Files\MATLAB\R2023b\bin\win64\mcos_impl.dll+02980988 mcos::heterogeneous::mcosIsHeterogeneousArray+00068572
[  8] 0x00007fff8dbf41c6 C:\Program Files\MATLAB\R2023b\bin\win64\mcos_impl.dll+04080070 namedArgsToCell+00000870
[  9] 0x00007fff8dcf6c0a C:\Program Files\MATLAB\R2023b\bin\win64\mcos_impl.dll+05139466 musBeA+00328458
[ 10] 0x00007fff8dcf55e6 C:\Program Files\MATLAB\R2023b\bin\win64\mcos_impl.dll+05133798 musBeA+00322790
[ 11] 0x00007fffa963b48f C:\Program Files\MATLAB\R2023b\bin\win64\m_dispatcher.dll+00046223
[ 12] 0x00007fffa965d70d C:\Program Files\MATLAB\R2023b\bin\win64\m_dispatcher.dll+00186125 Mfh_MATLAB_fn_impl::dispatch+00000045
[ 13] 0x00007fffa91c705d C:\Program Files\MATLAB\R2023b\bin\win64\libmwlxemainservices.dll+00421981 mwboost::archive::codecvt_null<wchar_t>::do_max_length+00313389
[ 14] 0x00007fffa91d6c3e C:\Program Files\MATLAB\R2023b\bin\win64\libmwlxemainservices.dll+00486462 mwboost::archive::codecvt_null<wchar_t>::do_max_length+00377870
[ 15] 0x00007fff9a2fa64e C:\Program Files\MATLAB\R2023b\bin\win64\m_lxe.dll+06071886 MathWorks::lxe::ReadOnlyXvaluePtr::operator=+00006318
[ 16] 0x00007fff9a2fbf39 C:\Program Files\MATLAB\R2023b\bin\win64\m_lxe.dll+06078265 MathWorks::lxe::ReadOnlyXvaluePtr::operator=+00012697
[ 17] 0x00007fff9a2fc32a C:\Program Files\MATLAB\R2023b\bin\win64\m_lxe.dll+06079274 MathWorks::lxe::ReadOnlyXvaluePtr::operator=+00013706
[...]
ramcdougal commented 1 year ago

You want:

pip install NEURON-nightly==9.0a0

The alpha is newer and includes changes to the data structures. There's a formal API under development to smooth all those issues, but it hasn't been merged yet: https://github.com/neuronsimulator/nrn/pull/2357

(And of course, once it is merged, we'll have to adjust this code to work with it, but hopefully that'll stablize the interface to NEURON.)

edovanveen commented 1 year ago

@ramcdougal Can we get a windows executable for that version? Just so that we have the exact same version on all platforms.

ramcdougal commented 1 year ago

Just acknowledging for now... this turns out to be slightly difficult because the installer that was built then has been automatically deleted over time and we can't just rerun because our scripts don't pin a certain version of mingw etc and now the current versions no longer quite work with the old build script...

edovanveen commented 11 months ago

@ramcdougal NEURON-nightly==9.0a0 also does not seem to work with the current matlab-neuron interface. So we still don't have a pip installable neuron-nightly version that works with the matlab-neuron interface on linux.

The fact that there is no publicly available neuron version (either for linux or windows) that works with the current matlab-neuron interface means that any users (other than us) have no way to get it to run.

ramcdougal commented 11 months ago

Okay, so... on my chromebook which works, we have MATLAB 2023a update 2 with NEURON 9.0.dev-1361-g98cad3ae4, which is what neuron.__version__ reports (in Python... I have 3.9.2 but that shouldn't be relevant since the MATLAB interface does not use Python) when you ask for 9.0a0 from neuron-nightly:

$ uname -a
Linux penguin 5.15.130-20472-g682e24dd583b #1 SMP PREEMPT Wed Oct 25 18:26:35 PDT 2023 x86_64 GNU/Linux

I apparently changed +utils/Paths.m to specify the location of my NEURON library:

value = '/home/rmcdougal/.local/lib/python3.9/site-packages/neuron/.data/lib/';

Here are the following relevant lines from .bashrc:

export PATH=$PATH:/usr/local/MATLAB/R2023a/bin/

# ONLY the first three variables should need to be set. However, depending on
# how NEURON was installed, also the relative parts of the second set of three variables
# might need to be adapted.
# Do include the final slash as part of these paths

# The directory where libmex is installed
NRNML_LIBMEXPATH="/usr/local/MATLAB/R2023a/bin/glnxa64/"
# The 'neuron' directory within the directory where this toolbox is installed
NRNML_INTERFACEPATH="/home/rmcdougal/matlabneuroninterface/neuron/"
# The directory where NEURON is installed, see also the next three variables
NRNML_NRNPATH="/home/rmcdougal/.local/lib/python3.9/site-packages/neuron/.data/"

# Exact location of the following subdirectories might depend on how NEURON was installed, do check!
# Directory containing libnrniv, libnrnpython3, libcorenrnmech_internal shared libraries
NRNML_LIBNRNPATH="${NRNML_NRNPATH}lib/"
# Directory containing stdlib.hoc, stdrun.hoc and many more .hoc files
NRNML_HOCNRNPATH="${NRNML_NRNPATH}share/nrn/lib/hoc/"

# Update to LD_LIBRARY_PATH needed, to find dependencies of the interface library
export LD_LIBRARY_PATH="${NRNML_LIBMEXPATH}:${NRNML_LIBNRNPATH}:${NRNML_INTERFACEPATH}:${LD_LIBRARY_PATH}"
# Update to HOC_LIBRARY_PATH needed, to be able to find the .hoc files distributed with NEURON
export HOC_LIBRARY_PATH="${NRNML_HOCNRNPATH}:${HOC_LIBRARY_PATH}"

# Update to PATH needed, to be able to find nrnivmodl for compiling mod files?
# It should usually NOT be needed to update the PATH variable, but it does depend on how NEURON is installed.
# If it is needed to update the path, it would be to be able to find the correct nrnivmodl.
# An example for a conda environment named neuron9:
# - The correct nrnivmodl will be in envs/neuron9/bin
# - The underlying nrnivmodl, that should NOT be put on the path, is in envs/neuron9/lib/python3.11/site-packages/neuron/.data/bin
# Thus, a correct path could be
#NRNML_BINNRNPATH="${HOME}/.conda/envs/neuron9/bin/"
#export PATH="${PATH}:${NRNML_BINNRNPATH}:"

export MATLABPATH=/home/rmcdougal/matlabneuroninterface:/home/rmcdougal/neuron
AljenU commented 11 months ago

Okay, so... on my chromebook which works, we have MATLAB 2023a update 2 with NEURON 9.0.dev-1361-g98cad3ae4, which is what neuron.__version__ reports (in Python... I have 3.9.2 but that shouldn't be relevant since the MATLAB interface does not use Python) when you ask for 9.0a0 from neuron-nightly: [.......]

# Thus, a correct path could be
#NRNML_BINNRNPATH="${HOME}/.conda/envs/neuron9/bin/"
#export PATH="${PATH}:${NRNML_BINNRNPATH}:"

export MATLABPATH=/home/rmcdougal/matlabneuroninterface:/home/rmcdougal/neuron

Hey @ramcdougal , as far as I can find, I now have the exact same setup in terms of matlabneuroninterface, neuron version and even matlab version (thanks to @vijayiyer05 I could try on Matlab 2023a), yet for me the linux still fails..... :(

Differences that I can see and might be relevant:

AljenU commented 11 months ago

I tried using a later g++, but that definitely creates problems. With g++ 13.2 i get:

matlab command window >> utils.build_interface
Error using utils.build_interface
Errors parsing interface generation files.
/home/aljen.uitbeijerse/.conda/envs/py39neuron90a0/x86_64-conda-linux-gnu/include/c++/13.2.0/type_traits(3363):
error: type name is not allowed
/home/aljen.uitbeijerse/.conda/envs/py39neuron90a0/x86_64-conda-linux-gnu/include/c++/13.2.0/type_traits(3363):
error: type name is not allowed
/home/aljen.uitbeijerse/.conda/envs/py39neuron90a0/x86_64-conda-linux-gnu/include/c++/13.2.0/type_traits(3363):
error: identifier "__is_convertible" is undefined
tkuenzmw commented 11 months ago

Please note: https://www.mathworks.com/support/requirements/supported-compilers-linux.html in general and https://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/system-requirements-release-2023a-supported-compilers.pdf for 23a. GCC 10.x is the supported compiler for 23a. Newer versions might work but are not guaranteed.

AljenU commented 11 months ago

Another update: I found the setup where I had it working earlier. With current matlabneuroninterface, and Neuron 9.0a0, Matlab2023a. I switched computers a while ago, due to graphics card problems, but I was able to still start my old computer and run without graphics. And there the example_run does work The difference is in the g++ compiler version

myprompt> g++ --version g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0

And, YAY, when I create a conda environment with this older g++, I can also do the matlabneuroninterface on my new Linux computer, and with Matlab R2023b.

AljenU commented 11 months ago

@ramcdougal So for Mac support, this means we need to know the exact clang version that is being used to create the Neuron for Mac. And then use that clang version also when compiling the matlabneuroninterface for Mac.

ramcdougal commented 11 months ago

I can tell you what we're using now, which is AppleClang 14.0.0.14000029. I can also tell you that wasn't what we used for neuron-nightly==9.0a0. These facts are probably not so helpful.

But none of this makes any sense... unless there's a subtlty to the name mangling that does something different in the different versions? Non-mangled code shouldn't care.

tkuenzmw commented 11 months ago

Another update: I found the setup where I had it working earlier. With current matlabneuroninterface, and Neuron 9.0a0, Matlab2023a. I switched computers a while ago, due to graphics card problems, but I was able to still start my old computer and run without graphics. And there the example_run does work The difference is in the g++ compiler version

myprompt> g++ --version g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0

And, YAY, when I create a conda environment with this older g++, I can also do the matlabneuroninterface on my new Linux computer, and with Matlab R2023b.

@AljenU For me it also worked with g++ version 10. I tried 11 but that failed with errors related to malloc parameters

AljenU commented 11 months ago

I can tell you what we're using now, which is AppleClang 14.0.0.14000029. I can also tell you that wasn't what we used for neuron-nightly==9.0a0. These facts are probably not so helpful.

But none of this makes any sense... unless there's a subtlty to the name mangling that does something different in the different versions? Non-mangled code shouldn't care.

Yes, name mangling can differ per compiler version, as per this for gcc. Also per that doc, it might be possible, if we know the compiler version that was used for compiling neuron, to specify at least for Linux+gcc, a flag that ensures the name mangling is compatible. I think there is an option in the matlab clib interface to specify compile flags.

ramcdougal commented 11 months ago

Looks like neuron-nightly==9.0a0 was built with xcode 13.2.1...

I learned today that we can find this via:

pip install neuron-nightly==9.0a0
strings env/lib/python3.10/site-packages/neuron/.data/lib/libnrniv.dylib| grep C_COMPILER
AljenU commented 10 months ago

@vijayiyer05 I have now tried with definitely the right neuron-nightly==9.0a0 also on the mac, and what should be the correct compiler, and it still doesn't work. I've created a utility to compare the name-mangled symbols, and there also no differences are found (https://github.com/mcdougallab/matlabneuroninterface/pull/90). So we are still stuck at the generic, not necessarily accurate matlab error message, see below. And thus do need MathWorker support to find out what is wrong.

$ ./doc/example_startup_scripts/mac_matlab.sh
/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/neuron/.data/bin/:
/Applications/MATLAB_R2023b.app/bin/maci64/:/Applications/MATLAB_R2023b.app/sys/os/maci64/:/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/neuron/.data/lib/:/Users/aljen.uitbeijerse/Documents/matlabneuroninterface/neuron/:
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/neuron/.data/share/nrn/lib/hoc/:

                                                       < M A T L A B (R) >
                                             Copyright 1984-2023 The MathWorks, Inc.
                                        R2023b Update 3 (23.2.0.2409890) 64-bit (maci64)
                                                         October 4, 2023

To get started, type doc.
For product information, visit www.mathworks.com.

        Event Support License -- for demonstration use and event support.  Not for government,
        research, commercial, or other organizational use.

>> setup
>> disp("The utils.build_interface, which has a lot of output, was already done in the previous matlab session")
The utils.build_interface, which has a lot of output, was already done in the previous matlab session
>> example_run
Unable to load interface library: '/Users/aljen.uitbeijerse/Documents/matlabneuroninterface/neuron/neuronInterface.dylib'. Reason:
The specified module could not be found.
Ensure the C++ dependent libraries for the interface library are added to run-time path.

Error in neuron.Neuron

Error in example_run (line 6)
n = neuron.Neuron();

>> getenv("DYLD_LIBRARY_PATH")

ans =

    '/Applications/MATLAB_R2023b.app/bin/maci64/:/Applications/MATLAB_R2023b.app/sys/os/maci64/:/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/neuron/.data/lib/:/Users/aljen.uitbeijerse/Documents/matlabneuroninterface/neuron/:'

>> binfile = fullfile(utils.Paths.neuron_lib_directory, '..', 'bin', 'nrniv')

binfile =

    '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/neuron/.data/lib/../bin/nrniv'

>> system([binfile, ' --version']);
NEURON -- VERSION 9.0.dev-1361-g98cad3ae4 HEAD (98cad3ae4) 2023-06-12
>>mex.getCompilerConfigurations('CPP')

ans =

  CompilerConfiguration with properties:

             Name: 'Xcode Clang++'
     Manufacturer: 'Apple'
         Language: 'C++'
          Version: '13.0.0'
         Location: '/Library/Developer/CommandLineTools'
        ShortName: 'Clang++'
         Priority: 'A'
          Details: [1x1 mex.CompilerConfigurationDetails]
       LinkerName: '/usr/bin/xcrun -sdk macosx11.3 clang++'
    LinkerVersion: ''
           MexOpt: '/Users/aljen.uitbeijerse/Library/Application Support/MathWorks/MATLAB/R2023b/mex_C++_maci64.xml'

>>
AljenU commented 10 months ago

From a list on the internet, what we want to use as compiler:

Xcode 13.2.1 (13C100) Apple clang version 13.0.0 (clang-1300.0.29.30) Target: x86_64-apple-darwin20.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

tkuenzmw commented 10 months ago

@AljenU on linux there was a difference between in-process and out-of-process execution of the clib... is this the same on mac? I remember that I had a hard time getting MATLAB to honor the environment variables set "outside" of MATLAB on linux resulting in the same error ("specific module not found")

AljenU commented 10 months ago

I tried with both inprocess and outofprocess. Also, I updated the dump of matlab output two comments above with some more info on e.g. the compiler.

edovanveen commented 9 months ago

Status update: