I wrote a simple scalar multiplication test on cublasSscal() in CUBLAS 2.2. The last element won’t be computed somehow. Here is the code and result.
/* A test program for scalar multiplication in CUBLAS 2.2.
*
* Compile: %gcc test.c -I/usr/local/cuda/include -lcublas -L/usr/local/cuda/lib
*
* Name: Dan Lo (danlo@ieee.org)
* Date: 5/15/2009
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "cublas.h"
int main(int argc, char**argv) {
float *a_h, *a_r_h;
float *a_d;
int i, N;
cublasStatus stat;
/* take dimension */
if (argc!=2) {
printf("usage: %s vector_dimension\n", argv[0]);
exit(1);
}
N = atoi(argv[1]);
a_h = (float*)malloc(N*sizeof(*a_h));
a_r_h = (float*)malloc(N*sizeof(*a_r_h));
if (!a_h || !a_r_h) {
printf("no host memory\n");
return EXIT_FAILURE;
}
/* init vector */
for (i=0;i<N;i++)
a_h[i] = i+1;
cublasInit();
/* alloc device memory */
stat = cublasAlloc(N, sizeof(*a_d), (void**)&a_d);
if (stat!=CUBLAS_STATUS_SUCCESS) {
printf("device memory alloc error\n");
cublasShutdown();
return EXIT_FAILURE;
}
/* copy vector to device */
stat = cublasSetVector(N, sizeof(*a_d), a_h, 1, a_d, 1);
if (stat!=CUBLAS_STATUS_SUCCESS) {
printf("set device memory error\n");
cublasShutdown();
return EXIT_FAILURE;
}
/* computation */
cublasSscal(N, 0.5f, &(a_d[-1]), 1);
/* copy result back to CPU memory */
stat = cublasGetVector(N, sizeof(*a_d), a_d, 1, a_r_h, 1);
if (stat!=CUBLAS_STATUS_SUCCESS) {
printf("device memory alloc error\n");
cublasShutdown();
return EXIT_FAILURE;
}
/* free GPU memory */
cublasFree(a_d);
cublasShutdown();
/* show result */
for (i=0;i<N;i++)
printf("%f %f\n", a_h[i], a_r_h[i]);
return EXIT_SUCCESS;
}/* main() */
1.000000 0.500000
2.000000 1.000000
3.000000 1.500000
4.000000 2.000000
5.000000 2.500000
6.000000 3.000000
7.000000 3.500000
8.000000 4.000000
9.000000 4.500000
10.000000 5.000000
11.000000 5.500000
12.000000 6.000000
13.000000 6.500000
14.000000 7.000000
15.000000 7.500000
16.000000 8.000000
17.000000 8.500000
18.000000 9.000000
19.000000 9.500000
20.000000 10.000000
21.000000 10.500000
22.000000 11.000000
23.000000 11.500000
24.000000 12.000000
25.000000 12.500000
26.000000 13.000000
27.000000 13.500000
28.000000 14.000000
29.000000 14.500000
30.000000 15.000000
31.000000 15.500000
32.000000 16.000000
33.000000 16.500000
34.000000 17.000000
35.000000 17.500000
36.000000 18.000000
37.000000 18.500000
38.000000 19.000000
39.000000 19.500000
40.000000 20.000000
41.000000 20.500000
42.000000 21.000000
43.000000 21.500000
44.000000 22.000000
45.000000 22.500000
46.000000 23.000000
47.000000 23.500000
48.000000 24.000000
49.000000 24.500000
50.000000 25.000000
51.000000 25.500000
52.000000 26.000000
53.000000 26.500000
54.000000 27.000000
55.000000 27.500000
56.000000 28.000000
57.000000 28.500000
58.000000 29.000000
59.000000 29.500000
60.000000 30.000000
61.000000 30.500000
62.000000 31.000000
63.000000 31.500000
64.000000 32.000000
65.000000 32.500000
66.000000 33.000000
67.000000 33.500000
68.000000 34.000000
69.000000 34.500000
70.000000 35.000000
71.000000 35.500000
72.000000 36.000000
73.000000 36.500000
74.000000 37.000000
75.000000 37.500000
76.000000 38.000000
77.000000 38.500000
78.000000 39.000000
79.000000 39.500000
80.000000 40.000000
81.000000 40.500000
82.000000 41.000000
83.000000 41.500000
84.000000 42.000000
85.000000 42.500000
86.000000 43.000000
87.000000 43.500000
88.000000 44.000000
89.000000 44.500000
90.000000 45.000000
91.000000 45.500000
92.000000 46.000000
93.000000 46.500000
94.000000 47.000000
95.000000 47.500000
96.000000 48.000000
97.000000 48.500000
98.000000 49.000000
99.000000 49.500000
100.000000 100.000000