-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpar_openmp.cpp
More file actions
96 lines (78 loc) · 2.12 KB
/
par_openmp.cpp
File metadata and controls
96 lines (78 loc) · 2.12 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <omp.h>
#include "utimer.cpp"
using namespace std;
#define vk vector
#define pb push_back
typedef vk<int> vi;
const int ARG_COUNT = 3;
const int MINN = -1e5;
const int MAXX = 1e5 + 10;
/// mt19937 random number generator
int rand_generator(int mn, int mx) {
thread_local random_device rd;
thread_local mt19937 rng(rd());
thread_local uniform_int_distribution<int> uid;
return uid(rng, decltype(uid)::param_type{mn,mx});
}
/// generate random vector
vi rand_vec(int N, int nw) {
vi randArr(N);
// utimer *timer_rand = new utimer("Generate Rand Vec");
#pragma omp parallel for num_threads(nw)
for(int &x: randArr)
x = rand_generator(MINN, MAXX);
// timer_rand->~utimer();
return randArr;
}
/// print vector
void print_vec(vi &vec) {
cout << "\n";
for (auto x: vec)
cout << x << "\t";
cout << "\n\n";
}
/// Odd-Even Sort
void OddEvenSort(vi &Arr, int nw) {
//
int N = Arr.size();
vi startIndex = {0, 1}; // 0 - Even, 1 - Odd
// Even Index starts from 0, Odd Index starts from 1.
// Both will increase by 2 in each step.
utimer *timer = new utimer("Parallel Code");
while (!is_sorted(Arr.begin(), Arr.end())) {
for (int ind: startIndex)
// start ind: 0 - even or 1 - odd
#pragma omp parallel for num_threads(nw)
for(int i = ind; i < N - 1; i+=2) {
if (Arr[i] > Arr[i + 1])
swap(Arr[i], Arr[i + 1]);
}
}
timer->~utimer();
}
int main(int argc, char * argv[]) {
// Read Input
if (argc != ARG_COUNT) {
cout << "Input Error. Number of argument is wrong..\n";
exit(0);
}
int N = atoi(argv[1]);
int nw = atoi(argv[2]);
// input validation
if (N < 0 || nw == 0) {
cout << "Input Error. Invalid Input..\n";
exit(0);
}
// genereate random vector
vi Arr = rand_vec(N, nw);
// print_vec(Arr);
// Run OddEvenSort
OddEvenSort(Arr, nw);
// Print Sorted Vector
// print_vec(Arr);
return 0;
}