因为这是一个任务,你可能想了解矢量化过程,我不提供可以编译的源代码(你应该在我的答案后做一些编码)。希望你能自己解决。
//The loop counter should be suitable for Vectorization Factor (VF)
//In this case VF=4 (assume your processor has 128-bit SIMD register and data are 32-bit.
//1757×4 = 7028 --> you will have 2 values that can not be put in vectos or you must pad the array to fit the vector.
for (i = 0; i < 7028; i+=4) {
a[7031 * i + 703] = b[i] * c[i];
a[7031 * (i+1) + 703] = b[i+1] * c[i+1];
a[7031 * (i+2) + 703] = b[i+2] * c[i+2];
a[7031 * (i+3) + 703] = b[i+3] * c[i+3];
}
a[7031 * i + 703] = b[i] * c[i];
i++;
a[7031 * i + 703] = b[i] * c[i];
//vec_b = (b[i], b[i+1], b[i+2], b[i+3]); // are adjacent -> thus can be loaded
//vec_c = (c[i], c[i+1], c[i+2], c[i+3]); // are adjacent -> thus can be loaded
//index = 7031*i + 703
//vec_a = (a[index], a[index + 7031], a[index + 7031*2], a[index + 7031*3]; //not adjacent!
vec_b = __mm_loadu_ps(&b[i]);
负载从相邻元件您ASLO可以使用从相邻元件intrinsic instruction这样载荷加载指令的向量到向量vec_c
。但关键是你应该将数据存储到非连续地址。如果处理器支持AVX-512
,则可以使用scatter
指令将矢量存储到非连续地址。 如果您没有scatter
说明,您可能需要提取元素并将其放入不同的目标地址。 _mm_extract_epi32
或_mm_cvtss_f32
和移位等
for (i = 0; i < 7030; i++) {
d[i] = a[7031 * i + 703 * 7030] + e;
}
需要再次进行矢量化,你需要了解数据的地方:
Index = 7031 * i + 703 * 7030
for (i = 0; i < 7028; i+=4) {
d[i] = a[Index] + e;
d[i+1] = a[Index + 7031] + e;
d[i+2] = a[Index + 7031*2] + e;
d[i+3] = a[Index + 7031*3] + e;
}
//extra computations for i = 7028, 7029;
//vec_a = (a[Index], a[Index + 7031], a[Index + 7031*2], a[Index + 7031*3])
//vec_a can be loaded with _mm_set_ps (a3, a2, a1, a0), etc but `gather` instruction is also use full to load from different addresses.
//vec_e = (e, e, e, e) : you can use _mm_set_ps1, _mm_set1...
最后如何乘或补充的吗?容易使用向量运算
vec_a = _mm_mul_ps(vec_b, vec_c);
vec_d = _mm_add_ps(vec_a, vec_e);
以及如何存储,以继续把一个向量?
_mm_store_ps(d[i],vec_d); //i=i+4 for the next store I mean your loop counter must be appropriate.
因此,矢量化的循环中,您可以使用内部函数作为一个明确的量化,也可以依靠隐性量化,如使用gcc /铛在-O3优化级别或适当标志启用gcc -ftree-vectorize -ftree-slp-vectorize