Why do I have the problem of different results every time when I use CUDA for calculations?

It’s simple, here is my code:

__int64 upFactor = 20000;
double sh = 0.999325;
double ans = upFactor / sh;

The right answer is 20013.50911865509, but i get different anwers like “20013.502754056615231”, “20013.502782080537145”,“20013.502756873367616”. Only two-bit accuracy is guaranteed!How could this be?
When i use C++, it could guarantee everytime it has the same answer, though the precision is not good enough.

When I run the following program, I get equal outputs for host code and device code.

cpu 20013.50911865509260678663849831
gpu 20013.50911865509260678663849831

#include <cstdio>
#include <cstdint>


__global__
void kernel(){
    std::int64_t upFactor = 20000;
    double sh = 0.999325;
    double ans = upFactor / sh;
    printf("gpu %.26f\n", ans);
}

int main(){
    std::int64_t upFactor = 20000;
    double sh = 0.999325;
    double ans = upFactor / sh;
    printf("cpu %.26f\n", ans);
    kernel<<<1,1>>>();
    cudaDeviceSynchronize();
}

What is your reproducer code?

I use “cout” to output the result:

	sh = upFactor*1.0 / sh;

	cout << setprecision(20) << sh << "\t" << endl;

Why would this happen? My result in C++ code is : 20013.502784094216622, which is all the same but not right as well.

I also used printf to test the code, but it’s still not right. My compile environment is “Windows11 Visual Studio 2019”, does this situation has something to do with the compile environment?

Please show a complete minimal example.

1 Like

I seem to find what is wrong. The value of “sh” changes all the time!!! I made mistake above this part of code. It takes me so long to debug T_T