0

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

为什么 Multi2sim83中的输出是正常运行中的输出100

另外,为什么在 Multi2Sim 上运行需要这么长时间?

任何帮助,将不胜感激。

4

1 回答 1

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++;
}    

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

于 2013-05-08T18:56:31.160 回答