- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的项目中,我必须做几个 vector 乘法,在 double *a
-vectors 或 float *a
-vectors 上完成。为了加快速度,我想使用 SIMD 操作或 omp
。为了获得最快的结果,我写了一个基准程序:
#include <iostream>
#include <memory>
#include <vector>
#include <omp.h>
#include <immintrin.h>
#include <stdlib.h>
#include <chrono>
#define SIZE 32768
#define ROUNDS 1e5
void multiply_singular(float *a, float *b, float *d)
{
for(int i = 0; i < SIZE; i++)
d[i] = a[i]*b[i];
}
void multiply_omp(float *a, float *b, float *d)
{
#pragma omp parallel for
for(int i = 0; i < SIZE; i++)
d[i] = a[i]*b[i];
}
void multiply_avx(float *a, float *b, float *d)
{
__m256 a_a, b_a, c_a;
for(int i = 0; i < SIZE/8; i++)
{
a_a = _mm256_loadu_ps(a+8*i);
b_a = _mm256_loadu_ps(b+8*i);
c_a = _mm256_mul_ps(a_a, b_a);
_mm256_storeu_ps (d+i*8, c_a);
}
}
void multiply_avx_omp(float *a, float *b, float *d)
{
__m256 a_a, b_a, c_a;
#pragma omp for
for(int i = 0; i < SIZE/8; i++)
{
a_a = _mm256_loadu_ps(a+8*i);
b_a = _mm256_loadu_ps(b+8*i);
c_a = _mm256_mul_ps(a_a, b_a);
_mm256_storeu_ps (d+i*8, c_a);
}
}
void multiply_singular_double(double *a, double *b, double *d)
{
for(int i = 0; i < SIZE; i++)
d[i] = a[i]*b[i];
}
void multiply_omp_double(double *a, double *b, double *d)
{
#pragma omp parallel for
for(int i = 0; i < SIZE; i++)
d[i] = a[i]*b[i];
}
void multiply_avx_double(double *a, double *b, double *d)
{
__m256d a_a, b_a, c_a;
for(int i = 0; i < SIZE/4; i++)
{
a_a = _mm256_loadu_pd(a+4*i);
b_a = _mm256_loadu_pd(b+4*i);
c_a = _mm256_mul_pd(a_a, b_a);
_mm256_storeu_pd (d+i*4, c_a);
}
}
void multiply_avx_double_omp(double *a, double *b, double *d)
{
__m256d a_a, b_a, c_a;
#pragma omp parallel for
for(int i = 0; i < SIZE/4; i++)
{
a_a = _mm256_loadu_pd(a+4*i);
b_a = _mm256_loadu_pd(b+4*i);
c_a = _mm256_mul_pd(a_a, b_a);
_mm256_storeu_pd (d+i*4, c_a);
}
}
int main()
{
float *a, *b, *c, *d, *e, *f;
double *a_d, *b_d, *c_d, *d_d, *e_d, *f_d;
a = new float[SIZE] {0};
b = new float[SIZE] {0};
c = new float[SIZE] {0};
d = new float[SIZE] {0};
e = new float[SIZE] {0};
f = new float[SIZE] {0};
a_d = new double[SIZE] {0};
b_d = new double[SIZE] {0};
c_d = new double[SIZE] {0};
d_d = new double[SIZE] {0};
e_d = new double[SIZE] {0};
f_d = new double[SIZE] {0};
for(int i = 0; i < SIZE; i++)
{
a[i] = i;
b[i] = i;
a_d[i] = i;
b_d[i] = i;
};
std::cout << "Now doing the single float rounds!\n";
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
for(int i = 0; i < ROUNDS; i++)
{
multiply_singular(a, b, c);
}
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
auto duration_ss = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Now doing the omp float rounds!\n";
t1 = std::chrono::high_resolution_clock::now();
for(int i = 0; i < ROUNDS*10; i++)
{
multiply_omp(a, b, d);
};
t2 = std::chrono::high_resolution_clock::now();
auto duration_so = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Now doing the avx float rounds!\n";
t1 = std::chrono::high_resolution_clock::now();
for(int i = 0; i < ROUNDS*10; i++)
{
multiply_avx(a, b, e);
};
t2 = std::chrono::high_resolution_clock::now();
auto duration_sa = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Now doing the avx omp float rounds!\n";
t1 = std::chrono::high_resolution_clock::now();
for(int i = 0; i < ROUNDS*10; i++)
{
multiply_avx_omp(a, b, e);
};
t2 = std::chrono::high_resolution_clock::now();
auto duration_sao = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Now doing the single double rounds!\n";
t1 = std::chrono::high_resolution_clock::now();
for(int i = 0; i < ROUNDS; i++)
{
multiply_singular_double(a_d, b_d, c_d);
};
t2 = std::chrono::high_resolution_clock::now();
auto duration_ds = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Now doing the omp double rounds!\n";
t1 = std::chrono::high_resolution_clock::now();
for(int i = 0; i < ROUNDS*10; i++)
{
multiply_omp_double(a_d, b_d, d_d);
};
t2 = std::chrono::high_resolution_clock::now();
auto duration_do = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Now doing the avx double rounds!\n";
t1 = std::chrono::high_resolution_clock::now();
for(int i = 0; i < ROUNDS*10; i++)
{
multiply_avx_double(a_d, b_d, e_d);
};
t2 = std::chrono::high_resolution_clock::now();
auto duration_da = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Now doing the avx omp double rounds!\n";
t1 = std::chrono::high_resolution_clock::now();
for(int i = 0; i < ROUNDS*10; i++)
{
multiply_avx_double_omp(a_d, b_d, f_d);
};
t2 = std::chrono::high_resolution_clock::now();
auto duration_dao = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Finished\n";
std::cout << "Elapsed time for functions:\n";
std::cout << "Function\ttime[ms]\n";
std::cout << "Singular float:\t" << duration_ss/ROUNDS << '\n';
std::cout << "OMP float:\t" << duration_so/(ROUNDS*10) << '\n';
std::cout << "AVX float avx:\t" << duration_sa/(ROUNDS*10) << '\n';
std::cout << "OMP AVX float avx omp:\t" << duration_sao/(ROUNDS*10) << '\n';
std::cout << "Singular double:\t" << duration_ds/ROUNDS << '\n';
std::cout << "OMP double:\t" << duration_do/(ROUNDS*10) << '\n';
std::cout << "AVX double:\t" << duration_da/(ROUNDS*10) << '\n';
std::cout << "OMP AVX double:\t" << duration_dao/(ROUNDS*10) << '\n';
delete[] a;
delete[] b;
delete[] c;
delete[] d;
delete[] e;
delete[] f;
delete[] a_d;
delete[] b_d;
delete[] c_d;
delete[] d_d;
delete[] e_d;
delete[] f_d;
return 0;
}
当使用 g++-5 -fopenmp -std=c++14 -march=native test_new.cpp -o test -lgomp
编译时,我得到了
Elapsed time for functions:
Function time[ms]
Singular float: 117.979
OMP float: 40.5385
AVX float avx: 60.2964
OMP AVX float avx omp: 61.4206
Singular double: 129.59
OMP double: 200.745
AVX double: 136.715
OMP AVX double: 122.176
或在第二次运行中
Elapsed time for functions:
Function time[ms]
Singular float: 113.932
OMP float: 39.2581
AVX float avx: 58.3029
OMP AVX float avx omp: 60.0023
Singular double: 123.575
OMP double: 66.0327
AVX double: 124.293
OMP AVX double: 318.038
这里显然纯 omp
函数比其他函数更快,甚至与 AVX 函数一样。将 -O3
-switch 添加到编译行时,我得到以下结果:
Elapsed time for functions:
Function time[ms]
Singular float: 12.7361
OMP float: 4.82436
AVX float avx: 14.7514
OMP AVX float avx omp: 14.7225
Singular double: 27.9976
OMP double: 8.50957
AVX double: 32.5175
OMP AVX double: 257.219
这里 omp
比其他任何方法都快得多,而 AVX 最慢,甚至比线性方法慢。这是为什么?是我的 AVX 函数实现很糟糕,还是有其他问题?
在 Ubuntu 14.04.1、i7 Sandy Bridge、gcc 版本 5.3.0 上执行。
编辑:我发现了一个错误:我应该将 avx
函数中的临时变量声明移到 for 循环内,这让我接近 omp
-级别(并提供正确的结果)。
编辑 2:当禁用 -O3
-switch 时,OMP
-AVX
-指令比 OMP
-functions,通过 switch 它们几乎是一样的。
编辑 3:每次在执行下一个循环之前用随机数据填充数组时,我得到(使用 -O3
):
Elapsed time for functions:
Function time[ms]
Singular float: 30.742
Cilk float: 24.0769
OMP float: 17.2415
AVX float avx: 33.0217
OMP AVX float avx omp: 10.1934
Singular double: 60.412
Cilk double: 34.6458
OMP double: 19.0739
AVX double: 66.8676
OMP AVX double: 22.3586
没有:
Elapsed time for functions:
Function time[ms]
Singular float: 274.402
Cilk float: 88.258
OMP float: 66.2124
AVX float avx: 117.066
OMP AVX float avx omp: 35.0313
Singular double: 238.652
Cilk double: 91.1667
OMP double: 127.621
AVX double: 249.516
OMP AVX double: 116.24
(我也添加了一个 cilk_for() 循环用于比较)。
更新:我还添加了(如答案中所建议的)使用 #pragma omp parallel for simd
的函数。结果是:
Elapsed time for functions:
Function time[ms]
Singular float: 106.081
Cilk float: 33.2761
OMP float: 17.0651
AVX float avx: 65.1129
OMP AVX float: 19.1496
SIMD OMP float: 2.6095
Aligned AVX OMP float: 18.1165
Singular double: 118.939
Cilk double: 53.1102
OMP double: 35.652
AVX double: 131.24
OMP AVX double: 39.4377
SIMD OMP double: 7.0748
Aligned AVX OMP double: 38.4474
最佳答案
对于支持 OpenMP4.x 的编译器,您可能希望从以下内容开始:
void multiply_singular_omp_for_simd(float *a, float *b, float *d)
{
#pragma omp parallel for simd schedule (static,16)
for(int i = 0; i < SIZE; i++)
d[i] = a[i]*b[i];
}
它将为您提供 SIMD 和线程并行性。并行分解将自动完成,首先将并行任务/ block 分布在线程/内核中,其次对于每个任务/ block ,将单个迭代“跨”simd“ channel ”分布。
如果您感到担心,请阅读给定的几篇文章: Threading and SIMD in OpenMP4 , ICC documentation .
从形式上讲,您表达问题的方式有点模棱两可,因为从 4.0 开始,OMP 循环可能是 SIMD、Threading 或 SIMD+Threading parallel。所以这不再是关于 OMP 与 SIMD 的问题了。相反,它是关于 OMP SIMD 与 OMP 线程的对比。
不确定您给定的 GCC 实现有多好,但 ICC/IFORT 现在可以在相对较长的时间内为 simd 处理 omp parallel。 GCC 也应该从 5.x 开始支持它(#pragma omp simd 被 GCC 支持了一段时间,但是对于 #pragma omp parallel for simd 来说没有必要)。
为了获得最佳的编译器驱动实现,您可能更愿意进行高速缓存阻塞并手动拆分迭代空间,以使外层循环由 omp parallel for 驱动,而最内层循环由 omp simd 驱动。但这可能稍微超出了原始问题的范围。
关于c++ - vector 乘法中的 SIMD 与 OMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37430964/
我有这个析构函数,它在运行时产生错误“vector 迭代器不可取消引用”。 gridMatrix 是一个 std::vector * > * > * > * > 我添加了 typename 和 typ
我有一个 vector 的 vector ,比方说 std::vector > my2dArray; 现在我想要一个 vector ,其中包含 my2dArray 中 vector 的大小。手动这看起
假设我有一些 vector :v1、v2、v3 假设我还有一个 vector 来保存这些 vList = {v1, v2, v3} 如果我同步了 (vList),这是否意味着 v1、v2 和 v3 也
我正在创建一个 char 的二维 vector 数组作为类变量,但我在将 vector 添加到 vector 数组中时遇到了麻烦。 我正在使用 C++ 11 标准运行 gcc。 我尝试使用 vecto
如何修改 Vec基于 Vec 中某项的信息没有对向量的不可变和可变引用? 我已尝试创建一个最小示例来演示我的特定问题。在我的真实代码中,Builder struct 已经是其他答案提出的中间结构。具体
这个问题在这里已经有了答案: What is the idiomatic Rust way to copy/clone a vector in a parameterized function? (
在我的程序中,我有一个整数 vector 的 vector 。现在我想从 vector 的 vector 中取出一个 vector 并在另一个 vector 容器中对其进行操作,但是我得到了错误...
我得到一个vector>数据由 OpenCV 提供。由于某些原因(例如偏移/缩放),我需要转换数据 Point至Point2f 。我怎样才能做到这一点? 例如: std::vector > conto
我有一个函数,该函数应使用来自字符串类型的给定 vector vector 中的某些元素初始化来自字符串类型的空 vector vector 。我的语法看起来像这样 std::vector> extr
我得到一个vector>数据由 OpenCV 提供。由于某些原因(例如偏移/缩放),我需要转换数据 Point至Point2f 。我怎样才能做到这一点? 例如: std::vector > conto
这里有很多类似的问题,但我没有真正找到任何可以特别回答我的问题的问题。 我有一个 vector 的 vector 作为类的属性。另一个属性是 bucket_count。我想将 vector 的 vec
如果我像这样创建一个 vector 的 vector : std::vector> myVectorOfVectors; 然后用一些东西填充它: std::vector myVector1; myVe
我正在用 C++ 编写自定义 vector 类。我对这样的代码有疑问: vector vec; vec.push_back(one); vec.push_back(two);
这是我发布的问题 c++ program for reading an unknown size csv file (filled only with floats) with constant (b
vector> a; for (int i=0;i v(i+1); iota(v.begin(),v.end(),1); a.push_back(v); } a.erase(a.beg
也许已经晚了,但我不明白为什么我会得到一个超出此代码范围的 vector 下标: int m = 3; int n = 2; std::vector> path(m, std::vector(n, 0
这个问题真的很奇怪,我似乎找不到任何导致它的原因。 所以这里有一个赋值运算符重载函数,鸟类和哺乳动物都是 vector 。 (下面是类) const Register& Register::opera
我怎么去 std::vector> 只是 std::vector> ?有真正有效的方法吗? 最佳答案 我会做这样的事情: #include #include int main() { //
我正在尝试将这些 vector 中的一些数据写入文本文件。当我运行代码时,它返回运行时错误。 Category、Product、Cart、Customer和Address都是struct 包含每个 g
显然它会因您使用的编译器而异,但我很好奇执行 vector> 时的性能问题与 vector*> ,尤其是在 C++ 中。具体来说: 假设您的外部 vector 已满,您想要开始将元素插入到第一个内部
我是一名优秀的程序员,十分优秀!