From f8e381a9b83573ef50981b8e23ebdf486fe1579c Mon Sep 17 00:00:00 2001 From: Yuto Makino Date: Tue, 8 Aug 2023 17:15:04 +0900 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E8=A6=8F=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E3=82=92=E3=82=A2=E3=83=83=E3=83=97=E3=83=AD=E3=83=BC?= =?UTF-8?q?=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "CUDA\350\252\262\351\241\214/streamfft.cu" | 68 +++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "CUDA\350\252\262\351\241\214/streamfft.cu" diff --git "a/CUDA\350\252\262\351\241\214/streamfft.cu" "b/CUDA\350\252\262\351\241\214/streamfft.cu" new file mode 100644 index 0000000..1057c84 --- /dev/null +++ "b/CUDA\350\252\262\351\241\214/streamfft.cu" @@ -0,0 +1,68 @@ +#include +#include +#include +#define NX 512 +#define NY 512 + +// 複素数の構造体定義 +struct Complex { + float real; + float imag; +}; + +int main() { + const int size = NX * NY; + const int complexSize = size * sizeof(Complex); + + cufftHandle plan; + + // ホスト上の入力データと出力データ + Complex hostInput[NX][NY]; + Complex hostOutput[size]; + + // デバイス上の入力データと出力データ + Complex *deviceInput; + Complex *deviceOutput; + + for (int i = 0; i < NX; i++) { + for (int j = 0; j < NY; j++) { + hostInput[i][j].real = static_cast(sin(i + j)); + hostInput[i][j].imag = static_cast(cos(i + j)); + } + } + + // デバイスメモリを確保 + cudaMalloc((void**)&deviceInput, complexSize); + cudaMalloc((void**)&deviceOutput, complexSize); + + // ストリームの生成 + cudaStream_t stream; + cudaStreamCreate(&stream); + + // 入力データ転送 + cudaMemcpyAsync(deviceInput, hostInput, complexSize, cudaMemcpyHostToDevice, stream); + + // 2次元FFTのプランを作成 + cufftPlan2d(&plan, NX, NY, CUFFT_C2C); + + // FFTを実行 + cufftExecC2C(plan, (cufftComplex*)deviceInput, (cufftComplex*)deviceOutput, CUFFT_FORWARD); + + // 出力データ転送 + cudaMemcpyAsync(hostOutput, deviceOutput, complexSize, cudaMemcpyDeviceToHost, stream); + + // 結果の出力 + for (int i = 0; i < NX; i++) { + for (int j = 0; j < NY; j++) { + int idx = i * NY + j; + printf("Input: %f + %fI, Output: %f + %fI\n", hostInput[i][j].real, hostInput[i][j].imag, hostOutput[idx].real, hostOutput[idx].imag); + } + } + + cudaStreamDestroy(stream); + cufftDestroy(plan); + cudaFree(deviceInput); + cudaFree(deviceOutput); + + return 0; +} \ No newline at end of file -- GitLab