0

One of my kernels compares each value of a double 1D array verifying that is greater than a scalar. This kernel fills a boolean array of the comparison results.

The resulting boolean vector should provide every element comparison result in TRUE or FALSE terms.

But I found that the device is storing other types of TRUE values that makes my code crash. I can find TRUE (63), TRUE (66), TRUE (240), etc. What is this?

Please download the images of the same array in two different times when the scalar is equal to zero.

https://onedrive.live.com/redir?resid=26B45E0F43F4667B!12766&authkey=!APVysjd1WK1nDhE&ithint=folder%2c

The 1D array contains 100 elements and only the first element is equal to 1 and the rest is equal to zero. The expected results is of course: {TRUE, FALSE, FALSE, FALSE, ... FALSE}

I found another question with an answer that suggests the use 32-bit integers ("Ideally you should aim to use a 32 bit type") but I don't understand how to do that:

Boolean operations on CUDA

This is my kernel:

__global__ void _vecCmpGreat(bool *output, double *input1, double input2) {
    unsigned int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < samples)
        output[i] = input1[i] > input2;
}

The part of my code that computes the comparison results is the following. It should push back into std::vector result when the transition "FALSE" to "TRUE" is found the the boolean array.

unsigned int j;
for (unsigned int i = 0; i < samples - 1; i++) {
    j = samples - i - 1;
    if (r[samples - 1]) {
        result.push_back(samples - i - 1);
        break;
    }
    else {
        if (r[j] == false && r[j - 1] == true) {
        result.push_back(j);
        break;
    }
}

Any suggestions will be appreciated.

Community
  • 1
  • 1
Vitrion
  • 405
  • 5
  • 14

2 Answers2

0

My suspicion is that they are simply those integers. In C++, 0 == false and true = !false

Therefore all the values of 0 are false, and any other values are true, such as 63, 66, and 240. These may be what you are finding.

Try typecasting them as integers or comparing them to known integers to see what they really are.

Disclaimer: I am not familiar with CUDA.

Community
  • 1
  • 1
ti7
  • 16,375
  • 6
  • 40
  • 68
0

Your problem is this line:

        if (r[j] == false && r[j - 1] == true)

What you need is:

        if (!r[j] && r[j - 1])

(which I find easier to read as:

        if (r[j - 1] && !r[j])

for some reason.)

Comparing boolean values against false and true in C based languages is almost always dangerous. It should work if r is declared as type bool, but otherwise any non-zero value will be treated as "true" when tested, but only 1 will compare equal to true. In fact comparisons with false will not have this problem - but once you compare with one boolean literal, you can easily forget and compare with the other. Just say no.