forked from BU-DiSC/cs561_templatezonemaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkload_generator.cpp
151 lines (121 loc) · 4.61 KB
/
workload_generator.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
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
#include <fstream>
#include <ctime>
#include <algorithm>
#include <vector>
#include <iostream>
void generate_one_file(unsigned long long pTOTAL_NUMBERS, unsigned int pdomain, unsigned long long pL, short ppercent_outRange, short plpercentage, int pseed);
std::string generate_partitions_stream(unsigned long long TOTAL_NUMBERS, unsigned int domain, unsigned long long L, short percent_outRange, short l_percentage, int seed, std::string folder);
void generate_one_file(unsigned long long pTOTAL_NUMBERS, unsigned int pdomain, unsigned long long pL, short ppercent_outRange, short plpercentage, int pseed = 1)
{
std::ofstream outfile;
srand(time(NULL));
outfile.open("dataledger.txt", std::ios_base::app);
std::string folder_name = "./";
outfile << generate_partitions_stream(pTOTAL_NUMBERS, pdomain, pL, ppercent_outRange, plpercentage, pseed, folder_name) << std::endl;
outfile.close();
}
unsigned int generate_random_in_range(unsigned long long position, unsigned long long Total_Numbers, double l_percentage)
{
unsigned int l = Total_Numbers * l_percentage;
if ((int)(position - l) <= 0)
{
return (rand() % (position + l));
}
else if ((int)(position + l) >= Total_Numbers)
{
return (rand() % (Total_Numbers - (position - l))) + (position - l);
}
else
{
return (rand() % ((position + l) - (position - l))) + (position - l);
}
}
/*
Function which generates uniform data over some domain, and write it in binary format.
Each partition of L elements is shuffled, and has some noise (randomness) linked to the
percent_outRange parameter.
*/
std::string generate_partitions_stream(unsigned long long TOTAL_NUMBERS, unsigned int domain, unsigned long long L, short percent_outRange, short l_percentage, int seed, std::string folder = "./Data")
{
float p_outOfRange = percent_outRange / 100.0;
std::srand(seed);
//unsigned long long array[TOTAL_NUMBERS];
std::vector<unsigned long long> array(TOTAL_NUMBERS, 0);
std::string fname = folder;
fname += "/data_";
fname += std::to_string(TOTAL_NUMBERS);
fname += "-elems_";
fname += std::to_string(percent_outRange);
fname += "-kperct_";
fname += std::to_string(l_percentage);
fname += "-lperct_";
fname += std::to_string(seed);
fname += "seed";
fname += std::to_string(std::time(nullptr));
fname += ".dat";
std::ofstream myfile1(fname, std::ios::binary);
unsigned long long noise_limit = TOTAL_NUMBERS * p_outOfRange;
unsigned long long noise_counter = 0;
for (unsigned long long i = 0; i < TOTAL_NUMBERS; i++)
{
array[i] = i;
float ran = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
//randomize generation
if (ran < p_outOfRange && noise_counter < noise_limit)
{
//generate position of shuffle
//unsigned long long r = (rand() % TOTAL_NUMBERS);
unsigned long long r = generate_random_in_range(i, TOTAL_NUMBERS, l_percentage / 100.0);
if (array[r] == 0)
{
if (r != 0)
{
array[r] = r;
}
}
unsigned long long temp = array[i];
array[i] = array[r];
array[r] = temp;
noise_counter++;
}
}
for (unsigned long long j = 0; j < TOTAL_NUMBERS; ++j)
{
myfile1.write(reinterpret_cast<char *>(&array[j]), sizeof(int));
}
myfile1.close();
return fname;
}
//arguments to program:
//unsigned long long pTOTAL_NUMBERS, unsigned int pdomain, unsigned long long pL, short ppercent_outRange, int pseed
int main(int argc, char **argv)
{
if (argc < 4)
{
std::cout << "Usage: ./execs/workload_generator.exe totalNumbers noisePercentage windowThreshold" << std::endl;
return 0;
}
unsigned long long totalNumbers = atoi(argv[1]);
short noisePercentage = atoi(argv[2]);
short lPercentage = atoi(argv[3]);
unsigned long long domain = totalNumbers;
unsigned long long windowSize = 1;
int seedValue = 1;
// simple checks
if (totalNumbers < 0)
{
std::cout << "No. of Elements cannot be < 0" << std::endl;
exit(1);
}
if (noisePercentage < 0 || noisePercentage > 50)
{
std::cout << "Please ensure 0 <= noisePercentage <= 50" << std::endl;
exit(1);
}
if (lPercentage < 0 || lPercentage > 50)
{
std::cout << "Please ensure 0 <= lPercentage <= 50" << std::endl;
exit(1);
}
generate_one_file(totalNumbers, domain, windowSize, noisePercentage, lPercentage, seedValue);
}