This repository has been archived by the owner on Jun 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main1.cpp
86 lines (78 loc) · 2.6 KB
/
main1.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
#include <sys/time.h>
#include <stdint.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
#include <chrono>
#include <iomanip>
#include <iterator>
#include <cassert>
#include <thread>
#include "support.hpp"
using namespace std::chrono;
void printDiff(const time_point<high_resolution_clock> &start, const time_point<high_resolution_clock> &stop)
{
std::cout << duration_cast<milliseconds>(stop-start).count() << " millisecond" << std::endl;
}
template<typename T>
void sort(const std::string &inFile, const std::string &outFile)
{
size_t inBuffLen = 1024 * 1024;
size_t outBuffLen = 256 * 1024 * 1024;
std::vector<T> inBuffer(inBuffLen / sizeof(T));
std::vector<size_t> outBuffer(outBuffLen / sizeof(size_t));
size_t iterCount = std::numeric_limits<T>::max() / (outBuffLen / sizeof(T)) +
(std::numeric_limits<T>::max() % (outBuffLen / sizeof(T)) ? 1 : 0);
std::cout << "Iteration count: " << iterCount << std::endl;
T fromVal = 0;
T toVal = std::min((size_t)(outBuffLen / sizeof(T)), (size_t)std::numeric_limits<T>::max());
std::cout << std::numeric_limits<T>::max() << std::endl;
std::ofstream out(outFile.c_str(), std::ios::out | std::ios::binary);
size_t readed = 0;
while(iterCount > 0)
{
std::ifstream in(inFile.c_str(), std::ios::in | std::ios::binary);
std::fill(outBuffer.begin(), outBuffer.end(), 0);
do
{
in.read((char*)&inBuffer[0], inBuffLen);
readed = in.gcount() / sizeof(T);
for(size_t i=0; i < readed; ++i)
{
if(fromVal <= inBuffer[i] && inBuffer[i] <= toVal)
outBuffer[inBuffer[i]-fromVal]++;
}
}while(!in.eof());
for(size_t i = 0; i < outBuffLen / sizeof(size_t); ++i)
{
if(outBuffer[i] > 0)
{
T val = fromVal + i;
for(size_t j=0; j<outBuffer[i]; ++j)
out.write((char*)&val, sizeof(T));
}
}
fromVal = toVal + 1;
toVal += outBuffLen / sizeof(size_t);
if(toVal == 0)
toVal = std::numeric_limits<T>::max();
iterCount--;
}
}
int main(int argc, char* argv[])
{
if(argc < 2)
{
std::cout << "Usage:\n\talgo_test file_name" << std::endl;
return -1;
}
const std::string input(argv[1]);
auto start = high_resolution_clock::now();
sort<uint32_t>(input, input+"_out");
auto stop = high_resolution_clock::now();
std::cout << "sort test: ";
printDiff(start, stop);
return 0;
}