Home > Back-end >  C /CUDA equation printing incorrect answer
C /CUDA equation printing incorrect answer

Time:12-09

currently I am trying to print out the answer to an equation using CUDA.

This equation is (x y)^2 / xy

An example of the outputs I am getting are in this image attached.


__global__ void proof() {
    int x = 1;
    int y = 1;
    int multi_number = 1000;

    while (true) {
        long eq = ((pow(x   y, 2)) / (x * y));

        if (y >= multi_number) {
            if (x >= multi_number) {
                printf("\nProof is true for all cases.");
                break;
            }
        }

        if (x >= multi_number) {
            x = 1;
            y = y   1;
        }

        printf("\nEquation being used: (%d", x);
        printf(" %d", y);
        printf(")^2 / %d", x);
        printf("*%d", y);
        printf(" >= 4");
        printf("\n%d", eq); // printing the equations answer

        if (eq < 4) {
            printf("\nProof Failed: %d", x);
            printf(", %d", y);
            break;
        }

        x = x   1;
    }
}

I have currently tried rewriting the equation in multiple different ways, this did not work.

For the failed test (55 55)^2 / 55*55 I was expecting 4 to be printed instead of 3.

An example of a correct answer would be (1 1)^2 / 1*1 = 4

CodePudding user response:

In a nutshell, pow() (in CUDA device code, at least) does not have the accuracy you need/desire when using truncation. I just answered a very similar question here.

The reason for the failure is that the result of ((pow(x y, 2)) / (x * y)) (evaluated at the point (55,55)) is not 4 like you would expect, it is 3, when converted to a long value via truncation.

According to my testing, you can work around this by changing this line:

    long eq = ((pow(x   y, 2)) / (x * y));

to this:

    long eq = ((x y)*(x y)) / (x * y);

CodePudding user response:

Try this code

    #include <stdio.h>
    #include <cuda_runtime.h>

   __global__ void calculate(float x, float y, float *result)
   {
*result = (x   y) * (x   y) / (x * y);
   }

 int main(void)
{
float x = 3.0, y = 2.0;
float result;

cudaMalloc((void **)&x, sizeof(float));
cudaMalloc((void **)&y, sizeof(float));
cudaMalloc((void **)&result, sizeof(float));

cudaMemcpy(x, &x, sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(y, &y, sizeof(float), cudaMemcpyHostToDevice);

calculate<<<1,1>>>(x, y, result);

cudaMemcpy(&result, result, sizeof(float), cudaMemcpyDeviceToHost);

cudaFree(x);
cudaFree(y);
cudaFree(result);

printf("The result of the calculation is %f\n", result);

return 0;
    }
  • Related