我试图了解AVX2 intel内在函数的收集功能。
根据官方文件Link,功能定义为,
__m256i _mm256_i32gather_epi32 (int const* base_addr, __m256i vindex, const int scale)
Gather 32-bit integers from memory using 32-bit indices. 32-bit elements are loaded from addresses starting at base_addr and offset by each 32-bit element in vindex (each index is scaled by the factor in scale). Gathered elements are merged into dst. scale should be 1, 2, 4 or 8.
因此,根据我的理解,它返回一个__m256i向量,该向量填充了数组中的8个整数,并且基索引base_addr
来自填充在vindex
中的indexs(8)。如果提及任何scale
,则该乘积也将相乘。现在,为了测试理解,我编写了一个代码,
#include<stdio.h>
#include <immintrin.h>
int main()
{
__m256i var, ind_intel;
int * arr = (int *) aligned_alloc(sizeof(__m256i), sizeof(int) * 64);
int * out = (int *) aligned_alloc(sizeof(__m256i), sizeof(int) * 8);
int * ind = (int *) aligned_alloc(sizeof(__m256i), sizeof(int) * 8);
int i;
ind[0] = 0;ind[1] = 2;ind[2] = 4;ind[3] = 6;ind[4] = 8;ind[5] = 10;ind[6] = 12;ind[7] = 14;
ind_intel = _mm256_load_si256((__m256i *)&ind[0]);
for(i=0;i<64;i++)
arr[i] = i;
var = _mm256_i32gather_epi32(arr,ind_intel,1);
_mm256_store_si256((__m256i *)&out[0], var);
for(i=0;i<8;i++)
printf("%d ",out[i]);
return 0;
}
现在,__m256i
变量ind_intel
获得的索引为0,2,..,14
。主数组arr
装载有0,1,..,63
。因此,收集应将数据加载为arr[0],arr[2],..,arr[14]
。但这是在打印值,
0 65536 1 131072 2 196608 3 262144
当然,我缺少一些大的东西。但是我找不到任何网站或文档清楚地提到了collect的用法。它们每个都重复与正式文件相同的描述。谁能在代码和理解中解释问题?
N.B。该代码仅用于测试目的。
vindex
中的偏移量以字节为单位。因此,您从地址{arr, arr+2, arr+4, ...}
收集32位整数值。将这些索引从{0,2,4...}
更改为{0,8,16,...}
,或将比例因子更新为:
var = _mm256_i32gather_epi32(arr,ind_intel, 4 ); // 1 -> 4
这将打印出期望值。