简体   繁体   中英

Cuda factorial value invalid

I'm starting to learn CUDA. I wrote program to calculate factorial. Code is working but when i calculate factorial more what 12 i getting wrong values. Why CUDA getting invalid value? How to fix this? This is my 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)
        return 1; //silnia z 0 i 1 wynosi 1
    return 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();
    return 0;
}

Thx for help.

12 factorial is 479001600 which fits in a int variable.

13 factorial is 6227020800 which does not fit in a int variable.

If you change int to unsigned long long (everywhere, including your printf format specifier) you'll get some more range, but eventually you will run out of range.

Simply because there is "overflow". If you continue to use "int" you cannot solve the problem ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM