-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenmp_3.cpp
More file actions
98 lines (88 loc) · 2.18 KB
/
openmp_3.cpp
File metadata and controls
98 lines (88 loc) · 2.18 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
97
98
#include <omp.h>
#include <iostream>
void bubbleSort(double *A, int n) {
double time;
time = omp_get_wtime()*1000;
for(int i = 0; i < n; i++) {
double t = 0;
if(i % 2 == 0) {
for(int j = 0; j < n; j += 2) {
if(j < n-1) {
if(A[j] > A[j+1])
{
int t = A[j];
A[j] = A[j+1];
A[j+1] = t;
}
}
}
} else {
for (int j = 1; j < n; j += 2) {
if (j < n-1) {
if(A[j] > A[j+1])
{
int t = A[j];
A[j] = A[j+1];
A[j+1] = t;
}
}
}
}
}
std::cout << "Bubble_time: " << 1000*omp_get_wtime() - time << "\n";
}
void parallelBubbleSort(double *A, int n) {
double time;
time = 1000 * omp_get_wtime();
for(int i = 0; i < n; i++) {
double t = 0;
if(i % 2 == 0) {
#pragma omp parallel for private(t)
for(int j = 0; j < n; j += 2){
if(j < n - 1){
if(A[j] > A[j+1]){
int t = A[j];
A[j] = A[j+1];
A[j+1] = t;
}
}
}
}else{
#pragma omp parallel for private(t)
for (int j = 1; j < n; j += 2){
if (j < n - 1){
if(A[j] > A[j+1]){
int t = A[j];
A[j] = A[j+1];
A[j+1] = t;
}
}
}
}
}
std::cout << "Paralleled Buble_time: " << 1000*omp_get_wtime() - time << "\n";
}
int main() {
for(int i = 0; i < 11; i++){
double array[1000];
for(int i = 0; i < 100; i++){
array[i] = rand()%1000;
}
std::cout << i << "length: 1000" << "\n";
bubbleSort(array, 100);
parallelBubbleSort(array, 100);
std::cout << "\n";
}
for(int i = 0; i < 6; i++) {
int n = rand() % 2000 + 100;
double array[n];
for(int i = 0; i < n; i++) {
array[i] = rand()%1000;
}
std::cout << i << "length: 1000" << n << "\n";
bubbleSort(array, n);
parallelBubbleSort(array, n);
std::cout << "\n";
}
return 0;
}