Skip to content

Commit

Permalink
wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
hczhai committed Oct 28, 2024
1 parent 44ed696 commit 104ebab
Show file tree
Hide file tree
Showing 19 changed files with 534 additions and 345 deletions.
85 changes: 76 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ permissions:

jobs:
test:
name: test ${{ matrix.python }} ${{ matrix.os }} ${{ matrix.parallel }}
name: test ${{ matrix.python }} ${{ matrix.os }} ${{ matrix.parallel }} ${{ matrix.plat }}
runs-on: ${{ matrix.os }}

strategy:
Expand All @@ -34,6 +34,11 @@ jobs:
parallel: mpi
- os: windows-2022
parallel: mpi
include:
- os: ubuntu-20.04
python: '3.12'
plat: wasm
parallel: serial
fail-fast: false

steps:
Expand All @@ -43,13 +48,32 @@ jobs:
python-version: ${{ matrix.python }}

- name: install requirements (linux / macos)
if: matrix.os == 'macos-13' || matrix.os == 'ubuntu-20.04'
if: (matrix.os == 'macos-13' || matrix.os == 'ubuntu-20.04') && matrix.plat == 'x86_64'
env:
MKLROOT: ~/.local
run: |
python -m pip install pip build twine setuptools --upgrade
python -m pip install mkl==2021.4 mkl-include intel-openmp numpy 'cmake>=3.19' pybind11==2.12.0
- name: install requirements (linux emscripten)
if: matrix.os == 'ubuntu-20.04' && matrix.plat == 'wasm'
run: |
python -m pip install pip build twine setuptools --upgrade
python -m pip install pyodide-build numpy 'cmake>=3.19' pybind11==2.12.0
- name: setup emscripten (linux emscripten)
if: matrix.os == 'ubuntu-20.04' && matrix.plat == 'wasm'
run: |
git clone -b main https://github.com/emscripten-core/emsdk.git
cd emsdk
PYODIDE_EMSCRIPTEN_VERSION=$(pyodide config get emscripten_version)
echo $PYODIDE_EMSCRIPTEN_VERSION
./emsdk install ${PYODIDE_EMSCRIPTEN_VERSION}
./emsdk activate ${PYODIDE_EMSCRIPTEN_VERSION}
source ./emsdk_env.sh
cd ..
em++ -v
- name: install requirements (linux / macos)
if: matrix.os == 'macos-14'
run: |
Expand All @@ -68,6 +92,25 @@ jobs:
python -m pip install pip build twine setuptools --upgrade
python -m pip install intel-openmp==2024.1.0 numpy 'cmake>=3.19' pybind11==2.12.0
- name: Download OpenBLAS (linux emscripten)
if: matrix.os == 'ubuntu-20.04' && matrix.plat == 'wasm'
run: |
wget -q https://github.com/pyodide/pyodide/releases/download/0.26.3/xbuildenv-0.26.3.tar.bz2
tar xjf xbuildenv-0.26.3.tar.bz2
- name: build gtest (linux emscripten)
if: matrix.os == 'ubuntu-20.04' && matrix.plat == 'wasm'
env:
GTESTROOT: ~/gtest
run: |
source emsdk/emsdk_env.sh
git clone -b release-1.8.0 https://github.com/google/googletest
cd googletest
export GTEST_ROOT=$PWD/googletest/install
CC=emcc CXX=em++ emcmake cmake CMakeLists.txt -DCMAKE_INSTALL_PREFIX=${GTESTROOT} -Dgtest_disable_pthreads=ON
emmake make -j 4
emmake make install
- name: Download OpenBLAS (windows)
if: matrix.os == 'windows-2022'
env:
Expand All @@ -83,14 +126,14 @@ jobs:
GTEST_ROOT: D:\a\gtest
run: |
Get-WmiObject -Class Win32_Processor
git clone -b release-1.8.0 https://github.com/google/googletest.git
git clone -b release-1.8.0 https://github.com/google/googletest.git
cd googletest
cmake -G "Visual Studio 17 2022" -S . -B . -DCMAKE_CXX_FLAGS="-D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING" -Dgtest_force_shared_crt=ON
cmake --build . -j 2 --config Release
cmake --install . --prefix $env:GTEST_ROOT
- name: build gtest (linux)
if: matrix.os == 'ubuntu-20.04'
if: matrix.os == 'ubuntu-20.04' && matrix.plat == 'x86_64'
env:
CC: gcc-9
CXX: g++-9
Expand All @@ -103,7 +146,7 @@ jobs:
sudo cp lib/*.a /usr/lib
- name: fix mkl for amd cpu
if: matrix.os == 'ubuntu-20.04'
if: matrix.os == 'ubuntu-20.04' && matrix.plat == 'x86_64'
env:
CC: gcc-9
CXX: g++-9
Expand Down Expand Up @@ -146,7 +189,9 @@ jobs:
brew install libomp
- name: build test (serial-pytest, linux | macos)
if: (matrix.parallel == 'serial-pytest' || matrix.parallel == 'main-test') && (matrix.os == 'ubuntu-20.04' || matrix.os == 'macos-13')
if: |
(matrix.parallel == 'serial-pytest' || matrix.parallel == 'main-test') &&
(matrix.os == 'ubuntu-20.04' || matrix.os == 'macos-13') && matrix.plat == 'x86_64'
env:
MKLROOT: ~/.local
MACOSX_DEPLOYMENT_TARGET: '11.0'
Expand Down Expand Up @@ -188,7 +233,7 @@ jobs:
cmake --build . -j 2 --config Release -- '/v:d'
- name: build test (any-symm-pytest, linux | macos)
if: matrix.parallel == 'any-symm-pytest' && (matrix.os == 'ubuntu-20.04' || matrix.os == 'macos-13')
if: matrix.parallel == 'any-symm-pytest' && (matrix.os == 'ubuntu-20.04' || matrix.os == 'macos-13') && matrix.plat == 'x86_64'
env:
MKLROOT: ~/.local
MACOSX_DEPLOYMENT_TARGET: '11.0'
Expand Down Expand Up @@ -241,8 +286,23 @@ jobs:
cmake -G "Visual Studio 17 2022" -S .. -B . -DUSE_MKL=OFF -DBUILD_TEST=ON -DLARGE_BOND=ON -DUSE_COMPLEX=ON -DUSE_SG=ON
cmake --build . -j 2 --config Release -- '/v:d'
- name: build test (serial, linux emscripten)
if: matrix.parallel == 'serial' && matrix.os == 'ubuntu-20.04' && matrix.plat == 'wasm'
env:
GTESTROOT: ~/gtest
run: |
source emsdk/emsdk_env.sh
export BLAS_ROOT=$PWD/xbuildenv/pyodide-root/packages/.libs
mkdir build_test
cd build_test
cmake --version
CC=emcc CXX=em++ cmake .. -DUSE_MKL=OFF -DARCH_EMSCRIPTEN=ON -DF77UNDERSCORE=ON -DBUILD_TEST=ON \
-DGTEST_ROOT=${GTESTROOT} -DLARGE_BOND=ON -DUSE_COMPLEX=OFF -DUSE_DMRG=ON \
-DSIMPLE_TEST=ON -DCMAKE_BUILD_TYPE=Release -DOMP_LIB=NONE
emmake make -j 2
- name: build test (serial, linux)
if: matrix.parallel == 'serial' && matrix.os == 'ubuntu-20.04'
if: matrix.parallel == 'serial' && matrix.os == 'ubuntu-20.04' && matrix.plat == 'x86_64'
env:
MKLROOT: ~/.local
CC: gcc-9
Expand Down Expand Up @@ -296,8 +356,15 @@ jobs:
& $dumpbin /DEPENDENTS .\block2_tests.exe
.\block2_tests.exe
- name: run test (serial, linux emscripten)
if: matrix.parallel == 'serial' && matrix.os == 'ubuntu-20.04' && matrix.plat == 'wasm'
run: |
source emsdk/emsdk_env.sh
cd build_test
node block2_tests.js
- name: run test (serial, linux)
if: matrix.parallel == 'serial' && matrix.os == 'ubuntu-20.04'
if: matrix.parallel == 'serial' && matrix.os == 'ubuntu-20.04' && matrix.plat == 'x86_64'
run: |
export CPUTYPE=$(lscpu | grep 'Vendor ID' | awk '{print $3}')
if [ "$CPUTYPE" = "AuthenticAMD" ]; then
Expand Down
28 changes: 24 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ OPTION(USE_SG "General Spin Symmetry" OFF)
OPTION(USE_SINGLE_PREC "Single Precision" OFF)
OPTION(USE_BLIS "BLIS" OFF)
OPTION(ARCH_ARM64 "MacOS arch arm64" OFF)
OPTION(ARCH_EMSCRIPTEN "Build for WebAssembly" OFF)
OPTION(APPLE_ACC_SINGLE_PREC "Fix Apple Accelerate single prec" ON)
OPTION(BUILD_LIB "Build python block2.so" OFF)
OPTION(BUILD_CLIB "Build C++ block2.so" OFF)
Expand Down Expand Up @@ -419,6 +420,11 @@ IF (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
FIND_LIBRARY(OMP_LIB_NAME NAMES iomp5md PATHS $ENV{OMPROOT} $ENV{OMPROOT}/lib $ENV{OMPROOT}/lib/intel64_win)
SET(MKL_OMP_VALUE 2)
ENDIF()
ELSEIF ("${OMP_LIB}" STREQUAL "NONE")
SET(OMP_LIB_NAME "")
SET(OMP_FLAG "")
UNSET(OMP_LIB_NAME)
SET(MKL_OMP_VALUE 4)
ELSEIF (WIN32)
SET(OMP_LIB_NAME "")
SET(PTHREAD "")
Expand Down Expand Up @@ -520,6 +526,13 @@ IF ((NOT APPLE) AND (NOT WIN32) AND (CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
SET(OPT_FLAG ${OPT_FLAG} -Wno-unused-command-line-argument -Wno-tautological-constant-compare)
ENDIF()

IF (${ARCH_EMSCRIPTEN})
SET(CMAKE_EXECUTABLE_SUFFIX ".js")
SET(WASM_LINK_FLAGS "-sALLOW_MEMORY_GROWTH -sSTACK_SIZE=64mb -sSTACK_OVERFLOW_CHECK --preload-file ${CMAKE_SOURCE_DIR}/data@data")
ELSE()
SET(WASM_LINK_FLAGS "")
ENDIF()

IF (${ARCH_ARM64})
SET(ARCH_FLAG -arch arm64)
SET(ARCH_LINK_FLAGS "-arch arm64")
Expand Down Expand Up @@ -769,7 +782,7 @@ FOREACH (target ${TARGETS})
SET_TARGET_PROPERTIES(${target} PROPERTIES POSITION_INDEPENDENT_CODE 1)
TARGET_LINK_LIBRARIES(${target} PRIVATE ${OMP_LIB_NAME} ${PTHREAD})
TARGET_LINK_LIBRARIES(${target} PRIVATE ${PTHREAD} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES} ${MKL_LIBS} ${MPI_LIBS} ${TBB_LIBS} ${BLIS_LIBS})
SET_TARGET_PROPERTIES(${target} PROPERTIES LINK_FLAGS "${ARCH_LINK_FLAGS} ${MPI_LINK_FLAGS}")
SET_TARGET_PROPERTIES(${target} PROPERTIES LINK_FLAGS "${ARCH_LINK_FLAGS} ${WASM_LINK_FLAGS} ${MPI_LINK_FLAGS}")

TARGET_INCLUDE_DIRECTORIES(${target} PUBLIC ${PYTHON_INCLUDE_DIRS} ${PYBIND_INCLUDE_DIRS}
${MKL_INCLUDE_DIR} ${MPI_INCLUDE_DIR} ${TBB_INCLUDE_DIR} ${BLIS_INCLUDE_DIR})
Expand All @@ -787,13 +800,20 @@ IF (${BUILD_TEST})
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS})

IF (${SIMPLE_TEST})
FILE(GLOB TSRCS unit_test/test_wick_*.cpp unit_test/test_npdm_*.cpp unit_test/test_dmrg_*.cpp)
FILE(GLOB TSRCS unit_test/test_batch_gemm.cpp unit_test/test_matrix.cpp
unit_test/test_cg.cpp unit_test/test_dmrg_n2_sto3g.cpp
unit_test/test_dmrg_1site_n2_sto3g.cpp unit_test/test_dmrg_sa_1site_n2_sto3g.cpp
unit_test/test_dmrg_sa_n2_sto3g.cpp unit_test/test_dmrg_sa_tto_n2_sto3g.cpp
unit_test/test_csf_space.cpp unit_test/test_det_n2_sto3g.cpp
unit_test/test_wick_ccsd.cpp unit_test/test_wick_ghf.cpp
unit_test/test_wick_ic_nevpt2.cpp unit_test/test_wick_sc_nevpt2.cpp
unit_test/test_wick_uga_ccsd.cpp unit_test/test_npdm_*.cpp)
ELSE()
FILE(GLOB TSRCS unit_test/test_*.cpp)
ENDIF()
MESSAGE(STATUS "TSRCS = ${TSRCS}")

IF ((NOT APPLE) AND (NOT WIN32))
IF ((NOT APPLE) AND (NOT WIN32) AND (NOT ${ARCH_EMSCRIPTEN}))
SET(OPT_FLAG ${OPT_FLAG} -fcompare-debug-second)
ENDIF()

Expand All @@ -804,7 +824,7 @@ IF (${BUILD_TEST})
${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} ${BLIS_FLAG}
${SU2SZ_FLAG} ${SANY_FLAG})
SET_TARGET_PROPERTIES(${PROJECT_NAME}_tests PROPERTIES LINK_FLAGS "${ARCH_LINK_FLAGS} ${MPI_LINK_FLAGS}")
SET_TARGET_PROPERTIES(${PROJECT_NAME}_tests PROPERTIES LINK_FLAGS "${ARCH_LINK_FLAGS} ${WASM_LINK_FLAGS} ${MPI_LINK_FLAGS}")

IF ((NOT APPLE) AND (NOT WIN32))
TARGET_LINK_LIBRARIES(${PROJECT_NAME}_tests rt)
Expand Down
4 changes: 4 additions & 0 deletions src/core/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
#include "tbb/scalable_allocator.h"
#endif
#include <algorithm>
#ifndef __EMSCRIPTEN__
#ifdef __unix__
#include <execinfo.h>
#endif
#endif
#include <cassert>
#include <complex>
#include <cstdint>
Expand All @@ -54,13 +56,15 @@ namespace block2 {
* Not working for non-unix systems.
*/
inline void print_trace() {
#ifndef __EMSCRIPTEN__
#ifdef __unix__
void *array[32];
size_t size = backtrace(array, 32);
char **strings = backtrace_symbols(array, size);

for (size_t i = 0; i < size; i++)
fprintf(stderr, "%s\n", strings[i]);
#endif
#endif
abort();
}
Expand Down
13 changes: 13 additions & 0 deletions src/core/clebsch_gordan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ struct SU2CG {
rst = sqrt_fact[t] * sqrt_fact[t - alpha1] * sqrt_fact[t - alpha2] *
sqrt_fact[beta1 - t] * sqrt_fact[beta2 - t] *
sqrt_fact[beta3 - t];
#ifndef __EMSCRIPTEN__
r += factor / (rst * rst);
#else
r += factor / (rst * rst + 1E-33L);
#endif
}
return r;
}
Expand Down Expand Up @@ -139,7 +143,12 @@ struct SU2CG {
sqrt_fact[t - alpha3] * sqrt_fact[t - alpha4] *
sqrt_fact[beta1 - t] * sqrt_fact[beta2 - t] *
sqrt_fact[beta3 - t];
#ifndef __EMSCRIPTEN__
r += factor * sqrt_fact[t + 1] * sqrt_fact[t + 1] / (rst * rst);
#else
r += factor * sqrt_fact[t + 1] * sqrt_fact[t + 1] /
(rst * rst + 1E-33L);
#endif
}
return r;
}
Expand Down Expand Up @@ -191,7 +200,11 @@ struct SU2CG {
for (int s = min_s; s <= max_s; ++s, factor *= rr) {
rst = sqrt_fact[jps - s] * sqrt_fact[s] * sqrt_fact[jpp - jps + s] *
sqrt_fact[jmp - s];
#ifndef __EMSCRIPTEN__
r += factor / (rst * rst);
#else
r += factor / (rst * rst + 1E-33L);
#endif
}
return r * sqrt_fact[jpp] * sqrt_fact[jmp] * sqrt_fact[jps] *
sqrt_fact[jms];
Expand Down
Loading

0 comments on commit 104ebab

Please sign in to comment.