Here is a minimal program illustrating the problem. I'm using a GTS 250 with 128 CUDA cores, CUDA 5.0 on Windows 7.
void cuda_ops_test(int N, float* R)
{
//Values of input matrix in CPU
fprintf(stderr, "\nValues of R: \n");
for (int i=0; i<N; ++i)
fprintf(stderr, "%f, ", R[i]);
fprintf(stderr, "\n");
//Initialize CUDA/CUBLAS
cublasHandle_t handle;
cublasStatus_t status;
status = cublasCreate(&handle);
if (status == CUBLAS_STATUS_SUCCESS)
fprintf(stderr, "CUBLAS initialization succeeded.\n");
//Allocate device memory
float *dR = 0;
cudaError_t alloc_status;
alloc_status = cudaMalloc((void**)&dR, N*sizeof(dR[0]));
if(alloc_status == cudaSuccess)
fprintf (stderr, "\nDevice memory allocation succeeded.\n");
//Load array into memory
status = cublasSetMatrix(1, N, sizeof(R[0]), R, N, dR, N);
if(status == CUBLAS_STATUS_SUCCESS)
fprintf (stderr, "\nDevice write succeeded.\n");
//First operation: summation
float ans;
status = cublasSasum(handle, N, dR, 1, &ans);
if (status == CUBLAS_STATUS_SUCCESS)
fprintf(stderr, "\ncublasSasum produced no error. Sum of dR: %d\n", ans);
else
fprintf(stderr, "\ncublasSasum error: %d.\n", status);
//Second operation: y = ax+y
const float alpha = 2.0;
status = cublasSaxpy(handle, N,
&alpha,
dR, 1,
dR, 1);
if (status == CUBLAS_STATUS_SUCCESS)
fprintf(stderr, "\ncublasSaxpy produced no error.\n");
else
fprintf(stderr, "\ncublasSaxpy error: %d.\n", status);
// transfer device dR to host R
status = cublasGetMatrix (1, N, sizeof(dR[0]), dR, N, R, N);
if(status == CUBLAS_STATUS_SUCCESS)
fprintf (stderr, "\nDevice read succeded\n");
//Display post-op values of R
fprintf(stderr, "\nValues of R, after cublasSaxpy: \n");
for (int i=0; i<N; ++i)
fprintf(stderr, "%f, ", R[i]);
fprintf(stderr, "\n");
//Attempt to zero with cudaMemset
cudaError_t stat = cudaMemset(dR, 0, N*sizeof(dR[0]));
if (stat==cudaSuccess)
fprintf(stderr, "\nZeroing with cudaMemset on R produced no error.\n");
//Again transfer device dR to host R, after zeroing
status = cublasGetMatrix (1, N, sizeof(dR[0]), dR, N, R, N);
if(status == CUBLAS_STATUS_SUCCESS)
fprintf (stderr, "\nDevice read succeded.\n");
//Display values of R again
fprintf(stderr, "\nValues of R, after zeroing with cudaMemset: \n");
for (int i=0; i<N; ++i)
fprintf(stderr, "%f, ", R[i]);
fprintf(stderr, "\n");
cudaFree(dR);
}
Here's the output, indicating that while the data were loaded into GPU memory, no operations actually occurred:
Values of R: 0.123020, 0.367809, 0.834681, 0.035096, 0.517014, 0.662984, 0.426221, 0.104678,
CUBLAS initialization succeeded.
Device memory allocation succeeded.
cublasSasum produced no error. Sum of dR: 0
cublasSaxpy produced no error.
Values of R, after cublasSaxpy: 0.123020, 0.367809, 0.834681, 0.035096, 0.517014, 0.662984, 0.426221, 0.104678,
Zeroing with cudaMemset on R produced no error.
Values of R, after zeroing with cudaMemset: 0.123020, 0.367809, 0.834681, 0.035096, 0.517014, 0.662984, 0.426221, 0.104678,
What's going on? (And happy holidays. :) )