Skip to content

Commit

Permalink
add support for macos arm64
Browse files Browse the repository at this point in the history
  • Loading branch information
hczhai committed Sep 2, 2022
1 parent ed53a80 commit 84c00f8
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 95 deletions.
5 changes: 3 additions & 2 deletions .github/actions/build-wheel/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PY_EXE=/opt/python/"${PY_VER}"/bin/python3
sed -i "/DPYTHON_EXECUTABLE/a \ '-DPYTHON_EXECUTABLE=${PY_EXE}'," setup.py

/opt/python/"${PY_VER}"/bin/pip install --upgrade --no-cache-dir pip
/opt/python/"${PY_VER}"/bin/pip install --no-cache-dir mkl==2019 mkl-include intel-openmp numpy cmake==3.17 pybind11
/opt/python/"${PY_VER}"/bin/pip install --no-cache-dir mkl==2019 mkl-include intel-openmp numpy 'cmake>=3.19' pybind11

if [ "${PARALLEL}" = "mpi" ]; then
yum install -y wget openssh-clients openssh-server
Expand All @@ -37,7 +37,7 @@ if [ "${PARALLEL}" = "mpi" ]; then
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
/opt/python/"${PY_VER}"/bin/pip install --no-cache-dir mpi4py
sed -i "/DUSE_MKL/a \ '-DMPI=ON'," setup.py
sed -i "s/name='block2'/name='block2-mpi'/g" setup.py
sed -i "s/name=\"block2\"/name=\"block2-mpi\"/g" setup.py
sed -i '/for soname, src_path/a \ if any(x in soname for x in ["libmpi", "libopen-pal", "libopen-rte"]): continue' \
$($(cat $(which auditwheel) | head -1 | awk -F'!' '{print $2}') -c "from auditwheel import repair;print(repair.__file__)")
sed -i '/for soname, src_path/a \ if "libmpi.so" in soname: patcher.replace_needed(fn, soname, "libmpi.so")' \
Expand All @@ -52,6 +52,7 @@ ${PY_EXE} -c 'import site; x = site.getsitepackages(); x += [xx.replace("site-pa
sed -i '/rpath_set\[rpath\]/a \ import site\n for x in set(["../lib" + p.split("lib")[-1] for p in open("/tmp/ptmp").read().strip().split("*")]): rpath_set[rpath.replace("../..", x)] = ""' \
$($(cat $(which auditwheel) | head -1 | awk -F'!' '{print $2}') -c "from auditwheel import repair;print(repair.__file__)")

cmake --version
/opt/python/"${PY_VER}"/bin/pip wheel . -w ./dist --no-deps

find . -type f -iname "*-linux*.whl" -exec sh -c "auditwheel repair '{}' -w \$(dirname '{}') --plat '${PLAT}'" \;
Expand Down
61 changes: 54 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
run: |
export PYT=$(which python)
python -m pip install pip build twine --upgrade
python -m pip install mkl==2019 mkl-include intel-openmp numpy cmake==3.17 pybind11
python -m pip install mkl==2019 mkl-include intel-openmp numpy 'cmake>=3.19' pybind11
- name: install requirements (linux / mpi)
if: matrix.parallel == 'mpi' && matrix.os == 'ubuntu-latest'
Expand Down Expand Up @@ -75,6 +75,7 @@ jobs:
run: |
mkdir build
cd build
cmake --version
cmake .. -DUSE_MKL=ON -DBUILD_LIB=ON -DLARGE_BOND=ON -DUSE_COMPLEX=ON -DUSE_SG=ON
make -j 2
Expand All @@ -85,16 +86,19 @@ jobs:
run: |
mkdir build_test
cd build_test
cmake --version
cmake .. -DUSE_MKL=ON -DBUILD_TEST=ON -DLARGE_BOND=ON -DUSE_COMPLEX=ON -DUSE_SINGLE_PREC=ON -DUSE_SG=ON
make -j 2
- name: build test (serial, macos)
if: matrix.parallel == 'serial' && matrix.os == 'macos-latest'
env:
MKLROOT: ~/.local
MACOSX_DEPLOYMENT_TARGET: '10.9'
run: |
mkdir build_test
cd build_test
cmake --version
cmake .. -DUSE_MKL=ON -DBUILD_TEST=ON -DLARGE_BOND=ON -DUSE_COMPLEX=ON -DUSE_SINGLE_PREC=ON -DUSE_SG=ON
make -j 2
Expand Down Expand Up @@ -135,13 +139,17 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
python: [ 3.6, 3.7, 3.8, 3.9 ]
python: [ '3.6', '3.7', '3.8', '3.9' ]
manylinux: [ 2010 ]
plat: [ x86_64 ]
plat: [ x86_64, arm64 ]
parallel: [ mpi, serial ]
exclude:
- os: macos-latest
parallel: mpi
- os: macos-latest
python: '3.6'
- os: ubuntu-latest
plat: arm64
fail-fast: false

steps:
Expand All @@ -156,20 +164,59 @@ jobs:
run: |
export PYT=$(which python)
python -m pip install pip build twine --upgrade
python -m pip install mkl==2019 mkl-include intel-openmp numpy cmake==3.17 pybind11
python -m pip install mkl==2019 mkl-include intel-openmp numpy 'cmake>=3.19' pybind11
- name: build wheels (macos)
if: startsWith(github.ref, 'refs/tags/') && matrix.os == 'macos-latest'
- name: build wheels (macos-x86_64)
if: startsWith(github.ref, 'refs/tags/') && matrix.os == 'macos-latest' && matrix.plat == 'x86_64'
env:
TAG_STRING: ${{ github.event.ref }}
MACOSX_DEPLOYMENT_TARGET: '10.9'
run: |
brew update
brew install libomp
brew install gnu-sed
export PATH=/usr/local/opt/gnu-sed/bin:$PATH
echo ${TAG_STRING:11}
sed -i "" "s/version=.*/version='${TAG_STRING:11}',/" setup.py
gsed -i "s/version=.*/version='${TAG_STRING:11}',/" setup.py
# gsed -i "/DUSE_MKL/a \ '-DOMP_LIB=OMP'," setup.py
# gsed -i "/intel-openmp/d" setup.py
# export OMPROOT=$(brew --prefix libomp)
# echo ${OMPROOT}
cmake --version
python -m pip install wheel
python -m pip wheel . -w ./dist --no-deps
- name: build wheels (macos-arm64)
if: startsWith(github.ref, 'refs/tags/') && matrix.os == 'macos-latest' && matrix.plat == 'arm64'
env:
TAG_STRING: ${{ github.event.ref }}
MACOSX_DEPLOYMENT_TARGET: '10.9'
run: |
brew update
brew install libomp
brew install gnu-sed
export PATH=/usr/local/opt/gnu-sed/bin:$PATH
echo ${TAG_STRING:11}
gsed -i "s/version=.*/version='${TAG_STRING:11}',/" setup.py
gsed -i "/DUSE_MKL/a \ '-DOMP_LIB=OMP'," setup.py
gsed -i "/DUSE_MKL/a \ '-DARCH_ARM64=ON'," setup.py
gsed -i "/mkl/d" setup.py
gsed -i "/intel-openmp/d" setup.py
gsed -i "/DUSE_MKL/c \ '-DUSE_MKL=OFF'," setup.py
git clone https://github.com/Homebrew/brew arm-brew
cd arm-brew && git checkout 3.5.10 && cd ..
OMPGZ=$(./arm-brew/bin/brew fetch --force --bottle-tag=arm64_big_sur libomp | grep "Downloaded to" | awk '{print $3}')
./arm-brew/bin/brew install ${OMPGZ}
export OMPROOT=$(./arm-brew/bin/brew --prefix libomp)
echo ${OMPROOT}
cmake --version
python -m pip install wheel delocate
export _PYTHON_HOST_PLATFORM="macosx-10.15-arm64"
export CROSS_COMPILE=1
export PLAT="arm64"
python -m pip wheel . -w ./dist --no-deps
delocate-wheel --require-archs=arm64 -k dist/*.whl
- name: prepare build wheels (manylinux)
if: startsWith(github.ref, 'refs/tags/') && matrix.os == 'ubuntu-latest'
env:
Expand Down
38 changes: 32 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ OPTION(USE_KSYMM "KSymmetry" OFF)
OPTION(USE_COMPLEX "ComplexNumber" OFF)
OPTION(USE_SG "SpinGeneral" OFF)
OPTION(USE_SINGLE_PREC "SinglePrecision" OFF)
OPTION(ARCH_ARM64 "macOS arch arm64" OFF)

IF (NOT ${USE_DMRG})
SET(USE_BIG_SITE OFF)
SET(USE_SP_DMRG OFF)
ENDIF()

# Project Name (must be python module name)

Expand Down Expand Up @@ -351,13 +357,21 @@ ELSEIF (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
SET(MKL_OMP_LIB_NAME mkl_intel_thread)
SET(MKL_OMP_VALUE 2)
ELSEIF (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
FIND_LIBRARY(OMP_LIB_NAME NAMES iomp5 PATHS /usr/local/lib /usr/lib64 ${PYTHON_LIB_PATH})
IF("${OMP_LIB}" STREQUAL "OMP")
SET(OMP_LIB_FIND_NAME omp)
ELSE()
SET(OMP_LIB_FIND_NAME iomp5)
ENDIF()
FIND_LIBRARY(OMP_LIB_NAME NAMES ${OMP_LIB_FIND_NAME} PATHS $ENV{OMPROOT} $ENV{OMPROOT}/lib NO_DEFAULT_PATH)
IF (NOT OMP_LIB_NAME)
FIND_LIBRARY(OMP_LIB_NAME NAMES ${OMP_LIB_FIND_NAME} PATHS /usr/local/lib /usr/lib64 ${PYTHON_LIB_PATH})
ENDIF()
IF (NOT OMP_LIB_NAME)
EXECUTE_PROCESS(
COMMAND ${CMAKE_CXX_COMPILER} -print-search-dirs
COMMAND grep "^lib" COMMAND awk -F "=" "{print $2}" COMMAND tr ":" ";"
OUTPUT_VARIABLE OMP_LIB_HINT OUTPUT_STRIP_TRAILING_WHITESPACE)
FIND_LIBRARY(OMP_LIB_NAME NAMES iomp5 PATHS ${OMP_LIB_HINT})
FIND_LIBRARY(OMP_LIB_NAME NAMES ${OMP_LIB_FIND_NAME} PATHS ${OMP_LIB_HINT})
ENDIF()
SET(MKL_OMP_LIB_NAME mkl_intel_thread)
SET(MKL_OMP_VALUE 2)
Expand Down Expand Up @@ -388,6 +402,14 @@ IF ((NOT APPLE) AND (NOT WIN32) AND (NOT (CMAKE_CXX_COMPILER_ID STREQUAL "Intel"
SET(OPT_FLAG ${OPT_FLAG} -Wno-psabi)
ENDIF()

IF (${ARCH_ARM64})
SET(ARCH_FLAG -arch arm64)
SET(ARCH_LINK_FLAGS "-arch arm64")
ELSE()
SET(ARCH_FLAG "")
SET(ARCH_LINK_FLAGS "")
ENDIF()

IF (${USE_MKL})
SET(USE_MKL_ANY ON)
ELSEIF(${USE_MKL64})
Expand Down Expand Up @@ -442,6 +464,9 @@ IF (${USE_MKL_ANY})
MESSAGE(STATUS "MKL_INCLUDE_DIR = ${MKL_INCLUDE_DIR}")
MESSAGE(STATUS "MKL_LIBS = ${MKL_LIBS}")
ELSE()
IF (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
SET(BLA_VENDOR Apple)
ENDIF()
# Check LAPACK and BLAS
FIND_PACKAGE(BLAS REQUIRED)
FIND_PACKAGE(LAPACK REQUIRED)
Expand Down Expand Up @@ -512,11 +537,12 @@ ENDIF()

TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC ${OMP_LIB_NAME} ${PTHREAD})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC ${PTHREAD} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES} ${MKL_LIBS} ${MPI_LIBS} ${TBB_LIBS})
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINK_FLAGS "${ARCH_LINK_FLAGS} ${MPI_LINK_FLAGS}")

MESSAGE(STATUS "SRCS = ${SRCS}")
MESSAGE(STATUS "PYBIND_SRCS = ${PYBIND_SRCS}")
MESSAGE(STATUS "OPT_FLAG = ${OPT_FLAG}")
MESSAGE(STATUS "ARCH_FLAG = ${ARCH_FLAG}")
MESSAGE(STATUS "BOND_FLAG = ${BOND_FLAG}")
MESSAGE(STATUS "MKL_FLAG = ${MKL_FLAG}")
MESSAGE(STATUS "CORE_FLAG = ${CORE_FLAG}")
Expand All @@ -537,7 +563,7 @@ MESSAGE(STATUS "TBB_LIBS = ${TBB_LIBS}")

TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} PUBLIC ${PYTHON_INCLUDE_DIRS} ${PYBIND_INCLUDE_DIRS}
${MKL_INCLUDE_DIR} ${MPI_INCLUDE_DIR} ${TBB_INCLUDE_DIR})
TARGET_COMPILE_OPTIONS(${PROJECT_NAME} BEFORE PUBLIC ${OPT_FLAG} ${MKL_FLAG} ${MPI_FLAG}
TARGET_COMPILE_OPTIONS(${PROJECT_NAME} BEFORE PUBLIC ${ARCH_FLAG} ${OPT_FLAG} ${MKL_FLAG} ${MPI_FLAG}
${TMPL_FLAG} ${BOND_FLAG} ${SCI_FLAG} ${CORE_FLAG} ${DMRG_FLAG} ${BIG_SITE_FLAG}
${SP_DMRG_FLAG} ${IC_FLAG} ${KSYMM_FLAG} ${SG_FLAG} ${COMPLEX_FLAG} ${SINGLE_PREC_FLAG}
${TBB_FLAG})
Expand All @@ -557,10 +583,10 @@ IF (${BUILD_TEST})
ADD_EXECUTABLE(${PROJECT_NAME}_tests ${TSRCS} ${SRCS})
TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME}_tests PUBLIC src ${MKL_INCLUDE_DIR} ${MPI_INCLUDE_DIR} ${TBB_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}_tests ${GTEST_BOTH_LIBRARIES} ${PTHREAD} ${MPI_LIBS} ${TBB_LIBS})
TARGET_COMPILE_OPTIONS(${PROJECT_NAME}_tests BEFORE PUBLIC ${OPT_FLAG} ${MKL_FLAG} ${MPI_FLAG}
TARGET_COMPILE_OPTIONS(${PROJECT_NAME}_tests BEFORE PUBLIC ${ARCH_FLAG} ${OPT_FLAG} ${MKL_FLAG} ${MPI_FLAG}
${TMPL_FLAG} ${BOND_FLAG} ${SCI_FLAG} ${CORE_FLAG} ${DMRG_FLAG} ${BIG_SITE_FLAG}
${SP_DMRG_FLAG} ${IC_FLAG} ${KSYMM_FLAG} ${SG_FLAG} ${COMPLEX_FLAG} ${SINGLE_PREC_FLAG} ${TBB_FLAG})
SET_TARGET_PROPERTIES(${PROJECT_NAME}_tests PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
SET_TARGET_PROPERTIES(${PROJECT_NAME}_tests PROPERTIES LINK_FLAGS "${ARCH_LINK_FLAGS} ${MPI_LINK_FLAGS}")

IF ((NOT APPLE) AND (NOT WIN32))
TARGET_LINK_LIBRARIES(${PROJECT_NAME}_tests rt)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/developer/notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Edit the ``setup.py``: ::
Instal dependencies and build: ::

python3 -m pip install pip build twine --upgrade
python3 -m pip install mkl==2019 mkl-include intel-openmp numpy cmake==3.17 pybind11
python3 -m pip install mkl==2019 mkl-include intel-openmp numpy 'cmake>=3.19' pybind11
python3 -m build

Change linux tag and upload: ::
Expand Down
Loading

0 comments on commit 84c00f8

Please sign in to comment.