Por que std :: fill (0) é mais lento que std :: fill (1)?
Eu observei em um sistema questd::fill
em um grandestd::vector<int>
foi significativamente e consistentemente mais lento ao definir um valor constante0
comparado a um valor constante1
ou um valor dinâmico:
5,8 GiB / s vs 7,5 GiB / s
No entanto, os resultados são diferentes para tamanhos de dados menores, ondefill(0)
é mais rápido
Com mais de um segmento, no tamanho de dados de 4 GiB,fill(1)
mostra uma inclinação mais alta, mas atinge um pico muito menor do quefill(0)
(51 GiB / s vs 90 GiB / s):
Isto levanta a questão secundária: por que o pico de largura de banda defill(1)
é muito menor.
O sistema de teste para isso era um processador Intel Xeon E5-2680 v3 de soquete duplo, definido em 2,5 GHz (via/sys/cpufreq
) com 8x16 GiB DDR4-2133. Eu testei com o GCC 6.1.0 -O3
) e o compilador Intel 17.0.1 -fast
), ambos obtêm resultados idênticos.GOMP_CPU_AFFINITY=0,12,1,13,2,14,3,15,4,16,5,17,6,18,7,19,8,20,9,21,10,22,11,23
foi configurado. Strem / add / 24 threads obtém 85 GiB / s no sistem
Consegui reproduzir esse efeito em um sistema de servidor de soquete duplo Haswell diferente, mas não em nenhuma outra arquitetura. Por exemplo, no Sandy Bridge EP, o desempenho da memória é idêntico, enquanto no cachefill(0)
é muito mais rápido.
Aqui está o código para reproduzir:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <omp.h>
#include <vector>
using value = int;
using vector = std::vector<value>;
constexpr size_t write_size = 8ll * 1024 * 1024 * 1024;
constexpr size_t max_data_size = 4ll * 1024 * 1024 * 1024;
void __attribute__((noinline)) fill0(vector& v) {
std::fill(v.begin(), v.end(), 0);
}
void __attribute__((noinline)) fill1(vector& v) {
std::fill(v.begin(), v.end(), 1);
}
void bench(size_t data_size, int nthreads) {
#pragma omp parallel num_threads(nthreads)
{
vector v(data_size / (sizeof(value) * nthreads));
auto repeat = write_size / data_size;
#pragma omp barrier
auto t0 = omp_get_wtime();
for (auto r = 0; r < repeat; r++)
fill0(v);
#pragma omp barrier
auto t1 = omp_get_wtime();
for (auto r = 0; r < repeat; r++)
fill1(v);
#pragma omp barrier
auto t2 = omp_get_wtime();
#pragma omp master
std::cout << data_size << ", " << nthreads << ", " << write_size / (t1 - t0) << ", "
<< write_size / (t2 - t1) << "\n";
}
}
int main(int argc, const char* argv[]) {
std::cout << "size,nthreads,fill0,fill1\n";
for (size_t bytes = 1024; bytes <= max_data_size; bytes *= 2) {
bench(bytes, 1);
}
for (size_t bytes = 1024; bytes <= max_data_size; bytes *= 2) {
bench(bytes, omp_get_max_threads());
}
for (int nthreads = 1; nthreads <= omp_get_max_threads(); nthreads++) {
bench(max_data_size, nthreads);
}
}
Resultados apresentados compilados comg++ fillbench.cpp -O3 -o fillbench_gcc -fopenmp
.