c - Cuda factorial value invalid -
i'm starting larn cuda. wrote programme calculate factorial. code working when calculate factorial more 12 getting wrong values. why cuda getting invalid value? how prepare this? code.
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include <conio.h> #define cuprintf cuprintf __device__ int silnia(int n) { if (n<2) homecoming 1; //silnia z 0 1 wynosi 1 homecoming n*silnia(n - 1); } __global__ void kernel(int *a) { *a = silnia(15); } int main() { cudaevent_t start, stop; float elapsedtime; cudaeventcreate(&start); cudaeventrecord(start, 0); int *dev_a,a; cudaeventrecord(start); cudamalloc((void**)&dev_a, sizeof(int)); kernel << <1, 1 >> >(dev_a); cudamemcpy(&a, dev_a, sizeof(int), cudamemcpydevicetohost); cudaeventcreate(&stop); cudaeventrecord(stop, 0); cudaeventsynchronize(stop); cudaeventelapsedtime(&elapsedtime, start, stop); printf("elapsed time : %f ms\n", elapsedtime); printf("%d", a); getch(); homecoming 0; }
thx help.
12 factorial 479001600 fits in int
variable.
13 factorial 6227020800 not fit in int
variable.
if alter int
unsigned long long
(everywhere, including printf
format specifier) you'll more range, run out of range.
c cuda
No comments:
Post a Comment