c++ - Strange output for a simple OpenMP program on Multi2sim v4.0.1 -
i'm trying run simple program using openmp
the program follows
#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 shared(sum) { (int i=0;i<100;i++) { sum++; } } cout<<"sum = "<<sum<<endl; } int main() { cout<<"open mp \n"; openmp(); return 0; }
now when compile using
g++ test.cpp -fopenmp -o test
and run on ubuntu terminal
./test
the output correct - think - follows
open mp sum = 100
but when try run using multi2sim using these 2 files given instructor
multicore-config:
[ general ] cores = 4 threads = 1
multicore-mem-config:
[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
and using instruction in ubuntu terminal
m2s --x86-config multicore-config.txt --mem-config multicore-mem-config.txt --x86-sim detailed test
i output
; multi2sim 4.0.1 - simulation framework cpu-gpu heterogeneous computing ; please use command 'm2s --help' 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
why output in multi2sim 83
while output in normal run 100
also why take time run on multi2sim ?
any appreciated.
i don't know m2s
, case culprit is:
#pragma omp parallel shared(sum) { (int i=0;i<100;i++) { sum++; // concurrent access shared variable!!! } }
in first test fact explicitly set number of threads 1
:
omp_set_num_threads(1);
saves race conditions. suggest trying with:
#pragma omp parallel shared(sum) reduction(+:sum) (int i=0;i<100;i++) { sum++; }
to see if can obtain desired behavior.
Comments
Post a Comment