When I invoke the cublasSdot function with the result alloced on device, it gave me an error.
But it successed with the result on host.
the code as follow:
#include <cublas.h>
#include <cublas_v2.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n = 10;
float *h_x = (float *)calloc(n,sizeof(float));
float *h_y = (float *)calloc(n,sizeof(float));
float *h_result = (float *)calloc(1,sizeof(float));
for (int k = 0; k < n; ++k)
{
h_x[k] = 1.0f;
h_y[k] = 1.0f;
}
cublasHandle_t handle;
cublasCreate(&handle);
float *d_x;
float *d_y;
float *d_result;
cublasAlloc(n,sizeof(float),(void **)&d_x);
cublasAlloc(n,sizeof(float),(void **)&d_y);
cublasAlloc(1,sizeof(float),(void **)&d_result);
cublasSetVector(n,sizeof(float),h_x,1,d_x,1);
cublasSetVector(n,sizeof(float),h_y,1,d_y,1);
cublasSdot(handle,n,d_x,1,d_y,1,d_result);
cublasGetVector(1,sizeof(float),d_result,1,h_result,1);
printf("%f\n", h_result);
cublasFree(d_x);
cublasFree(d_y);
cublasFree(d_result);
cublasDestory(handle);
free(h_x);
free(h_y);
free(h_result);
return 0;
}
Thanks for your help!!!
read the cublas documentation to set the scalar parameter pointer mode.
http://docs.nvidia.com/cuda/cublas/index.html#scalar-parameters
http://docs.nvidia.com/cuda/cublas/index.html#cublaspointermode_t
aside:
I’m curious what this is:
cublasDestory(handle);
That’s not valid CUBLAS code. Did you actually type this code into the browser? I don’t understand how this could be a copy-paste error, and this code won’t compile.
falao84
October 19, 2016, 12:11pm
3
EXCELLENT!!
Thanks a lot! Yesterday I was coding on my home pc without nv gpu. So the code has an error.
Now I could put the result on the device! I fix the code as follow. This code could run by win7+vs2012+cuda7.5.
#include <cublas.h>
#include <cublas_v2.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n = 10;
float *h_x = (float *)calloc(n,sizeof(float));
float *h_y = (float *)calloc(n,sizeof(float));
float *h_result = (float *)calloc(1,sizeof(float));
for (int k = 0; k < n; ++k)
{
h_x[k] = 1.0f;
h_y[k] = 1.0f;
}
cublasHandle_t handle;
cublasCreate(&handle);
cublasSetPointerMode(handle,CUBLAS_POINTER_MODE_DEVICE); // set here!!!
float *d_x;
float *d_y;
float *d_result;
cublasAlloc(n,sizeof(float),(void **)&d_x);
cublasAlloc(n,sizeof(float),(void **)&d_y);
cublasAlloc(1,sizeof(float),(void **)&d_result);
cublasSetVector(n,sizeof(float),h_x,1,d_x,1);
cublasSetVector(n,sizeof(float),h_y,1,d_y,1);
cublasSdot(handle,n,d_x,1,d_y,1,d_result);
cublasGetVector(1,sizeof(float),d_result,1,h_result,1);
printf("%f\n", *h_result); // fixed
cublasFree(d_x);
cublasFree(d_y);
cublasFree(d_result);
// cublasDestory(handle);
cublasDestroy(handle);
free(h_x);
free(h_y);
free(h_result);
return 0;
}
cublasDestory is incorrect. It should be cublasDestroy
[url]http://docs.nvidia.com/cuda/cublas/index.html#cublasdestroy[/url]
so I don’t think this is the code you are actually running. cublasDestory should not compile correctly
falao84
October 20, 2016, 12:18pm
5
Thank you very much!
I am sorry for that I wrote a wrong code, and did not check it.
It is a waste of time that pointing out my spelling mistake.