- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试将综合测试中的阵列对齐从 16 增加到 32,性能从 ~4100ms 降低到 ~4600ms。更高的对齐度会对性能造成多大的损害?
下面是我用于测试的代码(我在这里尝试使用 avx 指令)。使用 g++ test.cpp -O2 -ftree-vectorize -mavx2
构建(我不支持 avx512)。
#include <chrono>
#include <iostream>
#include <memory>
#include <cassert>
#include <cstring>
#include <cstdlib>
using Time = std::chrono::time_point<std::chrono::system_clock>;
using Clock = std::chrono::system_clock;
template <typename Duration>
auto as_ms(Duration const& duration) {
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
}
static const int repeats = 10000;
struct I {
static const int size = 524288;
int* pos;
I() : pos(new int[size]) { for (int i = 0; i != size; ++i) { pos[i] = i; } }
~I() { delete pos; }
};
static const int align = 16; // try to change here 16 (4100 ms) / 32 (4600 ms)
struct S {
static const int size = I::size;
alignas(align) float data[size];
S() { for (int i = 0; i != size; ++i) { data[i] = (i * 7 + 11) % 2; } }
};
void foo(const I& p, S& a, S& b) {
const int chunk = 32;
alignas(align) float aprev[chunk];
alignas(align) float anext[chunk];
alignas(align) float bprev[chunk];
alignas(align) float bnext[chunk];
const int N = S::size / chunk;
for (int j = 0; j != repeats; ++j) {
for (int i = 1; i != N-1; i++) {
int ind = p.pos[i] * chunk;
std::memcpy(aprev, &a.data[ind-1], sizeof(float) * chunk);
std::memcpy(anext, &a.data[ind+1], sizeof(float) * chunk);
std::memcpy(bprev, &b.data[ind-1], sizeof(float) * chunk);
std::memcpy(bnext, &b.data[ind+1], sizeof(float) * chunk);
for (int k = 0; k < chunk; ++k) {
int ind0 = ind + k;
a.data[ind0] = (b.data[ind0] - 1.0f) * aprev[k] * a.data[ind0] * bnext[k] + a.data[ind0] * anext[k] * (bprev[k] - 1.0f);
}
}
}
}
int main() {
S a, b;
I p;
Time start = Clock::now();
foo(p, a, b);
Time end = Clock::now();
std::cout << as_ms(end - start).count() << std::endl;
float sum = 0;
for (int i = 0; i != S::size; ++i) {
sum += a.data[i];
}
return sum;
}
检查缓存是否导致问题:
valgrind --tool=cachegrind ./a.out
对齐 = 16:
==4352== I refs: 3,905,614,100
==4352== I1 misses: 1,626
==4352== LLi misses: 1,579
==4352== I1 miss rate: 0.00%
==4352== LLi miss rate: 0.00%
==4352==
==4352== D refs: 2,049,454,623 (1,393,712,296 rd + 655,742,327 wr)
==4352== D1 misses: 66,707,929 ( 66,606,998 rd + 100,931 wr)
==4352== LLd misses: 66,681,897 ( 66,581,942 rd + 99,955 wr)
==4352== D1 miss rate: 3.3% ( 4.8% + 0.0% )
==4352== LLd miss rate: 3.3% ( 4.8% + 0.0% )
==4352==
==4352== LL refs: 66,709,555 ( 66,608,624 rd + 100,931 wr)
==4352== LL misses: 66,683,476 ( 66,583,521 rd + 99,955 wr)
==4352== LL miss rate: 1.1% ( 1.3% + 0.0% )
对齐 = 32
==4426== I refs: 2,857,165,049
==4426== I1 misses: 1,604
==4426== LLi misses: 1,560
==4426== I1 miss rate: 0.00%
==4426== LLi miss rate: 0.00%
==4426==
==4426== D refs: 1,558,058,149 (967,779,295 rd + 590,278,854 wr)
==4426== D1 misses: 66,706,930 ( 66,605,998 rd + 100,932 wr)
==4426== LLd misses: 66,680,898 ( 66,580,942 rd + 99,956 wr)
==4426== D1 miss rate: 4.3% ( 6.9% + 0.0% )
==4426== LLd miss rate: 4.3% ( 6.9% + 0.0% )
==4426==
==4426== LL refs: 66,708,534 ( 66,607,602 rd + 100,932 wr)
==4426== LL misses: 66,682,458 ( 66,582,502 rd + 99,956 wr)
==4426== LL miss rate: 1.5% ( 1.7% + 0.0% )
似乎问题不在缓存中。
检查该问题不在 Turbo Boost 中。
对齐:16 --> 32
启用 Turbo Boost :~4100ms --> ~4600ms
Turbo Boost 禁用:~5000ms --> ~5400ms
最佳答案
不是答案,但我在 Haswell E5-2680v3 上使用 GNU g++ 6.4.0 和 Intel icpc 18.0.1 进行了一些测量。所有时间都非常一致,只有几毫秒的偏差:
g++ -O2 -mavx2 -ftree-vectorize
align=16
: 6.99 [s]g++ -O2 -mavx2 -ftree-vectorize
align=32
: 6.67 [s]g++ -O3 -mavx2 -ftree-vectorize
align=16
: 6.72 [s]g++ -O3 -mavx2 -ftree-vectorize
align=32
: 6.60 [s]g++ -O2 -march=haswell -ftree-vectorize
align=16
: 6.45 [s]g++ -O2 -march=haswell -ftree-vectorize
align=32
: 6.45 [s]g++ -O3 -march=haswell -ftree-vectorize
align=16
: 6.44 [s]g++ -O3 -march=haswell -ftree-vectorize
align=32
: 6.44 [s]icpc -O2 -xCORE-AVX2
align=16
: 3.67 [s]icpc -O2 -xCORE-AVX2
align=32
: 3.51 [s]icpc -O3 -xCORE-AVX2
align=16
: 3.65 [s]icpc -O3 -xCORE-AVX2
align=32
: 3.59 [s]结论:
-march=haswell
比 -mavx2
更快,对于 -march=haswell
,实际上对优化级别和对齐方式。根据我的经验,ICC 通常比 GCC 好得多,至少在矢量化方面是这样。我没有研究生成的机器代码,但可以使用 Godbolt 等来了解为什么 ICC 在这种情况下如此出色。
关于c++ - 为什么增加阵列对齐会降低性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49403617/
我需要从 1024 增加 FD_SETSIZE 值至 4096 .我知道最好使用 poll()/epoll()但我想了解什么是优点/缺点。主要问题是:我要重新编译glibc吗? ?我读了几个线程,其中
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我在 HTML 文件中有这样的内容: var value = 0; add(x){ x++; do
有没有办法在用户向上滚动时增加变量,并在用户使用 JavaScript 向下滚动时减少变量?变量没有最大值或最小值,如果能够调整灵敏度就好了。我不知道从哪里开始,感谢您的帮助! 编辑:没有滚动条,因为
我是 ios 新手,遇到以下问题。 我想根据表格 View 中元素的数量增加和减少表格 View 的高度大小。如果在输入时客户端在输出时给出 3 个或超过 3 个元素,我希望看到一个比默认行大 2 行
所以我一直在四处搜索,似乎大多数人认为以下列方式递增 indexPath 是正确的方法: NSIndexPath *newIndexPath = [NSIndexPath indexPathForRo
我有一个关于 connSupervisionTimeout 的问题。 我正在使用 CoreBluetooth 编写应用程序。我检查了连接参数和 connSupervisionTimeout = 720
我正在尝试根据页面的滚动位置更改元素的填充;当用户向下滚动页面时,填充会增加,而当他们向上滚动时,填充会减少。 我的主要问题是滚动不是很流畅,有时如果我滚动到页面顶部太快,每次元素的填充大小都不一样。
我正在尝试计算 18456 个基因的相关性度量,但编译器 (Dev C) 在将宏 GENE 或 INDEX 增加到 4000 到 5000 之间的值后退出或大。例如,它适用于: # define GE
我有一个带有 position: absolute 和 CSS3 过渡的圆形元素(a 元素)。在 hover 事件中,我想增加圆的高度和宽度,但我想在所有边上添加像素,而不仅仅是在左侧或右侧。 示例如
为了改善用户体验,我计划在我网站的所有页面(A-、A、A+)上增加/减少/重置字体大小 我面临的问题是页面上不同元素使用的字体大小不统一。有些是 14px,有些是 18px,有些是 12px,有些是
本文实例讲述了Yii框架数据库查询、增加、删除操作。分享给大家供大家参考,具体如下: Yii 数据库查询 模型代码: ?
sql替换语句,用该命令可以整批替换某字段的内容,也可以批量在原字段内容上加上或去掉字符。 命令总解:update 表的名称 set 此表要替换的字段名=REPLACE(此表要替换的字段名, '原
sql不常用函数总结以及事务,增加,删除触发器 distinct 删除重复行 declare @x 申明一个变量 convert(varchar(20),t
要增加我使用的最大可用内存: export SPARK_MEM=1 g 或者我可以使用 val conf = new SparkConf() .setMaster("loca
我正在尝试将文本(自定义文本按钮)放入 AppBar 的前导属性中。但是,当文本太长时,文本会变成多行 Scaffold( appBar: AppBar( centerTi
我正在使用最新版本的 NetBeans,我需要增加输出和菜单的字体大小(不是代码部分)。我试过: netbeans_default_options=".... --fontsize 16" 但是当我将
我必须将 180000 个点绘制到一个 EPS 文件中。 使用标准 gnuplot 输出尺寸点彼此太接近,这使得它们无法区分。有没有办法增加图像的宽度和高度? 最佳答案 是的。 set termina
我有一个带有输入字段的 twitter bootstrap 3 导航栏。我想增加输入字段的宽度。我已尝试设置 col 大小,但它不起作用。 html比较长,请引用bootply http://www.
我正在尝试增加 ggplot 标题中下划线的大小/宽度/厚度。我曾尝试使用大小、宽度和长度,但没有成功。 这是我所做的一个例子。 test <- tibble(x = 1:5, y = 1, z =
我是一名优秀的程序员,十分优秀!