Search code examples
synchronizationarrayfire

ArrayFire array calculated data differs over executions although usage of sync and eval methods


I want to convert text data into corresponding values (vectorized), but final data is not filled all the time, it seems that if there is some latency it has time to fill the result array otherwise it completes just a portion of the final data although using the sync and eval methods:

enter image description here

// input: ['0','9',' ','1','1',' ','1','5',' ','0','2',' ','0','3',' ','0','8',' ','1','4']
void TextToByteFilter::transform(af::array& input) const
{
    using namespace af;
    const auto len = 2; // length of uniform values
    auto data = input;
    data(data >= 65 && data <= 70) -= 7;        // convert hex (A to F) -> (10 to 15)
    data(data >= 97 && data <= 102) -= 39;      // convert hex (a to f) -> (10 to 15)
    data = data(data != ' ') - 48;              // remove spaces & convert to number

    // base conversion 
    af::array target(data.elements() / len);
    for (auto i = 0; i < len; i += 1)
        target = target + data(seq(len - static_cast<double>(i) - 1.0, end, len)) * static_cast<unsigned>(pow(base_, i));

    // if I use u8 type for target array on definition results are incorrect due to overflow I think although I have never exceeded the u8 range so I have to cast it from f32.
    data = target.as(u8);
    af_print(data);
    auto v0 = af::toString("data", data);
    // possible data values:
    //     1. [9, 11, 15, 2, 3, 0, 0] - two last items are not filled
    //     2. [9, 11, 15, 2, 3, 8, 14]- happens sometimes (and if I drag the next statement arrow to the top of the function)

    input = data;
}

And one more thing is that if I use u8 type for target variable results will become incorrect due to overflow I think, although I have never exceeded the u8 range that's why I have to cast it from f32.

Note: if I rerun this code by moving the next statement indicator to the top of the function it works.

What should I do?


Solution

  • What @pradeep said was right:

    target variable is uninitialised, that would eventually cause some problem because you are accumulating into that variable in the subsequent for loop. I would think that is the problem, try array target = constant(0, data.elements()/len)