cuda - constant memory -
i write code copying integer constant memory , utilize in global function,but doesn't work correctly.i mean no "cuprintf" in global function works , nil printed!
i think because of "if(*num == 5)",since remove it, "cuprintf" print want!
i seek "if(num == 5)" visual studio doesn't compile , shows error.
why "num" right in "cuprintf" not right in "if" statement?
how should utilize "num" in "if" statement?
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include "stdio.h" #include "stdlib.h" #include "cuprintf.cu" __constant__ int* num; __global__ void kernel(){ cuprintf("\n num=%d\n",num); if(*num == 5) cuprintf("\n num equal 5"); else cuprintf("\n num not equal 5"); } void main(){ int x; printf("\n\nplease come in x:"); scanf("%d",&x); cudamemcpytosymbol( num, &x,sizeof(int)*1); cudaprintfinit(); kernel<<<1,1>>>(); cudaprintfdisplay(stdout, true); cudaprintfend(); int wait; scanf("%d",&wait); }
if change:
__constant__ int* num;
to
__constant__ int num;
and change:
cudamemcpytosymbol( num, &x,sizeof(int)*1);
to
cudamemcpytosymbol( &num, &x,sizeof(int)*1);
then
cuprintf("\n num=%d\n",num);
will show "num=0" input!
"num" should not pointer. changed code 1 below, works me (note requires sm 2.0 or newer printf):
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include "stdio.h" #include "stdlib.h" __constant__ int num; __global__ void kernel() { printf("\n num=%d\n", num); if (num == 5) printf("\n num equal 5"); else printf("\n num not equal 5"); } int main() { cudaerror_t err; int x; printf("\n\nplease come in x:"); scanf("%d", &x); err = cudamemcpytosymbol(num, &x, sizeof(int) * 1); if (err != cudasuccess) { printf("error: %d\n", err); homecoming 1; } kernel<<<1, 1>>>(); err = cudadevicesynchronize(); if (err != cudasuccess) { printf("error: %d\n", err); homecoming 1; } homecoming 0; }
cuda
No comments:
Post a Comment