/
main.cpp
117 lines (101 loc) · 3.39 KB
/
main.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
/*
* Host side master program
* Copyright (C) 2019 Hsuan-Ting Lu
*
* GNU General Public License v3.0+
* (see LICENSE or https://www.gnu.org/licenses/)
*/
// clang-format off
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <stdexcept>
#include <numeric>
#include "types.hpp"
#include "radix_sort.hpp"
// clang-format on
// TODO: pin memory
void read_input(std::ifstream* ifs, entry* array, const int INPUTSIZE) {
char buffer[65];
buffer[64] = '$';
for (int str_idx = 0; str_idx != INPUTSIZE; ++str_idx) {
ifs->read(buffer, 64);
ifs->ignore();
array[str_idx] = entry(buffer);
}
}
int main(int argc, char** argv) {
if (argc != 1 + 2) { // DEBUG:
throw std::invalid_argument("2 arguments needed");
}
// HACK: use "wc -l" for file line counting
const int INPUTSIZE = std::stoi(argv[2]);
const int EXPANDEDSIZE = 65 * INPUTSIZE;
std::cerr << "expected output size :: str_array: " << INPUTSIZE
<< ", rotate_expand: " << EXPANDEDSIZE << "\n";
entry* str_array;
entry_repr* repr_array;
// Read to memory
std::ifstream ifs(argv[1], std::ifstream::in);
if (ifs) {
// TODO: Count number of lines of the input file, HACK: use "wc -l"
// Allocate array
str_array = static_cast<entry*>(std::malloc(INPUTSIZE * sizeof(entry)));
repr_array = static_cast<entry_repr*>(
std::malloc(EXPANDEDSIZE * sizeof(entry_repr)));
// Read input
read_input(&ifs, str_array, INPUTSIZE);
ifs.close();
} else {
throw std::invalid_argument("Cannot open file");
}
std::cout << std::endl;
for (int i = 0; i != INPUTSIZE; ++i) {
// std::cout << str_array[i] << std::endl;
}
// std::cout << "\n";
sort::SingleThread::expand_rotation(str_array, INPUTSIZE, repr_array,
EXPANDEDSIZE);
for (int i = 0; i != EXPANDEDSIZE; ++i) {
if (!(i % 65)) {
// std::cout << "< " << i / 65 << " >\n";
}
// std::cout << repr_array[i] << std::endl;
}
// Scan for distribution
unsigned int partition_freq[sort::PARTITION_SIZE] = {};
unsigned int frequency[sort::RADIX_LEVELS][sort::RADIX_SIZE] = {};
sort::SingleThread::count_frequency(repr_array, EXPANDEDSIZE,
partition_freq, frequency);
std::cerr << "partition frequency:\n";
std::cerr << "(sum = "
<< std::accumulate(partition_freq,
partition_freq + sort::PARTITION_SIZE, 0)
<< ")\n";
for (int i = 0; i != sort::PARTITION_SIZE; ++i) {
// std::cerr << partition_freq[i] << " ";
if (!(i % 80) && i != 0) {
// std::cerr << "\n";
}
}
// std::cerr << "\n";
// Partition
std::cerr << "check partition\n";
sort::SingleThread::partitioning(repr_array, EXPANDEDSIZE, partition_freq);
for (int i = 0; i != EXPANDEDSIZE; ++i) {
// std::cout << repr_array[i] << std::endl;
}
// Sort
std::cerr << "check sorting\n";
sort::SingleThread::radix_sort(repr_array, EXPANDEDSIZE, frequency);
for (int i = 0; i != EXPANDEDSIZE; ++i) {
// std::cout << repr_array[i] << std::endl;
}
// cleanup
std::free(str_array);
std::free(repr_array);
}