我决定摆脱全局变量。那是您的代码,在几个地方进行了修改。
//timings.cpp
#include <sys/time.h>
#include <cstdlib>
#include <stdio.h>
#include <math.h>
#include <omp.h>
#include <unistd.h>
#define PI 3.14159265
#define large 100000
int main() {
int i;
timeval t1,t2;
double elapsedtime;
bool b=false;
double e[large];
double p[large];
omp_set_num_threads(1);
for(i=0;i<large;i++) {
e[i]=9.0;
}
/* for(i=0;i<large;i++) {
p[i]=9.0;
}*/
gettimeofday(&t1, NULL);
#pragma omp parallel for firstprivate(b) private(i) shared(e)
//#pragma omp parallel for firstprivate(b) private(e,i)
for(i=0;i<large;i++) {
if (!b)
{
printf("e[i]=%f, e address: %p, n=%d\n",e[i],&e,omp_get_thread_num());
b=true;
}
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
fmodf((exp(log((sin(e[i]*PI/180)+cos((e[i]*2)*PI/180))*10))*PI),3.0);
}
gettimeofday(&t2, NULL);
elapsedtime = (t2.tv_sec*1000000 + t2.tv_usec) - (t1.tv_sec * 1000000 + t1.tv_usec);
printf("%f ",elapsedtime/1000);
return 0;
}
我们将通过脚本“1.sh”运行它来自动测量时间,
#/bin/bash
sed -i '/parallel/ s,#,//#,g' timings.cpp
sed -i '/parallel/ s,////#,#,g' timings.cpp
g++ -O0 -fopenmp timings.cpp -o timings
> time1.txt
for loopvar in {1..10}
do
if [ "$loopvar" -eq 1 ]
then
./timings >> time1.txt;
cat time1.txt;
echo;
else
./timings | tail -1 >> time1.txt;
fi
done
echo "---------"
echo "Total time:"
echo `tail -1 time1.txt | sed s/' '/'+'/g | sed s/$/0/ | bc -li | tail -1`/`tail -1 time1.txt| wc -w | sed s/$/.0/` | bc -li | tail -1
以下是测试结果(Intel@ Core 2 Duo E8300):
1) #pragma omp parallel for firstprivate(b) private(i) shared(e)
user@comp:~ ./1.sh
Total time:
152.96380000000000000000
我们有奇怪的延迟。例如输出:
e[i]=9.000000, e address: 0x7fffb67c6960, n=0
e[i]=9.000000, e address: 0x7fffb67c6960, n=7
e[i]=9.000000, e address: 0x7fffb67c6960, n=8
//etc..
注意地址 - 所有数组都相同(因此称为shared)
2) #pragma omp parallel for firstprivate(e,b) private(i)
user@comp:~ ./1.sh
Total time:
157.48220000000000000000
我们将数据 e (firstprivate) 复制到每个线程,例如输出:
e[i]=9.000000, e address: 0x7ff93c4238e0, n=1
e[i]=9.000000, e address: 0x7ff939c1e8e0, n=6
e[i]=9.000000, e address: 0x7ff93ac208e0, n=4
3) #pragma omp parallel for firstprivate(b) private(e,i)
Total time:
123.97110000000000000000
没有数据复制,只有分配(私有使用未初始化) 例如输出:
e[i]=0.000000, e address: 0x7fca98bdb8e0, n=1
e[i]=0.000000, e address: 0x7fffa2d10090, n=0
e[i]=0.000000, e address: 0x7fca983da8e0, n=2
这里我们有不同的地址,但是所有的e值都包含内存垃圾(nills 可能是由于 mmap 内存页面预分配)。
要查看,由于复制数组,firstprivate(e) 速度较慢,让我们注释掉所有计算(带有“fmodf”的行)// #pragma omp parallel for firstprivate(b) private(i) shared(e)
Total time:
9.69700000000000000000
// #pragma omp parallel for firstprivate(e,b) private(i)
Total time:
12.83000000000000000000
// #pragma omp parallel for firstprivate(b) private(i,e)
Total time:
9.34880000000000000000
由于复制数组,Firstprivate(e) 很慢。由于计算线,Shared(e) 很慢。
使用 -O3 -ftree-vectorize 编译会略微减少共享时间:
// #pragma omp parallel for firstprivate(b) private(i) shared(e)
user@comp:~ ./1.sh
Total time:
141.38330000000000000000
// #pragma omp parallel for firstprivate(b) private(e,i)
Total time:
121.80390000000000000000
使用 schedule(static, 256) 并没有成功。
让我们继续打开 -O0 选项。注释掉数组填充: // e[i]=9.0;
// #pragma omp parallel for firstprivate(b) private(i) shared(e)
Total time:
121.40780000000000000000
// #pragma omp parallel for firstprivate(b) private(e,i)
Total time:
122.33990000000000000000
因此,“共享”速度较慢,因为“私有”数据未初始化(如评论者所建议的那样)。
让我们看看对线程数的依赖:
4threads
shared
Total time:
156.95030000000000000000
private
Total time:
121.11390000000000000000
2threads
shared
Total time:
155.96970000000000000000
private
Total time:
126.62130000000000000000
1thread (perfomance goes down ca. twice, I have 2-core machine)
shared
Total time:
283.06280000000000000000
private
Total time:
229.37680000000000000000
为了用 1.sh 编译它,我手动取消了两个“parallel for”行,以便给 1.sh 注释掉它们。
**1thread without parallel, initialized e[i]**
Total time:
281.22040000000000000000
**1thread without parallel, uninitialized e[i]**
Total time:
231.66060000000000000000
所以,这不是 OpenMP 问题,而是内存/缓存使用问题。生成asm代码
g++ -O0 -S timings.cpp
在这两种情况下都有两个不同之处:一个是可以忽略的,在标签 LC 计数中,另一个标签(L3)在初始化e数组时不包含 1,而是包含 5 个 asm 行:
L3:
movl -800060(%rbp), %eax
movslq %eax, %rdx
movabsq $4621256167635550208, %rax
movq %rax, -800016(%rbp,%rdx,8)
(初始化发生的地方)和公共线路:
addl $1, -800060(%rbp)
所以,这似乎是缓存问题。
这不是答案,您可以使用上面的代码进一步研究问题,