我的CUDA程序可以nvcc通过, 并能够完成运行而不弹出错误或自行终止. 但是无法完成预期的计算, 具体表现为cudaMemcpy(), cudaMemset()等函数失效, 不能正确地实现Host与Device之间的数据传输以及对Device memory的操作. 再就是在程序中调用kernel函数的部分, 实际未起作用.
cudasum.cu为一个简单的计算两个向量的和的cuda程序: c=a+b, 在我所使用的AMAX上的运行结果为 0 0 0 0
而我发到其他人那儿(也是AMAX PSC-2n Workstation)则可以得到正确的结果
我不知道是什么原因造成上述现象,只知道有可能是因为我的AMAX上的GPU未启动。
请问这个问题如何解决?如果是因为GPU未启动, 请教一下启动方法~
PS : cudasum.cu, 很很很简单的, 已验证程序本身是没错误的,因为在别的机器上可以以得到正确结果~
/**************************************************************************************************
c=a+b
both a and b are float vectors
***************************************************************************************************/
#include <cuda_runtime.h>
#include <stdio.h>
#include <malloc.h>
#define N 4
////////////////////////////////////////////////////////////////////////////////////////////////////
global void KerSum( float *a , float *b , float *c)
{
int i = threadIdx.x;
c[i] = a[i] + b[i];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
// GPU variables
float *ga , *gb , *gc;
// CPU variables
float *ca , *cb;
float *cpuresult, *gpuresult;
int ii;
// Allocate memory
ca = (float*)malloc( sizeof(float)N );
cb = (float)malloc( sizeof(float)N );
cpuresult = (float)malloc( sizeof(float)N );
gpuresult = (float)malloc( sizeof(float)*N );
cudaMalloc( (void**)&ga , sizeof(float)N );
cudaMalloc( (void*)&gb , sizeof(float)N );
cudaMalloc( (void*)&gc , sizeof(float)*N );
// Assign a and b, and calculate the CPU result at the same time
for( ii=0 ; ii<N ; ii++ )
{
ca[ii] = 2.5 ;
cb[ii] = 4.3 ;
cpuresult[ii] = ca[ii] + cb[ii] ;
}
// Show a , b and CPU result
printf(“a:: “);
for( ii=0 ; ii<N ; ii++ ) printf( “%15f” , ca[ii] );
printf(”\n”);
printf(“b:: “);
for( ii=0 ; ii<N ; ii++ ) printf( “%15f” , cb[ii] );
printf(”\n”);
printf(“cpu::”);
for( ii=0 ; ii<N ; ii++ ) printf( “%15f” , cpuresult[ii] );
printf(“\n”);
// Transfer data from CPU memory to GPU memory
cudaMemcpy( ga , ca , sizeof(float)*N , cudaMemcpyHostToDevice );
cudaMemcpy( gb , cb , sizeof(float)*N , cudaMemcpyHostToDevice );
// Call kernel
KerSum<<< 1 , N >>>( ga , gb , gc );
// Transfer data from GPU memory to CPU memory
cudaMemcpy( gpuresult , gc , sizeof(float)*N , cudaMemcpyDeviceToHost );
// Show the GPU result
printf(“gpu::”);
for( ii=0 ; ii<N ; ii++ ) printf( “%15f” , gpuresult[ii] );
printf(“\n”);
return 1 ;
}