下面是我希望工作的 cuda 代码。我收到“双重免费或损坏”错误。经过多次调试,我发现有问题的行是“cudaMemcpy(out,out_device ...)。我对为什么这个错误仍然存在感到完全傻眼。任何帮助都将不胜感激。ComplexFloat 的类型定义为 float2。
void covariance2(float alpha, complex float* in, complex float* out, int dims[5])
{
int x = dims[0];
int y = dims[1];
int z = dims[2];
int N = dims[3];
int M = dims[4];
ComplexFloat* in_device;
ComplexFloat* out_device;
int siz = x*y*z*N*(N+1)/2;
assert(cudaMalloc(&in_device, sizeof(ComplexFloat)*x*y*z*N*M) == cudaSuccess);
assert(cudaMalloc(&out_device, sizeof(ComplexFloat)*siz) == cudaSuccess);
assert(cudaMemcpy(in_device, in, sizeof(ComplexFloat)*x*y*z*N*M, cudaMemcpyHostToDevice) == cudaSuccess);
dim3 numBlocks(x,y,z);
dim3 numThreads(1);
size_t sharedMem = N*M + N*(N+1)/2;
cudaMemcpy(out, out_device, sizeof(ComplexFloat)*siz, cudaMemcpyDeviceToHost);
cudaFree(in_device);
cudaFree(out_device);
}
int main()
{
int xxx = 2;
int yyy = 2;
int zzz = 1;
int MMM = 7;
int NNN = 3;
int dims[5] = { xxx, yyy, zzz, MMM, NNN };
float alpha = 5.;
complex float a = 1.314 + 5.42*_Complex_I;
complex float* in = (complex float*) malloc(sizeof(complex float)*NNN*MMM*xxx*yyy*zzz);
for (int i = 0; i < xxx*yyy*zzz*MMM*NNN; i++)
in[i] = a*pow(i,2);
complex float* out = (complex float*) malloc(sizeof(complex float)*xxx*yyy*zzz*NNN*(NNN+1)/2);
assert(out);
covariance2(alpha, in, out, dims);
for (int i = 0; i < NNN*(NNN+1)/2; i++)
printf("i = %d, real = %f, imag = %f\n", i, __real__(out[i]), __imag__(out[i]));
free(out);
}