AVX 256-bit vectors slightly slower than scalar (~10%) for STREAM-like double add loop on huge arrays, on Xeon Gold

回眸只為那壹抹淺笑 提交于 2020-04-11 04:56:06

问题


I am new to AVX512 instruction set and I write the following code as demo.

#include <iostream>
#include <array>
#include <chrono>
#include <vector>
#include <cstring>
#include <omp.h>
#include <immintrin.h>
#include <cstdlib>

int main() {

  unsigned long m, n, k;
  m = n = k = 1 << 30;
  auto *a = static_cast<double*>(aligned_alloc(512, m*sizeof(double)));
  auto *b = static_cast<double*>(aligned_alloc(512, n*sizeof(double)));
  auto *c = static_cast<double*>(aligned_alloc(512, k*sizeof(double)));

  memset(a, 1, m * sizeof(double));
  memset(b, 1, n * sizeof(double));
  memset(c, 1, k * sizeof(double));

  std::chrono::time_point<std::chrono::system_clock> start, end;

  start = std::chrono::system_clock::now();
  for (int iter = 0; iter < 30; iter++) {
    for (unsigned long i = 0; i < n; i+=4) {
      // __m512d x1 = _mm512_load_pd(&a[i]);
      // __m512d x2 = _mm512_load_pd(&b[i]);
      // __m512d result = _mm512_add_pd(x1, x2);
      // _mm512_store_pd(&c[i], result);
      __m256d x1 = _mm256_load_pd(&a[i]);
      __m256d x2 = _mm256_load_pd(&b[i]);
      __m256d result = _mm256_add_pd(x1, x2);
      _mm256_store_pd(&c[i], result);
    }
  }
  end = std::chrono::system_clock::now();

  std::chrono::duration<double> elapsed_seconds = end - start;
  std::cout << "elapsed time: " << elapsed_seconds.count() << std::endl;

  return 0;
}

I allocate the aligned memory and use the AVX instruction set to improve the computation performance. However, after I compile and execute it as the following.

szhangcj@gpu3:~/HPC$ g++ -O2 -msse -msse2 -mavx512f -fopenmp main_avx.cpp -o avx
szhangcj@gpu3:~/HPC$ ./avx 
elapsed time: 77.8923
szhangcj@gpu3:~/HPC$ g++ -O2 main.cpp -o single
szhangcj@gpu3:~/HPC$ ./single
elapsed time: 70.0907

My single thread version just replaces the for loop part as the following.

  for (int iter = 0; iter < 30; iter++) {
    for (unsigned long i = 0; i < n; i++) {
      c[i] = a[i] + b[i];
    }
  }

I expect that the computation performance should be improved a lot. But it seems that there is no improvement at all. What is wrong with that? I also want to combine the OpenMP with AVX instruction set to further improve it.

The following information is about my server. gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)

Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              44
On-line CPU(s) list: 0-43
Thread(s) per core:  1
Core(s) per socket:  22
Socket(s):           2
NUMA node(s):        2
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Gold 6152 CPU @ 2.10GHz
Stepping:            4
CPU MHz:             1000.019
CPU max MHz:         2101.0000
CPU min MHz:         1000.0000
BogoMIPS:            4200.00
Virtualization:      VT-x
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            30976K
NUMA node0 CPU(s):   0-21
NUMA node1 CPU(s):   22-43
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke md_clear flush_l1d

来源:https://stackoverflow.com/questions/60466834/avx-256-bit-vectors-slightly-slower-than-scalar-10-for-stream-like-double-ad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!