我想做FFT加內核計算。 FFT:managedCUDA庫 內核計算:自己的內核1D FFT加內核計算與managedCUDA
C#代碼
public void cuFFTreconstruct() {
CudaContext ctx = new CudaContext(0);
CudaKernel cuKernel = ctx.LoadKernel("kernel_Array.ptx", "cu_ArrayInversion");
float[] fData = new float[Resolution * Resolution * 2];
float[] result = new float[Resolution * Resolution * 2];
CudaDeviceVariable<float> devData = new CudaDeviceVariable<float>(Resolution * Resolution * 2);
CudaDeviceVariable<float> copy_devData = new CudaDeviceVariable<float>(Resolution * Resolution * 2);
int i, j;
Random rnd = new Random();
double avrg = 0.0;
for (i = 0; i < Resolution; i++)
{
for (j = 0; j < Resolution; j++)
{
fData[(i * Resolution + j) * 2] = i + j * 2;
fData[(i * Resolution + j) * 2 + 1] = 0.0f;
}
}
devData.CopyToDevice(fData);
CudaFFTPlan1D plan1D = new CudaFFTPlan1D(Resolution * 2, cufftType.C2C, Resolution * 2);
plan1D.Exec(devData.DevicePointer, TransformDirection.Forward);
cuKernel.GridDimensions = new ManagedCuda.VectorTypes.dim3(Resolution/256, Resolution, 1);
cuKernel.BlockDimensions = new ManagedCuda.VectorTypes.dim3(256, 1, 1);
cuKernel.Run(devData.DevicePointer, copy_devData.DevicePointer, Resolution);
devData.CopyToHost(result);
for (i = 0; i < Resolution; i++)
{
for (j = 0; j < Resolution; j++)
{
ResultData[i, j, 0] = result[(i * Resolution + j) * 2];
ResultData[i, j, 1] = result[(i * Resolution + j) * 2 + 1];
}
}
ctx.FreeMemory(devData.DevicePointer);
ctx.FreeMemory(copy_devData.DevicePointer);
}
內核代碼
//Includes for IntelliSense
#define _SIZE_T_DEFINED
#ifndef __CUDACC__
#define __CUDACC__
#endif
#ifndef __cplusplus
#define __cplusplus
#endif
#include <cuda.h>
#include <device_launch_parameters.h>
#include <texture_fetch_functions.h>
#include "float.h"
#include <builtin_types.h>
#include <vector_functions.h>
// Texture reference
texture<float2, 2> texref;
extern "C"
{
__global__ void cu_ArrayInversion(float* data_A, float* data_B, int Resolution)
{
int image_x = blockIdx.x * blockDim.x + threadIdx.x;
int image_y = blockIdx.y;
data_B[(Resolution * image_x + image_y) * 2] = data_A[(Resolution * image_y + image_x) * 2];
data_B[(Resolution * image_x + image_y) * 2 + 1] = data_A[(Resolution * image_y + image_x) * 2 + 1];
}
}
然而這一方案不能很好地工作。 發生了以下錯誤:
ErrorLaunchFailed:設備在執行內核時發生異常。常見原因包括解除引用無效設備指針和訪問超出共享內存的限制。 上下文不能使用,所以它必須被銷燬(並且應該創建一個新的)。 如果程序要繼續使用CUDA,則此上下文中的所有現有設備內存分配均無效,並且必須進行重構。
也請發表您的更新主機代碼或再次檢查是否符合上面的代碼。如果我讓這兩個內核與我在這裏發佈的主機代碼一起運行,那麼一切運行良好。 Cuda內存檢查器找不到任何內容並且沒有錯誤信息。 – kunzmi
謝謝你的評論。我檢查了我的代碼。但是我找不到錯誤。我的程序是通過參考網站:(安裝程序:https://managedcuda.codeplex.com/documentatio,dll:https://github.com/kunzmi/managedCuda,示例代碼:https://github.com/ kunzmi/managedCuda)。當我使用managedCUDA製作2D cuda和1D cuda時,這些程序運行良好,我可以獲得良好的FFT結果。 – test
請準確地發佈您的主機代碼,因爲您可以重現您的問題。再說一遍:我上面發佈的主機代碼與內核一起運行沒有問題,所以一定有區別。 – kunzmi