I'm new with CUDA and not really familiar with C either. I wrote a Dll to implement CUDA methods (FFT) into my C# programm. I debugged first the dll as a console application to make sure it works properly, and just then built it as a dll. So my problem is that at the first call (cufftPlan1d()) of my dll causes an AccessViolationException. I've already looked up this blog and other google results so far, but nothing. I use unsafe code to handle pointers and allocate memory with Marshal.AllocHGlobal() method, so I don't really see where the problem is. Here is my code of my dll and the calling C# class:
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);
}
and the wrapper class:
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);
}
Thanks in advance, Szabolcs
plan
appears to never be allocated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With