-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathaltadd_omp.cpp
More file actions
81 lines (68 loc) · 1.53 KB
/
altadd_omp.cpp
File metadata and controls
81 lines (68 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "../../utils/utils.hh"
#include "../../utils/TimeStamp.hh"
#include "../../kernel-linux/syscall/dvmesg.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <omp.h>
using namespace std;
#undef DV_KERNEL_MESG
int nthreads = num_cpu();
int nprocs = num_cpu();
void addone(void *arg){
long *p = (long *)(arg);
*p += 1;
}
void addtwo(void *arg){
long *p = (long *)(arg);
*p += 2;
}
void ompmaster(long *list, int nthreads, int count){
for(int i=0; i < count/2;i++){
#pragma omp parallel \
num_threads(nthreads) \
default(none) \
shared(list)
{
int j = omp_get_thread_num();
addone((void *)(list+j));
}
#pragma omp parallel \
num_threads(nthreads) \
default(none) \
shared(list)
{
int j = omp_get_thread_num();
addtwo((void *)(list+j));
}
}
}
int main(){
long list[nthreads];
for(int i=0; i < nthreads; i++)
list[i] = 0;
int count = (nthreads<=nprocs)?1000*1000*100:1000*100;
#ifdef DV_KERNEL_MESG
count = 6;
cout<<"tgid = "<<getpid()<<endl;
ompmaster(list, nthreads, count);
set_dvflag(400);
ompmaster(list, nthreads, count);
set_dvflag(0);
#else
TimeStamp clk;
clk.tic();
ompmaster(list, nthreads, count);
double cycles = clk.toc();
verify_dir("DBG/");
ofstream ofile("DBG/altadd_omp.txt", ios_base::app);
ofile<<endl;
ofile<<"nprocs = "<<nprocs<<endl;
ofile<<"nthreads = "<<nthreads<<endl;
ofile<<"count = "<<count<<endl;
ofile<<"cycles per parallel region = "<<cycles/count<<endl;
ofile.close();
#endif
for(int i=0; i < nthreads; i++)
cout<<list[i]<<endl;
}