<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">
    <title type="text">Newest questions tagged mex - Stack Overflow</title>
    <link rel="self" href="https://stackoverflow.com/feeds/tag?tagnames=mex&amp;sort=newest" type="application/atom+xml" />
    <link rel="alternate" href="https://stackoverflow.com/questions/tagged?tagnames=mex&amp;sort=newest" type="text/html" />
    <subtitle>most recent 30 from stackoverflow.com</subtitle>
    <updated>2026-04-24T12:40:38Z</updated>
    <id>https://stackoverflow.com/feeds/tag?tagnames=mex&amp;sort=newest</id>
    <creativeCommons:license>https://creativecommons.org/licenses/by-sa/4.0/rdf</creativeCommons:license> 
    <entry>
        <id>https://stackoverflow.com/q/79750248</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Compiling boost for use with mex on Windows 11</title>
            <category scheme="https://stackoverflow.com/tags" term="boost" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="boost-serialization" />
        <author>
            <name>dogAwakeCat</name>
            <uri>https://stackoverflow.com/users/2833518</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79750248/compiling-boost-for-use-with-mex-on-windows-11" />
        <published>2025-08-29T11:53:05Z</published>
        <updated>2025-08-29T13:28:12Z</updated>
        <summary type="html">
            &lt;p&gt;I can&#x27;t compile the boost library on Windows 11, specifically serialization which is not header-only, for use with mex in MATLAB. I am compiling boost with &lt;code&gt;.\bootstrap.bat; .\b2.exe link=static runtime-link=static toolset=msvc address-model=64 architecture=x86 threading=multi --build-type=complete -a --reconfigure&lt;/code&gt;, however, no library (&lt;code&gt;.lib&lt;/code&gt;) files are created.&lt;/p&gt;&#xA;&lt;p&gt;Output (I&#x27;ve cut out bits that are not serialization related):&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Building Boost.Build engine&#xA;LOCALAPPDATA=C:\Users\user\AppData\Local&#xA;Found with vswhere C:\Program Files\Microsoft Visual Studio\2022\Community&#xA;Found with vswhere C:\Program Files\Microsoft Visual Studio\2022\Community&#xA;Call_If_Exists &amp;quot;C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\..\..\VC\Auxiliary\Build\vcvarsall.bat&amp;quot;  AMD64&#xA;**********************************************************************&#xA;** Visual Studio 2022 Developer Command Prompt v17.14.13&#xA;** Copyright (c) 2025 Microsoft Corporation&#xA;**********************************************************************&#xA;[vcvarsall.bat] Environment initialized for: &#x27;x64&#x27;&#xA;###&#xA;### Using &#x27;vc143&#x27; toolset.&#xA;###&#xA;&#xA;C:\Program Files\boost\boost_1_82_0\tools\build\src\engine&amp;gt;&amp;quot;cl&amp;quot; /nologo -TP /wd4996 /wd4675 /EHs /GR /Zc:throwingNew /O2 /Ob2 /W3 /MD /Zc:forScope /Zc:wchar_t /Zc:inline /Gw /favor:blend /Feb2   -DNDEBUG  builtins.cpp class.cpp command.cpp compile.cpp constants.cpp cwd.cpp debug.cpp debugger.cpp execcmd.cpp execnt.cpp execunix.cpp filent.cpp filesys.cpp fileunix.cpp frames.cpp function.cpp glob.cpp hash.cpp hcache.cpp hdrmacro.cpp headers.cpp jam.cpp jamgram.cpp lists.cpp make.cpp make1.cpp md5.cpp mem.cpp modules.cpp native.cpp object.cpp option.cpp output.cpp parse.cpp pathnt.cpp pathsys.cpp pathunix.cpp regexp.cpp rules.cpp scan.cpp search.cpp jam_strings.cpp startup.cpp subst.cpp sysinfo.cpp timestamp.cpp variable.cpp w32_getreg.cpp modules/order.cpp modules/path.cpp modules/property-set.cpp modules/regex.cpp modules/sequence.cpp modules/set.cpp /link kernel32.lib advapi32.lib user32.lib&#xA;builtins.cpp&#xA;class.cpp&#xA;command.cpp&#xA;compile.cpp&#xA;constants.cpp&#xA;cwd.cpp&#xA;debug.cpp&#xA;debugger.cpp&#xA;execcmd.cpp&#xA;execnt.cpp&#xA;execunix.cpp&#xA;filent.cpp&#xA;filesys.cpp&#xA;fileunix.cpp&#xA;frames.cpp&#xA;function.cpp&#xA;glob.cpp&#xA;hash.cpp&#xA;hcache.cpp&#xA;hdrmacro.cpp&#xA;Generating Code...&#xA;Compiling...&#xA;headers.cpp&#xA;jam.cpp&#xA;jamgram.cpp&#xA;lists.cpp&#xA;make.cpp&#xA;make1.cpp&#xA;md5.cpp&#xA;mem.cpp&#xA;modules.cpp&#xA;native.cpp&#xA;object.cpp&#xA;option.cpp&#xA;output.cpp&#xA;parse.cpp&#xA;pathnt.cpp&#xA;pathsys.cpp&#xA;pathunix.cpp&#xA;regexp.cpp&#xA;rules.cpp&#xA;scan.cpp&#xA;Generating Code...&#xA;Compiling...&#xA;search.cpp&#xA;jam_strings.cpp&#xA;startup.cpp&#xA;subst.cpp&#xA;sysinfo.cpp&#xA;timestamp.cpp&#xA;variable.cpp&#xA;w32_getreg.cpp&#xA;order.cpp&#xA;path.cpp&#xA;property-set.cpp&#xA;regex.cpp&#xA;sequence.cpp&#xA;set.cpp&#xA;Generating Code...&#xA;&#xA;C:\Program Files\boost\boost_1_82_0\tools\build\src\engine&amp;gt;dir *.exe&#xA; Volume in drive C has no label.&#xA; Volume Serial Number is 60A0-D31B&#xA;&#xA; Directory of C:\Program Files\boost\boost_1_82_0\tools\build\src\engine&#xA;&#xA;08/29/2025  07:38 AM           263,168 b2.exe&#xA;               1 File(s)        263,168 bytes&#xA;               0 Dir(s)  1,340,648,009,728 bytes free&#xA;&#xA;Generating Boost.Build configuration in project-config.jam for msvc...&#xA;&#xA;Bootstrapping is done. To build, run:&#xA;&#xA;    .\b2&#xA;&#xA;To adjust configuration, edit &#x27;project-config.jam&#x27;.&#xA;Further information:&#xA;&#xA;    - Command line help:&#xA;    .\b2 --help&#xA;&#xA;    - Getting started guide:&#xA;    http://boost.org/more/getting_started/windows.html&#xA;&#xA;    - Boost.Build documentation:&#xA;    http://www.boost.org/build/&#xA;&#xA;&#xA;Performing configuration checks&#xA;&#xA;    - default address-model    : 32-bit [1]&#xA;&#xA;    - default architecture     : arm [1]&#xA;&#xA;Building the Boost C&#x2B;&#x2B; Libraries.&#xA;    - compiler supports SSE2   : yes [2]&#xA;    - compiler supports SSE4.1 : yes [2]&#xA;    - has synchronization.lib  : no [2]&#xA;    - has std::atomic_ref      : yes [2]&#xA;    - has statx                : yes [2]&#xA;    - has BCrypt API           : no [2]&#xA;    - is Windows CE            : yes [2]&#xA;    - has init_priority attribute : yes [2]&#xA;    - has stat::st_blksize     : yes [2]&#xA;    - has stat::st_mtim        : yes [2]&#xA;    - has stat::st_mtimensec   : yes [2]&#xA;    - has stat::st_mtimespec   : yes [2]&#xA;    - has stat::st_birthtim    : yes [2]&#xA;    - has stat::st_birthtimensec : yes [2]&#xA;    - has stat::st_birthtimespec : yes [2]&#xA;    - has fdopendir(O_NOFOLLOW) : yes [2]&#xA;    - has POSIX *at APIs       : yes [2]&#xA;    - cxx11_auto_declarations  : yes [2]&#xA;    - cxx11_constexpr          : yes [2]&#xA;    - cxx11_defaulted_functions : yes [2]&#xA;    - cxx11_final              : yes [2]&#xA;    - cxx11_hdr_mutex          : yes [2]&#xA;    - cxx11_hdr_tuple          : yes [2]&#xA;    - cxx11_lambdas            : yes [2]&#xA;    - cxx11_noexcept           : yes [2]&#xA;    - cxx11_nullptr            : yes [2]&#xA;    - cxx11_rvalue_references  : yes [2]&#xA;    - cxx11_template_aliases   : yes [2]&#xA;    - cxx11_thread_local       : yes [2]&#xA;    - cxx11_variadic_templates : yes [2]&#xA;    - cxx11_auto_declarations  : yes [3]&#xA;    - cxx11_constexpr          : yes [3]&#xA;    - cxx11_defaulted_functions : yes [3]&#xA;    - cxx11_final              : yes [3]&#xA;    - cxx11_hdr_mutex          : yes [3]&#xA;    - cxx11_hdr_tuple          : yes [3]&#xA;    - cxx11_lambdas            : yes [3]&#xA;    - cxx11_noexcept           : yes [3]&#xA;    - cxx11_nullptr            : yes [3]&#xA;    - cxx11_rvalue_references  : yes [3]&#xA;    - cxx11_template_aliases   : yes [3]&#xA;    - cxx11_thread_local       : yes [3]&#xA;    - cxx11_variadic_templates : yes [3]&#xA;&#xA;    - has_icu builds           : no [2]&#xA;warning: Graph library does not contain MPI-based parallel components.&#xA;note: to enable them, add &amp;quot;using mpi ;&amp;quot; to your user-config.jam.&#xA;note: to suppress this message, pass &amp;quot;--without-graph_parallel&amp;quot; to bjam.&#xA;    - zlib                     : no [4]&#xA;    - bzip2                    : no [4]&#xA;    - lzma                     : no [4]&#xA;    - zstd                     : no [4]&#xA;    - has_lzma_cputhreads builds : no [2]&#xA;    - cxx11_decltype           : yes [2]&#xA;    - cxx11_basic_alignas      : yes [2]&#xA;    - iconv (libc)             : no [2]&#xA;    - iconv (separate)         : no [2]&#xA;    - icu                      : no [2]&#xA;    - cxx11_defaulted_moves    : yes [2]&#xA;    - cxx11_hdr_functional     : yes [2]&#xA;    - cxx11_hdr_type_traits    : yes [2]&#xA;    - cxx11_override           : yes [2]&#xA;    - cxx11_range_based_for    : yes [2]&#xA;    - cxx11_scoped_enums       : yes [2]&#xA;    - cxx11_smart_ptr          : yes [2]&#xA;    - cxx11_static_assert      : yes [2]&#xA;    - native atomic int32 supported : yes [2]&#xA;    - has message compiler     : no [2]&#xA;    - native syslog supported  : yes [2]&#xA;    - pthread supports robust mutexes : yes [2]&#xA;    - compiler supports SSSE3  : yes [2]&#xA;    - compiler supports AVX2   : yes [2]&#xA;    - gcc visibility           : yes [2]&#xA;    - sfinae_expr              : yes [2]&#xA;    - cxx11_unified_initialization_syntax : yes [2]&#xA;    - cxx11_hdr_initializer_list : yes [2]&#xA;    - cxx11_hdr_chrono         : yes [2]&#xA;    - cxx11_numeric_limits     : yes [2]&#xA;    - cxx11_hdr_array          : yes [2]&#xA;    - cxx11_hdr_atomic         : yes [2]&#xA;    - cxx11_allocator          : yes [2]&#xA;    - cxx11_explicit_conversion_operators : yes [2]&#xA;    - long double support      : yes [2]&#xA;warning: skipping optional Message Passing Interface (MPI) library.&#xA;note: to enable MPI support, add &amp;quot;using mpi ;&amp;quot; to user-config.jam.&#xA;note: to suppress this message, pass &amp;quot;--without-mpi&amp;quot; to bjam.&#xA;note: otherwise, you can safely ignore this message.&#xA;    - std::fstream is moveable and swappable : yes [2]&#xA;    - Has Large File Support   : no [2]&#xA;    - Has attribute init_priority : yes [2]&#xA;    - libbacktrace builds      : no [2]&#xA;    - libbacktrace builds      : no [3]&#xA;    - addr2line builds         : no [2]&#xA;    - addr2line builds         : no [3]&#xA;    - WinDbg builds            : no [2]&#xA;    - WinDbg builds            : no [3]&#xA;    - WinDbgCached builds      : no [2]&#xA;    - WinDbgCached builds      : no [3]&#xA;    - BOOST_COMP_GNUC &amp;gt;= 4.3.0 : yes [2]&#xA;    - cxx11_hdr_thread         : yes [2]&#xA;    - cxx11_hdr_regex          : yes [2]&#xA;    - compiler supports SSE2   : yes [5]&#xA;    - compiler supports SSE4.1 : yes [5]&#xA;    - has synchronization.lib  : no [5]&#xA;    - has std::atomic_ref      : yes [5]&#xA;    - has statx                : yes [5]&#xA;    - has BCrypt API           : no [5]&#xA;    - is Windows CE            : yes [5]&#xA;    - has init_priority attribute : yes [5]&#xA;    - has stat::st_blksize     : yes [5]&#xA;    - has stat::st_mtim        : yes [5]&#xA;    - has stat::st_mtimensec   : yes [5]&#xA;    - has stat::st_mtimespec   : yes [5]&#xA;    - has stat::st_birthtim    : yes [5]&#xA;    - has stat::st_birthtimensec : yes [5]&#xA;    - has stat::st_birthtimespec : yes [5]&#xA;    - has fdopendir(O_NOFOLLOW) : yes [5]&#xA;    - has POSIX *at APIs       : yes [5]&#xA;    - cxx11_auto_declarations  : yes [5]&#xA;    - cxx11_constexpr          : yes [5]&#xA;    - cxx11_defaulted_functions : yes [5]&#xA;    - cxx11_final              : yes [5]&#xA;    - cxx11_hdr_mutex          : yes [5]&#xA;    - cxx11_hdr_tuple          : yes [5]&#xA;    - cxx11_lambdas            : yes [5]&#xA;    - cxx11_noexcept           : yes [5]&#xA;    - cxx11_nullptr            : yes [5]&#xA;    - cxx11_rvalue_references  : yes [5]&#xA;    - cxx11_template_aliases   : yes [5]&#xA;    - cxx11_thread_local       : yes [5]&#xA;    - cxx11_variadic_templates : yes [5]&#xA;    - cxx11_auto_declarations  : yes [6]&#xA;    - cxx11_constexpr          : yes [6]&#xA;    - cxx11_defaulted_functions : yes [6]&#xA;    - cxx11_final              : yes [6]&#xA;    - cxx11_hdr_mutex          : yes [6]&#xA;    - cxx11_hdr_tuple          : yes [6]&#xA;    - cxx11_lambdas            : yes [6]&#xA;    - cxx11_noexcept           : yes [6]&#xA;    - cxx11_nullptr            : yes [6]&#xA;    - cxx11_rvalue_references  : yes [6]&#xA;    - cxx11_template_aliases   : yes [6]&#xA;    - cxx11_thread_local       : yes [6]&#xA;    - cxx11_variadic_templates : yes [6]&#xA;    - has_icu builds           : no [5]&#xA;    - zlib                     : no  (cached) [4]&#xA;    - bzip2                    : no  (cached) [4]&#xA;    - lzma                     : no  (cached) [4]&#xA;    - zstd                     : no  (cached) [4]&#xA;    - has_lzma_cputhreads builds : no [5]&#xA;    - cxx11_decltype           : yes [5]&#xA;    - cxx11_basic_alignas      : yes [5]&#xA;    - iconv (libc)             : no [5]&#xA;    - iconv (separate)         : no [5]&#xA;    - icu                      : no [5]&#xA;    - cxx11_defaulted_moves    : yes [5]&#xA;    - cxx11_hdr_functional     : yes [5]&#xA;    - cxx11_hdr_type_traits    : yes [5]&#xA;    - cxx11_override           : yes [5]&#xA;    - cxx11_range_based_for    : yes [5]&#xA;    - cxx11_scoped_enums       : yes [5]&#xA;    - cxx11_smart_ptr          : yes [5]&#xA;    - cxx11_static_assert      : yes [5]&#xA;    - native atomic int32 supported : yes [5]&#xA;    - has message compiler     : no [5]&#xA;    - native syslog supported  : yes [5]&#xA;    - pthread supports robust mutexes : yes [5]&#xA;    - compiler supports SSSE3  : yes [5]&#xA;    - compiler supports AVX2   : yes [5]&#xA;    - gcc visibility           : yes [5]&#xA;    - sfinae_expr              : yes [5]&#xA;    - cxx11_unified_initialization_syntax : yes [5]&#xA;    - cxx11_hdr_initializer_list : yes [5]&#xA;    - cxx11_hdr_chrono         : yes [5]&#xA;    - cxx11_numeric_limits     : yes [5]&#xA;    - cxx11_hdr_array          : yes [5]&#xA;    - cxx11_hdr_atomic         : yes [5]&#xA;    - cxx11_allocator          : yes [5]&#xA;    - cxx11_explicit_conversion_operators : yes [5]&#xA;    - long double support      : yes [5]&#xA;    - std::fstream is moveable and swappable : yes [5]&#xA;    - Has Large File Support   : no [5]&#xA;    - Has attribute init_priority : yes [5]&#xA;    - libbacktrace builds      : no [5]&#xA;    - libbacktrace builds      : no [6]&#xA;    - addr2line builds         : no [5]&#xA;    - addr2line builds         : no [6]&#xA;    - WinDbg builds            : no [5]&#xA;    - WinDbg builds            : no [6]&#xA;    - WinDbgCached builds      : no [5]&#xA;    - WinDbgCached builds      : no [6]&#xA;    - BOOST_COMP_GNUC &amp;gt;= 4.3.0 : yes [5]&#xA;    - cxx11_hdr_thread         : yes [5]&#xA;    - cxx11_hdr_regex          : yes [5]&#xA;[1] msvc-6.0&#xA;[2] msvc-6.0/debug/address-model-64/architecture-x86/link-static/python-3.13/runtime-link-static/threadapi-win32/threading-multi/visibility-hidden&#xA;[3] msvc-6.0/debug/address-model-64/architecture-x86/build-no/link-static/python-3.13/runtime-link-static/threadapi-win32/threading-multi/visibility-hidden&#xA;[4] link-static&#xA;[5] msvc-6.0/release/address-model-64/architecture-x86/link-static/python-3.13/runtime-link-static/threadapi-win32/threading-multi/visibility-hidden&#xA;[6] msvc-6.0/release/address-model-64/architecture-x86/build-no/link-static/python-3.13/runtime-link-static/threadapi-win32/threading-multi/visibility-hidden&#xA;&#xA;Component configuration:&#xA;&#xA;    - atomic                   : building&#xA;    - chrono                   : building&#xA;    - container                : building&#xA;    - context                  : building&#xA;    - contract                 : building&#xA;    - coroutine                : building&#xA;    - date_time                : building&#xA;    - exception                : building&#xA;    - fiber                    : building&#xA;    - filesystem               : building&#xA;    - graph                    : building&#xA;    - graph_parallel           : building&#xA;    - headers                  : building&#xA;    - iostreams                : building&#xA;    - json                     : building&#xA;    - locale                   : building&#xA;    - log                      : building&#xA;    - math                     : building&#xA;    - mpi                      : building&#xA;    - nowide                   : building&#xA;    - program_options          : building&#xA;    - python                   : building&#xA;    - random                   : building&#xA;    - regex                    : building&#xA;    - serialization            : building&#xA;    - stacktrace               : building&#xA;    - system                   : building&#xA;    - test                     : building&#xA;    - thread                   : building&#xA;    - timer                    : building&#xA;    - type_erasure             : building&#xA;    - url                      : building&#xA;    - wave                     : building&#xA;&#xA;...patience...&#xA;...patience...&#xA;...patience...&#xA;...patience...&#xA;...patience...&#xA;...patience...&#xA;&#xA;...found 17967 targets...&#xA;...updating 1296 targets...&#xA;boost-install.generate-cmake-config- bin.v2\libs\fiber\build\stage\boost_fiber-config.cmake&#xA;&#xA;boost-install.generate-cmake-config-version- bin.v2\libs\fiber\build\stage\boost_fiber-config-version.cmake&#xA;&#xA;boost-install.generate-cmake-config- bin.v2\libs\filesystem\build\stage\boost_filesystem-config.cmake&#xA;boost-install.generate-cmake-config-version- bin.v2\libs\filesystem\build\stage\boost_filesystem-config-version.cmake&#xA;&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_iserializer.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_oarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_xml_archive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_oserializer.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_pointer_iserializer.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_pointer_oserializer.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_text_iprimitive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_serializer_map.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_text_oprimitive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\binary_iarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\extended_type_info_typeid.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\extended_type_info.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\text_iarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\extended_type_info_no_rtti.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\text_oarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_oarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_iarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\binary_oarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_text_iarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\stl_port.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_text_oarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_binary_iarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_binary_oarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_xml_iarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_xml_oarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\xml_iarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\xml_grammar.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\xml_archive_exception.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\void_cast.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\xml_oarchive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_text_woprimitive.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\utf8_codecvt_facet.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\codecvt_null.obj&#xA;compile-c-c&#x2B;&#x2B; bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_text_wiprimitive.obj&#xA;msvc.archive bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\libboost_serialization-vc6-mt-s-x64-1_82.lib&#xA;The filename, directory name, or volume label syntax is incorrect.&#xA;&#xA;        if exist &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\libboost_serialization-vc6-mt-s-x64-1_82.lib&amp;quot; DEL &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\libboost_serialization-vc6-mt-s-x64-1_82.lib&amp;quot;&#xA;           /out:&amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\libboost_serialization-vc6-mt-s-x64-1_82.lib&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\archive_exception.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_archive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_iarchive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_iserializer.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_oarchive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_oserializer.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_pointer_iserializer.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_pointer_oserializer.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_serializer_map.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_text_iprimitive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_text_oprimitive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\basic_xml_archive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\binary_iarchive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\binary_oarchive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\extended_type_info.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\extended_type_info_typeid.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\extended_type_info_no_rtti.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_iarchive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\polymorphic_oarchive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\stl_port.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\text_iarchive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-static\runtime-link-static\threading-multi\text_oarchive.obj&amp;quot; &amp;quot;bin.v2\libs\serialization\build\msvc-6.0\release\address-model-64\architecture-x86\link-stati&#xA;&#xA;...&#xA;&#xA;...failed updating 76 targets...&#xA;...skipped 502 targets...&#xA;...updated 718 targets...&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;The issue I see is &lt;code&gt;The filename, directory name, or volume label syntax is incorrect.&lt;/code&gt;, but I&#x27;ve no idea where to go from here.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79725398</id>
        <re:rank scheme="https://stackoverflow.com">5</re:rank>
        <title type="text">std::format in a mex file</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>dogAwakeCat</name>
            <uri>https://stackoverflow.com/users/2833518</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79725398/stdformat-in-a-mex-file" />
        <published>2025-08-04T21:12:43Z</published>
        <updated>2025-08-05T09:31:59Z</updated>
        <summary type="html">
            &lt;p&gt;I&#x27;m trying to use &lt;code&gt;std::format&lt;/code&gt; in a mex file but get an &lt;code&gt;unresolved external symbol&lt;/code&gt; error when I use &lt;code&gt;std&lt;/code&gt; as a module.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;// test_mex.cpp&#xA;#include &amp;quot;mex.h&amp;quot;&#xA;&#xA;import std;&#xA;&#xA;void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {&#xA;    std::string temp{std::format(&amp;quot;{:.2f}&amp;quot;, 21.234)};&#xA;    mexPrintf(&amp;quot;%s\n&amp;quot;, temp.c_str());&#xA;    plhs[0] = mxCreateDoubleMatrix(10, 10, mxREAL);&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;compiled with&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;!cl /std:c&#x2B;&#x2B;latest /O2 /Oi /EHsc /MD /nologo /c -I&amp;quot;C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\ucrt&amp;quot; -I&amp;quot;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\include&amp;quot; &amp;quot;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\modules\std.ixx&amp;quot;&#xA;mex -O COMPFLAGS=&amp;quot;$COMPFLAGS -std:c&#x2B;&#x2B;latest /O2 /Oi /EHsc /Wall /MD&amp;quot; test_mex.cpp;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;gives this error&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Error using mex&#xA;   Creating library test_mex.lib and object test_mex.exp&#xA;test_mex.obj : error LNK2019: unresolved external symbol &amp;quot;public: static int const std::_General_precision_tables_2&amp;lt;double&amp;gt;::_Max_P&amp;quot; (?_Max_P@?$_General_precision_tables_2@N@std@@2HB) referenced in function &amp;quot;struct std::to_chars_result __cdecl std::_Floating_to_chars_general_precision&amp;lt;double&amp;gt;(char *,char * const,double,int)&amp;quot; (??$_Floating_to_chars_general_precision@N@std@@YA?AUto_chars_result@0@PEADQEADNH@Z)&#xA;test_mex.obj : error LNK2019: unresolved external symbol &amp;quot;public: static int const std::_General_precision_tables_2&amp;lt;float&amp;gt;::_Max_P&amp;quot; (?_Max_P@?$_General_precision_tables_2@M@std@@2HB) referenced in function &amp;quot;struct std::to_chars_result __cdecl std::_Floating_to_chars_general_precision&amp;lt;float&amp;gt;(char *,char * const,float,int)&amp;quot; (??$_Floating_to_chars_general_precision@M@std@@YA?AUto_chars_result@0@PEADQEADMH@Z)&#xA;test_mex.mexw64 : fatal error LNK1120: 2 unresolved externals&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;If you don&#x27;t use &lt;code&gt;std::format&lt;/code&gt; and instead just specify the string, for example &lt;code&gt;std::string temp{&amp;quot;test&amp;quot;}&lt;/code&gt; it compiles and runs.&#xA;Furthermore, instead of using a module if you replace &lt;code&gt;import std;&lt;/code&gt; with &lt;code&gt;#include &amp;lt;format&amp;gt;&lt;/code&gt; again it compiles and runs.&#xA;I&#x27;m using the Microsoft Visual C&#x2B;&#x2B; 2022 compiler.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79699706</id>
        <re:rank scheme="https://stackoverflow.com">6</re:rank>
        <title type="text">MATLAB 2025a vs. Fortran MEX files with internal subroutines</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="fortran" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Nuno</name>
            <uri>https://stackoverflow.com/users/11848912</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79699706/matlab-2025a-vs-fortran-mex-files-with-internal-subroutines" />
        <published>2025-07-13T02:27:36Z</published>
        <updated>2025-07-27T19:25:20Z</updated>
        <summary type="html">
            &lt;p&gt;This very likely is a bug in MATLAB R2025a on Ubuntu, and a bug report has been submitted to MathWorks. While they investigate, I would like to know: &lt;strong&gt;is there anything wrong with the Fortran MEX file (see below)?&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;In brief, I find that if the Fortran MEX file contains an internal subroutine used as an actual argument to another subroutine (which is allowed in Fortran 2008&#x2B;, ref. &lt;strong&gt;Note 12.18 on page 290 of WD 1539-1 J3/10-007r1, F2008 Working Document&lt;/strong&gt; and the discussions on &lt;a href=&quot;https://fortran-lang.discourse.group/t/thread-safe-implementation-of-a-parametrized-objective-function-without-using-internal-subroutines/9919&quot; rel=&quot;noreferrer&quot;&gt;Fortran Discourse&lt;/a&gt;), then MATLAB R2025a segfaults under Ubuntu 22.04 and 24.04. The problem did not occur until I upgraded to R2025a. Windows and macOS have not been tested.&lt;/p&gt;&#xA;&lt;p&gt;To reproduce the issue, execute the following in the MATLAB command window:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-matlab prettyprint-override&quot;&gt;&lt;code&gt;mex(&#x27;-v&#x27;, &#x27;-g&#x27;, &#x27;optimize.F90&#x27;); optimize();&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;, where optimize.F90 is the following Fortran MEX file:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;!----------------------------------------------------------------------!&#xA;! optimize.F90: a Fortran MEX file demonstrating a bug of MATLAB R2025a.&#xA;! To test, do the following in the MATLAB command window:&#xA;!&#xA;! mex -g optimize.F90; optimize(); &#xA;!&#xA;! In MATLAB R2025a under Ubuntu 24.04 with gfortran 13.3.0, this causes&#xA;! a segmentation fault. In earlier versions of MATLAB, it works fine.&#xA;!----------------------------------------------------------------------!&#xA;&#xA;&#xA;#include &amp;quot;fintrf.h&amp;quot;&#xA;&#xA;&#xA;!----------------------------------------------------------------------!&#xA;! solver_mod: a module defining an optimization solver&#xA;module solver_mod&#xA;&#xA;implicit none&#xA;private&#xA;public:: solver&#xA;&#xA;abstract interface&#xA;! OBJ: an abstract interface for an objective function&#xA;subroutine OBJ(x, y)&#xA;real, intent(in):: x&#xA;real, intent(out):: y&#xA;end subroutine OBJ&#xA;end interface&#xA;&#xA;contains&#xA;&#xA;! solver: a doing-nothing solver for demonstration. Its interface CANNOT&#xA;! be changed, e.g., to accept a hyper-parameter for `objective`.&#xA;subroutine solver(objective)&#xA;procedure(OBJ):: objective&#xA;real f&#xA;! The following line causes a segmentation fault in MATLAB R2025a when&#xA;! `solver(objective)` is called from `mexFunction`.&#xA;call objective(0.0, f)&#xA;end subroutine solver&#xA;&#xA;end module solver_mod&#xA;!----------------------------------------------------------------------!&#xA;&#xA;&#xA;!----------------------------------------------------------------------!&#xA;! mexFunction: the entry point for the MEX function `optimize`, which&#xA;! calls `solver` from solver_mod to optimize `objective`.&#xA;subroutine mexFunction(nlhs, plhs, nrhs, prhs)&#xA;&#xA;use solver_mod, only: solver&#xA;implicit none&#xA;integer nlhs, nrhs&#xA;mwPointer plhs(*), prhs(*)&#xA;&#xA;! `hyper_parameter`: a hyper-parameter for the objective function. In&#xA;! real applications, this parameter is not a constant, but a variable&#xA;! depending on the input arguments of `mexFunction`.&#xA;real hyper_parameter, f&#xA;hyper_parameter = 42&#xA;&#xA;! The following line works fine.&#xA;call objective(0.0, f)&#xA;&#xA;! The following line causes a segmentation fault in MATLAB R2025a.&#xA;call solver(objective)&#xA;&#xA;contains&#xA;&#xA;!--------------------------------------------------------------!&#xA;! objective: a simple objective function for demonstration.&#xA;! F2008 allows to pass internal procedures as actual arguments.&#xA;! See Note 12.18 on page 290 of WD 1539-1 J3/10-007r1 (F2008&#xA;! Working Document). We implement `objective` internally so that&#xA;! `hyper_parameter` is visible to it. Do NOT pass this parameter&#xA;! by a module variable, which is thread-unsafe.&#xA;subroutine objective(x, y)&#xA;real, intent(in):: x&#xA;real, intent(out):: y&#xA;y = (x&#x2B;hyper_parameter)**2&#xA;end subroutine objective&#xA;!--------------------------------------------------------------!&#xA;&#xA;end subroutine mexFunction&#xA;!----------------------------------------------------------------------!&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;The MATLAB output, including crash log, is:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-none prettyprint-override&quot;&gt;&lt;code&gt;Verbose mode is on.&#xA;No MEX options file identified; looking for an implicit selection.&#xA;... Looking for compiler &#x27;gfortran&#x27; ...&#xA;... Executing command &#x27;which gfortran&#x27; ...Yes (&#x27;/usr/bin/gfortran&#x27;).&#xA;... Looking for folder &#x27;/usr/bin&#x27; ...Yes.&#xA;... Executing command &#x27;which gfortran&#x27; ...Yes (&#x27;/usr/bin/gfortran&#x27;).&#xA;... Executing command &#x27;which gfortran&#x27; ...Yes (&#x27;/usr/bin/gfortran&#x27;).&#xA;... Executing command &#x27;gfortran -print-file-name=libgfortran.so&#x27; ...Yes (&#x27;/usr/lib/gcc/x86_64-linux-gnu/10/libgfortran.so&#x27;).&#xA;... Looking for folder &#x27;/usr/lib/x86_64-linux-gnu&#x27; ...Yes.&#xA;... Executing command &#x27;which gfortran&#x27; ...Yes (&#x27;/usr/bin/gfortran&#x27;).&#xA;... Executing command &#x27;gfortran -dumpversion | awk -F &#x27;.&#x27; &#x27;$1&amp;gt;=6&#x27; &#x27; ...Yes (&#x27;10&#x27;).&#xA;Found installed compiler &#x27;gfortran&#x27;.&#xA;... Looking for compiler &#x27;gfortran6-&#x27; ...&#xA;... Executing command &#x27;which gfortran&#x27; ...Yes (&#x27;/usr/bin/gfortran&#x27;).&#xA;... Looking for folder &#x27;/usr/bin&#x27; ...Yes.&#xA;... Executing command &#x27;which gfortran&#x27; ...Yes (&#x27;/usr/bin/gfortran&#x27;).&#xA;... Executing command &#x27;gfortran -print-file-name=libgfortran.so&#x27; ...Yes (&#x27;/usr/lib/gcc/x86_64-linux-gnu/10/libgfortran.so&#x27;).&#xA;... Looking for folder &#x27;/usr/lib/x86_64-linux-gnu&#x27; ...Yes.&#xA;... Executing command &#x27;which gfortran&#x27; ...Yes (&#x27;/usr/bin/gfortran&#x27;).&#xA;... Executing command &#x27;gfortran -print-file-name=libgfortranbegin.a&#x27; ...Yes (&#x27;libgfortranbegin.a&#x27;).&#xA;... Looking for folder &#x27;libgfortranbegin.a&#x27; ...No.&#xA;Did not find installed compiler &#x27;gfortran6-&#x27;.&#xA;Options file details&#xA;-------------------------------------------------------------------&#xA;    Compiler location: /usr/bin&#xA;    Options file: /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/mexopts/gfortran6.xml&#xA;    CMDLINE2 : /usr/bin/gfortran -pthread -shared -g -Wl,--version-script,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/lib/glnxa64/fortran_exportsmexfileversion.map /tmp/mex_1037477951999_9281/optimize.o /tmp/mex_1037477951999_9281/fortran_mexapi_version.o   -Wl,-rpath-link,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64  -L&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig&amp;quot; -L&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64&amp;quot; &amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig/libstdc&#x2B;&#x2B;.so.6&amp;quot; -lmx -lmex -lmat -lm -L&amp;quot;/usr/lib/x86_64-linux-gnu&amp;quot; -lgfortran -z noexecstack -o optimize.mexa64&#xA;    FC : /usr/bin/gfortran&#xA;    DEFINES : -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD&#xA;    FFLAGS : -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fallow-argument-mismatch -fdefault-integer-8&#xA;    INCLUDE : -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/include&amp;quot; -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/simulink/include&amp;quot;&#xA;    FOPTIMFLAGS : -O2&#xA;    FDEBUGFLAGS : -g&#xA;    LDF : /usr/bin/gfortran&#xA;    LDFLAGS : -pthread&#xA;    LDTYPE : -shared&#xA;    LINKEXPORT : -Wl,--version-script,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/lib/glnxa64/fexport.map&#xA;    LINKEXPORTVER : -Wl,--version-script,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/lib/glnxa64/fortran_exportsmexfileversion.map&#xA;    MWCPPLIB : &amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig/libstdc&#x2B;&#x2B;.so.6&amp;quot;&#xA;    LINKLIBS : -Wl,-rpath-link,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64  -L&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig&amp;quot; -L&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64&amp;quot; &amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig/libstdc&#x2B;&#x2B;.so.6&amp;quot; -lmx -lmex -lmat -lm -L&amp;quot;/usr/lib/x86_64-linux-gnu&amp;quot; -lgfortran&#xA;    LDOPTIMFLAGS : -O&#xA;    LDDEBUGFLAGS : -g&#xA;    MW_GLIBC_SHIM : $MW_GLIBC_SHIM&#xA;    LDEXECSTACK : -z noexecstack&#xA;    OBJEXT : .o&#xA;    LDEXT : .mexa64&#xA;    SETENV : FC=&amp;quot;/usr/bin/gfortran&amp;quot;&#xA;                FFLAGS=&amp;quot;-fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fallow-argument-mismatch -fdefault-integer-8 -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD   &amp;quot;&#xA;                FOPTIMFLAGS=&amp;quot;-O2&amp;quot;&#xA;                FDEBUGFLAGS=&amp;quot;-g&amp;quot;&#xA;                LD=&amp;quot;/usr/bin/gfortran&amp;quot;&#xA;                LDFLAGS=&amp;quot;-pthread -shared -Wl,-rpath-link,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64  -L&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig&amp;quot; -L&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64&amp;quot; &amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig/libstdc&#x2B;&#x2B;.so.6&amp;quot; -lmx -lmex -lmat -lm -L&amp;quot;/usr/lib/x86_64-linux-gnu&amp;quot; -lgfortran -Wl,--version-script,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/lib/glnxa64/fexport.map&amp;quot;&#xA;                LDDEBUGFLAGS=&amp;quot;-g&amp;quot;&#xA;    GFORTRAN_INSTALLDIR : /usr/bin&#xA;    GFORTRAN_EXECUTABLE : /usr/bin/gfortran&#xA;    GFORTRAN_LIBDIR : /usr/lib/x86_64-linux-gnu&#xA;    GFORTRAN_VERSION : 10&#xA;    MATLABROOT : /opt/hostedtoolcache/MATLAB/2025.1.999/x64&#xA;    ARCH : glnxa64&#xA;    SRC : &amp;quot;/home/runner/work/test_matlab/test_matlab/segfault_2025a/optimize.F90&amp;quot;;&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/version/fortran_mexapi_version.F&amp;quot;&#xA;    OBJ : /tmp/mex_1037477951999_9281/optimize.o;/tmp/mex_1037477951999_9281/fortran_mexapi_version.o&#xA;    OBJS : /tmp/mex_1037477951999_9281/optimize.o /tmp/mex_1037477951999_9281/fortran_mexapi_version.o&#xA;    SRCROOT : /home/runner/work/test_matlab/test_matlab/segfault_2025a/optimize&#xA;    DEF : /tmp/mex_1037477951999_9281/optimize.def&#xA;    EXP : &amp;quot;optimize.exp&amp;quot;&#xA;    LIB : &amp;quot;optimize.lib&amp;quot;&#xA;    EXE : optimize.mexa64&#xA;    ILK : &amp;quot;optimize.ilk&amp;quot;&#xA;    TEMPNAME : optimize&#xA;    EXEDIR :&#xA;    EXENAME : optimize&#xA;    MANIFEST : &amp;quot;optimize.mexa64.manifest&amp;quot;&#xA;    OPTIM : -g&#xA;    LINKOPTIM : -g&#xA;    CMDLINE1_0 : /usr/bin/gfortran -c -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD    -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/include&amp;quot; -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/simulink/include&amp;quot; -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fallow-argument-mismatch -fdefault-integer-8 -g &amp;quot;/home/runner/work/test_matlab/test_matlab/segfault_2025a/optimize.F90&amp;quot; -o /tmp/mex_1037477951999_9281/optimize.o&#xA;    CMDLINE1_1 : /usr/bin/gfortran -c -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD    -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/include&amp;quot; -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/simulink/include&amp;quot; -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fallow-argument-mismatch -fdefault-integer-8 -g &amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/version/fortran_mexapi_version.F&amp;quot; -o /tmp/mex_1037477951999_9281/fortran_mexapi_version.o&#xA;-------------------------------------------------------------------&#xA;Building with &#x27;gfortran&#x27;.&#xA;/usr/bin/gfortran -c -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD    -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/include&amp;quot; -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/simulink/include&amp;quot; -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fallow-argument-mismatch -fdefault-integer-8 -g &amp;quot;/home/runner/work/test_matlab/test_matlab/segfault_2025a/optimize.F90&amp;quot; -o /tmp/mex_1037477951999_9281/optimize.o&#xA;f951: Warning: Nonexistent include directory /opt/hostedtoolcache/MATLAB/2025.1.999/x64/simulink/include [-Wmissing-include-dirs]&#xA;&#xA;/usr/bin/gfortran -c -DMATLAB_DEFAULT_RELEASE=R2017b  -DUSE_MEX_CMD    -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/include&amp;quot; -I&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/simulink/include&amp;quot; -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer -fallow-argument-mismatch -fdefault-integer-8 -g &amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/version/fortran_mexapi_version.F&amp;quot; -o /tmp/mex_1037477951999_9281/fortran_mexapi_version.o&#xA;f951: Warning: Nonexistent include directory /opt/hostedtoolcache/MATLAB/2025.1.999/x64/simulink/include [-Wmissing-include-dirs]&#xA;&#xA;{&#xA;  &amp;quot;bundle.symbolic_name&amp;quot; : &amp;quot;be8e5504-e0f7-492e-b08a-03d11430c8f2&amp;quot;,&#xA;  &amp;quot;mw&amp;quot; :&#xA;  {&#xA;      &amp;quot;mex&amp;quot; :&#xA;      {&#xA;          &amp;quot;apiVersion&amp;quot; : 0,&#xA;          &amp;quot;release&amp;quot; : &amp;quot;R2025a&amp;quot;,&#xA;          &amp;quot;threadpoolSafe&amp;quot; : 0&#xA;      }&#xA;  }&#xA;}&#xA;&#xA;&#xA;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/usResourceCompiler3 --manifest-add /tmp/mex_1037477951999_9281//mw_mex_tempmex_manifest.json --bundle-name mexVersioning --out-file /tmp/mex_1037477951999_9281//mw_mex_tempmex_bundle.zip&#xA;&#xA;/usr/bin/gfortran -pthread -shared -g -Wl,--version-script,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/extern/lib/glnxa64/fortran_exportsmexfileversion.map /tmp/mex_1037477951999_9281/optimize.o /tmp/mex_1037477951999_9281/fortran_mexapi_version.o   -Wl,-rpath-link,/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64  -L&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig&amp;quot; -L&amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64&amp;quot; &amp;quot;/opt/hostedtoolcache/MATLAB/2025.1.999/x64/sys/os/glnxa64/orig/libstdc&#x2B;&#x2B;.so.6&amp;quot; -lmx -lmex -lmat -lm -L&amp;quot;/usr/lib/x86_64-linux-gnu&amp;quot; -lgfortran -z noexecstack -o optimize.mexa64 -Wl,--format=binary -Wl,/tmp/mex_1037477951999_9281//mw_mex_tempmex_bundle.zip -Wl,--format=default&#xA;MEX completed successfully.&#xA;&#xA;--------------------------------------------------------------------------------&#xA;          Segmentation violation detected at 2025-07-11 16:58:56 &#x2B;0000&#xA;--------------------------------------------------------------------------------&#xA;&#xA;Configuration:&#xA;  Crash Decoding           : Disabled - No sandbox or build area path&#xA;  Crash Mode               : continue (default)&#xA;  Default Encoding         : UTF-8&#xA;  Deployed                 : false&#xA;  GNU C Library            : 2.39 stable&#xA;  Graphics Driver          : Uninitialized software&#xA;  Graphics card 1          : 0x1414 ( 0x1414 ) 0x5353 Version 0.0.0.0 (0-0-0)&#xA;  MATLAB Architecture      : glnxa64&#xA;  MATLAB Entitlement ID    : 11373431&#xA;  MATLAB Root              : /opt/hostedtoolcache/MATLAB/2025.1.999/x64&#xA;  MATLAB Version           : 25.1.0.2943329 (R2025a)&#xA;  OpenGL                   : software&#xA;  Operating System         : Ubuntu 24.04.2 LTS&#xA;  Process ID               : 9281&#xA;  Processor ID             : x86 Family 25 Model 1 Stepping 1, AuthenticAMD&#xA;  Session Key              : 40xq29adxbsbdw85fpki4ykis&#xA;  Window System            : No active display&#xA;&#xA;Fault Count: 1&#xA;&#xA;&#xA;Abnormal termination:&#xA;Segmentation violation&#xA;&#xA;Current Thread: &#x27;MCR 0 interpret&#x27; id 140534882498240&#xA;&#xA;Register State (from fault):&#xA;  RAX = 00007fd0d3bf873c  RBX = 00007fd0d3bf8910&#xA;  RCX = 00007fd0ca49f11c  RDX = 00007fd0d3bf8774&#xA;  RSP = 00007fd0d3bf8718  RBP = 00007fd0d3bf8740&#xA;  RSI = 00007fd0d3bf873c  RDI = 00007fd0ca4a0000&#xA;&#xA;   R8 = 00007fd0d3bf8a00   R9 = 0000000000000000&#xA;  R10 = 00007fd0d3bf8770  R11 = 0000000000000246&#xA;  R12 = 00007fd0d3bf8900  R13 = 0000000000000000&#xA;  R14 = 00007fd0d3bf8a00  R15 = 00007fd0d3bf8970&#xA;&#xA;  RIP = 00007fd0d3bf8774  EFL = 0000000000010202&#xA;&#xA;   CS = 0033   FS = 0000   GS = 0000&#xA;&#xA;Stack Trace (from fault):&#xA;[  0] 0x00007fd0d3bf8774                                   &amp;lt;unknown-module&amp;gt;&#x2B;00000000&#xA;[  1] 0x00007fd0ca49f1d2 /home/runner/work/test_matlab/test_matlab/segfault_2025a/optimize.mexa64&#x2B;00004562 mexfunction_&#x2B;00000132&#xA;[  2] 0x00007fd12df672d2 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmex.so&#x2B;00975570&#xA;[  3] 0x00007fd12df67358 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmex.so&#x2B;00975704&#xA;[  4] 0x00007fd12df67015 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmex.so&#x2B;00974869&#xA;[  5] 0x00007fd12df681a4 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmex.so&#x2B;00979364&#xA;[  6] 0x00007fd12df524fa /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmex.so&#x2B;00890106&#xA;[  7] 0x00007fd12e77a4fe /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_dispatcher.so&#x2B;01549566 _ZN8Mfh_file20dispatch_file_commonEMS_FviPP11mxArray_tagiS2_EiS2_iS2_&#x2B;00000142&#xA;[  8] 0x00007fd12e77bc9a /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_dispatcher.so&#x2B;01555610&#xA;[  9] 0x00007fd12e77c04e /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_dispatcher.so&#x2B;01556558 _ZN8Mfh_file8dispatchEiPSt10unique_ptrI11mxArray_tagN6matrix6detail17mxDestroy_deleterEEiPPS1_&#x2B;00000030&#xA;[ 10] 0x00007fd12d8757a7 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;02578343&#xA;[ 11] 0x00007fd120762fd0 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;11939792&#xA;[ 12] 0x00007fd1207566da /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;11888346&#xA;[ 13] 0x00007fd1206d9fd1 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;11378641&#xA;[ 14] 0x00007fd1203c86cb /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08160971&#xA;[ 15] 0x00007fd1203ca41c /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08168476&#xA;[ 16] 0x00007fd1203c71ff /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08155647&#xA;[ 17] 0x00007fd1203d8c55 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08227925&#xA;[ 18] 0x00007fd1203d94a9 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08230057&#xA;[ 19] 0x00007fd1203c6ff4 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08155124&#xA;[ 20] 0x00007fd1203c70ff /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08155391&#xA;[ 21] 0x00007fd12053583b /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;09656379&#xA;[ 22] 0x00007fd12053a7e4 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;09676772&#xA;[ 23] 0x00007fd12da0b9b4 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;04241844&#xA;[ 24] 0x00007fd12d863683 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;02504323&#xA;[ 25] 0x00007fd12d865c05 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;02513925&#xA;[ 26] 0x00007fd12e77a4fe /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_dispatcher.so&#x2B;01549566 _ZN8Mfh_file20dispatch_file_commonEMS_FviPP11mxArray_tagiS2_EiS2_iS2_&#x2B;00000142&#xA;[ 27] 0x00007fd12e77bc9a /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_dispatcher.so&#x2B;01555610&#xA;[ 28] 0x00007fd12e77c04e /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_dispatcher.so&#x2B;01556558 _ZN8Mfh_file8dispatchEiPSt10unique_ptrI11mxArray_tagN6matrix6detail17mxDestroy_deleterEEiPPS1_&#x2B;00000030&#xA;[ 29] 0x00007fd12d8757a7 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;02578343&#xA;[ 30] 0x00007fd120762fd0 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;11939792&#xA;[ 31] 0x00007fd1207566da /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;11888346&#xA;[ 32] 0x00007fd1206d9fd1 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;11378641&#xA;[ 33] 0x00007fd1203c86cb /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08160971&#xA;[ 34] 0x00007fd1203ca41c /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08168476&#xA;[ 35] 0x00007fd1203c71ff /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08155647&#xA;[ 36] 0x00007fd1203d8c55 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08227925&#xA;[ 37] 0x00007fd1203d94a9 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08230057&#xA;[ 38] 0x00007fd1203c6ff4 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08155124&#xA;[ 39] 0x00007fd1203c70ff /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;08155391&#xA;[ 40] 0x00007fd12053583b /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;09656379&#xA;[ 41] 0x00007fd12053a7e4 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_lxe.so&#x2B;09676772&#xA;[ 42] 0x00007fd12da0b9b4 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;04241844&#xA;[ 43] 0x00007fd12d8df311 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;03011345&#xA;[ 44] 0x00007fd12d8df50d /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;03011853&#xA;[ 45] 0x00007fd12d9ac31d /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;03851037&#xA;[ 46] 0x00007fd12d9ac54e /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwlxemainservices.so&#x2B;03851598&#xA;[ 47] 0x00007fd12e356f0f /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwm_interpreter.so&#x2B;01404687 _Z51inEvalCmdWithLocalReturnInDesiredWSAndPublishEventsRKNSt7__cxx1112basic_stringIDsSt11char_traitsIDsESaIDsEEEPibbP15inWorkSpace_tagN9MathWorks3lxe10EvalSourceE&#x2B;00000063&#xA;[ 48] 0x00007fd12eadeecc /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00974540 _ZNK3iqm18InternalEvalPlugin24inEvalCmdWithLocalReturnERKNSt7__cxx1112basic_stringIDsSt11char_traitsIDsESaIDsEEEP15inWorkSpace_tag&#x2B;00000108&#xA;[ 49] 0x00007fd12eadff35 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00978741 _ZN3iqm18InternalEvalPlugin7executeEP15inWorkSpace_tag&#x2B;00000469&#xA;[ 50] 0x00007fd12eac7716 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00878358&#xA;[ 51] 0x00007fd12ea8c228 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00635432&#xA;[ 52] 0x00007fd12ea8c571 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00636273&#xA;[ 53] 0x00007fd13bf4954e /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmlutil.so&#x2B;09737550 _ZNK14cmddistributor16IIPRunNowMessage7deliverERKN10foundation7msg_svc8exchange7RoutingE&#x2B;00000030&#xA;[ 54] 0x00007fd13d3629cc /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwms.so&#x2B;03549644 _ZN10foundation7msg_svc8exchange12MessageQueue7deliverERKN7mwboost10shared_ptrIKNS1_8EnvelopeEEE&#x2B;00000252&#xA;[ 55] 0x00007fd13d36467f /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwms.so&#x2B;03556991&#xA;[ 56] 0x00007fd13d349f6d /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwms.so&#x2B;03448685&#xA;[ 57] 0x00007fd13d34ee74 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwms.so&#x2B;03468916&#xA;[ 58] 0x00007fd13d34911f /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwms.so&#x2B;03445023&#xA;[ 59] 0x00007fd13be86452 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmlutil.so&#x2B;08938578&#xA;[ 60] 0x00007fd13be8d3d1 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmlutil.so&#x2B;08967121&#xA;[ 61] 0x00007fd13ed2531e /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwrcf_framework.so&#x2B;00312094 _ZN7mwboost6detail17shared_state_base13wait_internalERNS_11unique_lockINS_5mutexEEEb&#x2B;00000222&#xA;[ 62] 0x00007fd12e93c612 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmcr.so&#x2B;00706066 _ZN7mwboost6futureIvE3getEv&#x2B;00000098&#xA;[ 63] 0x00007fd12e92ac0b /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmcr.so&#x2B;00633867&#xA;[ 64] 0x00007fd13e347897 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmvm.so&#x2B;03438743 _ZN14cmddistributor15PackagedTaskIIP10invokeFuncIN7mwboost8functionIFvvEEEEENS2_10shared_ptrINS2_6futureIDTclfp_EEEEEERKT_&#x2B;00000071&#xA;[ 65] 0x00007fd13e347bc8 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmvm.so&#x2B;03439560 _ZNSt17_Function_handlerIFN7mwboost3anyEvEZN14cmddistributor15PackagedTaskIIP10createFuncINS0_8functionIFvvEEEEESt8functionIS2_ET_EUlvE_E9_M_invokeERKSt9_Any_data&#x2B;00000024&#xA;[ 66] 0x00007fd12eae9e3b /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;01019451 _ZN3iqm18PackagedTaskPlugin7executeEP15inWorkSpace_tag&#x2B;00000091&#xA;[ 67] 0x00007fd12eac7716 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00878358&#xA;[ 68] 0x00007fd12ea934f9 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00664825&#xA;[ 69] 0x00007fd12e4ac0e9 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwbridge.so&#x2B;00499945&#xA;[ 70] 0x00007fd12e4ac5c3 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwbridge.so&#x2B;00501187&#xA;[ 71] 0x00007fd12e4c84ea /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwbridge.so&#x2B;00615658 _Z22mnGetCommandLineBufferbRbN7mwboost8optionalIKP15inWorkSpace_tagEEbRKNS0_9function2IN6mlutil14cmddistributor17inExecutionStatusERKNSt7__cxx1112basic_stringIDsSt11char_traitsIDsESaIDsEEES4_EE&#x2B;00000218&#xA;[ 72] 0x00007fd12e4c8881 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwbridge.so&#x2B;00616577 _Z8mnParserv&#x2B;00000513&#xA;[ 73] 0x00007fd12e964350 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmcr.so&#x2B;00869200&#xA;[ 74] 0x00007fd13e347897 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmvm.so&#x2B;03438743 _ZN14cmddistributor15PackagedTaskIIP10invokeFuncIN7mwboost8functionIFvvEEEEENS2_10shared_ptrINS2_6futureIDTclfp_EEEEEERKT_&#x2B;00000071&#xA;[ 75] 0x00007fd13e347bc8 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmvm.so&#x2B;03439560 _ZNSt17_Function_handlerIFN7mwboost3anyEvEZN14cmddistributor15PackagedTaskIIP10createFuncINS0_8functionIFvvEEEEESt8functionIS2_ET_EUlvE_E9_M_invokeERKSt9_Any_data&#x2B;00000024&#xA;[ 76] 0x00007fd12eae9e3b /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;01019451 _ZN3iqm18PackagedTaskPlugin7executeEP15inWorkSpace_tag&#x2B;00000091&#xA;[ 77] 0x00007fd12eac7716 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00878358&#xA;[ 78] 0x00007fd12ea918b2 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00657586&#xA;[ 79] 0x00007fd12ea9267d /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00661117&#xA;[ 80] 0x00007fd12ea92994 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwiqm.so&#x2B;00661908&#xA;[ 81] 0x00007fd12e94bd07 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmcr.so&#x2B;00769287&#xA;[ 82] 0x00007fd12e94c3ce /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmcr.so&#x2B;00771022&#xA;[ 83] 0x00007fd12e94c69d /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwmcr.so&#x2B;00771741&#xA;[ 84] 0x00007fd13ea0db17 /opt/hostedtoolcache/MATLAB/2025.1.999/x64/bin/glnxa64/libmwboost_thread.so.1.81.0&#x2B;00043799&#xA;[ 85] 0x00007fd13f09caa4                    /lib/x86_64-linux-gnu/libc.so.6&#x2B;00641700&#xA;[ 86] 0x00007fd13f129c3c                    /lib/x86_64-linux-gnu/libc.so.6&#x2B;01219644&#xA;&#xA;&#xA;This error was detected while a MEX-file was running. If the MEX-file&#xA;is not an official MathWorks function, please examine its source code&#xA;for errors. Please consult the External Interfaces Guide for information&#xA;on debugging MEX-files.&#xA;&#xA;&#xA;MATLAB is exiting because of fatal error&#xA;exit status 255&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;The same ran without error for me on earlier versions of MATLAB.&lt;/p&gt;&#xA;&lt;p&gt;For more details, see&#xA;&lt;a href=&quot;https://github.com/zequipe/test_matlab/tree/master/segfault_2025a&quot; rel=&quot;noreferrer&quot;&gt;https://github.com/zequipe/test_matlab/tree/master/segfault_2025a&lt;/a&gt; .&lt;/p&gt;&#xA;&lt;p&gt;See &lt;a href=&quot;https://github.com/zequipe/test_matlab/actions/runs/16096537922&quot; rel=&quot;noreferrer&quot;&gt;https://github.com/zequipe/test_matlab/actions/runs/16096537922&lt;/a&gt; for a GitHub Actions run that reproduces the issue.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79532477</id>
        <re:rank scheme="https://stackoverflow.com">3</re:rank>
        <title type="text">Converting MATLAB FFT-based Delay Function to CUDA MEX</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="cuda" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>TheCubeDude</name>
            <uri>https://stackoverflow.com/users/12590111</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79532477/converting-matlab-fft-based-delay-function-to-cuda-mex" />
        <published>2025-03-25T01:04:37Z</published>
        <updated>2025-03-25T01:04:37Z</updated>
        <summary type="html">
            &lt;p&gt;I am trying to convert a matlab function to its equivalent CUDA MEX function. Below is the matlab function:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;function [ShiftData_time] = simpledelayupdated_mat(RData, delay, fs)&#xA;    nfft = size(RData,1);&#xA;    binStart = floor(nfft/2);&#xA;    fftBin = (2*pi*ifftshift(((0:nfft-1)-binStart).&#x27;))/nfft; &#xA;    fftBin = fftBin&#x27;;&#xA;    &#xA;    RFFT = fft(RData, nfft, 1);&#xA;    ShiftData = RFFT .* ((exp(-1i * delay * fftBin)).&#x27;); &#xA;    ShiftData_time = ifft(ShiftData, nfft, 1);&#xA;    ShiftData_time = real(ShiftData_time);&#xA;end&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Here is the CUDA implementation using MEX :&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#include &amp;quot;mex.h&amp;quot;&#xA;#include &amp;lt;cuda_runtime.h&amp;gt;&#xA;#include &amp;lt;cufft.h&amp;gt;&#xA;#include &amp;lt;math.h&amp;gt;&#xA;#define M_PI 3.14159&#xA;&#xA;// Macro for cuFFT error checking.&#xA;#define CUFFT_CHECK_ERROR(call) { cufftResult err = call; if(err != CUFFT_SUCCESS) { \&#xA;    mexErrMsgIdAndTxt(&amp;quot;CUDA:CUFFTError&amp;quot;, &amp;quot;CUFFT error: %d&amp;quot;, err); } }&#xA;&#xA;// Macro for CUDA runtime error checking.&#xA;#define CUDA_CHECK_ERROR(call) { cudaError_t err = call; if(err != cudaSuccess) { \&#xA;    mexErrMsgIdAndTxt(&amp;quot;CUDA:RuntimeError&amp;quot;, &amp;quot;CUDA error: %s&amp;quot;, cudaGetErrorString(err)); } }&#xA;&#xA;// Updated CUDA kernel to apply the phase shift (delay) in the frequency domain.&#xA;// Instead of launching one block per column with nfft threads, we now use a 2D grid.&#xA;// grid.x corresponds to the column index, and grid.y is the number of blocks needed&#xA;// to cover all frequency bins (with a fixed block size).&#xA;__global__ void applyDelay(cufftDoubleComplex *data, const double *fftBin, double delay, int nfft, int nCols)&#xA;{&#xA;    int col = blockIdx.x;          // Column index.&#xA;    int blockId = blockIdx.y;      // Block index within the column.&#xA;    int threadsPerBlock = blockDim.x;&#xA;    int threadId = threadIdx.x;&#xA;&#xA;    // Compute global frequency bin index within this column.&#xA;    int index = blockId * threadsPerBlock &#x2B; threadId;&#xA;    &#xA;    if (col &amp;lt; nCols &amp;amp;&amp;amp; index &amp;lt; nfft)&#xA;    {&#xA;        int idx = col * nfft &#x2B; index;&#xA;        double phase = -delay * fftBin[index];&#xA;        double cosPhase = cos(phase);&#xA;        double sinPhase = sin(phase);&#xA;        &#xA;        cufftDoubleComplex factor;&#xA;        factor.x = cosPhase;&#xA;        factor.y = sinPhase;&#xA;        &#xA;        cufftDoubleComplex orig = data[idx];&#xA;        cufftDoubleComplex result;&#xA;        result.x = orig.x * factor.x - orig.y * factor.y;&#xA;        result.y = orig.x * factor.y &#x2B; orig.y * factor.x;&#xA;        data[idx] = result;&#xA;    }&#xA;}&#xA;&#xA;// The MEX gateway function.&#xA;void mexFunction(int nlhs, mxArray *plhs[],&#xA;                 int nrhs, const mxArray *prhs[])&#xA;{&#xA;    // Check input count: RData, delay, fs.&#xA;    if(nrhs != 3)&#xA;        mexErrMsgIdAndTxt(&amp;quot;MATLAB:simpledelayupdated:invalidNumInputs&amp;quot;, &#xA;                          &amp;quot;Three inputs required: RData, delay, fs.&amp;quot;);&#xA;    &#xA;    // Ensure RData is real and double precision.&#xA;    if(!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]))&#xA;        mexErrMsgIdAndTxt(&amp;quot;MATLAB:simpledelayupdated:inputNotDouble&amp;quot;, &#xA;                          &amp;quot;RData must be real double precision.&amp;quot;);&#xA;    &#xA;    // Get input RData and its dimensions.&#xA;    double *RData = mxGetPr(prhs[0]);&#xA;    mwSize nfft = mxGetM(prhs[0]);   // Number of rows.&#xA;    mwSize nCols = mxGetN(prhs[0]);   // Number of columns.&#xA;    &#xA;    // Read scalar inputs: delay and fs (fs is provided for compatibility).&#xA;    double delay = mxGetScalar(prhs[1]);&#xA;    double fs = mxGetScalar(prhs[2]); // Not used directly.&#xA;    &#xA;    // Create output array (real, same size as input).&#xA;    plhs[0] = mxCreateDoubleMatrix(nfft, nCols, mxREAL);&#xA;    double *ShiftData_time = mxGetPr(plhs[0]);&#xA;    &#xA;    // Allocate host memory for complex input data.&#xA;    cufftDoubleComplex *h_complex = (cufftDoubleComplex*) mxMalloc(nfft * nCols * sizeof(cufftDoubleComplex));&#xA;    for (mwSize col = 0; col &amp;lt; nCols; col&#x2B;&#x2B;) {&#xA;        for (mwSize row = 0; row &amp;lt; nfft; row&#x2B;&#x2B;) {&#xA;            int idx = col * nfft &#x2B; row;&#xA;            h_complex[idx].x = RData[idx];&#xA;            h_complex[idx].y = 0.0;&#xA;        }&#xA;    }&#xA;    &#xA;    // Precompute the FFT frequency bins.&#xA;    // This corresponds to the MATLAB expression:&#xA;    // fftBin = (2*pi*ifftshift(((0:nfft-1)-floor(nfft/2))))/nfft&#xA;    double *h_fftBin = (double*) mxMalloc(nfft * sizeof(double));&#xA;    int binStart = (int)(nfft / 2);&#xA;    for (mwSize row = 0; row &amp;lt; nfft; row&#x2B;&#x2B;) {&#xA;        int shifted_idx = (row &#x2B; binStart) % nfft;&#xA;        int centered = row - binStart;&#xA;        h_fftBin[shifted_idx] = (2.0 * M_PI * centered) / (double)nfft;&#xA;    }&#xA;    &#xA;    // Allocate device memory for the complex data and the fftBin vector.&#xA;    cufftDoubleComplex *d_data;&#xA;    CUDA_CHECK_ERROR(cudaMalloc((void**)&amp;amp;d_data, nfft * nCols * sizeof(cufftDoubleComplex)));&#xA;    CUDA_CHECK_ERROR(cudaMemcpy(d_data, h_complex, nfft * nCols * sizeof(cufftDoubleComplex), cudaMemcpyHostToDevice));&#xA;    &#xA;    double *d_fftBin;&#xA;    CUDA_CHECK_ERROR(cudaMalloc((void**)&amp;amp;d_fftBin, nfft * sizeof(double)));&#xA;    CUDA_CHECK_ERROR(cudaMemcpy(d_fftBin, h_fftBin, nfft * sizeof(double), cudaMemcpyHostToDevice));&#xA;    &#xA;    // Create a cuFFT plan for batched 1D FFTs (each column is one FFT of size nfft).&#xA;    cufftHandle plan;&#xA;    int rank = 1;&#xA;    int n[1] = {(int)nfft};&#xA;    int istride = 1, idist = nfft;&#xA;    int ostride = 1, odist = nfft;&#xA;    CUFFT_CHECK_ERROR(cufftPlanMany(&amp;amp;plan, rank, n,&#xA;                                    NULL, istride, idist,&#xA;                                    NULL, ostride, odist,&#xA;                                    CUFFT_Z2Z, nCols));&#xA;    &#xA;    // Execute forward FFT (time to frequency domain).&#xA;    CUFFT_CHECK_ERROR(cufftExecZ2Z(plan, d_data, d_data, CUFFT_FORWARD));&#xA;    &#xA;    // Launch the CUDA kernel to apply the phase shift.&#xA;    int threadsPerBlock = 256;  // Use a safe block size.&#xA;    int blocksPerColumn = (nfft &#x2B; threadsPerBlock - 1) / threadsPerBlock;&#xA;    dim3 grid(nCols, blocksPerColumn, 1);  // grid.x: columns, grid.y: blocks per column.&#xA;    dim3 block(threadsPerBlock, 1, 1);&#xA;    &#xA;    applyDelay&amp;lt;&amp;lt;&amp;lt;grid, block&amp;gt;&amp;gt;&amp;gt;(d_data, d_fftBin, delay, nfft, nCols);&#xA;    CUDA_CHECK_ERROR(cudaDeviceSynchronize());&#xA;    &#xA;    // Execute the inverse FFT to convert back to time domain.&#xA;    CUFFT_CHECK_ERROR(cufftExecZ2Z(plan, d_data, d_data, CUFFT_INVERSE));&#xA;    &#xA;    // Allocate host memory for the inverse FFT result and copy from device.&#xA;    cufftDoubleComplex *h_result = (cufftDoubleComplex*) mxMalloc(nfft * nCols * sizeof(cufftDoubleComplex));&#xA;    CUDA_CHECK_ERROR(cudaMemcpy(h_result, d_data, nfft * nCols * sizeof(cufftDoubleComplex), cudaMemcpyDeviceToHost));&#xA;    &#xA;    // Normalize the result by dividing by nfft and take the real part.&#xA;    for(mwSize i = 0; i &amp;lt; nfft * nCols; i&#x2B;&#x2B;) {&#xA;        ShiftData_time[i] = h_result[i].x / ((double)nfft);&#xA;    }&#xA;    &#xA;    // Clean up resources.&#xA;    cufftDestroy(plan);&#xA;    cudaFree(d_data);&#xA;    cudaFree(d_fftBin);&#xA;    mxFree(h_complex);&#xA;    mxFree(h_fftBin);&#xA;    mxFree(h_result);&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This is how I am calling the Matlab function:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;[temp] = simpledelayupdated(gather(RF_Arr_gpu), scalarDelay, sampling_Freq);&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;where RF_arr_gpu is a 2D MATLAB gpuArray, scalarDelay and sampling_Freq are scalars.&lt;/p&gt;&#xA;&lt;p&gt;The issue I&#x27;m facing is that the final output from both functions does not match when run on real data. However, I have tested individual components of the CUDA MEX function, and the differences between the MATLAB and CUDA MEX outputs are minimal.&lt;/p&gt;&#xA;&lt;p&gt;Can anyone help me identify where there could be issues in the code? I have attached the output from MATLAB and what we are currently getting from CUDA MEX. The MATLAB image is continuous while the CUDA MEX seems to be more discrete.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&quot;https://i.sstatic.net/ikgxXlj8.png&quot; alt=&quot;MATLAB image&quot; /&gt;&#xA;&lt;img src=&quot;https://i.sstatic.net/Qs7B732n.png&quot; alt=&quot;CUDA MEX image&quot; /&gt;&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/79447789</id>
        <re:rank scheme="https://stackoverflow.com">4</re:rank>
        <title type="text">need to vectorize efficiently calculating only certain values in the matrix multiplication A * B, using a logical array L the size of A * B</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="vectorization" />
            <category scheme="https://stackoverflow.com/tags" term="sparse-matrix" />
            <category scheme="https://stackoverflow.com/tags" term="matrix-multiplication" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Cal</name>
            <uri>https://stackoverflow.com/users/29686189</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/79447789/need-to-vectorize-efficiently-calculating-only-certain-values-in-the-matrix-mult" />
        <published>2025-02-18T09:48:59Z</published>
        <updated>2025-02-20T21:01:44Z</updated>
        <summary type="html">
            &lt;p&gt;I have matrices &lt;strong&gt;A&lt;/strong&gt; (m by v) and &lt;strong&gt;B&lt;/strong&gt; (v by n). I also have a logical matrix &lt;strong&gt;L&lt;/strong&gt; (m by n).&lt;/p&gt;&#xA;&lt;p&gt;I am interested in calculating only the values in &lt;strong&gt;A&lt;/strong&gt; * &lt;strong&gt;B&lt;/strong&gt; that correspond to logical values in &lt;strong&gt;L&lt;/strong&gt; (values of 1s). Essentially I am interested in the quantity ( &lt;strong&gt;A&lt;/strong&gt; * &lt;strong&gt;B&lt;/strong&gt; ) .* &lt;strong&gt;L&lt;/strong&gt; .&lt;/p&gt;&#xA;&lt;p&gt;For my problem, a typical L matrix has less than 0.1% percent of its values as 1s; the vast majority of the values are 0s. Thus, it makes no sense for me to literally perform ( &lt;strong&gt;A&lt;/strong&gt; * &lt;strong&gt;B&lt;/strong&gt; ) .* &lt;strong&gt;L&lt;/strong&gt; , it would actually be faster to loop over each row of &lt;strong&gt;A&lt;/strong&gt; * &lt;strong&gt;B&lt;/strong&gt; that I want to compute, but even that is inefficient.&lt;/p&gt;&#xA;&lt;hr /&gt;&#xA;&lt;p&gt;&lt;strong&gt;Possible solution (need help vectorizing this code if possible)&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;My particular problem may have a nice solution given that the logical matrix &lt;strong&gt;L&lt;/strong&gt; has a nice structure.&lt;/p&gt;&#xA;&lt;p&gt;Here&#x27;s an example of L for a very small scale example (in most applications L is much much bigger and has much fewer 1-yellow entries, and many more 0-blue entries).&#xA;&lt;img src=&quot;https://i.imgur.com/P4TLMiG.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&#xA;&lt;p&gt;This L matrix is nice in that it can be represented as something like a permuted block matrix. This L in particular is composed of 9 &amp;quot;blocks&amp;quot; of 1s, where each block of 1s has its own set of row and column indices, defining a particular submatrix in &lt;strong&gt;A&lt;/strong&gt; * &lt;strong&gt;B&lt;/strong&gt;. For instance, the highlighted area here can be seen the values of 1 as a particular submatrix in L.&#xA;&lt;img src=&quot;https://i.imgur.com/JjsoeW0.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&#xA;&lt;p&gt;My solution was to do this: break the problem into submatrices over these blocks, and do matrix multiplications over each submatrix. I can get the row indices and column indices per each block&#x27;s submatrix in L, organized in two cell lists &amp;quot;rowidxs_list&amp;quot; and &amp;quot;colidxs_list&amp;quot;, both with the number of cells equal to the number of blocks. For instance in the block example I gave, subblock 1, I could calculate those particular values in A * B by simply doing A( rowidxs_list{1} , : ) * B( : , colidxs_list{1} ) .&lt;/p&gt;&#xA;&lt;p&gt;That means that if I precomputed rowidxs_list and colidxs_list (ignore the costs of calculating these lists, they are negligable for my application), then my problem of calculating &lt;strong&gt;C&lt;/strong&gt; = ( &lt;strong&gt;A&lt;/strong&gt; * &lt;strong&gt;B&lt;/strong&gt; ) .* &lt;strong&gt;L&lt;/strong&gt;  could effectively be done by:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;C = sparse( m,n )&#xA;for i = 1:length( rowidxs_list )&#xA;    C( rowidxs_list{i} , colidxs_list{i} ) = ...&#xA;        A( rowidxs_list{i} , : ) * B( : , colidxs_list{i} );&#xA;end&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This seems like it would be the most efficient way to solve this problem &lt;strong&gt;if I knew how to vectorize this for loop&lt;/strong&gt;. Does anyone see a way to vectorize this?&lt;/p&gt;&#xA;&lt;p&gt;There may be ways to vectorize if certain things hold, e.g. only if rowidxs_list and colidxs_list are matrix arrays instead of cell lists of lists (where each column in an array is an index list, thus replacing use of rowidxs_list{i} with rowidxs_list(i,:) ). I&#x27;d prefer to use cell lists here if possible since different lists can have different numbers of elements.&lt;/p&gt;&#xA;&lt;hr /&gt;&#xA;&lt;p&gt;&lt;strong&gt;other suggested solution (creating a mex file?)&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;I first posted this question on the /r/matlab subreddit, &lt;a href=&quot;https://www.reddit.com/r/matlab/comments/1iro2m4/need_to_vectorize_efficiently_calculating_only/&quot; rel=&quot;nofollow noreferrer&quot;&gt;see here for the reddit thread&lt;/a&gt;. The user &amp;quot;qtac&amp;quot; recommended that a C-MEX function linking to C programming language:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;My gut feeling is the only way to really optimize this is with a C-MEX solution; otherwise, you are going to get obliterated by overhead from subsref in these loops. With C you could loop over L until you find a nonzero element, and then do only the row-column dot product needed to populate that specific element. You will miss out on a lot of the BLAS optimizations but the computational savings may make up for it.&#xA;Honestly I bet an LLM could write 90%&#x2B; of that MEX function for you; it&#x27;s a well-formulated problem.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;I think this could be a good solution to pursue, but I&#x27;d like other opinions as well.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/78876822</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Debugging REShAPE software mex and nvcc issue</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="visual-studio-2017" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="nvcc" />
        <author>
            <name>Reesh</name>
            <uri>https://stackoverflow.com/users/26752151</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/78876822/debugging-reshape-software-mex-and-nvcc-issue" />
        <published>2024-08-15T21:00:12Z</published>
        <updated>2024-08-16T21:21:20Z</updated>
        <summary type="html">
            &lt;p&gt;I am running this software called REShAPE on Windows 10 using Matlab 2019b and VS Studio 2017. although I get this error:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;&#xA;MATLAB: &amp;gt; In vl_compilenn&amp;gt;activate_nvcc (line 768)&#xA;MATLAB: In vl_compilenn (line 300)&#xA;MATLAB: In segmfunc (line 63)]&#xA;MATLAB: vl_compilenn:   CUDA: using NVCC &#x27;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc&#x27; (100205).&#xA;MATLAB: vl_compilenn:   CUDA: determining GPU compute capability (use the &#x27;CudaArch&#x27; option to override)&#xA;MATLAB: vl_compilenn:   CUDA: cannot determine the capabilities of the installed GPU and/or CUDA; falling back to default&#xA;MATLAB: vl_compilenn:   CUDA: NVCC architecture string: &#x27;-gencode=arch=compute_20,code=\&amp;quot;sm_20,compute_20\&amp;quot; -gencode=arch=compute_30,code=\&amp;quot;sm_30,compute_30\&amp;quot;&#x27;.&#xA;MATLAB: Warning: Support for toolboxdir(&#x27;distcomp&#x27;) will be removed in a future release.  Use toolboxdir(&#x27;parallel&#x27;) instead.]&#xA;MATLAB: &amp;gt; In toolboxdir (line 49)&#xA;MATLAB: In vl_compilenn (line 383)&#xA;MATLAB: In segmfunc (line 63)]&#xA;MATLAB: vl_compilenn: * Compiler and linker configurations *&#xA;MATLAB: vl_compilenn:   intermediate build products directory: C:\Users\gelme\REShAPE\Segmentation\matconvnet\matlab\mex\.build&#xA;MATLAB: vl_compilenn:   MEX files: C:\Users\gelme\REShAPE\Segmentation\matconvnet\matlab\mex/&#xA;MATLAB: vl_compilenn:   Base options: -DENABLE_GPU -DENABLE_DOUBLE -O -DNDEBUG&#xA;MATLAB: vl_compilenn:   MEX CXX: -R2018a -D__SSSE3__&#xA;MATLAB: vl_compilenn:   MEX CXXFLAGS:&#xA;MATLAB: vl_compilenn:   MEX CXXOPTIMFLAGS:&#xA;MATLAB: vl_compilenn:   MEX LINK: -R2018a -lmwblas -L&amp;quot;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64&amp;quot; -lcudart -lcublas -lgpu&#xA;MATLAB: vl_compilenn:   MEX LINK LDFLAGS:&#xA;MATLAB: vl_compilenn:   MEX LINK LDOPTIMFLAGS:&#xA;MATLAB: vl_compilenn:   MEX LINK LINKLIBS: gdiplus.lib&#xA;MATLAB: vl_compilenn:   MEX CUDA: -R2018a&#xA;MATLAB: vl_compilenn:   MEX CUDA CXXFLAGS:&#xA;MATLAB: vl_compilenn:   MEX CUDA CXXOPTIMFLAGS:&#xA;MATLAB: vl_compilenn:   NVCC: -D_FORCE_INLINES --std=c&#x2B;&#x2B;11 -I&amp;quot;C:\Program Files\MATLAB\R2019b\extern\include&amp;quot; -I&amp;quot;C:\Program Files\MATLAB\R2019b\toolbox\parallel\gpu\extern\include&amp;quot; -gencode=arch=compute_20,code=\&amp;quot;sm_20,compute_20\&amp;quot; -gencode=arch=compute_30,code=\&amp;quot;sm_30,compute_30\&amp;quot; --compiler-options=/MD&#xA;MATLAB: vl_compilenn: * Reading images *&#xA;MATLAB: vl_compilenn:   vl_imreadjpeg enabled&#xA;MATLAB: vl_compilenn:   image library: gdiplus&#xA;MATLAB: vl_compilenn:   image library compile flags:&#xA;MATLAB: vl_compilenn:   image library link flags: gdiplus.lib&#xA;MATLAB: vl_compilenn: NVCC CC: &amp;quot;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc&amp;quot; -c -o -v &amp;quot;C:\Users\gelme\REShAPE\Segmentation\matconvnet\matlab\mex\.build\bits\data.obj&amp;quot; &amp;quot;C:\Users\gelme\REShAPE\Segmentation\matconvnet\matlab\src\bits\data.cu&amp;quot; -DENABLE_GPU -DENABLE_DOUBLE -O -DNDEBUG -D_FORCE_INLINES --std=c&#x2B;&#x2B;11 -I&amp;quot;C:\Program Files\MATLAB\R2019b\extern\include&amp;quot; -I&amp;quot;C:\Program Files\MATLAB\R2019b\toolbox\parallel\gpu\extern\include&amp;quot; -gencode=arch=compute_20,code=\&amp;quot;sm_20,compute_20\&amp;quot; -gencode=arch=compute_30,code=\&amp;quot;sm_30,compute_30\&amp;quot; --compiler-options=/MD&#xA;MATLAB: nvcc fatal   : &#x27;-DNDEBUG&#x27;: expected a number&#xA;MATLAB: Command &amp;quot;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\nvcc&amp;quot; -c -o -v &amp;quot;C:\Users\gelme\REShAPE\Segmentation\matconvnet\matlab\mex\.build\bits\data.obj&amp;quot; &amp;quot;C:\Users\gelme\REShAPE\Segmentation\matconvnet\matlab\src\bits\data.cu&amp;quot; -DENABLE_GPU -DENABLE_DOUBLE -O -DNDEBUG -D_FORCE_INLINES --std=c&#x2B;&#x2B;11 -I&amp;quot;C:\Program Files\MATLAB\R2019b\extern\include&amp;quot; -I&amp;quot;C:\Program Files\MATLAB\R2019b\toolbox\parallel\gpu\extern\include&amp;quot; -gencode=arch=compute_20,code=\&amp;quot;sm_20,compute_20\&amp;quot; -gencode=arch=compute_30,code=\&amp;quot;sm_30,compute_30\&amp;quot; --compiler-options=/MD  failed.&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This seems to be an issue with the vl.compilenn.m showed in the image. It&#x27;s still using nvcc when I am trying to use mex. How can I fix this issue that it&#x27;s giving me in the image?&lt;/p&gt;&#xA;&lt;p&gt;This is the code I currently have for vl.compilenn&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;function vl_compilenn(varargin)&#xA;%VL_COMPILENN Compile the MatConvNet toolbox.&#xA;%   &#xA;opts.continue         = false;&#xA;opts.enableGpu        = false;&#xA;opts.enableImreadJpeg = true;&#xA;opts.enableCudnn      = false;&#xA;opts.enableDouble     = true;&#xA;opts.imageLibrary = [] ;&#xA;opts.imageLibraryCompileFlags = {} ;&#xA;opts.imageLibraryLinkFlags = [] ;&#xA;opts.verbose          = 0;&#xA;opts.debug            = false;&#xA;opts.cudaMethod       = &#x27;mex&#x27; ;&#xA;opts.cudaRoot         = [] ;&#xA;opts.cudaArch         = [] ;&#xA;opts.defCudaArch      = [...&#xA;  &#x27;-gencode=arch=compute_20,code=\&amp;quot;sm_20,compute_20\&amp;quot; &#x27;...&#xA;  &#x27;-gencode=arch=compute_30,code=\&amp;quot;sm_30,compute_30\&amp;quot;&#x27;];&#xA;opts.mexConfig        = &#x27;&#x27; ;&#xA;opts.mexCudaConfig    = &#x27;&#x27; ;&#xA;opts.cudnnRoot        = &#x27;local/cudnn&#x27; ;&#xA;opts.preCompileFn       = [] ;&#xA;&#xA;% &amp;lt;### Inserted by matconvnet_patch.py ###&amp;gt;&#xA;opts.mexFlags = {&#x27;-largeArrayDims&#x27;};&#xA;&#xA;opts = vl_argparse(opts, varargin);&#xA;&#xA;% --------------------------------------------------------------------&#xA;%                                                     Files to compile&#xA;% --------------------------------------------------------------------&#xA;&#xA;arch = computer(&#x27;arch&#x27;) ;&#xA;check_compability(arch);&#xA;if isempty(opts.imageLibrary)&#xA;  switch arch&#xA;    case &#x27;glnxa64&#x27;, opts.imageLibrary = &#x27;libjpeg&#x27; ;&#xA;    case &#x27;maci64&#x27;, opts.imageLibrary = &#x27;quartz&#x27; ;&#xA;    case &#x27;win64&#x27;, opts.imageLibrary = &#x27;gdiplus&#x27; ;&#xA;  end&#xA;end&#xA;if isempty(opts.imageLibraryLinkFlags)&#xA;  switch opts.imageLibrary&#xA;    case &#x27;libjpeg&#x27;, opts.imageLibraryLinkFlags = {&#x27;-ljpeg&#x27;} ;&#xA;    case &#x27;quartz&#x27;, opts.imageLibraryLinkFlags = {&#x27;-framework Cocoa -framework ImageIO&#x27;} ;&#xA;    case &#x27;gdiplus&#x27;, opts.imageLibraryLinkFlags = {&#x27;gdiplus.lib&#x27;} ;&#xA;  end&#xA;end&#xA;&#xA;lib_src = {} ;&#xA;mex_src = {} ;&#xA;&#xA;% Files that are compiled as CPP or CU depending on whether GPU support&#xA;% is enabled.&#xA;if opts.enableGpu, ext = &#x27;cu&#x27; ; else ext=&#x27;cpp&#x27; ; end&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;data.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;datamex.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnconv.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnfullyconnected.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnsubsample.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnpooling.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnnormalize.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnnormalizelp.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnbnorm.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnbias.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnbilinearsampler.&#x27; ext]) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,[&#x27;nnroipooling.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_nnconv.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_nnconvt.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_nnpool.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_nnnormalize.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_nnnormalizelp.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_nnbnorm.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_nnbilinearsampler.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_nnroipool.&#x27; ext]) ;&#xA;mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_taccummex.&#x27; ext]) ;&#xA;switch arch&#xA;  case {&#x27;glnxa64&#x27;,&#x27;maci64&#x27;}&#xA;    % not yet supported in windows&#xA;    mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,[&#x27;vl_tmove.&#x27; ext]) ;&#xA;end&#xA;&#xA;% CPU-specific files&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,&#x27;impl&#x27;,&#x27;im2row_cpu.cpp&#x27;) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,&#x27;impl&#x27;,&#x27;copy_cpu.cpp&#x27;) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,&#x27;impl&#x27;,&#x27;tinythread.cpp&#x27;) ;&#xA;lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,&#x27;imread.cpp&#x27;) ;&#xA;&#xA;% GPU-specific files&#xA;if opts.enableGpu&#xA;  lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,&#x27;impl&#x27;,&#x27;im2row_gpu.cu&#x27;) ;&#xA;  lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,&#x27;impl&#x27;,&#x27;copy_gpu.cu&#x27;) ;&#xA;  lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;bits&#x27;,&#x27;datacu.cu&#x27;) ;&#xA;  mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;,&#x27;vl_cudatool.cu&#x27;) ;&#xA;end&#xA;&#xA;% cuDNN-specific files&#xA;if opts.enableCudnn&#xA;end&#xA;&#xA;% Other files&#xA;if opts.enableImreadJpeg&#xA;  mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;, [&#x27;vl_imreadjpeg.&#x27; ext]) ;&#xA;  mex_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;, [&#x27;vl_imreadjpeg_old.&#x27; ext]) ;&#xA;  lib_src{end&#x2B;1} = fullfile(root,&#x27;matlab&#x27;,&#x27;src&#x27;, &#x27;bits&#x27;, &#x27;impl&#x27;, [&#x27;imread_&#x27; opts.imageLibrary &#x27;.cpp&#x27;]) ;&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;%                                                   Setup CUDA toolkit&#xA;% --------------------------------------------------------------------&#xA;&#xA;if opts.enableGpu&#xA;  opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s: * CUDA configuration *\n&#x27;, mfilename) ;&#xA;&#xA;  % Find the CUDA Devkit&#xA;  if isempty(opts.cudaRoot), opts.cudaRoot = search_cuda_devkit(opts) ; end&#xA;  opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s:\tCUDA: using CUDA Devkit &#x27;&#x27;%s&#x27;&#x27;.\n&#x27;, ...&#xA;                          mfilename, opts.cudaRoot) ;&#xA;&#xA;  opts.nvccPath = fullfile(opts.cudaRoot, &#x27;bin&#x27;, &#x27;nvcc&#x27;) ;&#xA;  switch arch&#xA;    case &#x27;win64&#x27;, opts.cudaLibDir = fullfile(opts.cudaRoot, &#x27;lib&#x27;, &#x27;x64&#x27;) ;&#xA;    case &#x27;maci64&#x27;, opts.cudaLibDir = fullfile(opts.cudaRoot, &#x27;lib&#x27;) ;&#xA;    case &#x27;glnxa64&#x27;, opts.cudaLibDir = fullfile(opts.cudaRoot, &#x27;lib64&#x27;) ;&#xA;  end&#xA;&#xA;  % Set the nvcc method as default for Win platforms&#xA;  if strcmp(arch, &#x27;win64&#x27;) &amp;amp;&amp;amp; isempty(opts.cudaMethod)&#xA;    opts.cudaMethod = &#x27;nvcc&#x27;;&#xA;  end&#xA;&#xA;  % Activate the CUDA Devkit&#xA;  cuver = activate_nvcc(opts.nvccPath) ;&#xA;  opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s:\tCUDA: using NVCC &#x27;&#x27;%s&#x27;&#x27; (%d).\n&#x27;, ...&#xA;                          mfilename, opts.nvccPath, cuver) ;&#xA;&#xA;  % Set the CUDA arch string (select GPU architecture)&#xA;  if isempty(opts.cudaArch), opts.cudaArch = get_cuda_arch(opts) ; end&#xA;  opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s:\tCUDA: NVCC architecture string: &#x27;&#x27;%s&#x27;&#x27;.\n&#x27;, ...&#xA;                          mfilename, opts.cudaArch) ;&#xA;end&#xA;&#xA;if opts.enableCudnn&#xA;  opts.cudnnIncludeDir = fullfile(opts.cudnnRoot, &#x27;include&#x27;) ;&#xA;  switch arch&#xA;    case &#x27;win64&#x27;, opts.cudnnLibDir = fullfile(opts.cudnnRoot, &#x27;lib&#x27;, &#x27;x64&#x27;) ;&#xA;    case &#x27;maci64&#x27;, opts.cudnnLibDir = fullfile(opts.cudnnRoot, &#x27;lib&#x27;) ;&#xA;    case &#x27;glnxa64&#x27;, opts.cudnnLibDir = fullfile(opts.cudnnRoot, &#x27;lib64&#x27;) ;&#xA;  end&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;%                                                     Compiler options&#xA;% --------------------------------------------------------------------&#xA;&#xA;% Build directories&#xA;flags.src_dir = fullfile(root, &#x27;matlab&#x27;, &#x27;src&#x27;) ;&#xA;flags.mex_dir = fullfile(root, &#x27;matlab&#x27;, &#x27;mex&#x27;) ;&#xA;flags.bld_dir = fullfile(flags.mex_dir, &#x27;.build&#x27;);&#xA;if ~exist(fullfile(flags.bld_dir,&#x27;bits&#x27;,&#x27;impl&#x27;), &#x27;dir&#x27;)&#xA;  mkdir(fullfile(flags.bld_dir,&#x27;bits&#x27;,&#x27;impl&#x27;)) ;&#xA;end&#xA;&#xA;% BASE: Base flags passed to `mex` and `nvcc` always.&#xA;flags.base = {} ;&#xA;if opts.enableGpu, flags.base{end&#x2B;1} = &#x27;-DENABLE_GPU&#x27; ; end&#xA;if opts.enableDouble, flags.base{end&#x2B;1} = &#x27;-DENABLE_DOUBLE&#x27; ; end&#xA;if opts.enableCudnn&#xA;  flags.base{end&#x2B;1} = &#x27;-DENABLE_CUDNN&#x27; ;&#xA;  flags.base{end&#x2B;1} = [&#x27;-I&amp;quot;&#x27; opts.cudnnIncludeDir &#x27;&amp;quot;&#x27;] ;&#xA;end&#xA;if opts.verbose &amp;gt; 1, flags.base{end&#x2B;1} = &#x27;-v&#x27; ; end&#xA;if opts.debug&#xA;  flags.base{end&#x2B;1} = &#x27;-g&#x27; ;&#xA;  flags.base{end&#x2B;1} = &#x27;-DDEBUG&#x27; ;&#xA;else&#xA;  flags.base{end&#x2B;1} = &#x27;-O&#x27; ;&#xA;  flags.base{end&#x2B;1} = &#x27;-DNDEBUG&#x27; ;&#xA;end&#xA;&#xA;% MEX: Additional flags passed to `mex` for compiling C&#x2B;&#x2B;&#xA;% code. CXX and CXXOPTIOM are passed directly to the encapsualted compiler.&#xA;&#xA;% &amp;lt;### Changed by matconvnet_patch.py ###&amp;gt;&#xA;% flags.mex = {&#x27;-largeArrayDims&#x27;} ;&#xA;flags.mex = opts.mexFlags;&#xA;&#xA;flags.cxx = {} ;&#xA;flags.cxxoptim = {} ;&#xA;if ~isempty(opts.mexConfig), flags.mex = horzcat(flags.mex, {&#x27;-f&#x27;, opts.mexConfig}) ; end&#xA;&#xA;% MEX: Additional flags passed to `mex` for compiling CUDA&#xA;% code. CXX and CXXOPTIOM are passed directly to the encapsualted compiler.&#xA;&#xA;% &amp;lt;### Changed by matconvnet_patch.py ###&amp;gt;&#xA;% flags.mexcuda = {&#x27;-largeArrayDims&#x27;} ;&#xA;flags.mexcuda = opts.mexFlags;&#xA;&#xA;flags.mexcuda_cxx = {} ;&#xA;flags.mexcuda_cxxoptim = {} ;&#xA;if ~isempty(opts.mexCudaConfig), flags.mexcuda = horzcat(flags.mexcuda, {&#x27;-f&#x27;, opts.mexCudaConfig}) ; end&#xA;&#xA;% MEX_LINK: Additional flags passed to `mex` for linking.&#xA;&#xA;% &amp;lt;### Changed by matconvnet_patch.py ###&amp;gt;&#xA;% flags.mexlink = {&#x27;-largeArrayDims&#x27;,&#x27;-lmwblas&#x27;} ;&#xA;flags.mexlink = horzcat(opts.mexFlags, {&#x27;-lmwblas&#x27;});&#xA;&#xA;flags.mexlink_ldflags = {} ;&#xA;flags.mexlink_ldoptimflags = {} ;&#xA;flags.mexlink_linklibs = {} ;&#xA;&#xA;% NVCC: Additional flags passed to `nvcc` for compiling CUDA code.&#xA;flags.nvcc = {&#x27;-D_FORCE_INLINES&#x27;, &#x27;--std=c&#x2B;&#x2B;11&#x27;, ...&#xA;  sprintf(&#x27;-I&amp;quot;%s&amp;quot;&#x27;,fullfile(matlabroot,&#x27;extern&#x27;,&#x27;include&#x27;)), ...&#xA;  sprintf(&#x27;-I&amp;quot;%s&amp;quot;&#x27;,fullfile(toolboxdir(&#x27;distcomp&#x27;),&#x27;gpu&#x27;,&#x27;extern&#x27;,&#x27;include&#x27;)), ...&#xA;  opts.cudaArch} ;&#xA;&#xA;switch arch&#xA;  case {&#x27;maci64&#x27;,&#x27;glnxa64&#x27;}&#xA;    flags.cxx{end&#x2B;1} = &#x27;--std=c&#x2B;&#x2B;11&#x27; ;&#xA;    flags.nvcc{end&#x2B;1} = &#x27;--compiler-options=-fPIC&#x27; ;&#xA;    if ~opts.debug&#xA;      flags.cxxoptim = horzcat(flags.cxxoptim,&#x27;-mssse3&#x27;,&#x27;-ffast-math&#x27;) ;&#xA;      flags.mexcuda_cxxoptim{end&#x2B;1} = &#x27;--compiler-options=-mssse3,-ffast-math&#x27; ;&#xA;      flags.nvcc{end&#x2B;1} = &#x27;--compiler-options=-mssse3,-ffast-math&#x27; ;&#xA;    end&#xA;  case &#x27;win64&#x27;&#xA;    % Visual Studio 2015 does C&#x2B;&#x2B;11 without futher switches&#xA;end&#xA;&#xA;if opts.enableGpu&#xA;  flags.mexlink = horzcat(flags.mexlink, ...&#xA;    {[&#x27;-L&amp;quot;&#x27; opts.cudaLibDir &#x27;&amp;quot;&#x27;], &#x27;-lcudart&#x27;, &#x27;-lcublas&#x27;}) ;&#xA;  switch arch&#xA;    case {&#x27;maci64&#x27;, &#x27;glnxa64&#x27;}&#xA;      flags.mexlink{end&#x2B;1} = &#x27;-lmwgpu&#x27; ;&#xA;    case &#x27;win64&#x27;&#xA;      flags.mexlink{end&#x2B;1} = &#x27;-lgpu&#x27; ;&#xA;  end&#xA;  if opts.enableCudnn&#xA;    flags.mexlink{end&#x2B;1} = [&#x27;-L&amp;quot;&#x27; opts.cudnnLibDir &#x27;&amp;quot;&#x27;] ;&#xA;    flags.mexlink{end&#x2B;1} = &#x27;-lcudnn&#x27; ;&#xA;  end&#xA;end&#xA;&#xA;switch arch&#xA;  case {&#x27;maci64&#x27;}&#xA;    flags.mex{end&#x2B;1} = &#x27;-cxx&#x27; ;&#xA;    flags.nvcc{end&#x2B;1} = &#x27;--compiler-options=-mmacosx-version-min=10.10&#x27; ;&#xA;    [s,r] = system(&#x27;xcrun -f clang&#x2B;&#x2B;&#x27;) ;&#xA;    if s == 0&#xA;      flags.nvcc{end&#x2B;1} = sprintf(&#x27;--compiler-bindir=&amp;quot;%s&amp;quot;&#x27;,strtrim(r)) ;&#xA;    end&#xA;    if opts.enableGpu&#xA;      flags.mexlink_ldflags{end&#x2B;1} = sprintf(&#x27;-Wl,-rpath -Wl,&amp;quot;%s&amp;quot;&#x27;, opts.cudaLibDir) ;&#xA;    end&#xA;    if opts.enableGpu &amp;amp;&amp;amp; opts.enableCudnn&#xA;      flags.mexlink_ldflags{end&#x2B;1} = sprintf(&#x27;-Wl,-rpath -Wl,&amp;quot;%s&amp;quot;&#x27;, opts.cudnnLibDir) ;&#xA;    end&#xA;&#xA;  case {&#x27;glnxa64&#x27;}&#xA;    flags.mex{end&#x2B;1} = &#x27;-cxx&#x27; ;&#xA;    flags.mexlink{end&#x2B;1} = &#x27;-lrt&#x27; ;&#xA;    if opts.enableGpu&#xA;      flags.mexlink_ldflags{end&#x2B;1} = sprintf(&#x27;-Wl,-rpath -Wl,&amp;quot;%s&amp;quot;&#x27;, opts.cudaLibDir) ;&#xA;    end&#xA;    if opts.enableGpu &amp;amp;&amp;amp; opts.enableCudnn&#xA;      flags.mexlink_ldflags{end&#x2B;1} = sprintf(&#x27;-Wl,-rpath -Wl,&amp;quot;%s&amp;quot;&#x27;, opts.cudnnLibDir) ;&#xA;    end&#xA;&#xA;  case {&#x27;win64&#x27;}&#xA;    % VisualC does not pass this even if available in the CPU architecture&#xA;    flags.mex{end&#x2B;1} = &#x27;-D__SSSE3__&#x27; ;&#xA;    cl_path = fileparts(check_clpath()); % check whether cl.exe in path&#xA;    flags.nvcc{end&#x2B;1} = &#x27;--compiler-options=/MD&#x27; ;&#xA;    % flags.nvcc{end&#x2B;1} = sprintf(&#x27;--compiler-bindir=&amp;quot;%s&amp;quot;&#x27;, cl_path) ;&#xA;end&#xA;&#xA;if opts.enableImreadJpeg&#xA;  flags.mex = horzcat(flags.mex, opts.imageLibraryCompileFlags) ;&#xA;  flags.mexlink_linklibs = horzcat(flags.mexlink_linklibs, opts.imageLibraryLinkFlags) ;&#xA;end&#xA;&#xA;if ~isempty(opts.preCompileFn)&#xA;  [opts, mex_src, lib_src, flags] = opts.preCompileFn(opts, mex_src, lib_src, flags) ;&#xA;end&#xA;&#xA;% Compile intermediate object files&#xA;srcs = horzcat(lib_src,mex_src) ;&#xA;for i = 1:numel(horzcat(lib_src, mex_src))&#xA;  [~,~,ext] = fileparts(srcs{i}) ; ext(1) = [] ;&#xA;  objfile = toobj(flags.bld_dir,srcs{i});&#xA;  if strcmp(ext,&#x27;cu&#x27;)&#xA;    if strcmp(opts.cudaMethod,&#x27;nvcc&#x27;)&#xA;      nvcc_compile(opts, srcs{i}, objfile, flags) ;&#xA;    else&#xA;      mexcuda_compile(opts, srcs{i}, objfile, flags) ;&#xA;    end&#xA;  else&#xA;    mex_compile(opts, srcs{i}, objfile, flags) ;&#xA;  end&#xA;  assert(exist(objfile, &#x27;file&#x27;) ~= 0, &#x27;Compilation of %s failed.&#x27;, objfile);&#xA;end&#xA;&#xA;% Link MEX files&#xA;for i = 1:numel(mex_src)&#xA;  objs = toobj(flags.bld_dir, [mex_src(i), lib_src]) ;&#xA;  mex_link(opts, objs, flags.mex_dir, flags) ;&#xA;end&#xA;&#xA;% Reset path adding the mex subdirectory just created&#xA;vl_setupnn() ;&#xA;&#xA;if strcmp(arch, &#x27;win64&#x27;) &amp;amp;&amp;amp; opts.enableCudnn&#xA;  if opts.verbose(), fprintf(&#x27;Copying CuDNN dll to mex folder.\n&#x27;); end&#xA;  copyfile(fullfile(opts.cudnnRoot, &#x27;bin&#x27;, &#x27;*.dll&#x27;), flags.mex_dir);&#xA;end&#xA;&#xA;% Save the last compile flags to the build dir&#xA;if isempty(opts.preCompileFn)&#xA;  save(fullfile(flags.bld_dir, &#x27;last_compile_opts.mat&#x27;), &#x27;-struct&#x27;, &#x27;opts&#x27;);&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;%                                                    Utility functions&#xA;% --------------------------------------------------------------------&#xA;&#xA;% --------------------------------------------------------------------&#xA;function check_compability(arch)&#xA;% --------------------------------------------------------------------&#xA;cc = mex.getCompilerConfigurations(&#x27;C&#x2B;&#x2B;&#x27;);&#xA;if isempty(cc)&#xA;  error([&#x27;Mex is not configured.&#x27;...&#xA;    &#x27;Run &amp;quot;mex -setup&amp;quot; to configure your compiler. See &#x27;,...&#xA;    &#x27;http://www.mathworks.com/support/compilers &#x27;, ...&#xA;    &#x27;for supported compilers for your platform.&#x27;]);&#xA;end&#xA;&#xA;switch arch&#xA;  case &#x27;win64&#x27;&#xA;    clversion = str2double(cc.Version);&#xA;    if clversion &amp;lt; 14&#xA;      error(&#x27;Unsupported VS C&#x2B;&#x2B; compiler, ver &amp;gt;=14.0 required (VS 2015).&#x27;);&#xA;    end&#xA;  case &#x27;maci64&#x27;&#xA;  case &#x27;glnxa64&#x27;&#xA;  otherwise, error(&#x27;Unsupported architecture &#x27;&#x27;%s&#x27;&#x27;.&#x27;, arch) ;&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;function done = check_deps(opts, tgt, src)&#xA;% --------------------------------------------------------------------&#xA;done = false ;&#xA;if ~iscell(src), src = {src} ; end&#xA;if ~opts.continue, return ; end&#xA;if ~exist(tgt,&#x27;file&#x27;), return ; end&#xA;ttime = dir(tgt) ; ttime = ttime.datenum ;&#xA;for i=1:numel(src)&#xA;  stime = dir(src{i}) ; stime = stime.datenum ;&#xA;  if stime &amp;gt; ttime, return ; end&#xA;end&#xA;fprintf(&#x27;%s: &#x27;&#x27;%s&#x27;&#x27; already there, skipping.\n&#x27;, mfilename, tgt) ;&#xA;done = true ;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function objs = toobj(bld_dir, srcs)&#xA;% --------------------------------------------------------------------&#xA;str = [filesep, &#x27;src&#x27;, filesep]; % NASTY. Do with regexp?&#xA;multiple = iscell(srcs) ;&#xA;if ~multiple, srcs = {srcs} ; end&#xA;objs = cell(1, numel(srcs));&#xA;for t = 1:numel(srcs)&#xA;  i = strfind(srcs{t},str);&#xA;  i = i(end); % last occurence of &#x27;/src/&#x27;&#xA;  objs{t} = fullfile(bld_dir, srcs{t}(i&#x2B;numel(str):end)) ;&#xA;end&#xA;if ~multiple, objs = objs{1} ; end&#xA;objs = regexprep(objs,&#x27;.cpp$&#x27;,[&#x27;.&#x27; objext]) ;&#xA;objs = regexprep(objs,&#x27;.cu$&#x27;,[&#x27;.&#x27; objext]) ;&#xA;objs = regexprep(objs,&#x27;.c$&#x27;,[&#x27;.&#x27; objext]) ;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function mex_compile(opts, src, tgt, flags)&#xA;% --------------------------------------------------------------------&#xA;if check_deps(opts, tgt, src), return ; end&#xA;args = horzcat({&#x27;-c&#x27;, &#x27;-outdir&#x27;, fileparts(tgt), src}, ...&#xA;  flags.base, flags.mex, ...&#xA;  {[&#x27;CXXFLAGS=$CXXFLAGS &#x27; strjoin(flags.cxx)]}, ...&#xA;  {[&#x27;CXXOPTIMFLAGS=$CXXOPTIMFLAGS &#x27; strjoin(flags.cxxoptim)]}) ;&#xA;opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s: MEX CC: %s\n&#x27;, mfilename, strjoin(args)) ;&#xA;mex(args{:}) ;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function mexcuda_compile(opts, src, tgt, flags)&#xA;% --------------------------------------------------------------------&#xA;if check_deps(opts, tgt, src), return ; end&#xA;% Hacky fix: In glnxa64 MATLAB includes the -ansi option by default, which&#xA;% prevents -std=c&#x2B;&#x2B;11 to work (an error?). This could be solved by editing the&#xA;% mex configuration file; for convenience, we take it out here by&#xA;% avoiding to append to the default flags.&#xA;glue = &#x27;$CXXFLAGS&#x27; ;&#xA;switch computer(&#x27;arch&#x27;)&#xA;  case {&#x27;glnxa64&#x27;}&#xA;    glue = &#x27;--compiler-options=-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread&#x27; ;&#xA;end&#xA;args = horzcat({&#x27;-c&#x27;, &#x27;-outdir&#x27;, fileparts(tgt), src}, ...&#xA;  flags.base, flags.mexcuda, ...&#xA;  {[&#x27;CXXFLAGS=&#x27; glue &#x27; &#x27; strjoin(flags.mexcuda_cxx)]}, ...&#xA;  {[&#x27;CXXOPTIMFLAGS=$CXXOPTIMFLAGS &#x27; strjoin(flags.mexcuda_cxxoptim)]}) ;&#xA;opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s: MEX CUDA: %s\n&#x27;, mfilename, strjoin(args)) ;&#xA;mexcuda(args{:}) ;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function nvcc_compile(opts, src, tgt, flags)&#xA;% --------------------------------------------------------------------&#xA;if check_deps(opts, tgt, src), return ; end&#xA;nvcc_path = fullfile(opts.cudaRoot, &#x27;bin&#x27;, &#x27;nvcc&#x27;);&#xA;nvcc_cmd = sprintf(&#x27;&amp;quot;%s&amp;quot; -c -o -v &amp;quot;%s&amp;quot; &amp;quot;%s&amp;quot; %s &#x27;, ...&#xA;                   nvcc_path, tgt, src, ...&#xA;                   strjoin(horzcat(flags.base,flags.nvcc)));&#xA;opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s: NVCC CC: %s\n&#x27;, mfilename, nvcc_cmd) ;&#xA;status = system(nvcc_cmd);&#xA;if status, error(&#x27;Command %s failed.&#x27;, nvcc_cmd); end;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function mex_link(opts, objs, mex_dir, flags)&#xA;% --------------------------------------------------------------------&#xA;args = horzcat({&#x27;-outdir&#x27;, mex_dir}, ...&#xA;  flags.base, flags.mexlink, ...&#xA;  {[&#x27;LDFLAGS=$LDFLAGS &#x27; strjoin(flags.mexlink_ldflags)]}, ...&#xA;  {[&#x27;LDOPTIMFLAGS=$LDOPTIMFLAGS &#x27; strjoin(flags.mexlink_ldoptimflags)]}, ...&#xA;  {[&#x27;LINKLIBS=&#x27; strjoin(flags.mexlink_linklibs) &#x27; $LINKLIBS&#x27;]}, ...&#xA;  objs) ;&#xA;opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s: MEX LINK: %s\n&#x27;, mfilename, strjoin(args)) ;&#xA;mex(args{:}) ;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function ext = objext()&#xA;% --------------------------------------------------------------------&#xA;% Get the extension for an &#x27;object&#x27; file for the current computer&#xA;% architecture&#xA;switch computer(&#x27;arch&#x27;)&#xA;  case &#x27;win64&#x27;, ext = &#x27;obj&#x27;;&#xA;  case {&#x27;maci64&#x27;, &#x27;glnxa64&#x27;}, ext = &#x27;o&#x27; ;&#xA;  otherwise, error(&#x27;Unsupported architecture %s.&#x27;, computer) ;&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;function cl_path = check_clpath()&#xA;% --------------------------------------------------------------------&#xA;% Checks whether the cl.exe is in the path (needed for the nvcc). If&#xA;% not, tries to guess the location out of mex configuration.&#xA;cc = mex.getCompilerConfigurations(&#x27;c&#x2B;&#x2B;&#x27;);&#xA;cl_path = fullfile(cc.Location, &#x27;VC&#x27;, &#x27;bin&#x27;, &#x27;amd64&#x27;);&#xA;[status, ~] = system(&#x27;cl.exe -help&#x27;);&#xA;if status == 1&#xA;  % Add cl.exe to system path so that nvcc can find it.&#xA;  warning(&#x27;CL.EXE not found in PATH. Trying to guess out of mex setup.&#x27;);&#xA;  prev_path = getenv(&#x27;PATH&#x27;);&#xA;  setenv(&#x27;PATH&#x27;, [prev_path &#x27;;&#x27; cl_path]);&#xA;  status = system(&#x27;cl.exe&#x27;);&#xA;  if status == 1&#xA;    setenv(&#x27;PATH&#x27;, prev_path);&#xA;    error(&#x27;Unable to find cl.exe&#x27;);&#xA;  else&#xA;    fprintf(&#x27;Location of cl.exe (%s) successfully added to your PATH.\n&#x27;, ...&#xA;      cl_path);&#xA;  end&#xA;end&#xA;&#xA;% -------------------------------------------------------------------------&#xA;function paths = which_nvcc()&#xA;% -------------------------------------------------------------------------&#xA;switch computer(&#x27;arch&#x27;)&#xA;  case &#x27;win64&#x27;&#xA;    [~, paths] = system(&#x27;where nvcc.exe&#x27;);&#xA;    paths = strtrim(paths);&#xA;    paths = paths(strfind(paths, &#x27;.exe&#x27;));&#xA;  case {&#x27;maci64&#x27;, &#x27;glnxa64&#x27;}&#xA;    [~, paths] = system(&#x27;which nvcc&#x27;);&#xA;    paths = strtrim(paths) ;&#xA;end&#xA;&#xA;% -------------------------------------------------------------------------&#xA;function cuda_root = search_cuda_devkit(opts)&#xA;% -------------------------------------------------------------------------&#xA;% This function tries to to locate a working copy of the CUDA Devkit.&#xA;&#xA;opts.verbose &amp;amp;&amp;amp; fprintf([&#x27;%s:\tCUDA: searching for the CUDA Devkit&#x27; ...&#xA;                    &#x27; (use the option &#x27;&#x27;CudaRoot&#x27;&#x27; to override):\n&#x27;], mfilename);&#xA;&#xA;% Propose a number of candidate paths for NVCC&#xA;paths = {getenv(&#x27;MW_NVCC_PATH&#x27;)} ;&#xA;paths = [paths, which_nvcc()] ;&#xA;for v = {&#x27;5.5&#x27;, &#x27;6.0&#x27;, &#x27;6.5&#x27;, &#x27;7.0&#x27;, &#x27;7.5&#x27;, &#x27;8.0&#x27;, &#x27;8.5&#x27;, &#x27;9.0&#x27;, &#x27;9.5&#x27;, &#x27;10.0&#x27;}&#xA;  switch computer(&#x27;arch&#x27;)&#xA;    case &#x27;glnxa64&#x27;&#xA;      paths{end&#x2B;1} = sprintf(&#x27;/usr/local/cuda-%s/bin/nvcc&#x27;, char(v)) ;&#xA;    case &#x27;maci64&#x27;&#xA;      paths{end&#x2B;1} = sprintf(&#x27;/Developer/NVIDIA/CUDA-%s/bin/nvcc&#x27;, char(v)) ;&#xA;    case &#x27;win64&#x27;&#xA;      paths{end&#x2B;1} = sprintf(&#x27;C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v%s\\bin\\nvcc.exe&#x27;, char(v)) ;&#xA;  end&#xA;end&#xA;paths{end&#x2B;1} = sprintf(&#x27;/usr/local/cuda/bin/nvcc&#x27;) ;&#xA;&#xA;% Validate each candidate NVCC path&#xA;for i=1:numel(paths)&#xA;  nvcc(i).path = paths{i} ;&#xA;  [nvcc(i).isvalid, nvcc(i).version] = validate_nvcc(paths{i}) ;&#xA;end&#xA;if opts.verbose&#xA;  fprintf(&#x27;\t| %5s | %5s | %-70s |\n&#x27;, &#x27;valid&#x27;, &#x27;ver&#x27;, &#x27;NVCC path&#x27;) ;&#xA;  for i=1:numel(paths)&#xA;    fprintf(&#x27;\t| %5d | %5d | %-70s |\n&#x27;, ...&#xA;            nvcc(i).isvalid, nvcc(i).version, nvcc(i).path) ;&#xA;  end&#xA;end&#xA;&#xA;% Pick an entry&#xA;index = find([nvcc.isvalid]) ;&#xA;if isempty(index)&#xA;  error(&#x27;Could not find a valid NVCC executable\n&#x27;) ;&#xA;end&#xA;[~, newest] = max([nvcc(index).version]);&#xA;nvcc = nvcc(index(newest)) ;&#xA;cuda_root = fileparts(fileparts(nvcc.path)) ;&#xA;&#xA;if opts.verbose&#xA;  fprintf(&#x27;%s:\tCUDA: choosing NVCC compiler &#x27;&#x27;%s&#x27;&#x27; (version %d)\n&#x27;, ...&#xA;          mfilename, nvcc.path, nvcc.version) ;&#xA;end&#xA;&#xA;% -------------------------------------------------------------------------&#xA;function [valid, cuver]  = validate_nvcc(nvccPath)&#xA;% -------------------------------------------------------------------------&#xA;[status, output] = system(sprintf(&#x27;&amp;quot;%s&amp;quot; --version&#x27;, nvccPath)) ;&#xA;valid = (status == 0) ;&#xA;if ~valid&#xA;  cuver = 0 ;&#xA;  return ;&#xA;end&#xA;match = regexp(output, &#x27;V(\d&#x2B;\.\d&#x2B;\.\d&#x2B;)&#x27;, &#x27;match&#x27;) ;&#xA;if isempty(match), valid = false ; return ; end&#xA;cuver = [1e4 1e2 1] * sscanf(match{1}, &#x27;V%d.%d.%d&#x27;) ;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function cuver = activate_nvcc(nvccPath)&#xA;% --------------------------------------------------------------------&#xA;&#xA;% Validate the NVCC compiler installation&#xA;[valid, cuver] = validate_nvcc(nvccPath) ;&#xA;if ~valid&#xA;  error(&#x27;The NVCC compiler &#x27;&#x27;%s&#x27;&#x27; does not appear to be valid.&#x27;, nvccPath) ;&#xA;end&#xA;&#xA;% Make sure that NVCC is visible by MEX by setting the MW_NVCC_PATH&#xA;% environment variable to the NVCC compiler path&#xA;if ~strcmp(getenv(&#x27;MW_NVCC_PATH&#x27;), nvccPath)&#xA;  warning(&#x27;Setting the &#x27;&#x27;MW_NVCC_PATH&#x27;&#x27; environment variable to &#x27;&#x27;%s&#x27;&#x27;&#x27;, nvccPath) ;&#xA;  setenv(&#x27;MW_NVCC_PATH&#x27;, nvccPath) ;&#xA;end&#xA;&#xA;% In some operating systems and MATLAB versions, NVCC must also be&#xA;% available in the command line search path. Make sure that this is%&#xA;% the case.&#xA;[valid_, cuver_] = validate_nvcc(&#x27;nvcc&#x27;) ;&#xA;if ~valid_ || cuver_ ~= cuver&#xA;  warning(&#x27;NVCC not found in the command line path or the one found does not matches &#x27;&#x27;%s&#x27;&#x27;.&#x27;, nvccPath);&#xA;  nvccDir = fileparts(nvccPath) ;&#xA;  prevPath = getenv(&#x27;PATH&#x27;) ;&#xA;  switch computer&#xA;    case &#x27;PCWIN64&#x27;, separator = &#x27;;&#x27; ;&#xA;    case {&#x27;GLNXA64&#x27;, &#x27;MACI64&#x27;}, separator = &#x27;:&#x27; ;&#xA;  end&#xA;  setenv(&#x27;PATH&#x27;, [nvccDir separator prevPath]) ;&#xA;  [valid_, cuver_] = validate_nvcc(&#x27;nvcc&#x27;) ;&#xA;  if ~valid_ || cuver_ ~= cuver&#xA;    setenv(&#x27;PATH&#x27;, prevPath) ;&#xA;    error(&#x27;Unable to set the command line path to point to &#x27;&#x27;%s&#x27;&#x27; correctly.&#x27;, nvccPath) ;&#xA;  else&#xA;    fprintf(&#x27;Location of NVCC (%s) added to your command search PATH.\n&#x27;, nvccDir) ;&#xA;  end&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;function cudaArch = get_cuda_arch(opts)&#xA;% --------------------------------------------------------------------&#xA;opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s:\tCUDA: determining GPU compute capability (use the &#x27;&#x27;CudaArch&#x27;&#x27; option to override)\n&#x27;, mfilename);&#xA;try&#xA;  gpu_device = gpuDevice();&#xA;  arch = str2double(strrep(gpu_device.ComputeCapability, &#x27;.&#x27;, &#x27;&#x27;));&#xA;  supparchs = get_nvcc_supported_archs(opts.nvccPath);&#xA;  [~, archi] = max(min(supparchs - arch, 0));&#xA;  arch_code = num2str(supparchs(archi));&#xA;  assert(~isempty(arch_code));&#xA;  cudaArch = ...&#xA;      sprintf(&#x27;-gencode=arch=compute_%s,code=\\\&amp;quot;sm_%s,compute_%s\\\&amp;quot; &#x27;, ...&#xA;              arch_code, arch_code, arch_code) ;&#xA;catch&#xA;  opts.verbose &amp;amp;&amp;amp; fprintf([&#x27;%s:\tCUDA: cannot determine the capabilities of the installed GPU and/or CUDA; &#x27; ...&#xA;                      &#x27;falling back to default\n&#x27;], mfilename);&#xA;  cudaArch = opts.defCudaArch;&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;function archs = get_nvcc_supported_archs(nvccPath)&#xA;% --------------------------------------------------------------------&#xA;switch computer(&#x27;arch&#x27;)&#xA;  case {&#x27;win64&#x27;}&#xA;    [status, hstring] = system(sprintf(&#x27;&amp;quot;%s&amp;quot; --help&#x27;,nvccPath));&#xA;  otherwise&#xA;    % fix possible output corruption (see manual)&#xA;    [status, hstring] = system(sprintf(&#x27;&amp;quot;%s&amp;quot; --help &amp;lt; /dev/null&#x27;,nvccPath)) ;&#xA;end&#xA;archs = regexp(hstring, &#x27;&#x27;&#x27;sm_(\d{2})&#x27;&#x27;&#x27;, &#x27;tokens&#x27;);&#xA;archs = cellfun(@(a) str2double(a{1}), archs);&#xA;if status, error(&#x27;NVCC command failed: %s&#x27;, hstring); end;&#xA;&#xA;% Force the use of x64 architecture in the mex command&#xA;if opts.enableGpu&#xA;  % If GPU is enabled, set the machine architecture explicitly to x64&#xA;  flags.mexcc = [flags.mexcc &#x27; -v COMPFLAGS=&amp;quot;$COMPFLAGS /machine:x64&amp;quot;&#x27;];&#xA;else&#xA;  % For CPU-only, also ensure x64 architecture&#xA;  flags.mexcc = [flags.mexcc &#x27; -v COMPFLAGS=&amp;quot;$COMPFLAGS /machine:x64&amp;quot;&#x27;];&#xA;end&#xA;&#xA;% Ensure that no ARM64 libraries are used; focus on x64 libraries only.&#xA;flags.link = [flags.link &#x27; -L&amp;quot;C:\path\to\x64\libs&amp;quot;&#x27;]; % Ensure correct x64 library paths&#xA;flags.lib = [flags.lib &#x27; -llibmx&#x27; &#x27; -llibmex&#x27; &#x27; -llibmat&#x27;]; % Ensure correct x64 libraries are linked&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/78868040</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Mex complier issue (windows 10, cuda 10.1, visual studio 2017)</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="visual-studio-2017" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="nvcc" />
        <author>
            <name>Reesh</name>
            <uri>https://stackoverflow.com/users/26752151</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/78868040/mex-complier-issue-windows-10-cuda-10-1-visual-studio-2017" />
        <published>2024-08-13T19:35:15Z</published>
        <updated>2024-08-18T14:27:08Z</updated>
        <summary type="html">
            &lt;p&gt;I am trying to follow this &lt;a href=&quot;https://github.com/NIH-NEI/REShAPE&quot; rel=&quot;nofollow noreferrer&quot;&gt;github&lt;/a&gt; and to open the code I am running into errors with the mex compiler:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;% Apply pre-compilation modifier function to adjust the flags and&#xA;% parameters. This can be used to add additional files to compile on the&#xA;% fly.&#xA;if ~isempty(opts.preCompileFn)&#xA;  [opts, mex_src, lib_src, flags] = opts.preCompileFn(opts, mex_src, lib_src, flags) ;&#xA;end&#xA;&#xA;% Compile intermediate object files&#xA;srcs = horzcat(lib_src,mex_src) ;&#xA;for i = 1:numel(horzcat(lib_src, mex_src))&#xA;  [~,~,ext] = fileparts(srcs{i}) ; ext(1) = [] ;&#xA;  objfile = toobj(flags.bld_dir,srcs{i});&#xA;  if strcmp(ext,&#x27;cu&#x27;)&#xA;    if strcmp(opts.cudaMethod,&#x27;nvcc&#x27;)&#xA;      nvcc_compile(opts, srcs{i}, objfile, flags) ;&#xA;    else&#xA;      mexcuda_compile(opts, srcs{i}, objfile, flags) ;&#xA;    end&#xA;  else&#xA;    mex_compile(opts, srcs{i}, objfile, flags) ;&#xA;  end&#xA;  assert(exist(objfile, &#x27;file&#x27;) ~= 0, &#x27;Compilation of %s failed.&#x27;, objfile);&#xA;end&#xA;&#xA;% Link MEX files&#xA;for i = 1:numel(mex_src)&#xA;  objs = toobj(flags.bld_dir, [mex_src(i), lib_src]) ;&#xA;  mex_link(opts, objs, flags.mex_dir, flags) ;&#xA;end&#xA;&#xA;% Reset path adding the mex subdirectory just created&#xA;vl_setupnn() ;&#xA;&#xA;if strcmp(arch, &#x27;win64&#x27;) &amp;amp;&amp;amp; opts.enableCudnn&#xA;  if opts.verbose(), fprintf(&#x27;Copying CuDNN dll to mex folder.\n&#x27;); end&#xA;  copyfile(fullfile(opts.cudnnRoot, &#x27;bin&#x27;, &#x27;*.dll&#x27;), flags.mex_dir);&#xA;end&#xA;&#xA;% Save the last compile flags to the build dir&#xA;if isempty(opts.preCompileFn)&#xA;  save(fullfile(flags.bld_dir, &#x27;last_compile_opts.mat&#x27;), &#x27;-struct&#x27;, &#x27;opts&#x27;);&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;%                                                    Utility functions&#xA;% --------------------------------------------------------------------&#xA;&#xA;% --------------------------------------------------------------------&#xA;function check_compability(arch)&#xA;% --------------------------------------------------------------------&#xA;cc = mex.getCompilerConfigurations(&#x27;C&#x2B;&#x2B;&#x27;);&#xA;if isempty(cc)&#xA;  error([&#x27;Mex is not configured.&#x27;...&#xA;    &#x27;Run &amp;quot;mex -setup&amp;quot; to configure your compiler. See &#x27;,...&#xA;    &#x27;http://www.mathworks.com/support/compilers &#x27;, ...&#xA;    &#x27;for supported compilers for your platform.&#x27;]);&#xA;end&#xA;&#xA;switch arch&#xA;  case &#x27;win64&#x27;&#xA;    clversion = str2double(cc.Version);&#xA;    if clversion &amp;lt; 14&#xA;      error(&#x27;Unsupported VS C&#x2B;&#x2B; compiler, ver &amp;gt;=14.0 required (VS 2015).&#x27;);&#xA;    end&#xA;  case &#x27;maci64&#x27;&#xA;  case &#x27;glnxa64&#x27;&#xA;  otherwise, error(&#x27;Unsupported architecture &#x27;&#x27;%s&#x27;&#x27;.&#x27;, arch) ;&#xA;end&#xA;&#xA;% --------------------------------------------------------------------&#xA;function done = check_deps(opts, tgt, src)&#xA;% --------------------------------------------------------------------&#xA;done = false ;&#xA;if ~iscell(src), src = {src} ; end&#xA;if ~opts.continue, return ; end&#xA;if ~exist(tgt,&#x27;file&#x27;), return ; end&#xA;ttime = dir(tgt) ; ttime = ttime.datenum ;&#xA;for i=1:numel(src)&#xA;  stime = dir(src{i}) ; stime = stime.datenum ;&#xA;  if stime &amp;gt; ttime, return ; end&#xA;end&#xA;fprintf(&#x27;%s: &#x27;&#x27;%s&#x27;&#x27; already there, skipping.\n&#x27;, mfilename, tgt) ;&#xA;done = true ;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function objs = toobj(bld_dir, srcs)&#xA;% --------------------------------------------------------------------&#xA;str = [filesep, &#x27;src&#x27;, filesep]; % NASTY. Do with regexp?&#xA;multiple = iscell(srcs) ;&#xA;if ~multiple, srcs = {srcs} ; end&#xA;objs = cell(1, numel(srcs));&#xA;for t = 1:numel(srcs)&#xA;  i = strfind(srcs{t},str);&#xA;  i = i(end); % last occurence of &#x27;/src/&#x27;&#xA;  objs{t} = fullfile(bld_dir, srcs{t}(i&#x2B;numel(str):end)) ;&#xA;end&#xA;if ~multiple, objs = objs{1} ; end&#xA;objs = regexprep(objs,&#x27;.cpp$&#x27;,[&#x27;.&#x27; objext]) ;&#xA;objs = regexprep(objs,&#x27;.cu$&#x27;,[&#x27;.&#x27; objext]) ;&#xA;objs = regexprep(objs,&#x27;.c$&#x27;,[&#x27;.&#x27; objext]) ;&#xA;&#xA;% --------------------------------------------------------------------&#xA;function mex_compile(opts, src, tgt, flags)&#xA;% --------------------------------------------------------------------&#xA;if check_deps(opts, tgt, src), return ; end&#xA;args = horzcat({&#x27;-c&#x27;, &#x27;-outdir&#x27;, fileparts(tgt), src}, ...&#xA;  flags.base, flags.mex, ...&#xA;  {[&#x27;CXXFLAGS=$CXXFLAGS &#x27; strjoin(flags.cxx)]}, ...&#xA;  {[&#x27;CXXOPTIMFLAGS=$CXXOPTIMFLAGS &#x27; strjoin(flags.cxxoptim)]}) ;&#xA;opts.verbose &amp;amp;&amp;amp; fprintf(&#x27;%s: MEX CC: %s\n&#x27;, mfilename, strjoin(args)) ;&#xA;mex(args{:}) ;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;specifically this error in this block of code:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Error in vl_compilenn&amp;gt;mex_compile (line 599)&#xA;mex(args{:}) ;&#xA;&#xA;Error in vl_compilenn (line 508)&#xA;    mex_compile(opts, srcs{i}, objfile, flags) ;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;How do I resolve this issue?&lt;/p&gt;&#xA;&lt;p&gt;We tried to use &#x27;nvcc&#x27; and switched over to &#x27;mex&#x27;. We haven&#x27;t been able to figure it out for months. We also installed matlab compiler MinGW-w64 C/C&#x2B;&#x2B; compiler.&lt;/p&gt;&#xA;&lt;p&gt;Here is more of the error message: and that works here is more of the error message:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;`:\Users\gelme\REShAPE\Segmentation\matconvnet\matlab\src\bits\impl\imread_gdiplus.cpp:14:&#xA;C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/lib/gcc/x86_64-w64-mingw32/6.3.0/include/tmmintrin.h:&#xA;In function &#x27;__m128i _mm_hadd_epi16(__m128i, __m128i)&#x27;:&#xA;C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/lib/gcc/x86_64-w64-mingw32/6.3.0/include/tmmintrin.h:42:70:&#xA;error: &#x27;__builtin_ia32_phaddw128&#x27; was not declared in this scope&#xA;   return (__m128i) __builtin_ia32_phaddw128 ((__v8hi)__X, (__v8hi)__Y);&#xA;                                                                      ^&#xA;C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/lib/gcc/x86_64-w64-mingw32/6.3.0/include/tmmintrin.h:&#xA;In function &#x27;__m128i _mm_hadd_epi32(__m128i, __m128i)&#x27;:&#xA;C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/lib/gcc/x86_64-w64-mingw32/6.3.0/include/tmmintrin.h:48:70:&#xA;error: &#x27;__builtin_ia32_phaddd128&#x27; was not declared in this scope&#xA;   return (__m128i) __builtin_ia32_phaddd128 ((__v4si)__X, (__v4si)__Y);&#xA;                                                                      ^&#xA;C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/lib/gcc/x86_64-w64-mingw32/6.3.0/include/tmmintrin.h:&#xA;In function &#x27;__m128i _mm_hadds_epi16(__m128i, __m128i)&#x27;:&#xA;C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/lib/gcc/x86_64-w64-mingw32/6.3.0/include/tmmintrin.h:54:71:&#xA;error: &#x27;__builtin_ia32_phaddsw128&#x27; was not declared in this scope&#xA;   return (__m128i) __builtin_ia32_phaddsw128 ((__v8hi)__X, (__v8hi)__Y);&#xA;                                                                       ^&#xA;C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/lib/gcc/x86_64-w64-mingw32/6.3.0/include/tmmintrin.h:&#xA;In function &#x27;__m64 _mm_hadd_pi16(__m64, __m64)&#x27;:&#xA;C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/lib/gcc/x86_64-w64-mingw32/6.3.0/include/tmmintrin.h:60:65:&#xA;error: &#x27;__builtin_ia32_phaddw&#x27; was not declared in this scope&#xA;   return (__m64) __builtin_ia32_phaddw ((__v4hi)__X, (__v4hi)__Y);`&#xA;                                                             ^&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/78439685</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">how do I copy uint16 bytes from one array into a MATLABString in mex C&#x2B;&#x2B;</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Jimbo</name>
            <uri>https://stackoverflow.com/users/764365</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/78439685/how-do-i-copy-uint16-bytes-from-one-array-into-a-matlabstring-in-mex-c" />
        <published>2024-05-07T01:07:48Z</published>
        <updated>2024-08-14T11:01:49Z</updated>
        <summary type="html">
            &lt;p&gt;I have a matrix of bytes, or for this example we can assume I&#x27;ve converted to uint16. I am curious how to copy a subset of the data elements into a MATLABString (i.e., using the mex C&#x2B;&#x2B; interface). I asked ChatGPT but it is using a charArray and not a string. It also looks like it has an unnecessary data copy going on at the end (I could be wrong about that). It also is not clear why I need an iterator but that may be because I am used to C and working more with pointers. Is this the best approach? The original solution had a loop to copy/cast each element but I asked it to use &lt;code&gt;memcpy&lt;/code&gt; as I&#x27;m under the impression that &lt;code&gt;memcpy&lt;/code&gt; may be faster and also just seems like it should be the right way to copy a lot of data from one array to another.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#include &amp;quot;mex.hpp&amp;quot;&#xA;#include &amp;quot;mexAdapter.hpp&amp;quot;&#xA;&#xA;class MexFunction : public matlab::mex::Function {&#xA;public:&#xA;    void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) override {&#xA;        // Check input and output arguments&#xA;        checkArguments(outputs, inputs);&#xA;&#xA;        // Get input arguments&#xA;        matlab::data::TypedArray&amp;lt;uint16_t&amp;gt; data = std::move(inputs[0]);&#xA;        double startIndex = inputs[1][0];&#xA;        double endIndex = inputs[2][0];&#xA;        size_t subsetLength = static_cast&amp;lt;size_t&amp;gt;(endIndex - startIndex &#x2B; 1);&#xA;&#xA;        // Check bounds&#xA;        size_t dataSize = data.getNumberOfElements();&#xA;        if (startIndex &amp;lt; 1 || endIndex &amp;gt;= dataSize || startIndex &amp;gt; endIndex) {&#xA;            matlab::engine::MATLABException::throwAsCaller(&amp;quot;Subset indices are out of bounds.&amp;quot;);&#xA;        }&#xA;&#xA;        // Extract subset of uint16 array&#xA;        matlab::data::TypedArray&amp;lt;uint16_t&amp;gt;::iterator subsetData = data.begin() &#x2B; static_cast&amp;lt;size_t&amp;gt;(startIndex) - 1;&#xA;&#xA;        // Convert subset to character array using memcpy&#xA;        std::string charArray(subsetLength, &#x27;\0&#x27;);&#xA;        memcpy(&amp;amp;charArray[0], subsetData, subsetLength * sizeof(uint16_t));&#xA;&#xA;        // Create MATLABString&#xA;        outputs[0] = factory.createCharArray(charArray);&#xA;    }&#xA;&#xA;private:&#xA;    matlab::data::ArrayFactory factory;&#xA;&#xA;    void checkArguments(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {&#xA;        if (inputs.size() != 3) {&#xA;            matlab::engine::MATLABException::throwAsCaller(&amp;quot;Three input arguments required.&amp;quot;);&#xA;        }&#xA;        if (outputs.size() != 1) {&#xA;            matlab::engine::MATLABException::throwAsCaller(&amp;quot;One output argument required.&amp;quot;);&#xA;        }&#xA;        if (!inputs[0].isUint16() || !inputs[1].isDouble() || !inputs[2].isDouble()) {&#xA;            matlab::engine::MATLABException::throwAsCaller(&amp;quot;Inputs must be uint16 and double arrays.&amp;quot;);&#xA;        }&#xA;    }&#xA;};&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/77986687</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">What software is required for compiling Matlab mex file from Fortran Silver-frost FTN95 project?</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="fortran" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="silverfrost-fortran" />
        <author>
            <name>Shinrei</name>
            <uri>https://stackoverflow.com/users/7617574</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/77986687/what-software-is-required-for-compiling-matlab-mex-file-from-fortran-silver-fros" />
        <published>2024-02-13T09:06:48Z</published>
        <updated>2024-02-13T09:06:48Z</updated>
        <summary type="html">
            &lt;p&gt;I want to call Fortran subroutines from Matlab and get the output from them. I&#x27;m using Windows 7 x64 and Matlab R2021b.&lt;/p&gt;&#xA;&lt;p&gt;I have tried compiling in Matlab using mex command.&#xA;At&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;mex -setup FORTRAN &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I have choosen Intel Parallel Studio XE 2019 for Fortran with Microsoft Visual Studio 2015.&lt;/p&gt;&#xA;&lt;p&gt;In mex settings: &#x27;C:\Program Files\MATLAB\R2021b\bin\win64\mexopts\intel_fortran_19_vs2015.xml&#x27; I have removed \fixed option from COMPFLAGS.&#xA;It appeared that it is not able to recognise f95 files:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Unknown file extension &#x27;.f95&#x27;.&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;After changing extension of fortran files from f95 to f90 and trying to compile I got error:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;error&#xA;#5078: Unrecognized token &#x27;@&#x27; skipped&#xA;    result_logic = FEXISTS@(filename,errorcode)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I&#x27;m not sure if this is the problem of compiler, which is probably not supporting Silver-frost FTN95, or I have to include FTN95 Library in some way.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/77961724</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">issues with SPM12 and iMac (Apple M1) - Sonoma</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="matlab-spm" />
        <author>
            <name>Isabella Mariani</name>
            <uri>https://stackoverflow.com/users/23365537</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/77961724/issues-with-spm12-and-imac-apple-m1-sonoma" />
        <published>2024-02-08T12:38:41Z</published>
        <updated>2024-02-10T21:21:12Z</updated>
        <summary type="html">
            &lt;p&gt;I get this error. I&#x27;ve tried to fix it following the guidelines online but it seems impossible to solve.&#xA;Can someone help me please?&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Error using spm_check_installation&amp;gt;check_basic&lt;br /&gt;&#xA;SPM uses a number of MEX files, which are compiled functions.&#xA;These need to be compiled for the various platforms on which SPM&#xA;is run. It seems that the compiled files for your computer platform&#xA;are missing or not compatible. See&#xA;&lt;a href=&quot;https://en.wikibooks.org/wiki/SPM/Installation_on_64bit_Mac_OS_(Intel)&quot; rel=&quot;nofollow noreferrer&quot;&gt;https://en.wikibooks.org/wiki/SPM/Installation_on_64bit_Mac_OS_(Intel)&lt;/a&gt;&#xA;for information about how to compile MEX files for MACI64&#xA;in MATLAB 23.2.0.2485118 (R2023b) Update 6.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Error in spm_check_installation (line 28)&#xA;       check_basic;&#xA;&#xA;Error in spm (line 290)&#xA;spm_check_installation(&#x27;basic&#x27;);&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;I tried to follow &lt;a href=&quot;https://en.wikibooks.org/wiki/SPM/Installation_on_64bit_Mac_OS_(Intel)and&quot; rel=&quot;nofollow noreferrer&quot;&gt;https://en.wikibooks.org/wiki/SPM/Installation_on_64bit_Mac_OS_(Intel)and&lt;/a&gt; also this command  &lt;code&gt;xattr -dr com.apple.quarantine *.mexmaci64&lt;/code&gt; suggested by Chris Rorden.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/77760595</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Using MATLAB objects from custom C&#x2B;&#x2B; libraries to be used by MEX functions</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="cmake" />
            <category scheme="https://stackoverflow.com/tags" term="shared-libraries" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Bryan</name>
            <uri>https://stackoverflow.com/users/22869820</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/77760595/using-matlab-objects-from-custom-c-libraries-to-be-used-by-mex-functions" />
        <published>2024-01-04T18:58:43Z</published>
        <updated>2024-01-05T18:35:50Z</updated>
        <summary type="html">
            &lt;p&gt;My current MEX file is properly compiling, however, I would like to create let&#x27;s say several MEX files:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;mex_a.cpp&lt;/li&gt;&#xA;&lt;li&gt;mex_b.cpp&lt;/li&gt;&#xA;&lt;li&gt;mex_c.cpp&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;At this moment, mex_a.cpp contains the typical function definition:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;class MexFunction : public matlab::mex::Function {&#xA;&#xA;private:&#xA;    // Get pointer to engine&#xA;    std::shared_ptr&amp;lt;matlab::engine::MATLABEngine&amp;gt; matlabPtr = getEngine();&#xA;&#xA;    // Get array factory&#xA;    std::shared_ptr&amp;lt;matlab::data::ArrayFactory&amp;gt; factoryPtr;&#xA;&#xA;private:&#xA; void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) override&#xA;    {&#xA;        try&#xA;        {&#xA;            // Parse inputs..&#xA;            mex_aux-&amp;gt;mex_common_function(...)&#xA;            // Parse outputs..&#xA;        }&#xA;        catch (int)&#xA;        {&#xA;            std::fprintf(stdout, &amp;quot;ERROR: Something unexpected happened...&amp;quot;);&#xA;        }&#xA;    }&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;In &lt;code&gt;mex_aux&lt;/code&gt;, we can find the definition and implementation of &lt;code&gt;mex_common_function&lt;/code&gt;. The purpose of this object is to be used by mex_a.cpp, mex_b.cpp and mex_c.cpp, basically what a common library do.&lt;/p&gt;&#xA;&lt;p&gt;My findings are:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;If mex_common_function is static, everything works.&lt;/li&gt;&#xA;&lt;li&gt;Otherwise, it doesn&#x27;t work while using matlab objects.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;The .hpp file looks like:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#pragma once&#xA;&#xA;// Include system libraries&#xA;# include &amp;lt;string&amp;gt;&#xA;# include &amp;lt;utility&amp;gt;&#xA;&#xA;// MATLAB stuff&#xA;#include &amp;quot;mex.hpp&amp;quot;&#xA;&#xA;class mex_aux&#xA;{&#xA;public:&#xA;    // Constructor&#xA;    mex_aux() = default;&#xA;&#xA;    // Destructor&#xA;    ~mex_aux() = default;&#xA;&#xA;private:&#xA;    // Get pointer to engine&#xA;    std::shared_ptr&amp;lt;matlab::engine::MATLABEngine&amp;gt; matlabPtr;&#xA;&#xA;    // Get array factory&#xA;    std::shared_ptr&amp;lt;matlab::data::ArrayFactory&amp;gt; factoryPtr;&#xA;&#xA;public:&#xA;    // Setters:&#xA;    void set_matlab_ptr(std::shared_ptr&amp;lt;matlab::engine::MATLABEngine&amp;gt;&amp;amp; matlab_ptr) {this-&amp;gt;matlabPtr = matlab_ptr; }&#xA;    void set_factory_ptr(std::shared_ptr&amp;lt;matlab::data::ArrayFactory&amp;gt;&amp;amp; factory_ptr) {this-&amp;gt;factoryPtr = factory_ptr; }&#xA;&#xA;public:&#xA;    // The used function, if this one is set as static (hence not using members of the class) it works.&#xA;    void mex_common_function();&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;What I am doing now, and hence getting the following error in MATLAB (not during building time) is:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Setting &lt;code&gt;matlabPtr&lt;/code&gt; as an internal pointer in &lt;code&gt;mex_aux&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Same for &lt;code&gt;factoryPtr&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;This both from the main mex function file (i.e., mex_a.cpp) after building &lt;code&gt;mex_aux&lt;/code&gt;, the setters are used.&lt;/p&gt;&#xA;&lt;p&gt;Error in MATLAB:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Invalid MEX-file&#xA;&#x27;/path/to/mex/mexfunction.mexa64&#x27;:&#xA;/path/to/external/libextern.so:&#xA;undefined symbol:&#xA;_ZN6matlab6engine12MATLABEngine5fevalERKNSt7__cxx1112basic_stringIDsSt11char_traitsIDsESaIDsEEEiRKSt6vectorINS_4data5ArrayESaISC_EERKSt10shared_ptrISt15basic_streambufIDsS5_EESM_&#xA;&#xA;Error in mex_vsod (line 50)&#xA;    b(:,:,i) = mexfunction(...);&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;My CMakeLists.txt:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;# MEX AUX Library&#xA;set(LIBRARY_MEX_AUX &amp;quot;mex_aux&amp;quot;)&#xA;&#xA;add_library(${LIBRARY_MEX_AUX} SHARED&#xA;        src/core/tools/mex_aux.cpp)&#xA;&#xA;target_link_libraries(${LIBRARY_MEX_AUX}&#xA;        ${Matlab_MEX_LIBRARY}&#xA;        ${Matlab_MX_LIBRARY})&#xA;&#xA;set_target_properties(${LIBRARY_MEX_AUX} PROPERTIES&#xA;        COMPILE_FLAGS &amp;quot;-fPIC&amp;quot;&#xA;        LINK_FLAGS &amp;quot;-Wl,-rpath,./&amp;quot;) # To use relative paths in shared libs&#xA;&#xA;&#xA;# MEX FILES&#xA;if (BUILD_WITH_MATLAB)&#xA;    # MEX functions files&#xA;    matlab_add_mex(NAME mex_vsaod&#xA;            SRC src/main/mex_cpp/mex_a.cpp&#xA;            LINK_TO ${LIBRARY_target1} ... ${LIBRARY_targetN} ${LIBRARY_MEX_AUX})&#xA;endif ()&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I guess something wrong is happening within the memory due to a pointer from &lt;code&gt;mex_aux&lt;/code&gt; pointing possibly to an un-accessible place in memory (only scoped to the mex file -mex_a.cpp?-)...&lt;/p&gt;&#xA;&lt;p&gt;Can someone give me a clue? I would really appreciate it.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/77434788</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">export LD_PRELOAD for matlab MEX files</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="libraries" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="shared-objects" />
            <category scheme="https://stackoverflow.com/tags" term="ld-preload" />
        <author>
            <name>Bryan</name>
            <uri>https://stackoverflow.com/users/22869820</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/77434788/export-ld-preload-for-matlab-mex-files" />
        <published>2023-11-06T23:37:49Z</published>
        <updated>2023-11-07T13:50:07Z</updated>
        <summary type="html">
            &lt;p&gt;I created a MEX executable and this one can be executed as a matlab function only if I &lt;code&gt;export LD_PRELOAD&lt;/code&gt; all the needed libraries.&lt;/p&gt;&#xA;&lt;p&gt;I wanted to automate this procedure cause it is using a bunch of in house made libraries and it&#x27;s a tedious task to write &lt;code&gt;export LD_PRELOAD&lt;/code&gt; every time in the terminal.&lt;/p&gt;&#xA;&lt;p&gt;So I did a bash script:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#!/bin/bash&#xA;echo &amp;quot;PRELOAD MATLAB SCRIPT&amp;quot;&#xA;&#xA;# Specify the folder containing the .so files&#xA;so_folder=&amp;quot;/path/to/lib&amp;quot;&#xA;&#xA;# Check if the folder exists&#xA;if [ ! -d &amp;quot;$so_folder&amp;quot; ]; then&#xA;  echo &amp;quot;The folder &#x27;$so_folder&#x27; does not exist.&amp;quot;&#xA;  exit 1&#xA;fi&#xA;&#xA;echo &amp;quot;Searching in .so folder: $so_folder ...&amp;quot;&#xA;&#xA;# STD library problem from LINUX users only&#xA;export LD_PRELOAD=$LD_PRELOAD:/lib/x86_64-linux-gnu/libstdc&#x2B;&#x2B;.so.6 matlab&#xA;&#xA;# Set DACE library&#xA;export LD_PRELOAD=$LD_PRELOAD:/path/to/dace/lib/libdace.so matlab&#xA;&#xA;# Loop through .so files in the folder&#xA;for so_file in &amp;quot;$so_folder&amp;quot;/*.so; do&#xA;  if [ -f &amp;quot;$so_file&amp;quot; ]; then&#xA;    # Set LD_PRELOAD and launch MATLAB for each .so file&#xA;    export LD_PRELOAD=&amp;quot;$LD_PRELOAD:$so_file&amp;quot; matlab&#xA;    echo &amp;quot;Preloading library $so_file to MATLAB...&amp;quot;&#xA;  fi&#xA;done&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Then, I call this script from the root of the project, where I also launch matlab afterwards:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;$ bash scripts/preload_matlab_libs.sh&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;After it, I launch MATLAB and the .so files are not found as the error shows.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; mex_test&#xA;Invalid MEX-file &#x27;/path/to/project/mex/mex_vsod.mexa64&#x27;: libdace.so.2: cannot open&#xA;shared object file: No such file or directory&#xA;&#xA;Error in mex_test (line 13)&#xA;b = mex_vsod(input1, input2)&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;No error is happening if I do the &lt;code&gt;export LD_PRELOAD&lt;/code&gt;...&lt;/p&gt;&#xA;&lt;p&gt;I&#x27;m a bit new in MEX compiled files. Therefore my questions:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Why is the bash script not working? How do I fix it?&lt;/li&gt;&#xA;&lt;li&gt;Is there any other better approach to solve this issue?&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Thanks!&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/77184611</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Errors when using Matlab mex</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="fortran" />
            <category scheme="https://stackoverflow.com/tags" term="intel" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="intel-oneapi" />
        <author>
            <name>kun zhao</name>
            <uri>https://stackoverflow.com/users/12611025</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/77184611/errors-when-using-matlab-mex" />
        <published>2023-09-27T04:01:13Z</published>
        <updated>2023-09-27T20:20:45Z</updated>
        <summary type="html">
            &lt;p&gt;I am using Matlab 2023a to call Fortran source file using mex. I use the following compiler for Fortran language compilation:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Intel oneAPI 2023 for Fortran with Microsoft Visual Studio 2022.&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;The code I used to compile is&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;mex ero_dep_fortran.f90 -output ero_dep_fortran&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;However, I got the following erros:&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;Error using mex&#xA;C:\Users\40967\Desktop\Paper bank collapse and meander migration\Draft-version2\Code\ero_dep_fortran.lib &#xFFFD;&#x376;&#xFFFD;&#xFFFD;&#xFFFD;&#xA;C:\Users\40967\Desktop\Paper bank collapse and meander migration\Draft-version2\Code\ero_dep_fortran.exp&#xA;ero_dep_fortran.obj : error LNK2019: &#xFFFD;&#x7B7;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#x2CBF;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD; NEW_EPOINTS_ASSIGNMENT&#xFFFD;&#xFFFD;&#xFFFD;&#xF7;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#x6BA;&#xFFFD;&#xFFFD;&#xFFFD; MEXFUNCTION &#xFFFD;&#x431;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xA;ero_dep_fortran.obj : error LNK2019: &#xFFFD;&#x7B7;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#x2CBF;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD; omp_get_wtime&#xFFFD;&#xFFFD;&#xFFFD;&#xF7;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#x6BA;&#xFFFD;&#xFFFD;&#xFFFD; BANK_EROSION_CALCULATION_ENTRY &#xFFFD;&#x431;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xA;C:\Users\40967\Desktop\Paper bank collapse and meander migration\&#xFFFD;&#x7B8;&#x13A;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;\Draft-version2\Code\ero_dep_fortran.mexw64 : fatal error LNK1120: 2&#xFFFD;&#xFFFD;&#xFFFD;&#x7B7;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&#x2CBF;&#xFFFD;&#xFFFD;&#xFFFD;&#xFFFD;&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;A lot of messy code in the error. I found the main problem maybe&lt;/p&gt;&#xA;&lt;blockquote&gt;&#xA;&lt;p&gt;ero_dep_fortran.obj : error LNK2019&lt;/p&gt;&#xA;&lt;/blockquote&gt;&#xA;&lt;p&gt;but I do not know how to slove it...This code can be run in other computers with lower version of Matlab and Intel Parallel Studio XE, Microsoft Visual Studio.&lt;/p&gt;&#xA;&lt;p&gt;Any suggestion is grateful....&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/77140911</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">What differences between win32 and win64 might cause error in executing mex file?</title>
            <category scheme="https://stackoverflow.com/tags" term="c" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>MarryD</name>
            <uri>https://stackoverflow.com/users/22598663</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/77140911/what-differences-between-win32-and-win64-might-cause-error-in-executing-mex-file" />
        <published>2023-09-20T09:06:44Z</published>
        <updated>2023-09-20T16:20:06Z</updated>
        <summary type="html">
            &lt;p&gt;I am trying to run example provided by field II on my computer. This example contains matlab functions as well as mex32 file. I have later version of matlab so i take initial .c file and converted it into mex64. However despite setting everything like in provided example my mex function does not work.  I am not that familiar with c programming so i am not sure where exacly in the main function problem occurs. If anyone please could direct me what is going wrong in this .c program. Also, can i succesfully transform this c code to matlab function instead of using mex file or is there some nuances in the C code that can not be fully captured in the MATLAB translation?&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&quot;https://i.sstatic.net/EJcDw.png&quot; rel=&quot;nofollow noreferrer&quot;&gt;Matlab error&lt;/a&gt;&#xA;&lt;a href=&quot;https://field-ii.dk/examples/ftp_files/kidney/fast_int.c&quot; rel=&quot;nofollow noreferrer&quot;&gt;full original C code&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;I was trying to write matlab function based on algorithm from C program but i am not sure if i am getting indexing correctly. I am aware that matlab indices starts from 1 not 0. What i don&#x27;t understand is this env_pointer being set to be one variable&#x27;s address and then is called like it contains N_samples&#x2B;1 variables.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;void make_interpolation (unsigned char  *envelope_data,   /*  The envelope detected and log-compressed data */&#xA;                 int            N_samples,        /*  Number of samples in one envelope line        */&#xA;             &#xA;                 int            *index_samp_line, /*  Index for the data sample number              */&#xA;                 int            *image_index,     /*  Index for the image matrix                    */&#xA;                         float          *weight_coef,     /*  The weight table                              */&#xA;                         int            N_values,         /*  Number of values to calculate in the image      */&#xA;             &#xA;                         unsigned char *image)            /*  The resulting image                           */&#xA;&#xA;{int           i;                 /*  Integer loop counter                */&#xA; int           ij_index_coef;     /*  Index into coefficient array        */&#xA; unsigned char *env_pointer;      /*  Pointer to the envelope data        */&#xA; float         *weight_pointer;   /*  Pointer to the weight coefficients  */&#xA; &#xA;  ij_index_coef = 0;&#xA;  for (i=0; i&amp;lt;N_values; i&#x2B;&#x2B;)&#xA;    {&#xA;     weight_pointer = &amp;amp;(weight_coef[ij_index_coef]);&#xA;     env_pointer = &amp;amp;(envelope_data[index_samp_line[i]]);&#xA;     image[image_index[i]] &#xA;              =         weight_pointer[0] * env_pointer[0]&#xA;                      &#x2B; weight_pointer[1] * env_pointer[1]&#xA;                      &#x2B; weight_pointer[2] * env_pointer[N_samples]&#xA;                      &#x2B; weight_pointer[3] * env_pointer[N_samples&#x2B;1] &#x2B; 0.5;&#xA;     ij_index_coef = ij_index_coef &#x2B; 4;&#xA;    }&#xA;}&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/77129875</id>
        <re:rank scheme="https://stackoverflow.com">2</re:rank>
        <title type="text">How do I access variables on Fortran from MATLAB using MEX (without copying them)?</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="fortran" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Vinicius Oliveira Fontes</name>
            <uri>https://stackoverflow.com/users/22588044</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/77129875/how-do-i-access-variables-on-fortran-from-matlab-using-mex-without-copying-them" />
        <published>2023-09-18T19:20:41Z</published>
        <updated>2023-09-19T01:06:03Z</updated>
        <summary type="html">
            &lt;p&gt;I would to ask how I can access a variable on Fortran from MATLAB without making a copy. Preferrably, I would like to access the variable a variable on Fortran (it may be &#x27;read-only&#x27;) that was declared and initialized on MATLAB and passed as an input to my Fortran-based MEX function as input (pretty much a pass-by-reference), and if possible, do it for a multidimensional array.&lt;/p&gt;&#xA;&lt;p&gt;The solutions that I have found so far require copying the variable, which is quite costly for large inputs.&lt;/p&gt;&#xA;&lt;p&gt;So far, I have tried the approach based on the solution given in the &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_external/create-fortran-source-mex-file.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;MATLAB documentation&lt;/a&gt;, where they implement a function in the form &lt;code&gt;y = times2(x)&lt;/code&gt; which doubles my double scalar &lt;code&gt;x&lt;/code&gt;. However, it requires copying the variable &lt;code&gt;x&lt;/code&gt;, so there is the original &lt;code&gt;x&lt;/code&gt; declared on MATLAB and another one on Fortran.&lt;/p&gt;&#xA;&lt;p&gt;For reference, MATLAB recommends first declaring a pointer and its associated variable&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;mwPointer x_ptr real*8 x_input&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;And then copying the content of the MATLAB variable &lt;code&gt;x&lt;/code&gt; into the Fortran variable &lt;code&gt;x_input&lt;/code&gt; via:&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;call mxCopyPtrToReal8(x_ptr,x_input,size)&lt;/code&gt;&lt;/p&gt;&#xA;&lt;p&gt;Where &lt;code&gt;size&lt;/code&gt; is 1, as &lt;code&gt;x&lt;/code&gt; is a scalar, but in my application I will eventually use a multi-dimensional array.&lt;/p&gt;&#xA;&lt;p&gt;This solution is unappealing when one needs to operate on large arrays, as they would be copied, doubling my memory requirements. I have found &lt;a href=&quot;https://www.mathworks.com/help/matlab/apiref/mexgetvariableptr.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;this function&lt;/a&gt; &lt;code&gt;mexGetVariablePtr&lt;/code&gt; which returns a read-only MATLAB pointer that theoretically should give me access to the variable &lt;code&gt;x&lt;/code&gt; without copying it.&lt;/p&gt;&#xA;&lt;p&gt;My question is: how do I access this variable, as the only function that I found in the API documentation, &lt;code&gt;mxCopyPtrToReal&lt;/code&gt;, copies the variable.&lt;/p&gt;&#xA;&lt;p&gt;Additional information:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;I only want to read the contents of the variable &#x27;x&#x27; and use as a normal Fortran variable without modifying it.&lt;/li&gt;&#xA;&lt;li&gt;The output of &#x27;mexGetVariablePtr&#x27; is a &#x27;mwPointer&#x27;, which seems to not be easy to dereference like regular Fortran pointers.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Thank you for your attention!&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/77105025</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Matlab exits without error upon clear mex with openMP</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="openmp" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>rinkert</name>
            <uri>https://stackoverflow.com/users/7054628</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/77105025/matlab-exits-without-error-upon-clear-mex-with-openmp" />
        <published>2023-09-14T12:45:54Z</published>
        <updated>2023-09-15T14:03:55Z</updated>
        <summary type="html">
            &lt;p&gt;I ran into a weird case where Matlab closes without an error.&lt;/p&gt;&#xA;&lt;p&gt;I have the following mex file that uses OpenMP&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-cpp prettyprint-override&quot;&gt;&lt;code&gt;// main.cpp&#xA;#include &amp;quot;mex.h&amp;quot;&#xA;#include &amp;quot;omp.h&amp;quot;&#xA;&#xA;void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])&#xA;{&#xA;    int num_threads;&#xA;    int kx;&#xA;#pragma omp parallel for&#xA;    for (kx = 0; kx &amp;lt; 100; kx&#x2B;&#x2B;)&#xA;    {&#xA;        if (kx == 0) num_threads = omp_get_num_threads();&#xA;        float tmp = (float)kx;&#xA;    }&#xA;    mexPrintf(&amp;quot;Num threads: %i\n&amp;quot;, num_threads);&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;When I compile this with &lt;code&gt;mex COMPFLAGS=&amp;quot;$COMPFLAGS /openmp&amp;quot; main.cpp -output test_openmp&lt;/code&gt; and run it, it works as expected&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; test_openmp()&#xA;Num threads: 8&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;However, if I immediately add a &lt;code&gt;clear mex&lt;/code&gt; after the call, Matlab crashes and immediately closes without error report.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; test_openmp(); clear mex&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;If I add a &lt;code&gt;pause(1)&lt;/code&gt;, there is no crash&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; test_openmp(); pause(1); clear mex&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This is a problem in another program where call a mex program can case an error and I need to do:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;try&#xA;    mexprogram()&#xA;catch ME&#xA;    clear mexprogram&#xA;end&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Does anybody know what is going on? When I run Matlab from the command line, I see the following return code: -1073741819. Attaching the debugger does not give any info.&lt;/p&gt;&#xA;&lt;p&gt;I am using MSVC 2022 and Matlab R2021b.&lt;/p&gt;&#xA;&lt;hr /&gt;&#xA;&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;Linking against the intel openMP implementation &lt;code&gt;libiomp5md.lib&lt;/code&gt; fixes the issue!&#xA;Compiling from MATLAB with&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;mex -v -g COMPFLAGS=&amp;quot;$COMPFLAGS /openmp&amp;quot; LINKFLAGS=&amp;quot;$LINKFLAGS /nodefaultlib:vcomp&amp;quot; &#x27;-LC:\Program Files\MATLAB\R2021b\bin\win64\&#x27; -llibiomp5md.lib main.cpp -output test_openmp_intel&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/76672265</id>
        <re:rank scheme="https://stackoverflow.com">2</re:rank>
        <title type="text">In Octave, how to compile a mex file from multiple C/C&#x2B;&#x2B; source files?</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="octave" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>zhaokg</name>
            <uri>https://stackoverflow.com/users/17431200</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/76672265/in-octave-how-to-compile-a-mex-file-from-multiple-c-c-source-files" />
        <published>2023-07-12T15:34:19Z</published>
        <updated>2023-07-12T20:18:25Z</updated>
        <summary type="html">
            &lt;p&gt;In Matlab, we can use &lt;code&gt;mex&lt;/code&gt; to compile a library from multiple files (e.g., 100&#x2B; .c files) using something like&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;mex *.c -o mylib.mex&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;But in Octave, it seems like that only one input source file accepted, it always gives an error&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;cc1.exe: fatal error: *.c: Invalid argument&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Tried googing a lot and find no solution. Any pointer to getting this problem solved will be appreciated.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/76474884</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Setting up a C&#x2B;&#x2B; Mex function with Visual Studio and Intel MKL</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="visual-studio" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="intel-mkl" />
        <author>
            <name>drakon101</name>
            <uri>https://stackoverflow.com/users/17059713</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/76474884/setting-up-a-c-mex-function-with-visual-studio-and-intel-mkl" />
        <published>2023-06-14T14:54:17Z</published>
        <updated>2023-06-14T14:54:17Z</updated>
        <summary type="html">
            &lt;p&gt;I am using a &lt;a href=&quot;https://marketplace.visualstudio.com/items?itemName=gharveymn.MEXFunctionTemplates&quot; rel=&quot;nofollow noreferrer&quot;&gt;visual studio template&lt;/a&gt; to produce Matlab MEX functions. Using this template alone, I can compile functions from Visual Studio using either Visual Studio&#x27;s compiler or one of the Intel C&#x2B;&#x2B;/DPC&#x2B;&#x2B; compilers. However, when I include mkl libraries, I encounter an error at the include line saying &amp;quot;cannot open source file mkl.h&amp;quot;&lt;/p&gt;&#xA;&lt;p&gt;I have included and linked all the relevant directories and libraries. Here are the command lines from the configuration properties in visual studio. Why could this be happening?&lt;/p&gt;&#xA;&lt;p&gt;C/C&#x2B;&#x2B; Properties:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;/GS /TP /W3 /I&amp;quot;C:\Program Files (x86)\Intel\oneAPI\mkl\latest\include&amp;quot; &#xA;/I&amp;quot;C:\Program Files\MATLAB\R2023a\extern\include\&amp;quot; /I&amp;quot;C:\Program Files\MATLAB\R2023a\simulink\include\&amp;quot; /Z7 /O2 /Zp8 /D &amp;quot;USE_MEX_CMD&amp;quot; &#xA;/D &amp;quot;_CRT_SECURE_NO_DEPRECATE&amp;quot; /D &amp;quot;_SCL_SECURE_NO_DEPRECATE&amp;quot; /D &amp;quot;_SECURE_SCL=0&amp;quot; &#xA;/D &amp;quot;MATLAB_MEX_FILE&amp;quot; /D &amp;quot;__INTEL_LLVM_COMPILER=20230100&amp;quot; /D &amp;quot;_WINDLL&amp;quot; /Zc:forScope /GR &#xA;/MD /Fa&amp;quot;C:\Users\nikun\source\repos\JCFMKL\JCFMKL\build\x64\Release\&amp;quot; /EHs &#xA;/nologo /Fo&amp;quot;C:\Users\nikun\source\repos\JCFMKL\JCFMKL\build\x64\Release\&amp;quot; &#xA;//fprofile-instr-use &amp;quot;C:\Users\nikun\source\repos\JCFMKL\JCFMKL\build\x64\Release\&amp;quot; &#xA;/Fp&amp;quot;C:\Users\nikun\source\repos\JCFMKL\JCFMKL\build\x64\Release\JCFMKL.pch&amp;quot; &#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Linker Properties&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;/OUT:&amp;quot;C:\Users\nikun\source\repos\JCFMKL\bin\x64\Release\JCFMKL.mexw64&amp;quot; /MANIFEST &#xA;/NXCOMPAT /PDB:&amp;quot;C:\Users\nikun\source\repos\JCFMKL\bin\x64\Release\JCFMKL.mexw64.pdb&amp;quot; &#xA;/DYNAMICBASE &amp;quot;mkl_rt.lib&amp;quot; &amp;quot;mkl_intel_c.lib&amp;quot; &amp;quot;mkl_intel_thread.lib&amp;quot; &amp;quot;mkl_intel_lp64.lib&amp;quot; &#xA;&amp;quot;mkl_core.lib&amp;quot; &amp;quot;libiomp5md.lib&amp;quot; &amp;quot;libmx.lib&amp;quot; &amp;quot;libmex.lib&amp;quot; &amp;quot;libmat.lib&amp;quot; &#xA;&amp;quot;libMatlabDataArray.lib&amp;quot; &amp;quot;libMatlabEngine.lib&amp;quot; &amp;quot;kernel32.lib&amp;quot; &amp;quot;user32.lib&amp;quot; &amp;quot;gdi32.lib&amp;quot; &#xA;&amp;quot;winspool.lib&amp;quot; &amp;quot;comdlg32.lib&amp;quot; &amp;quot;advapi32.lib&amp;quot; &amp;quot;shell32.lib&amp;quot; &amp;quot;ole32.lib&amp;quot; &amp;quot;oleaut32.lib&amp;quot; &#xA;&amp;quot;uuid.lib&amp;quot; &amp;quot;odbc32.lib&amp;quot; &amp;quot;odbccp32.lib&amp;quot; &#xA;/IMPLIB:&amp;quot;C:\Users\nikun\source\repos\JCFMKL\bin\x64\Release\JCFMKL.lib&amp;quot; /DLL /MACHINE:X64 &#xA;/INCREMENTAL:NO /MANIFESTUAC:&amp;quot;level=&#x27;asInvoker&#x27; uiAccess=&#x27;false&#x27;&amp;quot; &#xA;/ManifestFile:&amp;quot;C:\Users\nikun\source\repos\JCFMKL\JCFMKL\build\x64\Release\JCFMKL.mexw64.int&#xA;ermediate.manifest&amp;quot; /NOLOGO /LIBPATH:&amp;quot;C:\Program Files &#xA;(x86)\Intel\oneAPI\mkl\latest\lib\intel64&amp;quot; /LIBPATH:&amp;quot;C:\Program Files &#xA;(x86)\Intel\oneAPI\mkl\latest\lib&amp;quot; /LIBPATH:&amp;quot;C:\Program &#xA;Files\MATLAB\R2023a\extern\lib\win64\microsoft\&amp;quot; /TLBID:1 &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/76393248</id>
        <re:rank scheme="https://stackoverflow.com">2</re:rank>
        <title type="text">How can I build an Eigen::Map to a matlab::data::Array object that I intend to populate with data</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="eigen" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>drakon101</name>
            <uri>https://stackoverflow.com/users/17059713</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/76393248/how-can-i-build-an-eigenmap-to-a-matlabdataarray-object-that-i-intend-to-p" />
        <published>2023-06-02T20:18:49Z</published>
        <updated>2023-06-03T17:55:31Z</updated>
        <summary type="html">
            &lt;p&gt;This question is a follow up to a previous &lt;a href=&quot;https://stackoverflow.com/questions/76353633/efficient-method-to-pass-large-arrays-from-matlab-to-c-eigen-using-map-class/76353749#76353749&quot;&gt;question&lt;/a&gt; about building an Eigen::Map to an input matlab::data::Array object in the Matlab Data API for C&#x2B;&#x2B;. Now I want to know how I can implement something similar but to an output array which I will populate with data as I loop through it.&lt;/p&gt;&#xA;&lt;p&gt;In the previous question, I implemented the linked solution from &lt;a href=&quot;https://www.mathworks.com/matlabcentral/answers/1573713-c-mex-data-api-how-can-i-get-the-raw-pointer-of-an-array-input-without-copying-it&quot; rel=&quot;nofollow noreferrer&quot;&gt;Matlab&#x27;s forums&lt;/a&gt;. Here, I tried using the following to get a non-const pointer:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-cpp prettyprint-override&quot;&gt;&lt;code&gt;template &amp;lt;typename T&amp;gt;&#xA;T* getOutDataPtr(matlab::data::Array arr) {&#xA;  matlab::data::TypedArray&amp;lt;T&amp;gt; arr_t = arr;&#xA;  matlab::data::TypedIterator&amp;lt;T&amp;gt; it(arr_t.begin());&#xA;  return it.operator-&amp;gt;();&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Which is used like so:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-cpp prettyprint-override&quot;&gt;&lt;code&gt;// Initialize input parameters&#xA;long int numEl = inputs[0][0];&#xA;long int na = inputs[0][1];&#xA;long int nPoints = inputs[0][2];&#xA;        &#xA;// Preallocate output and define map to output array&#xA;outputs[0] = factory.createArray&amp;lt;double&amp;gt;&#xA;  ({static_cast&amp;lt;size_t&amp;gt;(nPoints),static_cast&amp;lt;size_t&amp;gt;(numEl*na)});&#xA;        &#xA;auto ptrRecon = getOutDataPtr&amp;lt;double&amp;gt;(outputs[0]);&#xA;Eigen::Map&amp;lt;Eigen::MatrixXd&amp;gt; Recon(ptrRecon,nPoints,numEl*na);&#xA;&#xA;for(int i = 0; i &amp;lt; nPoints; i&#x2B;&#x2B;) {&#xA; for(int n = 0; n &amp;lt; na; n&#x2B;&#x2B;) {&#xA;  for (int j = 0; j &amp;lt; numEl; j&#x2B;&#x2B;) {&#xA;   Recon(i,n*numEl &#x2B; j) = 5.0;&#xA;  }&#xA; }    &#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Even with a simple assignment like that however, I encounter runtime errors after compiling and Matlab crashes. What exactly is going wrong?&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Additional Trials&lt;/strong&gt;&#xA;I also tried the following for the inner loop which does seem to work:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-cpp prettyprint-override&quot;&gt;&lt;code&gt;for(int i = 0; i &amp;lt; nPoints; i&#x2B;&#x2B;) {&#xA;    for(int n = 0; n &amp;lt; na; n&#x2B;&#x2B;) {&#xA;        for (int j = 0; j &amp;lt; numEl; j&#x2B;&#x2B;) {&#xA;            outputs[0][i][n*numEl &#x2B; j] = 5.0;&#xA;&#xA;        }&#xA;    }    &#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I suspect this means there&#x27;s some issue with the way I am getting the pointer to the output matlab data array?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/76316325</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">How to speed up the computation of absolute loss matrix in C?</title>
            <category scheme="https://stackoverflow.com/tags" term="c" />
            <category scheme="https://stackoverflow.com/tags" term="matrix" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>andro1666</name>
            <uri>https://stackoverflow.com/users/21947713</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/76316325/how-to-speed-up-the-computation-of-absolute-loss-matrix-in-c" />
        <published>2023-05-23T15:19:31Z</published>
        <updated>2024-03-18T15:54:43Z</updated>
        <summary type="html">
            &lt;p&gt;I am running a Monte-Carlo experiment and evaluate the absolute loss function described below. Since it is very computationally intensive I would like to optimise my code and improve the speed further. My main code is in MATLAB, but I am evaluating the function in C using the MEX functionality of MATLAB.&lt;/p&gt;&#xA;&lt;p&gt;The mathematical problem is as follows: I have a matrix D with dimensions (M times N). Usually M is around 20,000 and N takes values around {10, 30, 144}.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&quot;https://i.sstatic.net/0J8wh.png&quot; rel=&quot;nofollow noreferrer&quot;&gt;Definition of matrix D&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;Effectively, I need to obtain L column vector with dimensions (M times 1) defined as&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&quot;https://i.sstatic.net/5N60v.png&quot; rel=&quot;nofollow noreferrer&quot;&gt;Definition of matrix L&lt;/a&gt;&lt;/p&gt;&#xA;&lt;p&gt;My C function looks like this:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;void absolute_loss(double *D, double *L, mwSize cols, mwSize rows)&#xA;{&#xA;&#xA;  double aux;&#xA;  int i;&#xA;  int j;&#xA;  int k;&#xA;  for (i = 0; i &amp;lt; rows; i&#x2B;&#x2B;) {&#xA;    for (j = 0; j &amp;lt; rows; j&#x2B;&#x2B;){&#xA;      aux = 0;&#xA;      for  (k = 0; k &amp;lt; cols; k&#x2B;&#x2B;) {&#xA;        aux = aux &#x2B; fabs(D[j &#x2B; rows * k] - D[i &#x2B; rows * k]);&#xA;      }&#xA;      L[i] = L[i] &#x2B; aux;&#xA;    }&#xA;  }&#xA;&#xA;  for (i = 0; i &amp;lt; rows; i&#x2B;&#x2B;) {&#xA;    L[i] /= rows;&#xA;  }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/75793203</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">How can I implement this for loop to convert an Eigen Matrix to a matlab data api object while preserving its dimensionality? [duplicate]</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="eigen" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>drakon101</name>
            <uri>https://stackoverflow.com/users/17059713</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/75793203/how-can-i-implement-this-for-loop-to-convert-an-eigen-matrix-to-a-matlab-data-ap" />
        <published>2023-03-20T17:00:20Z</published>
        <updated>2023-03-20T19:00:20Z</updated>
        <summary type="html">
            &lt;p&gt;I have the following code snippet:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-cpp prettyprint-override&quot;&gt;&lt;code&gt;Eigen::MatrixXcd Recon = Eigen::MatrixXcd::Zero(p.nPoints*p.na, p.numEl);&#xA;&#xA;// Do some functions to add data to Recon&#xA;&#xA;outputs[0] = factory.createArray&amp;lt;std::complex&amp;lt;double&amp;gt;&amp;gt;({static_cast&amp;lt;size_t&amp;gt;(p.numEl),static_cast&amp;lt;size_t&amp;gt;(p.na),static_cast&amp;lt;size_t&amp;gt;(p.nPoints)});&#xA;                  &#xA;for(int i = 0; i &amp;lt; p.na; i&#x2B;&#x2B;) {&#xA;   for (int j = 0; j &amp;lt; p.nPoints; j&#x2B;&#x2B;) {&#xA;      for (int n = 0; n &amp;lt; p.numEl; n&#x2B;&#x2B;) {&#xA;         outputs[n][i][j] = Recon( i*p.nPoints &#x2B; j ,n);&#xA;      }&#xA;   }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;How can I adjust my code above so that I am mapping the data in outputs to the data in Recon instead of copying it element by element?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/75777807</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Pointer of Pointers in MATLAB Mexfile Template</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="c" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Nukesub</name>
            <uri>https://stackoverflow.com/users/5154085</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/75777807/pointer-of-pointers-in-matlab-mexfile-template" />
        <published>2023-03-18T18:15:51Z</published>
        <updated>2023-03-19T07:19:21Z</updated>
        <summary type="html">
            &lt;p&gt;For some context, I am familiar with C programming (not an expert but a daily user) and also MATLAB. I have used C&#x2B;&#x2B; minimally and not recently.&lt;/p&gt;&#xA;&lt;p&gt;I had recently started looking into the MEX functionality of MATLAB as I hadn&#x27;t ever looked at it before. While I was working through a &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_external/standalone-example.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;basic example&lt;/a&gt; I noticed something I had not seen before in the entry point function &lt;code&gt;mexFunction&lt;/code&gt;, copy/pasted from the docs below.&lt;/p&gt;&#xA;&lt;p&gt;The output and input arguments are returned as pointers of type &lt;code&gt;mxArray&lt;/code&gt; but with an empty  bracket (e.g., &lt;code&gt;mxArray *plhs[]&lt;/code&gt;). Coming from C I would have just expected &lt;code&gt;mxArray *plhs&lt;/code&gt;. I can also understand you might want to return multiple types like scalars, matrices, etc. so maybe you want a pointer to pointers so wouldn&#x27;t you do &lt;code&gt;mxArray **plhs&lt;/code&gt;?&lt;/p&gt;&#xA;&lt;p&gt;From this post &lt;a href=&quot;https://stackoverflow.com/questions/10350461/c-asterisk-and-bracket-operators-used-together&quot;&gt;here&lt;/a&gt; I understand the syntax &lt;code&gt;mxArray *plhs[]&lt;/code&gt; means an array of pointers. Is that different than doing &lt;code&gt;mxArray **plhs&lt;/code&gt;?&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-matlab prettyprint-override&quot;&gt;&lt;code&gt;/* The gateway function */&#xA;void mexFunction(int nlhs, mxArray *plhs[],&#xA;                 int nrhs, const mxArray *prhs[])&#xA;{&#xA;/* variable declarations here */&#xA;&#xA;/* code here */&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/75693305</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">mexw64 debug build not forwarding std::cout to Matlab console [duplicate]</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="cmake" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>G. B.</name>
            <uri>https://stackoverflow.com/users/9963147</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/75693305/mexw64-debug-build-not-forwarding-stdcout-to-matlab-console" />
        <published>2023-03-10T07:31:18Z</published>
        <updated>2023-03-10T09:05:24Z</updated>
        <summary type="html">
            &lt;p&gt;We are building a matlab executable (.mexw64) from C&#x2B;&#x2B; using MSVC compiler (we tested compiler versions 15 and 16) and a cmake toolchain (not with the mex command). The mexw64 contains functionality, that we can call from Matlab, and these functions need to log. My problem is, that when building in debug, the std::cout is not routed to the matlab console (we display log messages this way).&lt;/p&gt;&#xA;&lt;p&gt;Details:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;if built in release mode, the log messages display correctly in R2017 and R2021&lt;/li&gt;&#xA;&lt;li&gt;if build in debug mode, the log messages are only displayed in R2017 and not displayed R2021&lt;/li&gt;&#xA;&lt;li&gt;MSVC compiler version 15 and 16 work the same way&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;What was changed in this regard between R2017 and R2021? How can I solve this issue? Changing the logging from std::cout to something else (e.g. mex logging function or std::cerr) is unfortunately not an option.&lt;/p&gt;&#xA;&lt;p&gt;Note: I noticed in the build log, that the option &lt;code&gt;-DMATLAB_DEFAULT_RELEASE=R2017b&lt;/code&gt;is set. What does this do? Is it ok, to have this value, even if my Matlab version is R2021b? The option is set in a cmake function:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;  # For 9.4 (R2018a) and newer, add API macro.&#xA;  # Add it for unknown versions too, just in case.&#xA;  if(NOT Matlab_VERSION_STRING VERSION_LESS &amp;quot;9.4&amp;quot;&#xA;      OR Matlab_VERSION_STRING STREQUAL &amp;quot;unknown&amp;quot;)&#xA;    if(${${prefix}_R2018a})&#xA;      set(MEX_API_MACRO &amp;quot;MATLAB_DEFAULT_RELEASE=R2018a&amp;quot;)&#xA;    else()&#xA;      set(MEX_API_MACRO &amp;quot;MATLAB_DEFAULT_RELEASE=R2017b&amp;quot;)&#xA;    endif()&#xA;  endif()&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Edit: I did not mention that we correctly &lt;a href=&quot;https://stackoverflow.com/questions/243696/correctly-over-loading-a-stringbuf-to-replace-cout-in-a-matlab-mex-file&quot;&gt;overload the std::streambuf&lt;/a&gt; as @Cris Luengo pointed out. I do not copy the code here, because it&#x27;s long and proprietary, but it works with R2017b and also with R2021b in release mode.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/74913843</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Compile Matlab generated C source code on a Linux platform to created a Linux MEX</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>SheppLogan</name>
            <uri>https://stackoverflow.com/users/7514181</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/74913843/compile-matlab-generated-c-source-code-on-a-linux-platform-to-created-a-linux-me" />
        <published>2022-12-25T14:22:26Z</published>
        <updated>2022-12-26T04:30:46Z</updated>
        <summary type="html">
            &lt;p&gt;I have a Windows Matlab R2020a and was able to generate C source code for my Matlab function. However, I need to run a MEX file (for speed) on a Linux platform which has a Matlab installed (without Coder) and no way to install it (even trial version).&lt;/p&gt;&#xA;&lt;p&gt;Please, provide the steps to compile this Matlab-generated C code on the linux (Ubuntu) system. I guess it can be done with gcc. I am unable to find a clear description on the web, as this process seems extremely complicated due to the obscure command line args needed to give to gcc (and i only rarely compile C code).&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/74722979</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Am I able to use parallel STL algorithms from C&#x2B;&#x2B;17/C&#x2B;&#x2B;20 in Matlab MEX functions?</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="std" />
            <category scheme="https://stackoverflow.com/tags" term="dynamic-linking" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>mfeuling</name>
            <uri>https://stackoverflow.com/users/5763459</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/74722979/am-i-able-to-use-parallel-stl-algorithms-from-c17-c20-in-matlab-mex-function" />
        <published>2022-12-07T21:25:43Z</published>
        <updated>2022-12-08T19:24:48Z</updated>
        <summary type="html">
            &lt;p&gt;I am putting together a minimal example leveraging &lt;a href=&quot;https://en.cppreference.com/w/cpp/experimental/parallelism&quot; rel=&quot;nofollow noreferrer&quot;&gt;parallelism features in C&#x2B;&#x2B;17/20&lt;/a&gt; within Matlab MEX functions. I am able to compile and run the mex function from Matlab, but when I set the &lt;a href=&quot;https://en.cppreference.com/w/cpp/experimental/execution_policy_tag_t&quot; rel=&quot;nofollow noreferrer&quot;&gt;execution policy&lt;/a&gt; of my C&#x2B;&#x2B; STL function to &amp;quot;par&amp;quot; instead of &amp;quot;seq&amp;quot;, Matlab gives a runtime linkage complaint. Code and error message follows:&lt;/p&gt;&#xA;&lt;p&gt;test.m (Matlab top-level script):&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;vec_in = zeros(5);&#xA;coeff = 0.05;&#xA;&#xA;vec_out = test_mex_gateway(vec_in, coeff);&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;test_mex_gateway.cpp (C&#x2B;&#x2B; interface to Matlab):&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#include &amp;quot;mex.h&amp;quot;&#xA;&#xA;extern void test_execute(float *array_in, float *array_out, const size_t vec_size, const float coeff);&#xA;&#xA;void mexFunction( int nlhs,&#xA;                  mxArray *plhs[],&#xA;                  int nrhs,&#xA;                  const mxArray *prhs[] )&#xA;{&#xA;    // Check for proper number of input and output arguments&#xA;    if( nrhs != 2 )&#xA;    {&#xA;        mexErrMsgTxt( &amp;quot;3 input arguments required: input_data, coeff&amp;quot; );&#xA;    }&#xA;&#xA;    if( nlhs &amp;gt; 2 )&#xA;    {&#xA;        mexErrMsgTxt( &amp;quot;Too many output arguments.&amp;quot; );&#xA;    }&#xA;&#xA;    const mwSize *matlab_data_dims_in;&#xA;    mwSize matlab_data_dims_out[1];&#xA;&#xA;    // Input Parameters&#xA;    float *input_data = (float *) mxGetData(prhs[0]);&#xA;    float coeff = mxGetScalar(prhs[1]);&#xA;&#xA;    // Get dimensions&#xA;    matlab_data_dims_in = mxGetDimensions(prhs[0]);&#xA;    const int vec_len = matlab_data_dims_in[1];&#xA;&#xA;    // Set output data dimension&#xA;    matlab_data_dims_out[0] = vec_len;&#xA;&#xA;    // Output data&#xA;    plhs[0] = mxCreateNumericArray(1, matlab_data_dims_out, mxSINGLE_CLASS, mxREAL);&#xA;    float *output_data = (float *) mxGetData(plhs[0]);&#xA;&#xA;    test_execute(input_data, output_data, vec_len, coeff);&#xA;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;test_execute.cpp (This is where the actual C&#x2B;&#x2B; STL call is made):&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#include &amp;lt;execution&amp;gt; // std::execution::*&#xA;#include &amp;lt;numeric&amp;gt;   // std::exclusive_scan()&#xA;&#xA;void test_execute(float *array_in, float *array_out, const size_t vec_size, const float coeff)&#xA;{&#xA;    std::exclusive_scan&#xA;    (&#xA;        std::execution::par, // std::execution::seq works here for Mex call, par does not&#xA;        array_in,&#xA;        array_in &#x2B; vec_size,&#xA;        array_out,&#xA;        0.0f,&#xA;        [coeff](float a, float b)&#xA;        {&#xA;            float ret = a &#x2B; b &#x2B; coeff;&#xA;            return ret;&#xA;        }&#xA;    );&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I also have a stand-alone main function to replace the Mex wrapper to do a pure C&#x2B;&#x2B; test, test_standalone.cpp:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#include &amp;lt;vector&amp;gt;&#xA;#include &amp;lt;iostream&amp;gt;&#xA;&#xA;size_t VEC_NUM_ELEM = 10;&#xA;&#xA;extern void test_execute(float *array_in, float *array_out, const size_t vec_size, const float coeff);&#xA;&#xA;int main(int argc, char **argv)&#xA;{&#xA;    if (argc != 2)&#xA;    {&#xA;        std::cout &amp;lt;&amp;lt; &amp;quot;Try: &amp;quot; &amp;lt;&amp;lt; argv[0] &amp;lt;&amp;lt; &amp;quot;&amp;lt;coeff&amp;gt;&amp;quot; &amp;lt;&amp;lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    const float coeff = std::stof(argv[1]);&#xA;&#xA;    std::cout &amp;lt;&amp;lt; &amp;quot;Coeff: &amp;quot; &amp;lt;&amp;lt; coeff &amp;lt;&amp;lt; std::endl;&#xA;&#xA;    float __attribute__ ((aligned (64))) *vec1_array = (float *)malloc(VEC_NUM_ELEM * sizeof(float));&#xA;    float __attribute__ ((aligned (64))) *vec2_array = (float *)malloc(VEC_NUM_ELEM * sizeof(float));&#xA;&#xA;    for (unsigned i = 0; i &amp;lt; VEC_NUM_ELEM; i&#x2B;&#x2B;)&#xA;    {&#xA;        vec1_array[i] = static_cast&amp;lt;float&amp;gt;(i);&#xA;    }&#xA;&#xA;    test_execute(vec1_array, vec2_array, VEC_NUM_ELEM, coeff);&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Here is how I am building and linking, build.sh:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#!/bin/bash&#xA;&#xA;rm *.o&#xA;rm *.exe&#xA;rm *.mexa64&#xA;&#xA;cstd=c&#x2B;&#x2B;17&#xA;&#xA;gpp910=/home/m/compilers/bin/g&#x2B;&#x2B;&#xA;&#xA;tbblib=/home/m/reqs/tbb/lib/intel64/gcc4.8&#xA;&#xA;echo &amp;quot;Building test_execute.cpp...&amp;quot;&#xA;$gpp910 -std=$cstd -I/home/m/reqs/tbb/include -L$tbblib -ltbb -Wl,rpath=$tbblib -c test_execute.cpp -fPIC&#xA;&#xA;echo &amp;quot;Building test_standalone.cpp...&amp;quot;&#xA;$gpp910 -std=$cstd -L$tbblib test_execute.o test_standalone.cpp -o test_standalone.exe -ltbb&#xA;&#xA;echo &amp;quot;Building test_mex_gateway.cpp...&amp;quot;&#xA;mex test_execute.o test_mex_gateway.cpp -L$tbblib -ltbb&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;The parallel STL calls has a requirement to link against the Intel TBB (Threading Building Blocks), so before I run Matlab to call test.m OR before I run my test_standalone.exe, I run:&lt;/p&gt;&#xA;&lt;p&gt;export LD_LIBRARY_PATH=/home/m/reqs/tbb/lib/intel64/gcc4.8:$LD_LIBRARY_PATH&lt;/p&gt;&#xA;&lt;p&gt;I also make sure to make the the C&#x2B;&#x2B; library associated with the version of GCC we built with available at runtime:&lt;/p&gt;&#xA;&lt;p&gt;export LD_LIBRARY_PATH=/home/m/compilers/lib64:$LD_LIBRARY_PATH&lt;/p&gt;&#xA;&lt;p&gt;When I run test_standalone.exe, everything behaves normally whether I have the execution policy set to &amp;quot;par&amp;quot; or &amp;quot;seq&amp;quot; on std::exclusive_scan. When run test.m, if &amp;quot;seq&amp;quot; was compiled, I can run with no errors. If &amp;quot;par&amp;quot; was compiled, Matlab complains at runtime about a linkage issue:&lt;/p&gt;&#xA;&lt;p&gt;&lt;em&gt;Invalid MEX-file &#x27;test_mex_gateway.mexa64&#x27;: test_mex_gateway.mexa64: undefined symbol:&#xA;_ZN3tbb10interface78internal20isolate_within_arenaERNS1_13delegate_baseEl&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;I suspect this was a function that was supposed to be linked from TBB, which I confirmed:&lt;/p&gt;&#xA;&lt;p&gt;$ nm /home/m/reqs/tbb/lib/intel64/gcc4.8/libtbb.so.2 | grep baseEl&lt;/p&gt;&#xA;&lt;p&gt;0000000000028a30 T _ZN3tbb10interface78internal20isolate_within_arenaERNS1_13delegate_baseEl&lt;/p&gt;&#xA;&lt;p&gt;000000000005ed70 r _ZN3tbb10interface78internal20isolate_within_arenaERNS1_13delegate_baseEl$$LSDA&lt;/p&gt;&#xA;&lt;p&gt;I confirmed Matlab&#x27;s LD_LIBRARY_PATH has the path I supplied in the above &amp;quot;export ..&amp;quot; to this library.&lt;/p&gt;&#xA;&lt;p&gt;I tried making sure my libraries came before the many Matlab-centric paths Matlab adds to LD_LIBRARY_PATH after it launches from the terminal.&lt;/p&gt;&#xA;&lt;p&gt;I tried baking the path to the linked libraries via a -Wl,rpath=&amp;lt;path_to_tbb.so&amp;gt; passage to the linker.&lt;/p&gt;&#xA;&lt;p&gt;After almost two days, I can&#x27;t figure out why Matlab is having this very specific runtime issue, especially when the pure C&#x2B;&#x2B; version is not. Any help would be appreciated.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;RHEL 7.9&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Matlab R2020a&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;GCC 9.1.0&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;TBB (Intel Thread Building Blocks) 2020.3&lt;/strong&gt;&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/74041072</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Fatal error C1083 while compiling mex file in MATLAB</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="visual-c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="compiler-errors" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Gagandeep Singh</name>
            <uri>https://stackoverflow.com/users/17960694</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/74041072/fatal-error-c1083-while-compiling-mex-file-in-matlab" />
        <published>2022-10-12T11:29:28Z</published>
        <updated>2022-10-12T15:34:38Z</updated>
        <summary type="html">
            &lt;p&gt;&lt;strong&gt;mex_example0.cpp&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;#include &amp;quot;mex.hpp&amp;quot;   &#xA;#include &amp;quot;mexAdapter.hpp&amp;quot;   &#xA;#include &amp;lt;iostream&amp;gt;    &#xA;using namespace matlab::data;   &#xA;using matlab::mex::ArgumentList;    &#xA;class MexFunction : public matlab::mex::Function {    &#xA;public:    &#xA;    void operator()(ArgumentList outputs, ArgumentList inputs) {    &#xA;        ArrayFactory factory;   &#xA;        TypedArray&amp;lt;double&amp;gt; x = std:: move(inputs[0]);   &#xA;        for (auto&amp;amp; i: x){   &#xA;            i &#x2B;= 5;   &#xA;        }    &#xA;        outputs[0] = x;    &#xA;        std::cout&amp;lt;&amp;lt;&amp;quot;Hello MEX&amp;quot;&amp;lt;&amp;lt;std::endl;     &#xA;    }   &#xA;};      &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;strong&gt;MATLAB code&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;mex mex_example0.cpp  &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;strong&gt;Error&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Building with &#x27;Microsoft Visual C&#x2B;&#x2B; 2012&#x27;.&#xA;Error using mex&#xA;mex_example0.cpp&#xA;C:\Users\iisermbionap\Downloads\mex_example0.cpp(1) : fatal error C1083: Cannot open include&#xA;file: &#x27;mex.hpp&#x27;: No such file or directory  &#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I am using MATLAB 2015a and MVC 2012 professional.&lt;br /&gt;&#xA;Can somebody please help me how to rectify this error.&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/73947526</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">Display C&#x2B;&#x2B; Output in Matlab using C&#x2B;&#x2B; Mexfunctions [duplicate]</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Hadi_IronSide</name>
            <uri>https://stackoverflow.com/users/20158033</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/73947526/display-c-output-in-matlab-using-c-mexfunctions" />
        <published>2022-10-04T11:57:34Z</published>
        <updated>2022-10-06T15:38:24Z</updated>
        <summary type="html">
            &lt;p&gt;I have a question about display C&#x2B;&#x2B; output in MATLAB. I wrote a mex function to include my C&#x2B;&#x2B; algorithm in MATLAB but I have a problem with displaying my output in MATLAB. The c&#x2B;&#x2B; mex function calls other classes where my output is generated. To keep the matter simple and clear, here is an example.&lt;/p&gt;&#xA;&lt;p&gt;Mexfunction.cpp:&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-cpp prettyprint-override&quot;&gt;&lt;code&gt;#include &amp;quot;mex.hpp&amp;quot;&#xA;#include &amp;quot;mexAdapter.hpp&amp;quot; &#xA;#include &amp;quot;Helper.h&amp;quot;&#xA;class MexFunction : public matlab::mex::Function {&#xA;public:&#xA;    void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {&#xA;        // Function implementation&#xA;        ...&#xA;            Helper();&#xA;        }&#xA;};&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;(Helper Class, that is called in Mexfunction.cpp)&lt;/p&gt;&#xA;&lt;pre class=&quot;lang-cpp prettyprint-override&quot;&gt;&lt;code&gt;#ifndef HELPER_H&#xA;#define HELPER_H&#xA;#include &amp;lt;iostream&amp;gt;&#xA;class Helper&#xA;{&#xA;public:&#xA;    Helper()&#xA;    {&#xA;        std::cout &amp;lt;&amp;lt; &amp;quot;Hello World&amp;quot; &amp;lt;&amp;lt; std::endl; &#xA;    }&#xA;};&#xA;#endif&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;How I can display the string &lt;code&gt;&amp;quot;Hello world&amp;quot;&lt;/code&gt; in the MATLAB command window?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/73928582</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Can&#x27;t get any #includes to work in Matlab/MEX with Visual Studio C&#x2B;&#x2B;</title>
            <category scheme="https://stackoverflow.com/tags" term="matlab" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Paul Aner</name>
            <uri>https://stackoverflow.com/users/16975921</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/73928582/cant-get-any-includes-to-work-in-matlab-mex-with-visual-studio-c" />
        <published>2022-10-02T19:06:27Z</published>
        <updated>2022-10-02T21:34:53Z</updated>
        <summary type="html">
            &lt;p&gt;I want to do some simulations that need C&#x2B;&#x2B;-Code in Matlab. This works like a charm, but as soon as I put in some additional &lt;code&gt;#include&lt;/code&gt;&#x27;s in the C&#x2B;&#x2B;/MEX-Code I get a ton of error messages like this:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(19) : error C2054: expected &#x27;(&#x27; to follow &#x27;using&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(19) : error C2061: syntax error : identifier &#x27;using&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(21) : error C2061: syntax error : identifier &#x27;using&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(21) : error C2061: syntax error : identifier &#x27;abs&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(21) : error C2059: syntax error : &#x27;;&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(21) : error C2061: syntax error : identifier &#x27;atexit&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(22) : error C2061: syntax error : identifier &#x27;atof&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(22) : error C2059: syntax error : &#x27;;&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(22) : error C2061: syntax error : identifier &#x27;atoi&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(22) : error C2061: syntax error : identifier &#x27;atol&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(23) : error C2061: syntax error : identifier &#x27;bsearch&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(23) : error C2059: syntax error : &#x27;;&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(23) : error C2061: syntax error : identifier &#x27;calloc&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(23) : error C2061: syntax error : identifier &#x27;div&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(24) : error C2061: syntax error : identifier &#x27;exit&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(24) : error C2059: syntax error : &#x27;;&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(24) : error C2061: syntax error : identifier &#x27;free&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(25) : error C2061: syntax error : identifier &#x27;labs&#x27; &#xA;    C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\cstdlib(25) : error C2059: syntax error : &#x27;;&#x27;&#xA;...&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I tried different versions of Matlab, Visual Studio and different C&#x2B;&#x2B;-Compilers. I have code that works perfectly fine and also tried this with example-code from Mathworks that also works fine.&lt;/p&gt;&#xA;&lt;p&gt;For testing-purposes I simply put &lt;code&gt;#include &amp;lt;random&amp;gt;&lt;/code&gt; or &lt;code&gt;include &amp;lt;algorithm&amp;gt;&lt;/code&gt; on top of working examples and then those error-messages occur. No &amp;quot;file not found&amp;quot;-errors.&lt;/p&gt;&#xA;&lt;p&gt;Can anybody give me a hint what I&#x27;m doing wrong here? I didn&#x27;t change anything in &lt;strong&gt;mexopts.bat&lt;/strong&gt; and I believe the right include-directories are in there. Programming in Visual Studio itself works without any problems with those &lt;code&gt;#include&lt;/code&gt;&#x27;s...&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/73885145</id>
        <re:rank scheme="https://stackoverflow.com">0</re:rank>
        <title type="text">How to include and link libraries to Octave C&#x2B;&#x2B; Dynamically Linked Functions?</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="octave" />
            <category scheme="https://stackoverflow.com/tags" term="linker-errors" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
            <category scheme="https://stackoverflow.com/tags" term="compileoptions" />
        <author>
            <name>Naveen</name>
            <uri>https://stackoverflow.com/users/3565667</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/73885145/how-to-include-and-link-libraries-to-octave-c-dynamically-linked-functions" />
        <published>2022-09-28T16:49:20Z</published>
        <updated>2022-09-28T17:08:59Z</updated>
        <summary type="html">
            &lt;p&gt;I have an octave DLD function say main.cpp which calls several functions declared and defined(in .h and .cpp files resp) in the directory mathlib/ which is in the same location as main.cpp.&lt;/p&gt;&#xA;&lt;p&gt;I&#x27;m referring to &lt;a href=&quot;https://docs.octave.org/v4.2.0/Getting-Started-with-Oct_002dFiles.html&quot; rel=&quot;nofollow noreferrer&quot;&gt;this&lt;/a&gt; octave documentation page for compile options for including and linking libraries&lt;/p&gt;&#xA;&lt;p&gt;I&#x27;m getting the following errors:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; mkoctfile -Imathlib \ main.cpp&#xA;g&#x2B;&#x2B;: warning:  main.cpp -o &amp;lt;PATH&amp;gt;\AppData\Local\Temp/oct-uoZEQc.o: linker input file unused because linking not done&#xA;g&#x2B;&#x2B;: error:  main.cpp -o &amp;lt;PATH&amp;gt;\AppData\Local\Temp/oct-uoZEQc.o: linker input file not found: Invalid argument&#xA;error: mkoctfile: building exited with failure status&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;What are the other compile options and steps needed to get the linking to work and compile main.cpp successfully?&lt;/p&gt;&#xA;
        </summary>
    </entry>
    <entry>
        <id>https://stackoverflow.com/q/73827088</id>
        <re:rank scheme="https://stackoverflow.com">1</re:rank>
        <title type="text">Problem with complex array input arguments to Octave C&#x2B;&#x2B; Dynamically Linked Functions</title>
            <category scheme="https://stackoverflow.com/tags" term="c&#x2B;&#x2B;" />
            <category scheme="https://stackoverflow.com/tags" term="octave" />
            <category scheme="https://stackoverflow.com/tags" term="mex" />
        <author>
            <name>Naveen</name>
            <uri>https://stackoverflow.com/users/3565667</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/73827088/problem-with-complex-array-input-arguments-to-octave-c-dynamically-linked-func" />
        <published>2022-09-23T11:35:47Z</published>
        <updated>2022-09-23T14:32:58Z</updated>
        <summary type="html">
            &lt;p&gt;Consider the following simplified example:&lt;/p&gt;&#xA;&lt;p&gt;File 1 (The Octave .m file)&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;% test_multiplyCxArrays.m&#xA;&#xA;clc; clear all; close all;&#xA;pkg load signal&#xA;mkoctfile multiplyCxArrays.cpp&#xA;disp(&#x27;Compiled Octave-C&#x2B;&#x2B; function multiplyCxArrays.cpp&#x27;);&#xA;&#xA;cx_one = 1&#x2B;1i;&#xA;a = [cx_one; cx_one*2; cx_one*3; cx_one*4; cx_one*5];&#xA;b = [cx_one; cx_one/2; cx_one/3; cx_one/4; cx_one/5];&#xA;&#xA;disp(&#x27;First input vector: &#x27;);&#xA;disp(a);&#xA;&#xA;disp(&#x27;First input vector inside DLD: &#x27;);&#xA;cx_out = multiplyCxArrays(a,b);&#xA;&#xA;disp(&#x27;Output vector returned DLD: &#x27;);&#xA;disp(cx_out);&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;File 2 (The C&#x2B;&#x2B; DLD file)&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;// multiplyCxArrays.cpp&#xA;&#xA;#include &amp;lt;iostream&amp;gt;&#xA;#include &amp;lt;octave/oct.h&amp;gt;&#xA;#include &amp;lt;iostream&amp;gt;&#xA;#include &amp;lt;complex&amp;gt;&#xA;#include &amp;lt;vector&amp;gt;&#xA;#include &amp;lt;fstream&amp;gt;&#xA;#include &amp;lt;cmath&amp;gt;&#xA;#include &amp;lt;math.h&amp;gt;&#xA;#include &amp;quot;CNDArray.h&amp;quot;&#xA;&#xA;using namespace std;&#xA;&#xA;DEFUN_DLD (multiplyCxArrays, args, nargout,&#xA;           &amp;quot;multiplyCxArrays is a c&#x2B;&#x2B; compiled example to illustrate dynamically loaded functions in octave &amp;quot;)&#xA;{&#xA;  ComplexNDArray cxIn1 = args(0).array_value();&#xA;  ComplexNDArray cxIn2 = args(1).array_value();&#xA;  const int cxIn1_length = cxIn1.rows();&#xA;  const int cxIn2_length = cxIn2.rows();&#xA;&#xA;  std::vector&amp;lt;std::complex&amp;lt;double&amp;gt;&amp;gt; cxVec;&#xA;&#xA;  std::complex&amp;lt;double&amp;gt; cx_elem1, cx_elem2;&#xA;  for(octave_idx_type i = 0; i &amp;lt; cxIn1_length; &#x2B;&#x2B;i)&#xA;  {&#xA;    cout &amp;lt;&amp;lt; &amp;quot;i = &amp;quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;, cxIn1(i) = &amp;quot; &amp;lt;&amp;lt; cxIn1(i) &amp;lt;&amp;lt; endl;&#xA;    cx_elem1 = cxIn1(i);&#xA;    cx_elem2 = cxIn2(i);&#xA;    cxVec.push_back(cx_elem1*cx_elem2);&#xA;  }&#xA;&#xA;  const int cxOut_length = (int)cxVec.size();&#xA;&#xA;  dim_vector dv (cxOut_length, 1);&#xA;  ComplexNDArray cxOut(dv);&#xA;  dv = cxOut.dims();&#xA;  for (octave_idx_type i = 0; i &amp;lt; cxOut_length; &#x2B;&#x2B;i)&#xA;  {&#xA;    cxOut(i) = cxVec[(int)i];&#xA;  }&#xA;&#xA;  octave_value_list retval(nargout);&#xA;  retval(0) = octave_value(cxOut);&#xA;  return retval;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;OUTPUT printout in Octave command window:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;First input vector:&#xA;   1 &#x2B; 1i&#xA;   2 &#x2B; 2i&#xA;   3 &#x2B; 3i&#xA;   4 &#x2B; 4i&#xA;   5 &#x2B; 5i&#xA;First input vector inside DLD:&#xA;i = 0, cxIn1(i) = (1,0)&#xA;i = 1, cxIn1(i) = (2,0)&#xA;i = 2, cxIn1(i) = (3,0)&#xA;i = 3, cxIn1(i) = (4,0)&#xA;i = 4, cxIn1(i) = (5,0)&#xA;Output vector returned DLD:&#xA;   1&#xA;   1&#xA;   1&#xA;   1&#xA;   1&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;For some reason the first argument of the DLD &lt;strong&gt;args(0).array_value()&lt;/strong&gt; is only returning the real part of the input complex array and zeroing out the imaginary part as you can see from the output printout for &lt;strong&gt;cxIn1(i)&lt;/strong&gt;. Is there any way to retrieve the full complex array from the input argument list?&lt;/p&gt;&#xA;
        </summary>
    </entry>
</feed>