From dc7a7995db705e97db9535fca8f215fa6edf9e97 Mon Sep 17 00:00:00 2001 From: Allan CORNET Date: Sat, 19 Oct 2024 21:39:14 +0200 Subject: [PATCH] load fftw library on macos if conda or homebrew used --- .github/workflows/ccpp.yml | 17 ----- bin/macOS/nelson | 3 + modules/fftw/src/cpp/FFTWWrapper.cpp | 76 +++++++++++++------ modules/integer/tests/test_times_1.m | 24 ++++++ .../tests/{test_times.m => test_times_2.m} | 15 ---- .../mex/tests/test_mxDuplicateArray_cell.m | 22 +++--- 6 files changed, 94 insertions(+), 63 deletions(-) create mode 100644 modules/integer/tests/test_times_1.m rename modules/integer/tests/{test_times.m => test_times_2.m} (69%) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 7b4c944ab3..f8ebc2286b 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -65,10 +65,6 @@ jobs: - name: build help run: | cmake --build . -- buildhelp - - name: minimal tests - run: | - export OMP_NUM_THREADS=1 - cmake --build . -- tests_minimal - name: package run: cmake --build . -- package - name: Install @@ -152,10 +148,6 @@ jobs: - name: build help run: | cmake --build . -- buildhelp - - name: minimal tests - run: | - export OMP_NUM_THREADS=1 - cmake --build . -- tests_minimal - name: package run: cmake --build . -- package - name: Install @@ -240,10 +232,6 @@ jobs: - name: build help run: | cmake --build . -- buildhelp - - name: minimal tests - run: | - export OMP_NUM_THREADS=1 - cmake --build . -- tests_minimal - name: package run: cmake --build . -- package - name: Install @@ -845,11 +833,6 @@ jobs: run: | %GITHUB_WORKSPACE%/bin/win32/nelson-cli --noipc --quiet -f %GITHUB_WORKSPACE%/tools/buildhelp/buildhelp.m - - name: Minimal tests - run: | - set NELSON_TERM_IS_UNICODE_SUPPORTED=TRUE - %GITHUB_WORKSPACE%/bin/win32/nelson-cli --noipc --quiet -f %GITHUB_WORKSPACE%/tools/tests_minimal/runtests_minimal.m - - name: Inno setup run: | %GITHUB_WORKSPACE%/bin/win32/nelson-cli --noipc --quiet -e run('%GITHUB_WORKSPACE%/tools/innosetup/innosetup.m');exit" diff --git a/bin/macOS/nelson b/bin/macOS/nelson index 3ebef384b3..9c646e9232 100755 --- a/bin/macOS/nelson +++ b/bin/macOS/nelson @@ -42,6 +42,9 @@ SCRIPT_DIRECTORY=$(cd "$(dirname "$0")"; pwd) #============================================================================== export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES #============================================================================== +# remove warning about deprecated functions on macOS Sequoia with Qt +export CFLOG_FORCE_DISABLE_STDERR=1 +#============================================================================== if test -d $SCRIPT_DIRECTORY/../lib/Nelson; then CURRENT_DIRECTORY=$(pwd -P) cd "$SCRIPT_DIRECTORY/../lib/Nelson" diff --git a/modules/fftw/src/cpp/FFTWWrapper.cpp b/modules/fftw/src/cpp/FFTWWrapper.cpp index cf8584d1f8..4db448a576 100644 --- a/modules/fftw/src/cpp/FFTWWrapper.cpp +++ b/modules/fftw/src/cpp/FFTWWrapper.cpp @@ -11,6 +11,8 @@ #if WITH_OPENMP #include #endif +#include +#include #include "FFTWDynamicLibrary.hpp" #include "FFTWWrapper.hpp" #include "DynamicLibrary.hpp" @@ -192,41 +194,71 @@ freeFFTWLibrary() return false; } //============================================================================= +#if defined(__APPLE__) +static bool +loadFFTWLibraryOnMacOs() +{ + // Lambda to attempt loading FFTW libraries from a given prefix + auto tryLoadLibrary = [](const char* prefix) -> bool { + if (prefix) { + std::string libPath = std::string(prefix) + "/lib/"; + std::string fftwLibraryName = libPath + "libfftw3.3" + get_dynamic_library_extension(); + std::string fftwfLibraryName = libPath + "libfftw3f" + get_dynamic_library_extension(); + return loadFFTWLibrary( + utf8_to_wstring(fftwLibraryName), utf8_to_wstring(fftwfLibraryName)); + } + return false; + }; + + // Try loading from CONDA_PREFIX first, then HOMEBREW_PREFIX if necessary + return tryLoadLibrary(std::getenv("CONDA_PREFIX")) + || tryLoadLibrary(std::getenv("HOMEBREW_PREFIX")); +} +#endif +//============================================================================= bool loadFFTWLibrary() { if (fftwLoaded) { return true; } - bool res = false; -#ifdef _MSC_VER - std::wstring fftwLibraryName = L"libfftw3-3.dll"; - std::wstring fftwfLibraryName = L"libfftw3f-3.dll"; - res = loadFFTWLibrary(fftwLibraryName, fftwfLibraryName); -#else - std::string fftwLibraryName = "libfftw3" + get_dynamic_library_extension(); - std::string fftwfLibraryName = "libfftw3f" + get_dynamic_library_extension(); - res = loadFFTWLibrary(utf8_to_wstring(fftwLibraryName), utf8_to_wstring(fftwfLibraryName)); - if (!res) { - std::string fftwLibraryName = "libfftw3" + get_dynamic_library_extension() + ".3"; - std::string fftwfLibraryName = "libfftw3f" + get_dynamic_library_extension() + ".3"; - res = loadFFTWLibrary(utf8_to_wstring(fftwLibraryName), utf8_to_wstring(fftwfLibraryName)); - } - if (!res) { - // try with version 3.3 macos specific - std::string fftwLibraryName = "libfftw3.3" + get_dynamic_library_extension(); - std::string fftwfLibraryName = "libfftw3f" + get_dynamic_library_extension() + ".3"; - res = loadFFTWLibrary(utf8_to_wstring(fftwLibraryName), utf8_to_wstring(fftwfLibraryName)); - } -#endif - if (res) { + auto getLibraryNames = []() -> std::vector> { + std::string ext = get_dynamic_library_extension(); + return { { "libfftw3-3" + ext, "libfftw3f-3" + ext }, + { "libfftw3" + ext, "libfftw3f" + ext }, + { "libfftw3" + ext + ".3", "libfftw3f" + ext + ".3" }, + { "libfftw3.3" + ext, "libfftw3f" + ext } }; + }; + + auto tryLoadLibrary = [](const auto& names) { + return loadFFTWLibrary(utf8_to_wstring(names.first), utf8_to_wstring(names.second)); + }; + + auto initializeFffwThreads = []() { #if WITH_OPENMP if (fftw_init_threadsPtr && fftw_plan_with_nthreadsPtr) { fftw_init_threadsPtr(); fftw_plan_with_nthreadsPtr(omp_get_max_threads()); } #endif + }; + + bool res = false; + for (auto names : getLibraryNames()) { + res = tryLoadLibrary(names); + if (res) { + break; + } + } + +#if defined(__APPLE__) + if (!res) { + res = loadFFTWLibraryOnMacOs(); + } +#endif + if (res) { + initializeFffwThreads(); } return res; } diff --git a/modules/integer/tests/test_times_1.m b/modules/integer/tests/test_times_1.m new file mode 100644 index 0000000000..2bf2019efd --- /dev/null +++ b/modules/integer/tests/test_times_1.m @@ -0,0 +1,24 @@ +%============================================================================= +% Copyright (c) 2016-present Allan CORNET (Nelson) +%============================================================================= +% This file is part of the Nelson. +%============================================================================= +% LICENCE_BLOCK_BEGIN +% SPDX-License-Identifier: LGPL-3.0-or-later +% LICENCE_BLOCK_END +%============================================================================= +R = intmax('int8') .* 2; +R = intmax('int8') .* -2; +%============================================================================= +R = intmax('int8') .* intmax('int8'); +REF = intmax('int8'); +assert_isequal(R, REF); +%============================================================================= +R = intmax('int8') .* intmin('int8'); +REF = intmin('int8'); +assert_isequal(R, REF); +%============================================================================= +R = intmin('int8') .* intmax('int8'); +REF = intmin('int8'); +assert_isequal(R, REF); +%============================================================================= diff --git a/modules/integer/tests/test_times.m b/modules/integer/tests/test_times_2.m similarity index 69% rename from modules/integer/tests/test_times.m rename to modules/integer/tests/test_times_2.m index a6f09cbb8a..b053960dd8 100644 --- a/modules/integer/tests/test_times.m +++ b/modules/integer/tests/test_times_2.m @@ -7,21 +7,6 @@ % SPDX-License-Identifier: LGPL-3.0-or-later % LICENCE_BLOCK_END %============================================================================= -R = intmax('int8') .* 2; -R = intmax('int8') .* -2; -%============================================================================= -R = intmax('int8') .* intmax('int8'); -REF = intmax('int8'); -assert_isequal(R, REF); -%============================================================================= -R = intmax('int8') .* intmin('int8'); -REF = intmin('int8'); -assert_isequal(R, REF); -%============================================================================= -R = intmin('int8') .* intmax('int8'); -REF = intmin('int8'); -assert_isequal(R, REF); -%============================================================================= R = intmin('int8') .* intmin('int8'); REF = intmax('int8'); assert_isequal(R, REF); diff --git a/modules/mex/tests/test_mxDuplicateArray_cell.m b/modules/mex/tests/test_mxDuplicateArray_cell.m index c9a0941a46..9517a2c4b7 100644 --- a/modules/mex/tests/test_mxDuplicateArray_cell.m +++ b/modules/mex/tests/test_mxDuplicateArray_cell.m @@ -12,16 +12,20 @@ end %============================================================================= if exist('mxDuplicateArray') == 0 - test_dir = [tempdir(), 'mxDuplicateArray_cell']; - if isdir(test_dir) - rmdir(test_dir,'s'); + try + test_dir = [tempdir(), 'mxDuplicateArray_cell']; + if isdir(test_dir) + rmdir(test_dir,'s'); + end + mkdir(test_dir); + status = copyfile('mxDuplicateArray.c', test_dir); + assert_istrue(status); + cd(test_dir); + mex('mxDuplicateArray.c'); + addpath(pwd()) + catch NE + rethrow(NE); end - mkdir(test_dir); - status = copyfile('mxDuplicateArray.c', test_dir); - assert_istrue(status); - cd(test_dir); - mex('mxDuplicateArray.c'); - addpath(pwd()) end %============================================================================= REF = {1, single(2); 'Nelson Text in a cell', false};