-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.cpp
116 lines (91 loc) · 2.79 KB
/
generate.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
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <vector>
#include <mutex>
#include <random>
std::mutex mtx;
FILE *outputFile;
typedef struct Str
{
char character[16];
} Str;
Str Str_NULL = {'\0'};
Str generateRandomString(int length)
{
Str randomString;
static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, sizeof(alphabet) - 2);
for (int i = 0; i < length; i++)
{
int index = dis(gen);
randomString.character[i] = alphabet[index];
}
randomString.character[length] = '\n';
return randomString;
}
void generateAndWriteRandomStrings(int numLines, int stringLength, int threadId)
{
int batchSize = 10485760;
int numBatches = (numLines + batchSize - 1) / batchSize;
for (int batch = 0; batch < numBatches; batch++)
{
int linesToGenerate = std::min(batchSize, numLines - batch * batchSize);
// Create a vector to store the lines
Str *lines = (Str *)malloc((linesToGenerate + 1) * sizeof(Str));
for (int i = 0; i < linesToGenerate; i++)
{
// Add the random string to the vector
lines[i] = generateRandomString(stringLength);
}
lines[linesToGenerate] = Str_NULL;
// Lock the mutex before writing to the file
mtx.lock(); // 不加锁就注释掉
std::cout << "Writing " << linesToGenerate * 16 / 1024 / 1024 << "MB";
// Write the lines to file
// std::cout << lines[0].character;
fprintf(outputFile, lines[0].character);
fflush(outputFile);
free(lines);
std::cout << " OK" << std::endl;
mtx.unlock(); // 不加锁就注释掉
}
}
int main()
{
srand(time(0));
std::string filename;
double N_GB;
std::cout << "Enter the filename: ";
std::cin >> filename;
std::cout << "Enter the size in GB: ";
std::cin >> N_GB;
outputFile = fopen(filename.c_str(), "wb");
if (!outputFile)
{
std::cerr << "Failed to open file." << std::endl;
return 1;
}
long long numLines = N_GB * 1024 * 1024 * 1024 / 16;
std::cout << "Lines: " << numLines << std::endl;
int stringLength = 15;
int numThreads = std::thread::hardware_concurrency();
std::vector<std::thread> threads;
for (int i = 0; i < numThreads; i++)
{
threads.emplace_back(generateAndWriteRandomStrings, i < numThreads - 1 ? numLines / numThreads : numLines - (numLines / numThreads) * (numThreads - 1), stringLength, i);
}
// Wait for all threads to finish
for (auto &thread : threads)
{
thread.join();
}
fflush(outputFile);
fclose(outputFile);
return 0;
}