-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.cpp
140 lines (113 loc) · 2.46 KB
/
MergeSort.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <omp.h>
#define SWAP(a,b) {int temp = a; a = b; b = temp;}
#define SIZE 1048576
void setUp(int a[], int size);
void tearDown(double start, double end, int a[], int size);
void merge(int a[], int size, int temp[]);
void mergesort_serial(int a[], int size, int temp[]);
void mergesort_parallel_omp
(int a[], int size, int temp[], int threads);
int main() {
int a[SIZE];
int temp[SIZE];
double startTime, endTime;
int num_threads;
#pragma omp parallel
{
#pragma omp master
{
num_threads = omp_get_num_threads();
}
}
setUp(a, SIZE);
startTime = omp_get_wtime();
mergesort_parallel_omp(a, SIZE, temp, num_threads);
endTime = omp_get_wtime();
tearDown(startTime, endTime, a, SIZE);
}
void setUp(int a[], int size){
int i;
srand(time(NULL));
for (i = 0; i<size; ++i) {
a[i] = rand() % size;
}
return;
}
void tearDown(double start, double end, int a[], int size) {
int sorted = 1;
int i;
printf("Time to execute: %f\n", end-start);
for (i = 0; i < size-1; ++i) {
sorted &= (a[i] <= a[i+1]);
}
printf("Array sorted: %d\n", sorted);
#pragma omp parallel
{
#pragma omp master
{
printf("Num threads: %d\n", omp_get_num_threads());
}
}
}
void merge(int a[], int size, int temp[]) {
int i1 = 0;
int i2 = size / 2;
int it = 0;
while(i1 < size/2 && i2 < size) {
if (a[i1] <= a[i2]) {
temp[it] = a[i1];
i1 += 1;
}
else {
temp[it] = a[i2];
i2 += 1;
}
it += 1;
}
while (i1 < size/2) {
temp[it] = a[i1];
i1++;
it++;
}
while (i2 < size) {
temp[it] = a[i2];
i2++;
it++;
}
memcpy(a, temp, size*sizeof(int));
}
void mergesort_serial(int a[], int size, int temp[]) {
int i;
if (size == 2) {
if (a[0] <= a[1])
return;
else {
SWAP(a[0], a[1]);
return;
}
}
mergesort_serial(a, size/2, temp);
mergesort_serial(a + size/2, size - size/2, temp);
merge(a, size, temp);
}
void mergesort_parallel_omp
(int a[], int size, int temp[], int threads) {
if ( threads == 1) {
mergesort_serial(a, size, temp);
}
else if (threads > 1) {
#pragma omp parallel sections
{
#pragma omp section
mergesort_parallel_omp(a, size/2, temp, threads/2);
#pragma omp section
mergesort_parallel_omp(a + size/2, size-size/2,
temp + size/2, threads-threads/2);
}
merge(a, size, temp);
} // threads > 1
}