下面是我希望工作的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);
}
答案 0 :(得分:1)
在您的covariance2
功能中:
int N = dims[3];
int M = dims[4];
但在你的main()
内:
int dims[5] = { xxx, yyy, zzz, MMM, NNN };
请注意,M是dims[]
由于您的malloc(out
)和cudaMalloc(out_device
)依赖于N,因此您使用两个不同的N(3和7)值来计算大小。这意味着out
和out_device
的大小不同,我认为这不是您的意图。由于({size})out
的计算结果为N为3,并且({size})out_device
的计算结果为N为7,因此您尝试将较大的数据结构cudaMemcpy放到较小的数据结构上。在协方差2中,修正大概是这样的:
int M = dims[3];
int N = dims[4];