[英]Benchmarking a pure C++ function
如何防止GCC / Clang内联和优化纯函数的多次调用?
我正在尝试对这种形式的代码进行基准测试
int __attribute__ ((noinline)) my_loop(int const* array, int len) {
// Use array to compute result.
}
我的基准代码看起来像这样:
int main() {
const int number = 2048;
// My own aligned_malloc implementation.
int* input = (int*)aligned_malloc(sizeof(int) * number, 32);
// Fill the array with some random numbers.
make_random(input, number);
const int num_runs = 10000000;
for (int i = 0; i < num_runs; i++) {
const int result = my_loop(input, number); // Call pure function.
}
// Since the program exits I don't free input.
}
不出所料,Clang似乎可以在O2(甚至在O1)将其变为无操作。
我尝试实际对实现进行基准测试的几件事是:
将中间结果累加为整数,并在最后打印结果:
const int num_runs = 10000000; uint64_t total = 0; for (int i = 0; i < num_runs; i++) { total += my_loop(input, number); // Call pure function. } printf("Total is %llu\\n", total);
不幸的是,这似乎不起作用。 Clang至少很聪明,足以意识到这是一个纯函数,并将基准转换为如下形式:
int result = my_loop(); uint64_t total = num_runs * result; printf("Total is %llu\\n", total);
在每次循环迭代结束时,使用release语义设置一个原子变量:
const int num_runs = 10000000; std::atomic<uint64_t> result_atomic(0); for (int i = 0; i < num_runs; i++) { int result = my_loop(input, number); // Call pure function. // Tried std::memory_order_release too. result_atomic.store(result, std::memory_order_seq_cst); } printf("Result is %llu\\n", result_atomic.load());
我的希望是,因为原子引入了事前happens-before
关系,所以Clang将被迫执行我的代码。 但是可悲的是,它仍然进行了上述优化,并将atomic的值设置为num_runs * result
一发子弹,而不是运行该函数的num_runs
迭代。
在每个循环的末尾设置一个volatile int并求和。
const int num_runs = 10000000; uint64_t total = 0; volatile int trigger = 0; for (int i = 0; i < num_runs; i++) { total += my_loop(input, number); // Call pure function. trigger = 1; } // If I take this printf out, Clang optimizes the code away again. printf("Total is %llu\\n", total);
这似乎可以解决问题,而我的基准测试似乎也有效。 由于多种原因,这并不理想。
根据我对C ++ 11内存模型的理解, volatile set operations
不会happens before
关系建立happens before
因此我不能确定某些编译器不会决定执行相同的num_runs * result_of_1_run
优化。
同样,这种方法似乎是不可取的,因为现在我在每次循环运行时都要设置volatile int的开销(但是很小)。
是否存在防止Clang / GCC优化此结果的规范方法。 也许有某种实用性? 如果这种理想的方法可在所有编译器上使用,则可加分。
您可以将指令直接插入到程序集中。 有时,我使用宏来拆分程序集,例如从计算和分支中分离负载。
#define GCC_SPLIT_BLOCK(str) __asm__( "//\n\t// " str "\n\t//\n" );
然后在源代码中插入
GCC_SPLIT_BLOCK(“请继续保存”)
您的职能之前和之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.