|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2025, Alex Chiang <jyxemperor@gmail.com> |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * - Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * - Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * - Neither the name of the copyright holder nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software |
| 16 | + * without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +#include <cuda.h> |
| 32 | +#include <cuda_runtime.h> |
| 33 | +#include <cufft.h> |
| 34 | +#include <stdio.h> |
| 35 | + |
| 36 | +#define CUDA_SAFE_CALL(err) __cudaSafeCall(err, __FILE__, __LINE__) |
| 37 | +#define CUFFT_SAFE_CALL(err) __cufftSafeCall(err, __FILE__, __LINE__) |
| 38 | +#define CUDA_GET_LAST_ERROR() __cudaCheckError(__FILE__, __LINE__) |
| 39 | + |
| 40 | +void inline __cudaSafeCall(cudaError_t err, const char * file, const int line) |
| 41 | +{ |
| 42 | + if (err != cudaSuccess) |
| 43 | + { |
| 44 | + printf("CUDA Error %d: %s.\n%s(%d)\n", (int)err, cudaGetErrorString(err), file, line); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void inline __cudaCheckError(const char * file, const int line) |
| 49 | +{ |
| 50 | + cudaError_t err = cudaDeviceSynchronize(); |
| 51 | + if (err != cudaSuccess) |
| 52 | + { |
| 53 | + printf("CUDA Error %d: %s.\n%s(%d)\n", (int)err, cudaGetErrorString(err), file, line); |
| 54 | + } |
| 55 | + |
| 56 | + err = cudaGetLastError(); |
| 57 | + if (err != cudaSuccess) |
| 58 | + { |
| 59 | + printf("CUDA Error %d: %s.\n%s(%d)\n", (int)err, cudaGetErrorString(err), file, line); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +const inline char * __cufftResultToString(cufftResult err) |
| 64 | +{ |
| 65 | + switch (err) |
| 66 | + { |
| 67 | + case CUFFT_SUCCESS: return "CUFFT_SUCCESS."; |
| 68 | + case CUFFT_INVALID_PLAN: return "CUFFT_INVALID_PLAN."; |
| 69 | + case CUFFT_ALLOC_FAILED: return "CUFFT_ALLOC_FAILED."; |
| 70 | + case CUFFT_INVALID_TYPE: return "CUFFT_INVALID_TYPE."; |
| 71 | + case CUFFT_INVALID_VALUE: return "CUFFT_INVALID_VALUE."; |
| 72 | + case CUFFT_INTERNAL_ERROR: return "CUFFT_INTERNAL_ERROR."; |
| 73 | + case CUFFT_EXEC_FAILED: return "CUFFT_EXEC_FAILED."; |
| 74 | + case CUFFT_SETUP_FAILED: return "CUFFT_SETUP_FAILED."; |
| 75 | + case CUFFT_INVALID_SIZE: return "CUFFT_INVALID_SIZE."; |
| 76 | + case CUFFT_UNALIGNED_DATA: return "CUFFT_UNALIGNED_DATA."; |
| 77 | + default: return "CUFFT Unknown error code."; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void inline __cufftSafeCall(cufftResult err, const char * file, const int line) |
| 82 | +{ |
| 83 | + if (CUFFT_SUCCESS != err) |
| 84 | + { |
| 85 | + printf("CUFFT error %d: %s\n%s(%d)\n", (int)err, __cufftResultToString(err), file, line); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: |
0 commit comments