首页 > 解决方案 > 尽管使用了 sync 和 eval 方法,但 ArrayFire 数组计算的数据在执行过程中有所不同

问题描述

我想将文本数据转换为相应的值(矢量化),但最终数据并未一直填充,似乎如果有一些延迟它有时间填充结果数组,否则它只完成最终数据的一部分虽然使用同步评估方法:

在此处输入图像描述

// 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;
}

还有一件事是,如果我将 u8 类型用于目标变量,我认为由于溢出,结果会变得不正确,尽管我从未超过 u8 范围,这就是为什么我必须从 f32 转换它的原因。

注意:如果我通过将下一条语句指示器移动到函数顶部来重新运行此代码,它会起作用。

我该怎么办?

标签: synchronizationarrayfire

解决方案


@pradeep 说的是对的:

target变量未初始化,这最终会导致一些问题,因为您在随后的 for 循环中累积到该变量中。我认为这是问题,尝试array target = constant(0, data.elements()/len)


推荐阅读