-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlect2.cpp
111 lines (91 loc) · 2.61 KB
/
lect2.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
#include <cilk/cilk.h>
#include <iostream>
#include <vector>
#include <cilk/cilk_api.h>
#include <cilk/reducer_max.h>
#include <cilk/reducer_min.h>
#include <cilk/reducer_vector.h>
#include <chrono>
using namespace std::chrono;
using namespace std;
void ReducerMinTest(int *mass_pointer, const long size)
{
cilk::reducer<cilk::op_min_index<long, int>> minimum;
cilk_for(long i = 0; i < size; ++i)
{
minimum->calc_min(i, mass_pointer[i]);
}
printf("Minimal element = %d has index = %d\n",
minimum->get_reference(), minimum->get_index_reference());
}
void ParallelSort(int *begin, int *end)
{
if (begin != end)
{
--end;
int *middle = std::partition(begin, end, std::bind2nd(std::less<int>(), *end));
std::swap(*end, *middle);
cilk_spawn ParallelSort(begin, middle);
ParallelSort(++middle, ++end);
cilk_sync;
}
}
void CompareForAndCilk_For(size_t sz)
{
high_resolution_clock::time_point start;
high_resolution_clock::time_point end;
cilk::reducer<cilk::op_vector<int>>red_vec;
vector<int> vec;
duration<double> duration;
cout << "Comparing for and cilk_for for size " << sz << endl;
//parallel
start = high_resolution_clock::now();
cilk_for(size_t i = 0; i < sz; ++i)
red_vec->push_back(rand() % 20000 + 1);
end = high_resolution_clock::now();
duration = (end - start);
cout << "Parallel duration is: " << duration.count() << " seconds" << endl;
//sync
start = high_resolution_clock::now();
for (size_t i = 0; i < sz; ++i)
vec.push_back(rand() % 20000 + 1);
end = high_resolution_clock::now();
duration = (end - start);
cout << "Sync duration is: " << duration.count() << " seconds" << endl;
cout << endl;
}
int main()
{
srand((unsigned)time(0));
__cilkrts_set_param("nworkers", "4");
long i;
high_resolution_clock::time_point start;
high_resolution_clock::time_point end;
duration<double> duration;
int ms_size[4] = { 10000, 100000, 500000, 1000000 };
for (long mass_size : ms_size)
{
cout << "Testing sort for size " << mass_size << endl;
int *mass_begin, *mass_end;
int *mass = new int[mass_size];
for (i = 0; i < mass_size; ++i)
{
mass[i] = (rand() % 25000) + 1;
}
mass_begin = mass;
mass_end = mass_begin + mass_size;
ReducerMinTest(mass, mass_size);
start = high_resolution_clock::now();
ParallelSort(mass_begin, mass_end);
end = high_resolution_clock::now();
duration = (end - start);
cout << "Sort duration is: " << duration.count() << " seconds" << endl;
ReducerMinTest(mass, mass_size);
cout << endl;
delete[]mass;
}
int size[8] = { 1000000, 100000, 10000, 1000, 500, 100, 50, 10 };
for (int sz : size)
CompareForAndCilk_For(sz);
return 0;
}