5

我是 CUDA 的新手,对 C 也不是很熟悉。我写了一个 Dll 来在我的 C# 程序中实现 CUDA 方法 (FFT)。我首先将 dll 作为控制台应用程序进行调试,以确保它正常工作,然后将其构建为 dll。所以我的问题是我的 dll 的第一次调用 (cufftPlan1d()) 会导致 AccessViolationException。到目前为止,我已经查看了这个博客和其他谷歌结果,但什么也没有。我使用不安全的代码来处理指针并使用 Marshal.AllocHGlobal() 方法分配内存,所以我真的看不出问题出在哪里。这是我的 dll 代码和调用 C# 类:

DLL:

extern "C" __declspec(dllexport)
unsigned int fftPlan(unsigned int* plan, int signal_size, int batch)
{
    if(cufftPlan1d(plan, signal_size, CUFFT_D2Z, batch) != CUFFT_SUCCESS) return 0;
    return 1;
}

extern "C" __declspec(dllexport)
int allocateMemory(double** signalGPU, cufftDoubleComplex** signalFft, int size)
{
    if(cudaMalloc(signalGPU, size) != cudaSuccess) return 0;
    if(cudaMalloc(signalFft, size+16) != cudaSuccess) return 0;
    return 1;
}

extern "C" __declspec(dllexport)
int fftCaller(unsigned int* plan, const double* real, double* realGPU,             cufftDoubleComplex* signalFft, cufftDoubleComplex* signalFftGPU, int size)
{
    cufftDoubleReal *idata=(cufftDoubleReal*)realGPU;
    if(cudaMemcpy(idata, real, size, cudaMemcpyHostToDevice) != cudaSuccess) return 0;
    if(cufftExecD2Z(*plan, idata, signalFftGPU) != CUFFT_SUCCESS) return 0;
    if(cudaMemcpy(signalFft, signalFftGPU, size+16, cudaMemcpyDeviceToHost) !=    cudaSuccess) return 0;
    return 1;
}

extern "C" __declspec(dllexport)
void cudaClean(void* GPUPtr)
{
    cudaFree(GPUPtr);
}

和包装类:

unsafe public class CudaFft
    public struct cufftDoubleComplex
    {
            public double x;
            public double y;
    }

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    unsafe public delegate int fftPlan(int* plan, int signal_size, int batch);

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    unsafe public delegate int allocateMemory(double** signalGPU, cufftDoubleComplex** signalFftGPU, int size);

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    unsafe public delegate int fftcaller(int* plan, double* signal, double* signalGPU, cufftDoubleComplex* signalFft, cufftDoubleComplex* signalFftGPU, int size);

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    unsafe public delegate int cudaclean(void* GPUPtr);

    public static int pDll, a;
    //static IntPtr signal, signalFft;
    unsafe static int* plan;
    unsafe static double* signal;
    unsafe static double** signalGPU;
    unsafe static int signal_size;
    unsafe static cufftDoubleComplex* signalFft;
    unsafe static cufftDoubleComplex** signalFftGPU;

    unsafe public static int Plan(int* plan, int signal_size, int batch)
    {
        IntPtr pAddressOfFunctionToCall = DllImport.GetProcAddress(pDll, "fftPlan");


        fftPlan fftplan = (fftPlan)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(fftPlan));
        return fftplan(plan, signal_size, batch); //THIS LINE CAUSES THE EXCEPTION 
    }  
    (...) //some irrelevant code here
    unsafe public CudaFft(int signal_length) //constructor
    {
        pDll = DllImport.LoadLibrary("d:\\CudaFft.dll");
        a = DllImport.GetLastError();
        signal_size = signal_length;
        signal = (double*)Marshal.AllocHGlobal(signal_size * 8).ToPointer();
        signalFft = (cufftDoubleComplex*)Marshal.AllocHGlobal((signal_size / 2 + 1) * 16).ToPointer();
        CudaFft.Plan(plan, signal_length, 1);
        CudaFft.allocMemory(signalGPU, signalFftGPU, signal_size);
    }

提前致谢, Szabolcs

4

1 回答 1

1

plan似乎永远不会被分配。

于 2012-12-31T17:01:50.147 回答