gpt4 book ai didi

c++ - Multi2sim v4.0.1 上简单 OpenMP 程序的奇怪输出

转载 作者:行者123 更新时间:2023-11-28 07:37:33 25 4
gpt4 key购买 nike

我正在尝试使用 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

多核内存配置:

[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 上运行要花这么多时间?

如有任何帮助,我们将不胜感激。

最佳答案

我真的不知道 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++;
}

看看您是否可以获得所需的行为。

关于c++ - Multi2sim v4.0.1 上简单 OpenMP 程序的奇怪输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16446956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com