Skip to content

Commit

Permalink
[SPARSE] Add support for rocsparse backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Rbiessy committed Jul 24, 2024
1 parent ef80783 commit 77c439b
Show file tree
Hide file tree
Showing 34 changed files with 2,536 additions and 89 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ option(ENABLE_PORTFFT_BACKEND "Enable the portFFT DFT backend for the DFT interf

# sparse
option(ENABLE_CUSPARSE_BACKEND "Enable the cuSPARSE backend for the SPARSE_BLAS interface" OFF)
option(ENABLE_ROCSPARSE_BACKEND "Enable the rocSPARSE backend for the SPARSE_BLAS interface" OFF)

set(ONEMKL_SYCL_IMPLEMENTATION "dpc++" CACHE STRING "Name of the SYCL compiler")
set(HIP_TARGETS "" CACHE STRING "Target HIP architectures")
Expand Down Expand Up @@ -106,7 +107,8 @@ if(ENABLE_MKLGPU_BACKEND
endif()
if(ENABLE_MKLCPU_BACKEND
OR ENABLE_MKLGPU_BACKEND
OR ENABLE_CUSPARSE_BACKEND)
OR ENABLE_CUSPARSE_BACKEND
OR ENABLE_ROCSPARSE_BACKEND)
list(APPEND DOMAINS_LIST "sparse_blas")
endif()

Expand Down Expand Up @@ -134,7 +136,7 @@ if(CMAKE_CXX_COMPILER OR NOT ONEMKL_SYCL_IMPLEMENTATION STREQUAL "dpc++")
endif()
else()
if(ENABLE_CUBLAS_BACKEND OR ENABLE_CURAND_BACKEND OR ENABLE_CUSOLVER_BACKEND OR ENABLE_CUFFT_BACKEND OR ENABLE_CUSPARSE_BACKEND
OR ENABLE_ROCBLAS_BACKEND OR ENABLE_ROCRAND_BACKEND OR ENABLE_ROCSOLVER_BACKEND OR ENABLE_ROCFFT_BACKEND)
OR ENABLE_ROCBLAS_BACKEND OR ENABLE_ROCRAND_BACKEND OR ENABLE_ROCSOLVER_BACKEND OR ENABLE_ROCFFT_BACKEND OR ENABLE_ROCSPARSE_BACKEND)
set(CMAKE_CXX_COMPILER "clang++")
elseif(ENABLE_MKLGPU_BACKEND)
if(UNIX)
Expand Down
6 changes: 3 additions & 3 deletions cmake/FindCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ if(is_dpcpp)
list(APPEND UNIX_INTERFACE_LINK_OPTIONS
-fsycl-targets=nvptx64-nvidia-cuda)
elseif(ENABLE_ROCBLAS_BACKEND OR ENABLE_ROCRAND_BACKEND
OR ENABLE_ROCSOLVER_BACKEND)
OR ENABLE_ROCSOLVER_BACKEND OR ENABLE_ROCSPARSE_BACKEND)
list(APPEND UNIX_INTERFACE_COMPILE_OPTIONS
-fsycl-targets=amdgcn-amd-amdhsa -fsycl-unnamed-lambda
-Xsycl-target-backend --offload-arch=${HIP_TARGETS})
Expand All @@ -54,7 +54,7 @@ if(is_dpcpp)
--offload-arch=${HIP_TARGETS})
endif()
if(ENABLE_CURAND_BACKEND OR ENABLE_CUSOLVER_BACKEND OR ENABLE_CUSPARSE_BACKEND OR ENABLE_ROCBLAS_BACKEND
OR ENABLE_ROCRAND_BACKEND OR ENABLE_ROCSOLVER_BACKEND)
OR ENABLE_ROCRAND_BACKEND OR ENABLE_ROCSOLVER_BACKEND OR ENABLE_ROCSPARSE_BACKEND)
set_target_properties(ONEMKL::SYCL::SYCL PROPERTIES
INTERFACE_COMPILE_OPTIONS "${UNIX_INTERFACE_COMPILE_OPTIONS}"
INTERFACE_LINK_OPTIONS "${UNIX_INTERFACE_LINK_OPTIONS}"
Expand All @@ -71,7 +71,7 @@ if(is_dpcpp)
INTERFACE_LINK_LIBRARIES ${SYCL_LIBRARY})
endif()

if(ENABLE_ROCBLAS_BACKEND OR ENABLE_ROCRAND_BACKEND OR ENABLE_ROCSOLVER_BACKEND)
if(ENABLE_ROCBLAS_BACKEND OR ENABLE_ROCRAND_BACKEND OR ENABLE_ROCSOLVER_BACKEND OR ENABLE_ROCSPARSE_BACKEND)
# Allow find_package(HIP) to find the correct path to libclang_rt.builtins.a
# HIP's CMake uses the command `${HIP_CXX_COMPILER} -print-libgcc-file-name --rtlib=compiler-rt` to find this path.
# This can print a non-existing file if the compiler used is icpx.
Expand Down
18 changes: 11 additions & 7 deletions docs/building_the_project_with_dpcpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ The most important supported build options are:
* - ENABLE_ROCRAND_BACKEND
- True, False
- False
* - ENABLE_ROCSPARSE_BACKEND
- True, False
- False
* - ENABLE_MKLCPU_THREAD_TBB
- True, False
- True
Expand Down Expand Up @@ -198,14 +201,14 @@ Building for ROCm
^^^^^^^^^^^^^^^^^

The ROCm backends can be enabled with ``ENABLE_ROCBLAS_BACKEND``,
``ENABLE_ROCFFT_BACKEND``, ``ENABLE_ROCSOLVER_BACKEND`` and
``ENABLE_ROCRAND_BACKEND``.
``ENABLE_ROCFFT_BACKEND``, ``ENABLE_ROCSOLVER_BACKEND``,
``ENABLE_ROCRAND_BACKEND``, and ``ENABLE_ROCSPARSE_BACKEND``.

For *RocBLAS*, *RocSOLVER* and *RocRAND*, the target device architecture must be
set. This can be set with using the ``HIP_TARGETS`` parameter. For example, to
enable a build for MI200 series GPUs, ``-DHIP_TARGETS=gfx90a`` should be set.
Currently, DPC++ can only build for a single HIP target at a time. This may
change in future versions.
For *RocBLAS*, *RocSOLVER*, *RocRAND*, and *RocSPARSE*, the target device
architecture must be set. This can be set with using the ``HIP_TARGETS``
parameter. For example, to enable a build for MI200 series GPUs,
``-DHIP_TARGETS=gfx90a`` should be set. Currently, DPC++ can only build for a
single HIP target at a time. This may change in future versions.

A few often-used architectures are listed below:

Expand Down Expand Up @@ -380,6 +383,7 @@ disabled:
-DENABLE_ROCFFT_BACKEND=True \
-DENABLE_ROCBLAS_BACKEND=True \
-DENABLE_ROCSOLVER_BACKEND=True \
-DENABLE_ROCSPARSE_BACKEND=True \
-DHIP_TARGETS=gfx90a \
-DBUILD_FUNCTIONAL_TESTS=False
Expand Down
35 changes: 35 additions & 0 deletions docs/domains/sparse_linear_algebra.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ Currently known limitations:
<https://docs.nvidia.com/cuda/cusparse/index.html#coordinate-coo>`_.


rocSPARSE backend
----------------

Currently known limitations:

- Using ``spmv`` with a ``type_view`` other than ``matrix_descr::general`` will
throw an ``oneapi::mkl::unimplemented`` exception.
- The COO format requires the indices to be sorted by row then by column. It is
not required to set the property
``oneapi::mkl::sparse::matrix_property::sorted`` to a sparse matrix handle.
See the `rocSPARSE documentation
<https://rocm.docs.amd.com/projects/rocSPARSE/en/latest/how-to/basics.html#coo-storage-format>`_.
- The same sparse matrix handle cannot be reused for multiple ``spmm`` or
``spmv`` operations. See `#332
<https://github.com/ROCm/rocSPARSE/issues/332>`_.


Operation algorithms mapping
----------------------------

Expand All @@ -73,41 +90,50 @@ spmm
- Default algorithm.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_ALG_DEFAULT``
| rocSPARSE: ``rocsparse_spmm_alg_default``
* - ``no_optimize_alg``
- Default algorithm but may skip some optimizations. Useful only if an
operation with the same configuration is run once.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_ALG_DEFAULT``
| rocSPARSE: ``rocsparse_spmm_alg_default``
* - ``coo_alg1``
- Should provide best performance for COO format, small ``nnz`` and
column-major layout.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_COO_ALG1``
| rocSPARSE: ``rocsparse_spmm_alg_coo_segmented``
* - ``coo_alg2``
- Should provide best performance for COO format and column-major layout.
Produces deterministic results.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_COO_ALG2``
| rocSPARSE: ``rocsparse_spmm_alg_coo_atomic``
* - ``coo_alg3``
- Should provide best performance for COO format and large ``nnz``.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_COO_ALG3``
| rocSPARSE: ``rocsparse_spmm_alg_coo_segmented_atomic``
* - ``coo_alg4``
- Should provide best performance for COO format and row-major layout.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_COO_ALG4``
| rocSPARSE: none
* - ``csr_alg1``
- Should provide best performance for CSR format and column-major layout.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_CSR_ALG1``
| rocSPARSE: ``rocsparse_spmm_alg_csr``
* - ``csr_alg2``
- Should provide best performance for CSR format and row-major layout.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_CSR_ALG2``
| rocSPARSE: ``rocsparse_spmm_alg_csr_row_split``
* - ``csr_alg3``
- Deterministic algorithm for CSR format.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_CSR_ALG3``
| rocSPARSE: ``rocsparse_spmm_alg_csr_merge``

spmv
Expand All @@ -124,31 +150,38 @@ spmv
- Default algorithm.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMV_ALG_DEFAULT``
| rocSPARSE: ``rocsparse_spmv_alg_default``
* - ``no_optimize_alg``
- Default algorithm but may skip some optimizations. Useful only if an
operation with the same configuration is run once.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_ALG_DEFAULT``
| rocSPARSE: ``rocsparse_spmv_alg_default``
* - ``coo_alg1``
- Default algorithm for COO format.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMV_COO_ALG1``
| rocSPARSE: ``rocsparse_spmv_alg_coo``
* - ``coo_alg2``
- Deterministic algorithm for COO format.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMV_COO_ALG2``
| rocSPARSE: ``rocsparse_spmv_alg_coo_atomic``
* - ``csr_alg1``
- Default algorithm for CSR format.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMV_CSR_ALG1``
| rocSPARSE: ``rocsparse_spmv_alg_csr_adaptive``
* - ``csr_alg2``
- Deterministic algorithm for CSR format.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMV_CSR_ALG2``
| rocSPARSE: ``rocsparse_spmv_alg_csr_stream``
* - ``csr_alg3``
- LRB variant of the algorithm for CSR format.
- | MKL: none
| cuSPARSE: none
| rocSPARSE: ``rocsparse_spmv_alg_csr_lrb``

spsv
Expand All @@ -165,8 +198,10 @@ spsv
- Default algorithm.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_ALG_DEFAULT``
| rocSPARSE: ``rocsparse_spsv_alg_default``
* - ``no_optimize_alg``
- Default algorithm but may skip some optimizations. Useful only if an
operation with the same configuration is run once.
- | MKL: none
| cuSPARSE: ``CUSPARSE_SPMM_ALG_DEFAULT``
| rocSPARSE: ``rocsparse_spsv_alg_default``
3 changes: 3 additions & 0 deletions examples/sparse_blas/run_time_dispatching/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ endif()
if(ENABLE_CUSPARSE_BACKEND)
list(APPEND DEVICE_FILTERS "cuda:gpu")
endif()
if(ENABLE_ROCSPARSE_BACKEND)
list(APPEND DEVICE_FILTERS "hip:gpu")
endif()

message(STATUS "ONEAPI_DEVICE_SELECTOR will be set to the following value(s): [${DEVICE_FILTERS}] for run-time dispatching examples")

Expand Down
4 changes: 4 additions & 0 deletions include/oneapi/mkl/detail/backends.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ enum class backend {
rocfft,
portfft,
cusparse,
rocsparse,
unsupported
};

typedef std::map<backend, std::string> backendmap;

// clang-format off
static backendmap backend_map = { { backend::mklcpu, "mklcpu" },
{ backend::mklgpu, "mklgpu" },
{ backend::cublas, "cublas" },
Expand All @@ -60,7 +62,9 @@ static backendmap backend_map = { { backend::mklcpu, "mklcpu" },
{ backend::rocfft, "rocfft" },
{ backend::portfft, "portfft" },
{ backend::cusparse, "cusparse" },
{ backend::rocsparse, "rocsparse" },
{ backend::unsupported, "unsupported" } };
// clang-format on

} //namespace mkl
} //namespace oneapi
Expand Down
6 changes: 6 additions & 0 deletions include/oneapi/mkl/detail/backends_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ static std::map<domain, std::map<device, std::vector<const char*>>> libraries =
{
#ifdef ENABLE_CUSPARSE_BACKEND
LIB_NAME("sparse_blas_cusparse")
#endif
} },
{ device::amdgpu,
{
#ifdef ENABLE_ROCSPARSE_BACKEND
LIB_NAME("sparse_blas_rocsparse")
#endif
} } } },
};
Expand Down
3 changes: 3 additions & 0 deletions include/oneapi/mkl/sparse_blas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#ifdef ENABLE_CUSPARSE_BACKEND
#include "sparse_blas/detail/cusparse/sparse_blas_ct.hpp"
#endif
#ifdef ENABLE_ROCSPARSE_BACKEND
#include "sparse_blas/detail/rocsparse/sparse_blas_ct.hpp"
#endif

#include "sparse_blas/detail/sparse_blas_rt.hpp"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/***************************************************************************
* Copyright (C) Codeplay Software Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* For your convenience, a copy of the License has been included in this
* repository.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/

#ifndef _ONEMKL_SPARSE_BLAS_DETAIL_ROCSPARSE_ONEMKL_SPARSE_BLAS_ROCSPARSE_HPP_
#define _ONEMKL_SPARSE_BLAS_DETAIL_ROCSPARSE_ONEMKL_SPARSE_BLAS_ROCSPARSE_HPP_

#include "oneapi/mkl/detail/export.hpp"
#include "oneapi/mkl/sparse_blas/detail/helper_types.hpp"
#include "oneapi/mkl/sparse_blas/types.hpp"

namespace oneapi::mkl::sparse::rocsparse {

namespace detail = oneapi::mkl::sparse::detail;

#include "oneapi/mkl/sparse_blas/detail/onemkl_sparse_blas_backends.hxx"

} // namespace oneapi::mkl::sparse::rocsparse

#endif // _ONEMKL_SPARSE_BLAS_DETAIL_ROCSPARSE_ONEMKL_SPARSE_BLAS_ROCSPARSE_HPP_
40 changes: 40 additions & 0 deletions include/oneapi/mkl/sparse_blas/detail/rocsparse/sparse_blas_ct.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/***************************************************************************
* Copyright (C) Codeplay Software Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* For your convenience, a copy of the License has been included in this
* repository.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/

#ifndef _ONEMKL_SPARSE_BLAS_DETAIL_ROCSPARSE_SPARSE_BLAS_CT_HPP_
#define _ONEMKL_SPARSE_BLAS_DETAIL_ROCSPARSE_SPARSE_BLAS_CT_HPP_

#include "oneapi/mkl/detail/backends.hpp"
#include "oneapi/mkl/detail/backend_selector.hpp"

#include "onemkl_sparse_blas_rocsparse.hpp"

namespace oneapi {
namespace mkl {
namespace sparse {

#define BACKEND rocsparse
#include "oneapi/mkl/sparse_blas/detail/sparse_blas_ct.hxx"
#undef BACKEND

} //namespace sparse
} //namespace mkl
} //namespace oneapi

#endif // _ONEMKL_SPARSE_BLAS_DETAIL_ROCSPARSE_SPARSE_BLAS_CT_HPP_
1 change: 1 addition & 0 deletions src/config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#cmakedefine ENABLE_ROCFFT_BACKEND
#cmakedefine ENABLE_ROCRAND_BACKEND
#cmakedefine ENABLE_ROCSOLVER_BACKEND
#cmakedefine ENABLE_ROCSPARSE_BACKEND
#cmakedefine BUILD_SHARED_LIBS
#cmakedefine REF_BLAS_LIBNAME "@REF_BLAS_LIBNAME@"
#cmakedefine REF_CBLAS_LIBNAME "@REF_CBLAS_LIBNAME@"
Expand Down
4 changes: 4 additions & 0 deletions src/sparse_blas/backends/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ endif()
if(ENABLE_CUSPARSE_BACKEND)
add_subdirectory(cusparse)
endif()

if(ENABLE_ROCSPARSE_BACKEND)
add_subdirectory(rocsparse)
endif()
Loading

0 comments on commit 77c439b

Please sign in to comment.