-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.cpp
78 lines (62 loc) · 1.8 KB
/
driver.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
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <chrono>
#include <omp.h>
#include "psort.h"
void check_sorted(uint32_t *data, uint32_t n)
{
/*std::fstream fs("output1.txt", std::fstream::out);
for (uint64_t i=0; i<n; i++)
fs << data[i] << std::endl;*/
for (uint64_t i = 0; i < n - 1; i++)
{
if (data[i] > data[i + 1])
{
std::cout << "Data is not sorted.\n";
return;
}
}
std::cout << "Data is sorted.\n";
return;
}
void SortData(uint32_t *data, uint32_t n, int p, int n_threads)
{
#pragma omp parallel num_threads(n_threads)
{
#pragma omp single
ParallelSort(data, n, p);
}
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cout << "Insufficient Arguments\n"; // <datafile> <n_threads>
return 0;
}
std::fstream fs(argv[1], std::fstream::in);
// Reading data
uint32_t n, d = 0;
int p, n_threads = atoi(argv[2]);
fs >> n >> p;
uint32_t *data = new uint32_t[n];
std::cout << "n_threads = " << n_threads << std::endl;
std::cout << "N = " << n << " p = " << p << std::endl;
while (fs >> data[d++])
if(d == n)
break;
fs.close();
// Sorting
auto begin = std::chrono::high_resolution_clock::now();
SortData(data, n, p, n_threads);
auto end = std::chrono::high_resolution_clock::now();
// auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
double duration = (1e-6 * (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin)).count());
check_sorted(data, n);
std::cout << "Time taken for sorting " << n << " elements with "
<< p << " buckets = " << duration << "ms" << std::endl;
// Clean-up
delete data;
return 0;
}