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
154
155
156
157
158
159
160
161
162
163
164
165
|
//~ Libraries
#include "../shared_libraries/lr/lr.h"
PUSH_WARNINGS
#define STB_SPRINTF_IMPLEMENTATION
#include "libs/stb_sprintf.h"
POP_WARNINGS
#include "libs/listing_065.cpp"
//~ Standard library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include "haversine_random.h"
//~ Macro's
#define MemoryCopy memcpy
//~ Types
#include "generated/types.h"
//~ Helpers
void WriteMemoryTofile(umm Size, void *Memory, umm PairCount, char *Name, char *Extension)
{
char FileName[256] = {};
stbsp_sprintf(FileName, "data_%lu_%s.%s", PairCount, Name, Extension);
int File = open(FileName, O_RDWR|O_CREAT|O_TRUNC, 0600);
if(File != -1)
{
smm BytesWritten = write(File, Memory, Size);
Assert(BytesWritten == Size);
}
else
{
}
}
//~ Main
int main(int ArgsCount, char *Args[], char *Env[])
{
// 1. haversine_generator [uniform/cluster] [random seed] [number of pairs to generate]
if(ArgsCount >= 4)
{
u32 Method = 0;
u64 RandomSeed = 0;;
u64 PairCount = 0;
char *MethodName = Args[1];
char *SeedString = Args[2];
u64 ClusterCountLeft = U64Max;
f64 MaxAllowedX = 180.0;
f64 MaxAllowedY = 90.0;
f64 XCenter = 0.0;
f64 YCenter = 0.0;
f64 XRadius = MaxAllowedX;
f64 YRadius = MaxAllowedY;
if(!strcmp(MethodName, "cluster"))
{
ClusterCountLeft = 0;
}
else if(strcmp(MethodName, "uniform"))
{
MethodName = "uniform";
printf("Warning: Unknown method '%s', uniform used.\n", MethodName);
}
RandomSeed = atoll(Args[2]);
PairCount = atoll(Args[3]);
u64 MaxPairCount = (1LL << 34);
if(PairCount < MaxPairCount)
{
umm JsonMemorySize = Gigabytes(4);
u8 *JsonMemory = (u8 *)mmap(0, JsonMemorySize, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
u8 *JsonOut = JsonMemory;
umm BinMemorySize = Gigabytes(4);
u8 *BinMemory = (u8 *)mmap(0, BinMemorySize, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
u8 *BinOut = BinMemory;
JsonOut += stbsp_sprintf((char *)JsonOut,
"{\n"
" \"pairs\":\n"
" [\n");
pcg64_random_t Series = {};
pcg64_srandom_r(&Series, RandomSeed, RandomSeed);
u64 ClusterCountMax = 1 + (PairCount / 64);
f64 SumCoefficient = 1.0/(f64)PairCount;
f64 Sum = 0;
for(u32 PairIndex = 0;
PairIndex < PairCount;
PairIndex += 1)
{
if(ClusterCountLeft-- == 0)
{
ClusterCountLeft = ClusterCountMax;
XCenter = RandomBetween(&Series, -MaxAllowedX, MaxAllowedX);
YCenter = RandomBetween(&Series, -MaxAllowedY, MaxAllowedY);
XRadius = RandomBetween(&Series, 0, MaxAllowedX);
YRadius = RandomBetween(&Series, 0, MaxAllowedY);
}
f64 X0 = RandomDegree(&Series, XCenter, XRadius, MaxAllowedX);
f64 Y0 = RandomDegree(&Series, YCenter, YRadius, MaxAllowedY);
f64 X1 = RandomDegree(&Series, XCenter, XRadius, MaxAllowedX);
f64 Y1 = RandomDegree(&Series, YCenter, YRadius, MaxAllowedY);
f64 EarthRadius = 6372.8;
f64 Distance = ReferenceHaversine(X0, Y0, X1, Y1, EarthRadius);
Sum += SumCoefficient*Distance;
*(f64 *)BinOut = Distance;
BinOut += sizeof(Distance);
const char *Separator = (PairIndex == PairCount - 1) ? "\n" : ",\n";
JsonOut += stbsp_sprintf((char *)JsonOut,
" { \"x0\": %.16f, \"y0\": %.16f, \"x1\": %.16f, \"y1\": %.16f }%s",
X0, Y0, X1, Y1, Separator);
}
*(f64 *)BinOut = Sum;
BinOut += sizeof(Sum);
printf("Method: %s\n"
"Random seed: %lu\n"
"Pairs count: %lu\n"
, MethodName, RandomSeed, PairCount);
printf("Average sum: %.16f\n", Sum);
JsonOut += stbsp_sprintf((char *)JsonOut,
" ]\n"
"}\n");
WriteMemoryTofile(JsonOut - JsonMemory, JsonMemory, PairCount, "flex", "json");
WriteMemoryTofile(BinOut - BinMemory, BinMemory, PairCount, "haveranswer", "f64");
}
else
{
printf("Massive files unsupported. Number of pairs must be less than %lu.\n", MaxPairCount);
}
}
else
{
printf("Usage: %s [uniform/cluster] [random seed] [number of pairs to generate]\n",
Args[0]);
}
return 0;
}
|