我一直有这个想法,减少迭代次数是的方式来使程序更加高效.由于我从未真正确认过,我开始测试这个.
我制作了以下C++程序来测量两个不同函数的时间:
完整的测试代码:
#include <iostream>
#include <chrono>
using namespace std;
int* list1; int* list2;
int* list3; int* list4;
int* list5; int* list6;
int* list7; int* list8;
int* list9; int* list10;
const int n = 1e7;
// **************************************
void myFunc1()
{
for (int i = 0; i < n; i++)
{
list1[i] = 2;
list2[i] = 4;
list3[i] = 8;
list4[i] = 16;
list5[i] = 32;
list6[i] = 64;
list7[i] = 128;
list8[i] = 256;
list9[i] = …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算矩阵中相邻元素的平均值,但无法让 OpenMP 的矢量化正常工作。据我了解第二个嵌套 for 循环,该reduction子句应确保在写入 的元素时不会发生竞争条件next。但是,在编译代码时(我尝试使用 GCC GCC 7.3.0 和 ICC 以及 OpenMP > 4.5 进行自动矢量化),我收到报告:“错误:在进入此 OpenMP 编译指示时必须共享缩减变量“next””。为什么默认共享变量时会出现这种情况?shared(next)由于添加似乎没有帮助,我该如何解决这个问题?
// CODE ABOVE (...)
size_t const width = 100;
size_t const height = 100;
float * restrict next = malloc(sizeof(float)*width*height);
// INITIALIZATION OF 'next' (this works fine)
#pragma omp for simd collapse(2)
for(size_t j = 1; j < height-1; j++)
for(size_t i = 1; i < width-1; i++)
next[j*width+i] = 0.0f;
// COMPUTE AVERAGE FOR INNER …Run Code Online (Sandbox Code Playgroud)