-
Notifications
You must be signed in to change notification settings - Fork 55
Add CUDA backend for FFT and IFFT #548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #pragma once | ||
|
|
||
| /* | ||
| * Copyright (c) 2025, Alex Chiang <[email protected]> | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * - Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * - Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * - Neither the name of the copyright holder nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| #include <cuda.h> | ||
| #include <cuda_runtime.h> | ||
| #include <cufft.h> | ||
| #include <stdio.h> | ||
|
|
||
| #define CUDA_SAFE_CALL(err) __cudaSafeCall(err, __FILE__, __LINE__) | ||
| #define CUFFT_SAFE_CALL(err) __cufftSafeCall(err, __FILE__, __LINE__) | ||
| #define CUDA_GET_LAST_ERROR() __cudaCheckError(__FILE__, __LINE__) | ||
|
|
||
| void inline __cudaSafeCall(cudaError_t err, const char * file, const int line) | ||
| { | ||
| if (err != cudaSuccess) | ||
| { | ||
| printf("CUDA Error %d: %s.\n%s(%d)\n", (int)err, cudaGetErrorString(err), file, line); | ||
| } | ||
| } | ||
|
|
||
| void inline __cudaCheckError(const char * file, const int line) | ||
| { | ||
| cudaError_t err = cudaDeviceSynchronize(); | ||
| if (err != cudaSuccess) | ||
| { | ||
| printf("CUDA Error %d: %s.\n%s(%d)\n", (int)err, cudaGetErrorString(err), file, line); | ||
| } | ||
|
|
||
| err = cudaGetLastError(); | ||
| if (err != cudaSuccess) | ||
| { | ||
| printf("CUDA Error %d: %s.\n%s(%d)\n", (int)err, cudaGetErrorString(err), file, line); | ||
| } | ||
| } | ||
|
|
||
| const inline char * __cufftResultToString(cufftResult err) | ||
| { | ||
| switch (err) | ||
| { | ||
| case CUFFT_SUCCESS: return "CUFFT_SUCCESS."; | ||
| case CUFFT_INVALID_PLAN: return "CUFFT_INVALID_PLAN."; | ||
| case CUFFT_ALLOC_FAILED: return "CUFFT_ALLOC_FAILED."; | ||
| case CUFFT_INVALID_TYPE: return "CUFFT_INVALID_TYPE."; | ||
| case CUFFT_INVALID_VALUE: return "CUFFT_INVALID_VALUE."; | ||
| case CUFFT_INTERNAL_ERROR: return "CUFFT_INTERNAL_ERROR."; | ||
| case CUFFT_EXEC_FAILED: return "CUFFT_EXEC_FAILED."; | ||
| case CUFFT_SETUP_FAILED: return "CUFFT_SETUP_FAILED."; | ||
| case CUFFT_INVALID_SIZE: return "CUFFT_INVALID_SIZE."; | ||
| case CUFFT_UNALIGNED_DATA: return "CUFFT_UNALIGNED_DATA."; | ||
| default: return "CUFFT Unknown error code."; | ||
| } | ||
| } | ||
|
|
||
| void inline __cufftSafeCall(cufftResult err, const char * file, const int line) | ||
| { | ||
| if (CUFFT_SUCCESS != err) | ||
| { | ||
| printf("CUFFT error %d: %s\n%s(%d)\n", (int)err, __cufftResultToString(err), file, line); | ||
| } | ||
| } | ||
|
|
||
| // vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| #pragma once | ||
|
|
||
| /* | ||
| * Copyright (c) 2025, Alex Chiang <[email protected]> | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * - Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * - Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * - Neither the name of the copyright holder nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| #include <modmesh/modmesh.hpp> | ||
| #include <modmesh/buffer/buffer.hpp> | ||
| #include <modmesh/device/cuda/cuda_error_handle.hpp> | ||
|
|
||
| #define FFT_CUDA_IMPL(CUFFT_DATA_TYPE, CUFFT_EXEC_TYPE) \ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jysh1214 could you please evaluate if this can be made as a template function? If it can be done within a couple of hours, please update this macro to use a function template or merged it into the function template If you cannot make it quickly or do not know how to make it, please leave a TODO comment to state that a future contributor should evaluate how to turn the macro into a function template. |
||
| { \ | ||
| cufftHandle plan; \ | ||
| CUFFT_DATA_TYPE * host_in = nullptr; \ | ||
| CUFFT_DATA_TYPE * host_out = nullptr; \ | ||
| CUFFT_DATA_TYPE * device_in = nullptr; \ | ||
| CUFFT_DATA_TYPE * device_out = nullptr; \ | ||
| host_in = (CUFFT_DATA_TYPE*)malloc(sizeof(CUFFT_DATA_TYPE) * N); \ | ||
| host_out = (CUFFT_DATA_TYPE*)malloc(sizeof(CUFFT_DATA_TYPE) * N); \ | ||
| for (size_t i = 0; i < N; ++i) \ | ||
| { \ | ||
| host_in[i].x = in[i].real(); \ | ||
| host_in[i].y = in[i].imag(); \ | ||
| } \ | ||
| CUDA_SAFE_CALL(cudaMalloc((void**)&device_in, sizeof(CUFFT_DATA_TYPE) * N)); \ | ||
| CUDA_SAFE_CALL(cudaMalloc((void**)&device_out, sizeof(CUFFT_DATA_TYPE) * N)); \ | ||
| CUDA_SAFE_CALL(cudaMemcpy(device_in, host_in, sizeof(CUFFT_DATA_TYPE) * N, cudaMemcpyHostToDevice)); \ | ||
| CUFFT_SAFE_CALL(cufftPlan1d(&plan, N, CUFFT_##CUFFT_EXEC_TYPE, 1)); \ | ||
| CUFFT_SAFE_CALL(cufftExec##CUFFT_EXEC_TYPE(plan, device_in, device_out, CUFFT_FORWARD)); \ | ||
| CUDA_SAFE_CALL(cudaMemcpy(host_out, device_out, sizeof(CUFFT_DATA_TYPE) * N, cudaMemcpyDeviceToHost)); \ | ||
| for (size_t i = 0; i < N; ++i) \ | ||
| { \ | ||
| out[i] = T1<T2>{ host_out[i].x, host_out[i].y }; \ | ||
| } \ | ||
| CUFFT_SAFE_CALL(cufftDestroy(plan)); \ | ||
| CUDA_SAFE_CALL(cudaFree(device_in)); \ | ||
| CUDA_SAFE_CALL(cudaFree(device_out)); \ | ||
| free(host_in); \ | ||
| free(host_out); \ | ||
| } | ||
|
|
||
| namespace modmesh | ||
| { | ||
|
|
||
| template <template <typename> class T1, typename T2> | ||
| void fft_cuda(SimpleArray<T1<T2>> const & in, SimpleArray<T1<T2>> & out) | ||
| { | ||
| size_t N = in.size(); | ||
| if constexpr (std::is_same_v<T2, float>) | ||
| { | ||
| FFT_CUDA_IMPL(cufftComplex, C2C) | ||
| } | ||
| else if constexpr (std::is_same_v<T2, double>) | ||
| { | ||
| FFT_CUDA_IMPL(cufftDoubleComplex, Z2Z) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
|
|
||
| } /* namespace modmesh */ | ||
|
|
||
| // vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,10 @@ | |
| #include <modmesh/math/math.hpp> | ||
| #include <modmesh/buffer/buffer.hpp> | ||
|
|
||
| #if defined(BUILD_CUDA) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to avoid any |
||
| #include <modmesh/transform/fourier.cuh> | ||
| #endif | ||
|
|
||
| namespace modmesh | ||
| { | ||
|
|
||
|
|
@@ -63,22 +67,37 @@ class FourierTransform | |
| FourierTransform & operator=(FourierTransform && other) = delete; | ||
|
|
||
| template <template <typename> class T1, typename T2> | ||
| static void fft(SimpleArray<T1<T2>> const & in, SimpleArray<T1<T2>> & out) | ||
| static void fft(SimpleArray<T1<T2>> const & in, SimpleArray<T1<T2>> & out, std::string const && backend) | ||
| { | ||
| const size_t N = in.size(); | ||
|
|
||
| if ((N & (N - 1)) == 0) | ||
| if (backend == "cpu") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String comparison can only be done during runtime, but the comparison should be available during compile-time. |
||
| { | ||
| if ((N & (N - 1)) == 0) | ||
| { | ||
| detail::fft_radix_2<T1, T2>(in, out); | ||
| } | ||
| else | ||
| { | ||
| detail::fft_bluestein<T1, T2>(in, out); | ||
| } | ||
| } | ||
| else if (backend == "cuda") | ||
| { | ||
| detail::fft_radix_2<T1, T2>(in, out); | ||
| #if defined(BUILD_CUDA) | ||
| modmesh::fft_cuda<T1, T2>(in, out); | ||
| #else | ||
| throw std::runtime_error("CUDA is not available."); | ||
| #endif | ||
| } | ||
| else | ||
| { | ||
| detail::fft_bluestein<T1, T2>(in, out); | ||
| throw std::runtime_error("unsupported backend."); | ||
| } | ||
| } | ||
|
|
||
| template <template <typename> class T1, typename T2> | ||
| static void ifft(SimpleArray<T1<T2>> const & in, SimpleArray<T1<T2>> & out) | ||
| static void ifft(SimpleArray<T1<T2>> const & in, SimpleArray<T1<T2>> & out, std::string const && backend) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use an |
||
| { | ||
| size_t N = in.size(); | ||
| SimpleArray<T1<T2>> in_conj{modmesh::small_vector<size_t>{N}, T1<T2>{0.0, 0.0}}; | ||
|
|
@@ -88,7 +107,7 @@ class FourierTransform | |
| in_conj[i] = in[i].conj(); | ||
| } | ||
|
|
||
| fft<T1, T2>(in_conj, out); | ||
| fft<T1, T2>(in_conj, out, std::move(backend)); | ||
|
|
||
| for (size_t i = 0; i < N; ++i) | ||
| { | ||
|
|
@@ -152,7 +171,7 @@ void fft_bluestein(SimpleArray<T1<T2>> const & in, SimpleArray<T1<T2>> & out) | |
| A[i] *= B[i]; | ||
| } | ||
|
|
||
| FourierTransform::ifft<T1, T2>(A, a); | ||
| FourierTransform::ifft<T1, T2>(A, a, "cpu"); | ||
|
|
||
| for (size_t i = 0; i < N; ++i) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's make it off by default. Perhaps the variable
BUILD_CUDAcan be moved to be adjacent toBUILD_METALlike: