通过 host() 从 af::array 检索数据会导致错误的数据

Retrieving data from af::array via host() results in wrong data

本文关键字:数据 错误 检索 array host af 通过      更新时间:2023-10-16

当尝试通过host()从设备从af::array(arrayfire(检索数据时,我在主机上的输出数据是错误的(即错误的值(。为了测试这一点,我编写了一个小代码示例(基于 https://stackoverflow.com/a/29212923/2546099(:

int main(void) {  
size_t vector_size = 16;
af::array in_test_array = af::constant(1., vector_size), out_test_array = af::constant(0., vector_size);
af_print(in_test_array);
double *local_data_ptr = new double[vector_size]();
for(int i = 0; i < vector_size; ++i)
std::cout << local_data_ptr[i] << 't';
std::cout << 'n';
in_test_array.host(local_data_ptr);
for(int i = 0; i < vector_size; ++i)
std::cout << local_data_ptr[i] << 't';
std::cout << 'n';
delete[] local_data_ptr;
out_test_array = in_test_array;
af_print(out_test_array);
return 0;
}

我的输出是

in_test_array
[16 1 1 1]
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
0       0       0       0       0       0       0       0       0       0       0       0       0       0       0       0
0.007813        0.007813        0.007813        0.007813        0.007813        0.007813        0.007813        0.007813        0       0       0       0       0       0       0       0
out_test_array
[16 1 1 1]
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000 
1.0000

为什么指针中的一半值设置为0.007813,而不是所有值都设置为1?将in_test_array的默认值更改为2时,一半的值设置为2,对于3,这些值设置为32。为什么会这样?

arrayfire 和 C 之间的数据类型是冲突的。

浮子使用:

af::array in_test_array = af::constant(1., vector_size), 
out_test_array = af::constant(0., vector_size);
float *local_data_ptr = new float[vector_size]();

双重使用:

af::array in_test_array = af::constant(1., vector_size, f64),
out_test_array = af::constant(0., vector_size, f64)
double *local_data_ptr = new double[vector_size]();

在上述两种情况下,您都会看到 ARRAYFIRE 将在 local_data_ptr 缓冲区中返回 1.0,尽管数据类型不同。