我正在尝试使用OpenMP运行一个简单的程序
程序如下
#include <iostream>
#include <fstream>
#include <vector>
#include <omp.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <string>
#include <ctime>
using namespace std;
#define NUM 10
void openMP()
{
omp_set_num_threads(1);
int sum =0;
#pragma omp parallel for shared(sum)
{
for (int i=0;i<100;i++)
{
sum++;
}
}
cout<<"sum = "<<sum<<endl;
}
int main()
{
cout<<"Open MP \n";
openMP();
return 0;
}
现在我用
编译它 g++ test.cpp -fopenmp -o test
并在ubuntu终端上运行
./test
输出是正确的 - 我认为 - 如下
Open MP
sum = 100
但是当我尝试使用Multi2sim使用这两个文件运行它时,我是由我的导师给出的
多核配置:
[ General ]
Cores = 4
Threads = 1
多核-MEM-配置:
[CacheGeometry geo-l1]
Sets = 256
Assoc = 2
BlockSize = 64
Latency = 2
Policy = LRU
Ports = 2
[CacheGeometry geo-l2]
Sets = 512
Assoc = 4
BlockSize = 64
Latency = 20
Policy = LRU
Ports = 4
[Module mod-l1-0]
Type = Cache
Geometry = geo-l1
LowNetwork = net-l1-l2
LowModules = mod-l2
[Module mod-l1-1]
Type = Cache
Geometry = geo-l1
LowNetwork = net-l1-l2
LowModules = mod-l2
[Module mod-l2]
Type = Cache
Geometry = geo-l2
HighNetwork = net-l1-l2
LowNetwork = net-l2-mm
LowModules = mod-mm
[Module mod-mm]
Type = MainMemory
BlockSize = 256
Latency = 200
HighNetwork = net-l2-mm
[Network net-l2-mm]
DefaultInputBufferSize = 1024
DefaultOutputBufferSize = 1024
DefaultBandwidth = 256
[Network net-l1-l2]
DefaultInputBufferSize = 1024
DefaultOutputBufferSize = 1024
DefaultBandwidth = 256
[Entry core-0]
Arch = x86
Core = 0
Thread = 0
DataModule = mod-l1-0
InstModule = mod-l1-0
[Entry core-1]
Arch = x86
Core = 1
Thread = 0
DataModule = mod-l1-0
InstModule = mod-l1-0
[Entry core-2]
Arch = x86
Core = 2
Thread = 0
DataModule = mod-l1-0
InstModule = mod-l1-0
[Entry core-3]
Arch = x86
Core = 3
Thread = 0
DataModule = mod-l1-0
InstModule = mod-l1-0
然后在Ubuntu终端中使用此指令
m2s --x86-config multicore-config.txt --mem-config multicore-mem-config.txt --x86-sim detailed test
我得到了输出
; Multi2Sim 4.0.1 - A Simulation Framework for CPU-GPU Heterogeneous Computing
; Please use command 'm2s --help' for a list of command-line options.
; Last compilation: May 8 2013 10:01:31
Open MP
sum = 83
;
; Simulation Statistics Summary
;
[ General ]
Time = 53.17
SimEnd = ContextsFinished
Cycles = 3691870
[ x86 ]
SimType = Detailed
Time = 53.15
Contexts = 4
Memory = 37056512
EmulatedInstructions = 3292450
EmulatedInstructionsPerSecond = 61943
Cycles = 3691558
CyclesPerSecond = 69452
FastForwardInstructions = 0
CommittedInstructions = 2081157
CommittedInstructionsPerCycle = 0.5638
CommittedMicroInstructions = 3113721
CommittedMicroInstructionsPerCycle = 0.8435
BranchPredictionAccuracy = 0.9375
为什么Multi2sim 83
中的输出正常运行时的输出为100
为什么在Multi2Sim上运行会花费这么多时间?
任何帮助都将不胜感激。
答案 0 :(得分:1)
我真的不知道m2s
,但可能是罪魁祸首是:
#pragma omp parallel for shared(sum)
{
for (int i=0;i<100;i++)
{
sum++; // Concurrent access to a shared variable!!!
}
}
在第一次测试中,您明确将线程数设置为1
:
omp_set_num_threads(1);
让您远离竞争条件。我建议尝试:
#pragma omp parallel for shared(sum) reduction(+:sum)
for (int i=0;i<100;i++) {
sum++;
}
看看你是否可以获得所需的行为。