| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
|
| 17 |
#ifndef KOKKOSLAPACK_CUSOLVER_HPP_ |
| 18 |
#define KOKKOSLAPACK_CUSOLVER_HPP_ |
| 19 |
|
| 20 |
#ifdef KOKKOSKERNELS_ENABLE_TPL_CUSOLVER |
| 21 |
#include <cusolverDn.h> |
| 22 |
|
| 23 |
namespace KokkosLapack { |
| 24 |
namespace Impl { |
| 25 |
|
| 26 |
// Declaration of the singleton for cusolver |
| 27 |
// this is the only header that needs to be |
| 28 |
// included when using cusolverDn. |
| 29 |
struct CudaLapackSingleton { |
| 30 |
cusolverDnHandle_t handle; |
| 31 |
|
| 32 |
CudaLapackSingleton(); |
| 33 |
|
| 34 |
static CudaLapackSingleton& singleton(); |
| 35 |
|
| 36 |
static bool is_initialized(); |
| 37 |
|
| 38 |
private: |
| 39 |
static std::unique_ptr<CudaLapackSingleton>& get_instance(); |
| 40 |
}; |
| 41 |
|
| 42 |
inline void cusolver_internal_error_throw(cusolverStatus_t cusolverStatus, const char* name, const char* file, |
| 43 |
const int line) { |
| 44 |
std::ostringstream out; |
| 45 |
out << name << " error( "; |
| 46 |
switch (cusolverStatus) { |
| 47 |
case CUSOLVER_STATUS_NOT_INITIALIZED: |
| 48 |
out << "CUSOLVER_STATUS_NOT_INITIALIZED): cusolver handle was not " |
| 49 |
"created correctly."; |
| 50 |
break; |
| 51 |
case CUSOLVER_STATUS_ALLOC_FAILED: |
| 52 |
out << "CUSOLVER_STATUS_ALLOC_FAILED): you might tried to allocate too " |
| 53 |
"much memory"; |
| 54 |
break; |
| 55 |
case CUSOLVER_STATUS_INVALID_VALUE: out << "CUSOLVER_STATUS_INVALID_VALUE)"; break; |
| 56 |
case CUSOLVER_STATUS_ARCH_MISMATCH: out << "CUSOLVER_STATUS_ARCH_MISMATCH)"; break; |
| 57 |
case CUSOLVER_STATUS_EXECUTION_FAILED: out << "CUSOLVER_STATUS_EXECUTION_FAILED)"; break; |
| 58 |
case CUSOLVER_STATUS_INTERNAL_ERROR: out << "CUSOLVER_STATUS_INTERNAL_ERROR)"; break; |
| 59 |
case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED: out << "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED)"; break; |
| 60 |
default: out << "unrecognized error code): this is bad!"; break; |
| 61 |
} |
| 62 |
if (file) { |
| 63 |
out << " " << file << ":" << line; |
| 64 |
} |
| 65 |
throw std::runtime_error(out.str()); |
| 66 |
} |
| 67 |
|
| 68 |
inline void cusolver_internal_safe_call(cusolverStatus_t cusolverStatus, const char* name, const char* file = nullptr, |
| 69 |
const int line = 0) { |
| 70 |
if (CUSOLVER_STATUS_SUCCESS != cusolverStatus) { |
| 71 |
cusolver_internal_error_throw(cusolverStatus, name, file, line); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// The macro below defines is the public interface for the safe cusolver calls. |
| 76 |
// The functions themselves are protected by impl namespace. |
| 77 |
#define KOKKOSLAPACK_IMPL_CUSOLVER_SAFE_CALL(call) \ |
| 78 |
KokkosLapack::Impl::cusolver_internal_safe_call(call, #call, __FILE__, __LINE__) |
| 79 |
|
| 80 |
} // namespace Impl |
| 81 |
} // namespace KokkosLapack |
| 82 |
#endif // KOKKOSKERNELS_ENABLE_TPL_CUSOLVER |
| 83 |
#endif // KOKKOSLAPACK_CUSOLVER_HPP_ |