动态对齐的多维数组不被 GCC 编译器视为对齐
Dynamically aligned multi-dimensional array is not regarded as aligned by the GCC compiler
我是循环矢量化的新手。但是我无法通过搜索互联网几天来找到我简单任务的答案。
我有一个二维数组,它在第一维中有 4 个双精度。我想矢量化这 4 个元素。如果我在下面的代码中使用静态数组 gas[32768][4],并使用
g++ -O2 -ftree-vectorize -ftree-vectorizer-verbose=7 stack.cpp
它显示
vect_model_load_cost:对齐。
对于底部 K 循环。但是,如果我使用如下所示的动态数组,它会显示
矢量化未对齐的访问
但是,我认为我的动态数组确实是对齐的。我想知道如何强制编译器知道数组确实对齐了。
我也尝试了结构数组而不是二维数组。同样,如果它是一个静态数组,则可以。但是,如果它是使用>new 动态分配的结构数组,则编译器将无法识别对齐方式。
#include <iostream>
#include <time.h>
#include <new>
#include <malloc.h>
#include <stdio.h>
int main()
{
clock_t t;
double temp[4];
int ng=32768;
int i, j, k;
double **gas;
gas = (double **)memalign(__BIGGEST_ALIGNMENT__,32768*sizeof(double*));
gas[0] = (double *)memalign(__BIGGEST_ALIGNMENT__,32768*4*sizeof(double));
for (i=0; i<32768; i++){
gas[i] = (double *)((unsigned char *)gas[0] + i*4*sizeof(double));
}
/*
replace above 7 lines with static assignment: double gas[32768][4]; then the compiler recoganize that the data are aligned
*/
for (i=0; i<ng; i++){
for (k=0; k<4; k++){
gas[i][k]=i*1.0;
}
}
for (j=0; j<10000; j++){
for (k=0; k<4; k++){
temp[k]=gas[j][k];
temp[k]+=gas[j+1][k];
temp[k]+=gas[j+2][k];
temp[k]+=gas[j+13][k];
temp[k]+=gas[j+14][k];
temp[k]+=gas[j+15][k];
temp[k]+=gas[j+16][k];
}
}
std::cout<<temp[0]<<" "<<temp[1]<<" "<<temp[2]<<" "<<temp[3]<<std::endl;
}
如果你使用__builtin_assume_aligned它有效吗?请参阅 https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html 。您可以使用它来告诉编译器您的指针已充分对齐。例如类似的东西
double *tmp = memalign (16, some_size);
double *ptr = __builtin_assume_aligned(tmp, 16);
// Now *ptr points to tmp and the compiler should be able to assume that *ptr
// is 16 byte aligned.
相关文章:
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- 64位机器上的C++内存对齐
- 为什么我可以将变量存储在不是其最小对齐方式的倍数的地址?
- 使 std::vector 分配对齐内存的现代方法
- gcc 是否在 2 条短裤的过度对齐结构的比较中错过了优化机会?
- gcc 中的多个对齐属性是否可以用于保证缓存行分隔?
- gcc矢量扩展中未对齐的加载/存储
- GCC 生成了未对齐的"ldrd"指令
- 在使用GCC时,如何使用__Align__中的大小进行缓存对齐
- gcc是否保证对volatile整数的对齐访问是原子访问
- 动态对齐的多维数组不被 GCC 编译器视为对齐
- AVX矢量化代码中的分段故障,GCC __attribute__对齐为32字节
- 是否可以强制GCC为内存对齐的对象生成高效的构造函数
- GCC/Clang中内存对齐的通用属性
- 针对Intel x64的GCC中指向整型的指针的对齐
- gcc x86 Windows堆栈对齐
- GCC中的结构对齐(应该在typedef中指定对齐吗?)
- GCC中的结构成员对齐