Skip to content

Commit

Permalink
Merge branch 'bugfix/magma-cudatoolkit' into bugfix/find-magma
Browse files Browse the repository at this point in the history
  • Loading branch information
balos1 committed Oct 20, 2023
2 parents e800b15 + 2d33152 commit 0f19a3e
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 46 deletions.
24 changes: 20 additions & 4 deletions cmake/tpl/FindMAGMA.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,34 @@ if(MAGMA_LIBRARY AND MAGMA_INCLUDE_DIR)
OR lib STREQUAL "-L\${libdir}" OR lib STREQUAL "") )

# Remove -l only from the beginning of the string
string(REPLACE "^-l" "" lib ${lib})
list(APPEND _interface_libraires ${lib})
string(REPLACE "-l" "" lib "${lib}")

# Check if we need to find roc::hipblas or roc::hipsparse
if(SUNDIALS_MAGMA_BACKENDS MATCHES "HIP")
if((lib STREQUAL "roc::hipblas") AND (NOT TARGET roc::hipblas))
if(NOT TARGET roc::hipblas)
find_package(hipblas REQUIRED)
endif()
if((lib STREQUAL "roc::hipsparse") AND (NOT TARGET roc::hipsparse))
if(NOT TARGET roc::hipsparse)
find_package(hipsparse REQUIRED)
endif()
# MAGMA does not reliably include these in the pkgconfig file
set(lib "roc::hipblas;roc::hipsparse")
endif()

# Check if we need to find cusparse or cublas
if(SUNDIALS_MAGMA_BACKENDS MATCHES "CUDA")
if (NOT TARGET CUDA::cudart)
find_package(CUDAToolkit REQUIRED)
endif()
# Ignore cublas, cusolver, cusparse because the library path in the magma pkgconfig is not reliable.
# Sepcifically, the path is wrong on systems like Perlmutter where the NVIDIA HPC SDK is used.
# We just link to these in the relevant sundials targets using the CMake CUDA targets instead.
if((lib STREQUAL "cublas") OR (lib STREQUAL "cusolver") OR (lib STREQUAL "cusparse"))
set(lib "CUDA::${lib}")
endif()
endif()

list(APPEND _interface_libraires ${lib})

endif()
endforeach()
Expand Down
2 changes: 2 additions & 0 deletions scripts/shared
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ $tar $tarfile $distrobase/src/sundials/sundials_band.c
$tar $tarfile $distrobase/src/sundials/sundials_context_impl.h
$tar $tarfile $distrobase/src/sundials/sundials_context.c
$tar $tarfile $distrobase/src/sundials/sundials_cuda.h
$tar $tarfile $distrobase/src/sundials/sundials_cusolver.h
$tar $tarfile $distrobase/src/sundials/sundials_cusparse.h
$tar $tarfile $distrobase/src/sundials/sundials_cuda_kernels.cuh
$tar $tarfile $distrobase/src/sundials/sundials_debug.h
$tar $tarfile $distrobase/src/sundials/sundials_dense.c
Expand Down
39 changes: 1 addition & 38 deletions src/sundials/sundials_cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include <stdio.h>

#include <cuda_runtime.h>
#include <cusolverSp.h>
#include <cusparse.h>

#include <sundials/sundials_types.h>

Expand All @@ -38,8 +36,6 @@ extern "C" {
* ---------------------------------------------------------------------------*/

#define SUNDIALS_CUDA_VERIFY(cuerr) SUNDIALS_CUDA_Assert(cuerr, __FILE__, __LINE__)
#define SUNDIALS_CUSPARSE_VERIFY(cuerr) SUNDIALS_CUSPARSE_Assert(cuerr, __FILE__, __LINE__)
#define SUNDIALS_CUSOLVER_VERIFY(cuerr) SUNDIALS_CUSOLVER_Assert(cuerr, __FILE__, __LINE__)

#define SUNDIALS_KERNEL_NAME(...) __VA_ARGS__
#ifndef SUNDIALS_DEBUG_CUDA_LASTERROR
Expand Down Expand Up @@ -75,42 +71,9 @@ inline booleantype SUNDIALS_CUDA_Assert(cudaError_t cuerr, const char *file, int
return SUNTRUE; /* Assert OK */
}

inline booleantype SUNDIALS_CUSPARSE_Assert(cusparseStatus_t status, const char *file, int line)
{
if (status != CUSPARSE_STATUS_SUCCESS)
{
#ifdef SUNDIALS_DEBUG
fprintf(stderr,
"ERROR in cuSPARSE runtime operation: cusparseStatus_t = %d %s:%d\n",
status, file, line);
#ifdef SUNDIALS_DEBUG_ASSERT
assert(false);
#endif
#endif
return SUNFALSE; /* Assert failed */
}
return SUNTRUE; /* Assert OK */
}

inline booleantype SUNDIALS_CUSOLVER_Assert(cusolverStatus_t status, const char *file, int line)
{
if (status != CUSOLVER_STATUS_SUCCESS)
{
#ifdef SUNDIALS_DEBUG
fprintf(stderr,
"ERROR in cuSOLVER runtime operation: cusolverStatus_t = %d %s:%d\n",
status, file, line);
#ifdef SUNDIALS_DEBUG_ASSERT
assert(false);
#endif
#endif
return SUNFALSE; /* Assert failed */
}
return SUNTRUE; /* Assert OK */
}

#ifdef __cplusplus /* wrapper to enable C++ usage */
}
#endif

#endif /* _SUNDIALS_CUDA_H */
#endif /* _SUNDIALS_CUDA_H */
68 changes: 68 additions & 0 deletions src/sundials/sundials_cusolver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* -----------------------------------------------------------------
* Programmer(s): Cody J. Balos @ LLNL
* -----------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2023, Lawrence Livermore National Security
* and Southern Methodist University.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
* -----------------------------------------------------------------
* This header files defines internal utility functions and macros
* for working with CUDA.
* -----------------------------------------------------------------
*/

#include <assert.h>
#include <stdio.h>

#include <cuda_runtime.h>
#include <cusolverDn.h>
#include <cusolverSp.h>

#include <sundials/sundials_types.h>

#ifndef _SUNDIALS_CUSOLVER_H
#define _SUNDIALS_CUSOLVER_H

#ifdef __cplusplus /* wrapper to enable C++ usage */
extern "C" {
#endif

/* ---------------------------------------------------------------------------
* Utility macros
* ---------------------------------------------------------------------------*/

#define SUNDIALS_CUSOLVER_VERIFY(cuerr) SUNDIALS_CUSOLVER_Assert(cuerr, __FILE__, __LINE__)


/* ---------------------------------------------------------------------------
* Utility functions
* ---------------------------------------------------------------------------*/

inline booleantype SUNDIALS_CUSOLVER_Assert(cusolverStatus_t status, const char *file, int line)
{
if (status != CUSOLVER_STATUS_SUCCESS)
{
#ifdef SUNDIALS_DEBUG
fprintf(stderr,
"ERROR in cuSOLVER runtime operation: cusolverStatus_t = %d %s:%d\n",
status, file, line);
#ifdef SUNDIALS_DEBUG_ASSERT
assert(false);
#endif
#endif
return SUNFALSE; /* Assert failed */
}
return SUNTRUE; /* Assert OK */
}

#ifdef __cplusplus /* wrapper to enable C++ usage */
}
#endif

#endif /* _SUNDIALS_CUSOLVER_H */
66 changes: 66 additions & 0 deletions src/sundials/sundials_cusparse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* -----------------------------------------------------------------
* Programmer(s): Cody J. Balos @ LLNL
* -----------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2023, Lawrence Livermore National Security
* and Southern Methodist University.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
* -----------------------------------------------------------------
* This header files defines internal utility functions and macros
* for working with CUDA.
* -----------------------------------------------------------------
*/

#include <assert.h>
#include <stdio.h>

#include <cuda_runtime.h>
#include <cusparse.h>

#include <sundials/sundials_types.h>

#ifndef _SUNDIALS_CUSPARSE_H
#define _SUNDIALS_CUSPARSE_H

#ifdef __cplusplus /* wrapper to enable C++ usage */
extern "C" {
#endif

/* ---------------------------------------------------------------------------
* Utility macros
* ---------------------------------------------------------------------------*/

#define SUNDIALS_CUSPARSE_VERIFY(cuerr) SUNDIALS_CUSPARSE_Assert(cuerr, __FILE__, __LINE__)

/* ---------------------------------------------------------------------------
* Utility functions
* ---------------------------------------------------------------------------*/

inline booleantype SUNDIALS_CUSPARSE_Assert(cusparseStatus_t status, const char *file, int line)
{
if (status != CUSPARSE_STATUS_SUCCESS)
{
#ifdef SUNDIALS_DEBUG
fprintf(stderr,
"ERROR in cuSPARSE runtime operation: cusparseStatus_t = %d %s:%d\n",
status, file, line);
#ifdef SUNDIALS_DEBUG_ASSERT
assert(false);
#endif
#endif
return SUNFALSE; /* Assert failed */
}
return SUNTRUE; /* Assert OK */
}

#ifdef __cplusplus /* wrapper to enable C++ usage */
}
#endif

#endif /* _SUNDIALS_CUSPARSE_H */
1 change: 1 addition & 0 deletions src/sunlinsol/cusolversp/sunlinsol_cusolversp_batchqr.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <sunlinsol/sunlinsol_cusolversp_batchqr.h>

#include "sundials_cuda.h"
#include "sundials_cusolver.h"
#include "sundials_debug.h"

#define ZERO RCONST(0.0)
Expand Down
2 changes: 1 addition & 1 deletion src/sunlinsol/magmadense/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if(SUNDIALS_MAGMA_BACKENDS MATCHES "CUDA")
set(_libs_needed sundials_sunmatrixmagmadense sundials_nveccuda)
elseif(SUNDIALS_MAGMA_BACKENDS MATCHES "HIP")
set_source_files_properties(sunlinsol_magmadense.cpp PROPERTIES LANGUAGE CXX)
set(_libs_needed sundials_sunmatrixmagmadense sundials_nvechip)
set(_libs_needed sundials_sunmatrixmagmadense sundials_nvechip hip::device)
endif()

# Add the sunlinsol_magmadense library
Expand Down
3 changes: 1 addition & 2 deletions src/sunmatrix/cusparse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ sundials_add_library(sundials_sunmatrixcusparse
sundials_generic_obj
sundials_sunmemcuda_obj
LINK_LIBRARIES
PUBLIC CUDA::cusparse
PRIVATE CUDA::cusolver
PUBLIC CUDA::cusparse CUDA::cusolver
OUTPUT_NAME
sundials_sunmatrixcusparse
VERSION
Expand Down
1 change: 1 addition & 0 deletions src/sunmatrix/cusparse/sunmatrix_cusparse.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sunmatrix/sunmatrix_cusparse.h>

#include "sundials_cuda.h"
#include "sundials_cusparse.h"
#include "sundials_debug.h"
#include "cusparse_kernels.cuh"

Expand Down
2 changes: 1 addition & 1 deletion src/sunmatrix/magmadense/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ install(CODE "MESSAGE(\"\nInstall SUNMATRIX_MAGMADENSE with ${SUNDIALS_MAGMA_BAC

if(SUNDIALS_MAGMA_BACKENDS MATCHES "CUDA")
set_source_files_properties(sunmatrix_magmadense.cpp PROPERTIES LANGUAGE CUDA)
set(_libs_needed sundials_nveccuda ${CUDA_CUBLAS_LIBRARIES})
set(_libs_needed sundials_nveccuda)
elseif(SUNDIALS_MAGMA_BACKENDS MATCHES "HIP")
set_source_files_properties(sunmatrix_magmadense.cpp PROPERTIES LANGUAGE CXX)
set(_libs_needed sundials_nvechip hip::device)
Expand Down

0 comments on commit 0f19a3e

Please sign in to comment.