synchronization - 尽管使用了 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 转换它的原因。
注意:如果我通过将下一条语句指示器移动到函数顶部来重新运行此代码,它会起作用。
我该怎么办?
解决方案
@pradeep 说的是对的:
target
变量未初始化,这最终会导致一些问题,因为您在随后的 for 循环中累积到该变量中。我认为这是问题,尝试array target = constant(0, data.elements()/len)
推荐阅读
- c++ - 从调用的可调用目标将 std::function 设置为 nullptr
- sql - Go 中数据库迁移的正确策略
- python - python中的csv到txt
- java - 我们可以依靠 maven 在运行时处理类加载问题吗?
- php - Content-Type 标头值
- json - Json Schema Validation:对子模式的依赖
- angular - Angular 6:使用 npm start 启动我的应用程序时出错
- batch-file - 如何为主机列表循环一组命令?
- c# - C# - WPF:循环浏览 PDF 文件并在浏览器控件中显示
- python - Pyinstaller 给出关于未声明编码的错误