From 33e5ec6426fb902af28d9f0081e695b01290c21f Mon Sep 17 00:00:00 2001 From: Ruiyang Wu Date: Thu, 19 Jun 2025 13:03:21 -0400 Subject: [PATCH 1/8] openblas: use `cmake` and depend on `libomp` --- Formula/o/openblas.rb | 54 ++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/Formula/o/openblas.rb b/Formula/o/openblas.rb index db192eecfed29..05a474fe564f9 100644 --- a/Formula/o/openblas.rb +++ b/Formula/o/openblas.rb @@ -1,7 +1,7 @@ class Openblas < Formula desc "Optimized BLAS library" homepage "https://www.openblas.net/" - url "https://github.com/OpenMathLib/OpenBLAS/archive/refs/tags/v0.3.30.tar.gz" + url "https://github.com/OpenMathLib/OpenBLAS/releases/download/v0.3.30/OpenBLAS-0.3.30.tar.gz" sha256 "27342cff518646afb4c2b976d809102e368957974c250a25ccc965e53063c95d" # The main license is BSD-3-Clause. Additionally, # 1. OpenBLAS is based on GotoBLAS2 so some code is under original BSD-2-Clause-Views @@ -9,6 +9,7 @@ class Openblas < Formula # 3. interface/{gemmt.c,sbgemmt.c} is BSD-2-Clause # 4. relapack/ is MIT but license is omitted as it is not enabled license all_of: ["BSD-3-Clause", "BSD-2-Clause-Views", "BSD-3-Clause-Open-MPI", "BSD-2-Clause"] + revision 1 head "https://github.com/OpenMathLib/OpenBLAS.git", branch: "develop" livecheck do @@ -30,22 +31,18 @@ class Openblas < Formula keg_only :shadowed_by_macos, "macOS provides BLAS in Accelerate.framework" + depends_on "cmake" => :build depends_on "pkgconf" => :test depends_on "gcc" # for gfortran - fails_with :clang + + on_macos do + depends_on "libomp" + end def install ENV.runtime_cpu_detection - ENV.deparallelize # build is parallel by default, but setting -j confuses it - - # The build log has many warnings of macOS build version mismatches. - ENV["MACOSX_DEPLOYMENT_TARGET"] = MacOS.version.to_s if OS.mac? - ENV["DYNAMIC_ARCH"] = "1" - ENV["USE_OPENMP"] = "1" - # Force a large NUM_THREADS to support larger Macs than the VMs that build the bottles - ENV["NUM_THREADS"] = "56" - # See available targets in TargetList.txt - ENV["TARGET"] = case Hardware.oldest_cpu + + target = case Hardware.oldest_cpu when :arm_vortex_tempest "VORTEX" when :westmere @@ -54,17 +51,30 @@ def install Hardware.oldest_cpu.upcase.to_s end - # Apple Silicon does not support SVE - # https://github.com/OpenMathLib/OpenBLAS/issues/4212 - ENV["NO_SVE"] = "1" if Hardware::CPU.arm? + args = %W[ + -DUSE_OPENMP=ON + -DBUILD_SHARED_LIBS=ON + -DBUILD_STATIC_LIBS=ON + -DNUM_THREADS=64 + -DTARGET=#{target} + ] + + args << "-DDYNAMIC_ARCH=ON" if !OS.mac? || Hardware::CPU.intel? + + if OS.mac? + args << "-DOpenMP_Fortran_LIB_NAMES=omp" + args << "-DOpenMP_omp_LIBRARY=#{Formula["libomp"].opt_lib}/libomp.dylib" + end - # Must call in two steps - system "make", "CC=#{ENV.cc}", "FC=gfortran", "libs", "netlib", "shared" - system "make", "PREFIX=#{prefix}", "install" + system "cmake", "-S", ".", "-B", "build", *args, *std_cmake_args + system "cmake", "--build", "build" + system "cmake", "--install", "build" lib.install_symlink shared_library("libopenblas") => shared_library("libblas") lib.install_symlink shared_library("libopenblas") => shared_library("liblapack") pkgshare.install "cpp_thread_test" + + inreplace lib/"pkgconfig/openblas.pc", prefix, opt_prefix end test do @@ -89,12 +99,14 @@ def install return 0; } C - system ENV.cc, "test.c", "-I#{include}", "-L#{lib}", "-lopenblas", "-o", "test" - system "./test" - cp_r pkgshare/"cpp_thread_test/.", testpath ENV.prepend_path "PKG_CONFIG_PATH", lib/"pkgconfig" if OS.mac? flags = shell_output("pkgconf --cflags --libs openblas").chomp.split + system ENV.cc, "test.c", "-o", "test", *flags + system "./test" + + flags += %W[-I#{Formula["libomp"].opt_include} -L#{Formula["libomp"].opt_lib} -lomp] if OS.mac? + cp_r pkgshare/"cpp_thread_test/.", testpath %w[dgemm_thread_safety dgemv_thread_safety].each do |test| inreplace "#{test}.cpp", '"../cblas.h"', '"cblas.h"' system ENV.cxx, *ENV.cxxflags.to_s.split, "-std=c++11", "#{test}.cpp", "-o", test, *flags From 8dac6144e5cb51e5f343e908a20afc736c3d08bd Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Fri, 8 Aug 2025 16:18:39 -0400 Subject: [PATCH 2/8] fftw: use `libomp` on macOS --- Formula/f/fftw.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Formula/f/fftw.rb b/Formula/f/fftw.rb index bed1fb2affda1..b26bf3cf703ad 100644 --- a/Formula/f/fftw.rb +++ b/Formula/f/fftw.rb @@ -4,7 +4,7 @@ class Fftw < Formula url "https://fftw.org/fftw-3.3.10.tar.gz" sha256 "56c932549852cddcfafdab3820b0200c7742675be92179e59e6215b340e26467" license all_of: ["GPL-2.0-or-later", "BSD-2-Clause"] - revision 2 + revision 3 livecheck do url :homepage @@ -28,11 +28,9 @@ class Fftw < Formula depends_on "open-mpi" on_macos do - depends_on "gcc" + depends_on "libomp" end - fails_with :clang - # Fix the cmake config file when configured with autotools, upstream pr ref, https://github.com/FFTW/fftw3/pull/338 patch do url "https://github.com/FFTW/fftw3/commit/394fa85ab5f8914b82b3404844444c53f5c7f095.patch?full_index=1" @@ -42,6 +40,11 @@ class Fftw < Formula def install ENV.runtime_cpu_detection + if OS.mac? + ENV["OPENMP_CFLAGS"] = "-Xpreprocessor -fopenmp" + ENV.append "LIBS", "-lomp" + end + args = [ "--enable-shared", "--disable-debug", From 300b85abc9f9099f4785d04b7b84476f975c68e9 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Fri, 8 Aug 2025 16:38:07 -0400 Subject: [PATCH 3/8] gromacs: use `libomp` on macOS --- Formula/g/gromacs.rb | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/Formula/g/gromacs.rb b/Formula/g/gromacs.rb index 5b7f3f5146e1a..0c8b7085e4959 100644 --- a/Formula/g/gromacs.rb +++ b/Formula/g/gromacs.rb @@ -4,7 +4,7 @@ class Gromacs < Formula url "https://ftp.gromacs.org/pub/gromacs/gromacs-2025.3.tar.gz" sha256 "8bdfca0268f3f10a7ca3c06e59b62f73ea02420c67211c0ff3912f32d7833c65" license "LGPL-2.1-or-later" - revision 1 + revision 2 livecheck do url "https://ftp.gromacs.org/pub/gromacs/" @@ -23,31 +23,25 @@ class Gromacs < Formula depends_on "cmake" => :build depends_on "pkgconf" => :build depends_on "fftw" - depends_on "gcc" # for OpenMP depends_on "lmfit" + depends_on "muparser" depends_on "openblas" uses_from_macos "zlib" on_macos do - conflicts_with "muparser", because: "gromacs ships its own copy of muparser" + depends_on "libomp" end - on_linux do - depends_on "muparser" - end - - fails_with :clang - def install # Non-executable GMXRC files should be installed in DATADIR inreplace "scripts/CMakeLists.txt", "CMAKE_INSTALL_BINDIR", "CMAKE_INSTALL_DATADIR" # Avoid superenv shim reference - gcc = Formula["gcc"] - cc = gcc.opt_bin/"gcc-#{gcc.any_installed_version.major}" - cxx = gcc.opt_bin/"g++-#{gcc.any_installed_version.major}" + cc = DevelopmentTools.locate(ENV.cc) + cxx = DevelopmentTools.locate(ENV.cxx) + inreplace "src/gromacs/gromacs-hints.in.cmake" do |s| s.gsub! "@CMAKE_LINKER@", "/usr/bin/ld" s.gsub! "@CMAKE_C_COMPILER@", cc @@ -75,15 +69,9 @@ def install -DGMX_INSTALL_LEGACY_API=ON -DGMX_EXTERNAL_ZLIB=ON -DGMX_USE_LMFIT=EXTERNAL + -DGMX_USE_MUPARSER=EXTERNAL -DGMX_SIMD=#{gmx_simd} ] - args << if OS.mac? - # Use bundled `muparser` as brew formula is linked to libc++ on macOS but we need libstdc++. - # TODO: Try switching `gromacs` and its dependency tree to use Apple Clang + `libomp` - "-DFETCHCONTENT_SOURCE_DIR_MUPARSER=#{buildpath}/src/external/muparser" - else - "-DGMX_USE_MUPARSER=EXTERNAL" - end system "cmake", "-S", ".", "-B", "build", *std_cmake_args, *args system "cmake", "--build", "build" From f7b6f8673c0eb4b7bb52cf7a77917b369b00ad88 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Fri, 8 Aug 2025 18:38:51 -0400 Subject: [PATCH 4/8] muparser: remove conflict with `gromacs` --- Formula/m/muparser.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/Formula/m/muparser.rb b/Formula/m/muparser.rb index 2cb7ab6022401..4d2b65acde3a0 100644 --- a/Formula/m/muparser.rb +++ b/Formula/m/muparser.rb @@ -22,7 +22,6 @@ class Muparser < Formula on_macos do depends_on "libomp" - conflicts_with "gromacs", because: "gromacs ships its own copy of muparser" end def install From b8ba4493dbafb5e024f0671fb3961ece41173a48 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Wed, 13 Aug 2025 15:02:03 -0400 Subject: [PATCH 5/8] dynare: use `libomp` on macOS --- Formula/d/dynare.rb | 61 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/Formula/d/dynare.rb b/Formula/d/dynare.rb index 91767e8a93b84..4ae14c6241626 100644 --- a/Formula/d/dynare.rb +++ b/Formula/d/dynare.rb @@ -4,7 +4,7 @@ class Dynare < Formula url "https://www.dynare.org/release/source/dynare-6.4.tar.xz" sha256 "9865e2e7f6b3705155538d5fb1fb0b01bc9decf07250b3b054d3555d651c3843" license "GPL-3.0-or-later" - revision 1 + revision 2 head "https://git.dynare.org/Dynare/dynare.git", branch: "master" livecheck do @@ -39,11 +39,22 @@ class Dynare < Formula depends_on "openblas" depends_on "suite-sparse" + on_macos do + depends_on "llvm" => :build if DevelopmentTools.clang_build_version <= 1600 + depends_on "libomp" + + # Work around LLVM issue with structured bindings[^1] by partly reverting + # commit[^2]. Upstream isn't interested in supporting Clang build[^3]. + # + # [^1]: https://github.com/llvm/llvm-project/issues/33025 + # [^2]: https://git.dynare.org/Dynare/dynare/-/commit/6ff7d4c56c26a2b7546de633dbcfe2f163bf846d + # [^3]: https://git.dynare.org/Dynare/dynare/-/issues/1977 + patch :DATA + end + fails_with :clang do - cause <<~EOS - GCC is the only compiler supported by upstream - https://git.dynare.org/Dynare/dynare/-/blob/master/README.md#general-instructions - EOS + build 1600 + cause "needs C++20 std::jthreads" end resource "slicot" do @@ -68,8 +79,14 @@ def install # Help meson find `boost`, `suite-sparse` and `slicot` ENV["BOOST_ROOT"] = Formula["boost"].opt_prefix - ENV.append_path "LIBRARY_PATH", Formula["suite-sparse"].opt_lib ENV.append_path "LIBRARY_PATH", buildpath/"slicot/lib" + # NOTE: LIBRARY_PATH doesn't seem to work in Clang when `static` arg is used. + # May be related to https://github.com/mesonbuild/meson/issues/10172 + inreplace "meson.build", "'umfpack', dirs : octlibdir / '../..',", + "'umfpack', dirs : '#{Formula["suite-sparse"].opt_lib}'," + + # Enable jthreads in Xcode 16.3-16.4: https://github.com/llvm/llvm-project/issues/104154 + ENV.append_to_cflags "-fexperimental-library" if OS.mac? && MacOS.version == :sequoia system "meson", "setup", "build", "-Dbuild_for=octave", *std_meson_args system "meson", "compile", "-C", "build", "--verbose" @@ -112,3 +129,35 @@ def caveats "--no-history", "--path", "#{lib}/dynare/matlab", "dyn_test.m" end end + +__END__ +diff --git a/mex/sources/local_state_space_iterations/local_state_space_iteration_2.cc b/mex/sources/local_state_space_iterations/local_state_space_iteration_2.cc +index 5d0b0800b..97708e607 100644 +--- a/mex/sources/local_state_space_iterations/local_state_space_iteration_2.cc ++++ b/mex/sources/local_state_space_iterations/local_state_space_iteration_2.cc +@@ -69,8 +69,10 @@ ss2Iteration_pruning(double* y2, double* y1, const double* yhat2, const double* + const double one = 1.0; + const blas_int ONE = 1; + #endif +- auto [ii1, ii2, ii3] = set_vector_of_indices(n, m); // vector indices for ghxx +- auto [jj1, jj2, jj3] = set_vector_of_indices(q, m); // vector indices for ghuu ++ std::vector ii1, ii2, ii3;// vector indices for ghxx ++ std::tie(ii1, ii2, ii3) = set_vector_of_indices(n, m); ++ std::vector jj1, jj2, jj3;// vector indices for ghuu ++ std::tie(jj1, jj2, jj3) = set_vector_of_indices(q, m); + #pragma omp parallel for num_threads(number_of_threads) + for (int particle = 0; particle < s; particle++) + { +@@ -148,8 +150,10 @@ ss2Iteration(double* y, const double* yhat, const double* epsilon, const double* + const double one = 1.0; + const blas_int ONE = 1; + #endif +- auto [ii1, ii2, ii3] = set_vector_of_indices(n, m); // vector indices for ghxx +- auto [jj1, jj2, jj3] = set_vector_of_indices(q, m); // vector indices for ghuu ++ std::vector ii1, ii2, ii3;// vector indices for ghxx ++ std::tie(ii1, ii2, ii3) = set_vector_of_indices(n, m); ++ std::vector jj1, jj2, jj3;// vector indices for ghuu ++ std::tie(jj1, jj2, jj3) = set_vector_of_indices(q, m); + #pragma omp parallel for num_threads(number_of_threads) + for (int particle = 0; particle < s; particle++) + { From ce6f999d2a3aeb4f22e3aa7e3809a65e7fb10b26 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Wed, 13 Aug 2025 17:13:13 -0400 Subject: [PATCH 6/8] cp2k: use `libomp` on macOS --- Formula/c/cp2k.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Formula/c/cp2k.rb b/Formula/c/cp2k.rb index 5178446b2ac8a..6938eb6195839 100644 --- a/Formula/c/cp2k.rb +++ b/Formula/c/cp2k.rb @@ -4,6 +4,7 @@ class Cp2k < Formula url "https://github.com/cp2k/cp2k/releases/download/v2025.1/cp2k-2025.1.tar.bz2" sha256 "65c8ad5488897b0f995919b9fa77f2aba4b61677ba1e3c19bb093d5c08a8ce1d" license "GPL-2.0-or-later" + revision 1 livecheck do url :stable @@ -33,8 +34,8 @@ class Cp2k < Formula depends_on "openblas" depends_on "scalapack" - fails_with :clang do - cause "needs OpenMP support for C/C++ and Fortran" + on_macos do + depends_on "libomp" end resource "libint" do @@ -59,12 +60,12 @@ def install ENV.prepend_path "PKG_CONFIG_PATH", libexec/"lib/pkgconfig" end - # TODO: Add workaround to link to LLVM OpenMP (libomp) with gfortran after migrating OpenBLAS + # Workaround to link to LLVM OpenMP (libomp) with gfortran omp_args = [] - # if OS.mac? - # omp_args << "-DOpenMP_Fortran_LIB_NAMES=omp" - # omp_args << "-DOpenMP_omp_LIBRARY=#{Formula["libomp"].opt_lib}/libomp.dylib" - # end + if OS.mac? + omp_args << "-DOpenMP_Fortran_LIB_NAMES=omp" + omp_args << "-DOpenMP_omp_LIBRARY=#{Formula["libomp"].opt_lib}/libomp.dylib" + end # TODO: Remove dbcsr build along with corresponding CMAKE_PREFIX_PATH # and add -DCP2K_BUILD_DBCSR=ON once `cp2k` build supports this option. @@ -86,9 +87,7 @@ def install # Avoid trying to access /proc/self/statm on macOS ENV.append "FFLAGS", "-D__NO_STATM_ACCESS" if OS.mac? - # Set -lstdc++ to allow gfortran to link libint cp2k_cmake_args = %W[ - -DCMAKE_SHARED_LINKER_FLAGS=-lstdc++ -DCMAKE_INSTALL_RPATH=#{rpath};#{rpath(target: libexec/"lib")} -DCP2K_BLAS_VENDOR=OpenBLAS -DCP2K_USE_LIBINT2=ON From b69286c161fdbbc2c028a4b4f68e183894774892 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Wed, 13 Aug 2025 19:02:29 -0400 Subject: [PATCH 7/8] nwchem: use `libomp` on macOS --- Formula/n/nwchem.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Formula/n/nwchem.rb b/Formula/n/nwchem.rb index 9628afb0e531d..721f7310d3f8c 100644 --- a/Formula/n/nwchem.rb +++ b/Formula/n/nwchem.rb @@ -5,7 +5,7 @@ class Nwchem < Formula version "7.2.3" sha256 "7788e6af9be8681e6384b8df4df5ac57d010b2c7aa50842d735c562d92f94c25" license "ECL-2.0" - revision 1 + revision 2 livecheck do url :stable @@ -37,6 +37,10 @@ class Nwchem < Formula uses_from_macos "libxcrypt" + on_macos do + depends_on "libomp" + end + # fix download url in build_dftd3a.sh, upstream pr ref, https://github.com/nwchemgit/nwchem/pull/1054 patch do url "https://github.com/nwchemgit/nwchem/commit/65ce7726d9fa418f7c01665bebfc1e2181f15adf.patch?full_index=1" @@ -47,6 +51,9 @@ def install pkgshare.install "QA" cd "src" do + # Workaround to link to LLVM OpenMP (libomp) with gfortran + inreplace "config/makefile.h", /(\bLDOPTIONS *\+= *)-fopenmp$/, "\\1-lomp" if OS.mac? + (prefix/"etc").mkdir (prefix/"etc/nwchemrc").write <<~EOS nwchem_basis_library #{pkgshare}/libraries/ From ca9a5b9dc1d96187848763c64489a5092986502a Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Tue, 19 Aug 2025 08:54:52 -0400 Subject: [PATCH 8/8] pytorch: update linkage to `libomp` --- Formula/p/pytorch.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Formula/p/pytorch.rb b/Formula/p/pytorch.rb index c119a427bdda5..de505f7737f1d 100644 --- a/Formula/p/pytorch.rb +++ b/Formula/p/pytorch.rb @@ -6,7 +6,7 @@ class Pytorch < Formula url "https://github.com/pytorch/pytorch/releases/download/v2.8.0/pytorch-v2.8.0.tar.gz" sha256 "c70a2c9488f6f6e8af5982a10d1cc2c37b7df5e6506d839daa5d5e250953d7b5" license "BSD-3-Clause" - revision 3 + revision 4 livecheck do url :stable