diff --git a/.github/workflows/ci_ubuntu.yml b/.github/workflows/ci_ubuntu.yml deleted file mode 100644 index 3f1a341a..00000000 --- a/.github/workflows/ci_ubuntu.yml +++ /dev/null @@ -1,129 +0,0 @@ -name: Ubuntu CI - -on: - push: - paths-ignore: - - ".devcontainer/**" - - ".vscode/**" - - "doc/**" - - "*.md" - pull_request: - paths-ignore: - - ".devcontainer/**" - - ".vscode/**" - - "doc/**" - - "*.md" - -jobs: - gcc11-build: - name: GCC11 build - strategy: - matrix: - python-version: ["3.10"] - runs-on: "ubuntu-22.04" - env: - CXX_COMPILER: "/usr/lib/ccache/g++" - C_COMPILER: "/usr/lib/ccache/gcc" - SCALUQ_USE_TEST: "Yes" - steps: - - uses: actions/checkout@v4 - - - name: Install gcc-11/g++-11 - run: | - sudo apt update && \ - sudo apt install -y software-properties-common && \ - sudo apt update && \ - sudo apt install -y gcc-11 g++-11 && \ - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100 && \ - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100 - - - name: Setup cmake - uses: lukka/get-cmake@latest - - - name: Install Ninja - run: sudo apt install ninja-build - - - name: Setup ccache - uses: hendrikmuhs/ccache-action@v1.2 - with: - key: "${{ github.job }}-ubuntu-22.04" - verbose: 2 - - - name: Install scaluq for Ubuntu - run: CMAKE_BUILD_TYPE=Debug ./script/build_gcc.sh - - - name: Check format - run: | - ninja -C build format - diff=$(git diff) - echo -n "$diff" - test $(echo -n "$diff" | wc -l) -eq 0 - - - name: Install scaluq Python module - run: pip install .[ci] - - - name: Test in Ubuntu - run: | - OMP_PROC_BIND=false ninja test -C build -j $(nproc) - - - name: Test if stub exists - run: | - echo -e "from scaluq import StateVector\nfrom scaluq.gate import I" > /tmp/stub_sample.py - mypy /tmp/stub_sample.py - - nvcc-build: - name: NVCC build - strategy: - matrix: - python-version: ["3.10"] - runs-on: "ubuntu-22.04" - env: - CXX_COMPILER: "/usr/lib/ccache/g++" - C_COMPILER: "/usr/lib/ccache/gcc" - SCALUQ_USE_TEST: "Yes" - SCALUQ_USE_CUDA: "Yes" - SCALUQ_CUDA_ARCH: "PASCAL61" - steps: - - uses: actions/checkout@v4 - - - name: Install gcc-11/g++-11 - run: | - sudo apt update && \ - sudo apt install -y software-properties-common && \ - sudo apt update && \ - sudo apt install -y gcc-11 g++-11 && \ - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100 && \ - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100 - - - name: Setup cmake - uses: lukka/get-cmake@latest - - - name: Install Ninja - run: sudo apt install ninja-build - - - name: Setup ccache - uses: hendrikmuhs/ccache-action@v1.2 - with: - key: "${{ github.job }}-ubuntu-22.04" - verbose: 2 - - - name: Install CUDA toolkit - uses: Jimver/cuda-toolkit@v0.2.11 - with: - cuda: "12.2.0" - method: "network" - - - name: Show installed Compiler version - run: | - nvcc --version - gcc --version - g++ --version - cmake --version - - - name: Install scaluq for Ubuntu - run: ./script/build_gcc.sh - - - name: Install scaluq Python module - run: SCALUQ_USE_CUDA=ON pip install .[ci] - - # GitHub Actions cannot run CUDA program diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 00000000..14010729 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,40 @@ +name: Format + +on: + push: + paths-ignore: + - ".devcontainer/**" + - ".vscode/**" + - "doc/**" + - "*.md" + pull_request: + paths-ignore: + - ".devcontainer/**" + - ".vscode/**" + - "doc/**" + - "*.md" + +jobs: + check-format: + name: Check Format + runs-on: "ubuntu-22.04" + steps: + - uses: actions/checkout@v4 + + - name: Setup cmake + uses: lukka/get-cmake@latest + + - name: Install Ninja + run: sudo apt install ninja-build + + - name: Configure + run: | + mkdir -p ./build + cmake -B build -G Ninja + + - name: Check format + run: | + ninja -C build format + diff=$(git diff) + echo -n "$diff" + test $(echo -n "$diff" | wc -l) -eq 0 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..68605fad --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,97 @@ +name: Build and Test + +on: + push: + paths-ignore: + - ".devcontainer/**" + - ".vscode/**" + - "doc/**" + - "*.md" + pull_request: + paths-ignore: + - ".devcontainer/**" + - ".vscode/**" + - "doc/**" + - "*.md" + +jobs: + test: + name: Build and Test + strategy: + matrix: + os: ["linux", "macos"] + architecture: ["x86_64"] + device: ["cpu", "cuda"] + python-version: ["3.10"] + exclude: + - os: "macos" + device: "cuda" + include: + - os: "linux" + architecture: "x86_64" + runs-on: "ubuntu-22.04" + - os: "macos" + architecture: "x86_64" + runs-on: "macos-13" + runs-on: ${{ matrix.runs-on }} + env: + CMAKE_C_COMPILER: ${{ matrix.os == 'macos' && '/usr/local/opt/ccache/libexec/gcc-14' || '/usr/lib/ccache/gcc' }} + CMAKE_CXX_COMPILER: ${{ matrix.os == 'macos' && '/usr/local/opt/ccache/libexec/g++-14' || '/usr/lib/ccache/g++' }} + CMAKE_BUILD_TYPE: ${{ matrix.device == 'cuda' && 'Release' || 'Debug' }} + SCALUQ_USE_TEST: "ON" + SCALUQ_USE_CUDA: ${{ matrix.device == 'cuda' && 'ON' || 'OFF' }} + SCALUQ_CUDA_ARCH: "PASCAL61" + steps: + - uses: actions/checkout@v4 + + - name: Install Ninja + if: ${{ matrix.os == 'linux' }} + run: sudo apt update && sudo apt install ninja-build + + - name: Install Ninja + if: ${{ matrix.os == 'macos' }} + run: brew install ninja + + - name: Setup ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: "${{ github.job }}-${{ matrix.os }}-${{ matrix.device }}" + verbose: 2 + + - name: Install CUDA toolkit + if: ${{ matrix.device == 'cuda' }} + uses: Jimver/cuda-toolkit@v0.2.11 + with: + cuda: "12.2.0" + method: "network" + + - name: Show installed Compiler version + run: | + [ $SCALUQ_USE_CUDA = 'ON' ] && nvcc --version + ccache --version + $CMAKE_C_COMPILER --version + $CMAKE_CXX_COMPILER --version + cmake --version + ninja --version + + - name: Install scaluq for Ubuntu + run: ./script/build_gcc.sh + + - name: Install scaluq Python module + run: pip install .[ci] + + - name: Test in Ubuntu + if: ${{ matrix.device == 'cpu' }} # currently GPU runner is not supported + run: | + if [ "$(uname)" == 'Darwin' ]; then + NPROC=$(sysctl -n hw.logicalcpu) + else + NPROC=$(nproc) + fi + OMP_PROC_BIND=false ninja test -C build -j ${NPROC} + + - name: Test if stub exists + if: ${{ matrix.device == 'cpu' }} # currently GPU runner is not supported + run: | + echo -e "from scaluq import StateVector\nfrom scaluq.gate import I" > /tmp/stub_sample.py + mypy /tmp/stub_sample.py diff --git a/.github/workflows/wheel.yml b/.github/workflows/wheel.yml index 9fe3aa55..4e4e32fc 100644 --- a/.github/workflows/wheel.yml +++ b/.github/workflows/wheel.yml @@ -18,11 +18,18 @@ jobs: strategy: fail-fast: false matrix: - os-arch: ["manylinux_x86_64"] + os: ["linux", "macos"] + arch: ["x86_64"] cibw-python: ["cp38", "cp39", "cp310", "cp311", "cp312"] include: - - os-arch: "manylinux_x86_64" - os: "ubuntu-22.04" + - os: "linux" + arch: "x86_64" + runs-on: "ubuntu-22.04" + cibw-os-arch: "manylinux_x86_64" + - os: "macos" + arch: "x86_64" + runs-on: "macos-13" + cibw-os-arch: "macosx_x86_64" - cibw-python: "cp38" python-version: "3.8" - cibw-python: "cp39" @@ -33,10 +40,13 @@ jobs: python-version: "3.11" - cibw-python: "cp312" python-version: "3.12" - runs-on: ${{ matrix.os }} + runs-on: ${{ matrix.runs-on }} env: - CIBW_BUILD: ${{ matrix.cibw-python }}-${{ matrix.os-arch }} + CMAKE_C_COMPILER: ${{ matrix.os == 'macos' && 'gcc-14' || 'gcc' }} + CMAKE_CXX_COMPILER: ${{ matrix.os == 'macos' && 'g++-14' || 'g++' }} + CIBW_BUILD: ${{ matrix.cibw-python }}-${{ matrix.cibw-os-arch }} PYTHON: ${{ matrix.python-version }} + MACOSX_DEPLOYMENT_TARGET: "13.0" steps: - uses: actions/checkout@v4 @@ -53,7 +63,7 @@ jobs: - name: Upload wheel to GitHub uses: actions/upload-artifact@v4 with: - name: ${{ matrix.cibw-python }}-${{ matrix.os-arch }} + name: ${{ matrix.cibw-python }}-${{ matrix.cibw-os-arch }} path: ./wheels/*.whl - name: Upload wheel data if the Git tag is set diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f930b27..b34d6aab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,30 +43,20 @@ endif(SCALUQ_USE_CUDA) ### Fetch dependencies ### # Kokkos FetchContent_Declare( - kokkos_fetch + kokkos GIT_REPOSITORY https://github.com/kokkos/kokkos GIT_TAG 4.2.00 ) -FetchContent_GetProperties(kokkos_fetch) -if(NOT kokkos_fetch_POPULATED) - message(STATUS "Fetch Kokkos for parallel execution") - FetchContent_Populate(kokkos_fetch) - add_subdirectory(${kokkos_fetch_SOURCE_DIR}) - set_property(TARGET kokkoscore PROPERTY POSITION_INDEPENDENT_CODE ON) -endif(NOT kokkos_fetch_POPULATED) +FetchContent_MakeAvailable(kokkos) +set_property(TARGET kokkoscore PROPERTY POSITION_INDEPENDENT_CODE ON) # Eigen FetchContent_Declare( - eigen_fetch + eigen GIT_REPOSITORY https://gitlab.com/libeigen/eigen GIT_TAG 3.4.0 ) -FetchContent_GetProperties(eigen_fetch) -if(NOT eigen_fetch_POPULATED) - message(STATUS "Fetch Eigen for matrix operation") - FetchContent_Populate(eigen_fetch) - add_subdirectory(${eigen_fetch_SOURCE_DIR}) -endif(NOT eigen_fetch_POPULATED) +FetchContent_MakeAvailable(eigen) # nanobind if(SKBUILD) @@ -88,16 +78,11 @@ endif(SKBUILD) # Google test if(SCALUQ_USE_TEST) FetchContent_Declare( - googletest_fetch + googletest GIT_REPOSITORY https://github.com/google/googletest GIT_TAG release-1.12.1 ) - FetchContent_GetProperties(googletest_fetch) - if(NOT googletest_fetch_POPULATED) - message(STATUS "Fetch googletest for C++ testing") - FetchContent_Populate(googletest_fetch) - add_subdirectory(${googletest_fetch_SOURCE_DIR}) - endif() + FetchContent_MakeAvailable(googletest) else() message(STATUS "Skip downloding googletest") endif(SCALUQ_USE_TEST) @@ -140,8 +125,12 @@ if ((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") OR (${CMAKE_CXX_COMPILER_ID} STREQ endif() # Debug options - target_compile_options(scaluq PUBLIC $,-O0 -g -fsanitize=address$undefined,-O3>) - target_link_options(scaluq PUBLIC $<$:-fsanitize=address$undefined>) + if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + target_compile_options(scaluq PUBLIC $,-O0 -g,-O3>) + else() + target_compile_options(scaluq PUBLIC $,-O0 -g -fsanitize=address$undefined,-O3>) + target_link_options(scaluq PUBLIC $<$:-fsanitize=address$undefined>) + endif() endif() ### Add subdirectories ### diff --git a/pyproject.toml b/pyproject.toml index 939ee578..1dbdd207 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,8 @@ metadata.version.provider = "scikit_build_core.metadata.setuptools_scm" sdist.include = ["python/scaluq/_version.py"] [tool.scikit-build.cmake.define] +CMAKE_C_COMPILER = {env="CMAKE_C_COMPILER", default="gcc"} +CMAKE_CXX_COMPILER = {env="CMAKE_CXX_COMPILER", default="g++"} SCALUQ_USE_OMP = {env="SCALUQ_USE_OMP", default="Yes"} SCALUQ_USE_CUDA = {env="SCALUQ_USE_CUDA", default="No"} SCALUQ_CUDA_ARCH = {env="SCALUQ_CUDA_ARCH"} diff --git a/scaluq/util/utility.hpp b/scaluq/util/utility.hpp index aab4f5d1..e4e267b6 100644 --- a/scaluq/util/utility.hpp +++ b/scaluq/util/utility.hpp @@ -144,8 +144,8 @@ inline std::vector> convert_2d_device_view_to_host_vector( const Kokkos::View& view_d) { auto view_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), view_d); std::vector> result(view_d.extent(0), std::vector(view_d.extent(1), 0)); - for (size_t i = 0; i < view_d.extent(0); ++i) { - for (size_t j = 0; j < view_d.extent(1); ++j) { + for (std::size_t i = 0; i < view_d.extent(0); ++i) { + for (std::size_t j = 0; j < view_d.extent(1); ++j) { result[i][j] = view_h(i, j); } } diff --git a/script/build_gcc.sh b/script/build_gcc.sh index 75bcf6ca..a4e37088 100755 --- a/script/build_gcc.sh +++ b/script/build_gcc.sh @@ -2,7 +2,8 @@ set -eux -GXX_COMMAND=${CXX_COMPILER:-"g++"} +CMAKE_C_COMPILER=${CMAKE_C_COMPILER:-"gcc"} +CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER:-"g++"} SCALUQ_USE_OMP=${SCALUQ_USE_OMP:-"ON"} SCALUQ_USE_CUDA=${SCALUQ_USE_CUDA:-"OFF"} @@ -11,7 +12,8 @@ SCALUQ_USE_EXE=${SCALUQ_USE_EXE:-"ON"} CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-"Release"} -CMAKE_OPS="-D CMAKE_CXX_COMPILER=${GXX_COMMAND} \ +CMAKE_OPS="-D CMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -D CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} \ -D CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \ -D SCALUQ_USE_OMP=${SCALUQ_USE_OMP} \ -D SCALUQ_USE_CUDA=${SCALUQ_USE_CUDA} \ @@ -21,6 +23,12 @@ if [ -n "${SCALUQ_CUDA_ARCH:-""}" ]; then CMAKE_OPS="${CMAKE_OPS} -D SCALUQ_CUDA_ARCH=${SCALUQ_CUDA_ARCH}" fi +if [ "$(uname)" == 'Darwin' ]; then + NPROC=$(sysctl -n hw.logicalcpu) +else + NPROC=$(nproc) +fi + mkdir -p ./build cmake -B build -G Ninja ${CMAKE_OPS} -ninja -C build -j $(nproc) +ninja -C build -j ${NPROC} diff --git a/tests/state/state_vector_test.cpp b/tests/state/state_vector_test.cpp index 054fa60f..37c22acd 100644 --- a/tests/state/state_vector_test.cpp +++ b/tests/state/state_vector_test.cpp @@ -217,7 +217,7 @@ TEST(StateVectorTest, SamplingSuperpositionState) { state.add_state_vector_with_coef(1 << i, tmp_state); } state.normalize(); - std::vector res = state.sampling(nshot); + std::vector res = state.sampling(nshot); std::array cnt = {}; for (std::uint64_t i = 0; i < nshot; ++i) {