cufft_utils.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <raft/core/error.hpp>
9 
10 #include <cufft.h>
11 
12 // TODO move to raft https://github.com/rapidsai/raft/issues/91
13 namespace raft {
14 
18 struct cufft_error : public raft::exception {
19  explicit cufft_error(char const* const message) : raft::exception(message) {}
20  explicit cufft_error(std::string const& message) : raft::exception(message) {}
21 };
22 
23 const char* getCufftErrStr(cufftResult status)
24 {
25  // https://docs.nvidia.com/cuda/cufft/index.html#cufftresult
26  switch (status) {
27  case CUFFT_SUCCESS: return "The cuFFT operation was successful.";
28  case CUFFT_INVALID_PLAN: return "cuFFT was passed an invalid plan handle.";
29  case CUFFT_ALLOC_FAILED: return "cuFFT failed to allocate GPU or CPU memory.";
30  case CUFFT_INVALID_VALUE: return "User specified an invalid pointer or parameter.";
31  case CUFFT_INTERNAL_ERROR: return "Driver or internal cuFFT library error.";
32  case CUFFT_EXEC_FAILED: return "Failed to execute an FFT on the GPU.";
33  case CUFFT_SETUP_FAILED: return "The cuFFT library failed to initialize.";
34  case CUFFT_INVALID_SIZE: return "User specified an invalid transform size.";
35 #if defined(CUDART_VERSION) && CUDART_VERSION < 13000
36  case CUFFT_INCOMPLETE_PARAMETER_LIST: return "Missing parameters in call.";
37 #endif
38  case CUFFT_INVALID_DEVICE:
39  return "Execution of a plan was on different GPU than plan creation.";
40 #if defined(CUDART_VERSION) && CUDART_VERSION < 13000
41  case CUFFT_PARSE_ERROR: return "Internal plan database error.";
42 #endif
43  case CUFFT_NO_WORKSPACE: return "No workspace has been provided prior to plan execution.";
44  case CUFFT_NOT_IMPLEMENTED:
45  return "Function does not implement functionality for parameters given.";
46  case CUFFT_NOT_SUPPORTED: return "Operation is not supported for parameters given.";
47  default: return "Unknown error.";
48  }
49 }
50 
57 #define CUFFT_TRY(call) \
58  do { \
59  const cufftResult status = call; \
60  if (status != CUFFT_SUCCESS) { \
61  std::string msg{}; \
62  SET_ERROR_MSG(msg, \
63  "cuFFT error encountered at: ", \
64  "call='%s', Reason=%s", \
65  #call, \
66  raft::getCufftErrStr(status)); \
67  throw raft::cufft_error(msg); \
68  } \
69  } while (0)
70 
71 class CuFFTHandle {
72  public:
73  CuFFTHandle(cudaStream_t stream)
74  {
75  CUFFT_TRY(cufftCreate(&handle));
76  CUFFT_TRY(cufftSetStream(handle, stream));
77  }
78  ~CuFFTHandle() { cufftDestroy(handle); }
79  operator cufftHandle() const { return handle; }
80 
81  private:
82  cufftHandle handle;
83 };
84 
85 } // namespace raft
Definition: cufft_utils.h:71
~CuFFTHandle()
Definition: cufft_utils.h:78
CuFFTHandle(cudaStream_t stream)
Definition: cufft_utils.h:73
#define CUFFT_TRY(call)
Error checking macro for cuFFT functions.
Definition: cufft_utils.h:57
Definition: dbscan.hpp:14
const char * getCufftErrStr(cufftResult status)
Definition: cufft_utils.h:23
Exception thrown when a cuFFT error is encountered.
Definition: cufft_utils.h:18
cufft_error(std::string const &message)
Definition: cufft_utils.h:20
cufft_error(char const *const message)
Definition: cufft_utils.h:19