-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.cpp
More file actions
153 lines (126 loc) · 4.63 KB
/
Copy pathsplit.cpp
File metadata and controls
153 lines (126 loc) · 4.63 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Chunks are written inline by the thread that sorted them.
//
// A writer thread was tried here and it's a null result -- three interleaved
// cold cycles gave +9.53 / -0.36 / -5.42s. The
// merge had a measured serialization; this doesn't. Each thread sorts for
// ~0.46s per 500MB chunk and writes for ~175ms, so eight threads already
// drift out of phase enough to overlap. Don't re-add one without measuring
// that reads and writes actually fail to overlap.
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <chrono>
#include <cstring>
#include <cstdint>
#include <thread>
#include <atomic>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
int CHUNK_SIZE;
const int RECORD_SIZE = 100;
struct KeyIdx {
char key[10];
uint32_t idx; // record's slot within the chunk buffer
};
// pread() may return fewer bytes than asked for; loop until the chunk is whole.
static bool readFully(int fd, char* p, size_t n, off_t off) {
while (n) {
ssize_t r = pread(fd, p, n, off);
if (r <= 0) return false;
// Advance pointers and offset by the number of bytes actually read
p += r;
n -= (size_t)r;
off += r;
}
return true;
}
static bool writeFully(int fd, const char* p, size_t n, off_t off) {
while (n) {
ssize_t w = pwrite(fd, p, n, off);
if (w <= 0) { perror("pwrite"); return false; }
p += w; n -= (size_t)w; off += w;
}
return true;
}
int main(int argc, char* argv[]) {
auto start = chrono::high_resolution_clock::now();
if (argc < 2 || argc > 3) {
cout << "Usage: ./split_program <chunk_size> [threads]\n";
return 1;
}
CHUNK_SIZE = stoi(argv[1]);
if (CHUNK_SIZE <= 0) {
cout << "Chunk size must be positive\n";
return 1;
}
const int T = argc > 2 ? stoi(argv[2])
: (int)thread::hardware_concurrency();
// One shared read-only fd.
int fd = open("input.txt", O_RDONLY);
if (fd < 0) {
perror("open input.txt");
return 1;
}
const size_t totalRecs = (size_t)lseek(fd, 0, SEEK_END) / RECORD_SIZE;
const size_t nchunks = (totalRecs + CHUNK_SIZE - 1) / CHUNK_SIZE;
atomic<size_t> next{0}; // hands out chunk indices
// The loop becomes a worker lambda
auto worker = [&]() {
// Per-thread buffers
vector<char> buf(static_cast<size_t>(CHUNK_SIZE) * RECORD_SIZE);
vector<char> out(static_cast<size_t>(CHUNK_SIZE) * RECORD_SIZE);
vector<KeyIdx> keys;
keys.reserve(CHUNK_SIZE);
size_t c;
// fetch_add coordinates which thread gets which chunk safely
while ((c = next.fetch_add(1)) < nchunks) {
const size_t startRec = c * static_cast<size_t>(CHUNK_SIZE);
const size_t n = min(static_cast<size_t>(CHUNK_SIZE), totalRecs - startRec);
if (!readFully(fd, buf.data(), n * RECORD_SIZE, static_cast<off_t>(startRec) * RECORD_SIZE)) {
cerr << "read failed on chunk " << c << "\n";
return;
}
// build small key+index array and sort ONLY that
keys.resize(n);
for (size_t i = 0; i < n; i++) {
memcpy(keys[i].key, &buf[i * RECORD_SIZE], 10);
keys[i].idx = static_cast<uint32_t>(i);
}
sort(keys.begin(), keys.end(),
[](const KeyIdx& a, const KeyIdx& b){
return memcmp(a.key, b.key, 10) < 0;
});
// reorder the buffer ONE time
for (size_t i = 0; i < n; i++) {
memcpy(&out[i * RECORD_SIZE], &buf[keys[i].idx * RECORD_SIZE], RECORD_SIZE);
}
// one sequential write of full, sorted records
string path = "run" + to_string(c) + ".dat";
int rfd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (rfd < 0) { perror(path.c_str()); return; }
writeFully(rfd, out.data(), n * RECORD_SIZE, 0);
close(rfd);
}
};
// spin up the threads
vector<thread> pool;
for (int t = 0; t < T; t++) {
pool.emplace_back(worker);
}
// wait for all threads to finish
for (auto& th : pool) {
th.join();
}
close(fd);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = end - start;
cout << "Split time: "
<< elapsed.count() << " seconds"
<< " [threads=" << T
<< " chunks=" << nchunks
<< " records=" << totalRecs << "]\n";
return 0;
}