【发布时间】:2016-08-26 06:37:05
【问题描述】:
我正在使用英特尔 IPP 进行 2 个图像(数组)的乘法运算。
我正在使用 Intel Composer 2015 Update 6 附带的 Intel IPP 8.2。
我创建了一个简单的函数来放大太大的图像(整个项目附后,见下文)。
我想看看使用英特尔 IPP 多线程库的好处。
这是一个简单的项目(我还附上了完整的项目表格Visual Studio):
#include "ippi.h"
#include "ippcore.h"
#include "ipps.h"
#include "ippcv.h"
#include "ippcc.h"
#include "ippvm.h"
#include <ctime>
#include <iostream>
using namespace std;
const int height = 6000;
const int width = 6000;
Ipp32f mInput_image [1 * width * height];
Ipp32f mOutput_image[1 * width * height] = {0};
int main()
{
IppiSize size = {width, height};
double start = clock();
for (int i = 0; i < 200; i++)
ippiMul_32f_C1R(mInput_image, 6000 * 4, mInput_image, 6000 * 4, mOutput_image, 6000 * 4, size);
double end = clock();
double douration = (end - start) / static_cast<double>(CLOCKS_PER_SEC);
cout << douration << endl;
cin.get();
return 0;
}
我曾使用英特尔 IPP 单线程和英特尔 IPP 多线程编译过这个项目。
我尝试了不同大小的数组,但在所有这些数组中,多线程版本都没有收益(有时甚至更慢)。
我想知道,为什么多线程在这个任务中没有任何收获?
我知道英特尔 IPP 使用 AVX,我想也许任务变成了内存受限?
我尝试了另一种方法,手动使用 OpenMP 来使用英特尔 IPP 单线程实现的多线程方法。
这是代码:
#include "ippi.h"
#include "ippcore.h"
#include "ipps.h"
#include "ippcv.h"
#include "ippcc.h"
#include "ippvm.h"
#include <ctime>
#include <iostream>
using namespace std;
#include <omp.h>
const int height = 5000;
const int width = 5000;
Ipp32f mInput_image [1 * width * height];
Ipp32f mOutput_image[1 * width * height] = {0};
int main()
{
IppiSize size = {width, height};
double start = clock();
IppiSize blockSize = {width, height / 4};
const int NUM_BLOCK = 4;
omp_set_num_threads(NUM_BLOCK);
Ipp32f* in;
Ipp32f* out;
// ippiMul_32f_C1R(mInput_image, width * 4, mInput_image, width * 4, mOutput_image, width * 4, size);
#pragma omp parallel \
shared(mInput_image, mOutput_image, blockSize) \
private(in, out)
{
int id = omp_get_thread_num();
int step = blockSize.width * blockSize.height * id;
in = mInput_image + step;
out = mOutput_image + step;
ippiMul_32f_C1R(in, width * 4, in, width * 4, out, width * 4, blockSize);
}
double end = clock();
double douration = (end - start) / static_cast<double>(CLOCKS_PER_SEC);
cout << douration << endl;
cin.get();
return 0;
}
结果是一样的,同样没有性能提升。
有没有办法在这种任务中从多线程中受益?
如何验证任务是否成为内存受限的,因此并行化它没有好处?
将 CPU 上的 2 个数组与 AVX 相乘的任务并行化是否有好处?
我试用的计算机基于 Core i7 4770k (Haswell)。
这是Project in Visual Studio 2013的链接。
谢谢。
【问题讨论】:
-
你使用什么编译选项?您应该使用
/O2 /openmp或/O2 /openmp /arch:AVX2。 -
这并不重要,因为英特尔 IPP 是使用汇编和 CPU 调度构建的。但它是 /O2 /OpenMP。谢谢。
标签: c++ multithreading openmp intel-ipp