summaryrefslogtreecommitdiff
path: root/src/haversine/haversine_generator.cpp
blob: b30dbe81d72a4d812a44f2890a86eeae70af44fd (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//~ Libraries
#include "libs/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


//~ Constants
#define ClusterCount 64
// NOTE(luca): A double's mantissa is 52 bits.  2^52 - 1 is 4503599627370495 which has 
// 16 digits.
#define PointJsonFormat "{ \"x0\": %.15f, \"y0\": %.15f, \"x1\": %.15f, \"y1\": %.15f }\n"

//~ Types
#include "generated/types.h"

struct cluster
{
    f64 X;
    f64 Y;
    f64 Width;
    f64 Height;
};

//~ 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;
        b32 Error = false;
        
        char *MethodString = Args[1];
        char *SeedString = Args[2];
        char *PairCountString = Args[3];
        
        if(!strcmp(MethodString, "uniform"))
        {
            Method = Method_Uniform;
        }
        else if(!strcmp(MethodString, "cluster"))
        {
            Method = Method_Cluster;
        }
        else
        {
            Error = true;
        }
        
        RandomSeed = atoll(SeedString);
        
        if(RandomSeed == 0)
        {
            if(SeedString[0] == '0')
            {
                RandomSeed = 0;
            }
            else
            {
                Error = true;
            }
        }
        
        PairCount = atoll(PairCountString);
        if(PairCount == 0)
        {
            Error = true;
        }
        
        if(!Error)
        {
            printf("Method: %s\n"
                   "Random seed: %lu\n"
                   "Pairs count: %lu\n"
                   , MethodString, RandomSeed, PairCount);
            
            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;
            
            // Generate pairs in the following format.
            // 
            // {
            //  "pairs": 
            //   [
            //    { "x0": ..., "y0": ..., "x1": ..., "y1": ... },
            //    { "x0": ..., "y0": ..., "x1": ..., "y1": ... }
            //   ]
            // }
            // 
            
            char *JsonHeader = 
                "{\n"
                " \"pairs\":\n"
                "   [\n";
            char *JsonFooter =
                "   ]\n"
                "}\n";
            
            JsonOut += stbsp_sprintf((char *)JsonOut, "%s", JsonHeader);
            
            pcg64_random_t RNG = {};
            pcg64_srandom_r(&RNG, RandomSeed, RandomSeed);
            
            if(0) {}
            else if(Method == Method_Uniform)
            {
                f64 AverageSum = 0;
                f64 TotalSum = 0;
                for(u64 PairsIndex = 0;
                    PairsIndex < PairCount;
                    PairsIndex += 1)
                {
                    f64 X0 = RandomBetween(&RNG, -180.0, 180.0);
                    f64 Y0 = RandomBetween(&RNG, -90.0, 90.0);
                    f64 X1 = RandomBetween(&RNG, -180.0, 180.0); 
                    f64 Y1 = RandomBetween(&RNG, -90.0, 360.0);
                    
                    f64 Sum = ReferenceHaversine(X0, Y0, X1, Y1, 6372.8);
                    TotalSum += Sum;
                    
                    *(f64 *)BinOut = Sum;
                    BinOut += sizeof(Sum);
                    
                    JsonOut += stbsp_sprintf((char *)JsonOut, "    " PointJsonFormat, X0, Y0, X1, Y1);
                }
                AverageSum = TotalSum / (f64)PairCount;
                
                *(f64 *)BinOut = AverageSum;
                BinOut += sizeof(AverageSum);
                
                printf("Average sum: %f\n", AverageSum);
            }
            else if(Method == Method_Cluster)
            {
                cluster Clusters[ClusterCount] = {};
                for(u32 ClusterIndex = 0;
                    ClusterIndex < ClusterCount;
                    ClusterIndex += 1)
                {
                    cluster *ClusterAt = Clusters + ClusterIndex;
                    ClusterAt->X = RandomBetween(&RNG, -180.0, 180.0);
                    ClusterAt->Y = RandomBetween(&RNG, -90.0, 90.0);
                    ClusterAt->Width = RandomBetween(&RNG, 0.0, 180.0);
                    ClusterAt->Height = RandomBetween(&RNG, 0.0, 90.0);
                }
                
                f64 AverageSum = 0;
                f64 TotalSum = 0;
                u32 ClusterIndex = 0;
                for(u32 PairIndex = 0;
                    PairIndex < PairCount;
                    PairIndex += 1)
                {
                    cluster *ClusterAt = Clusters + ClusterIndex;
                    
                    f64 X0 = RandomBetween(&RNG, ClusterAt->X - ClusterAt->Width, ClusterAt->X + ClusterAt->Width);
                    f64 Y0 = RandomBetween(&RNG, ClusterAt->Y - ClusterAt->Height, ClusterAt->Y + ClusterAt->Height);
                    f64 X1 = RandomBetween(&RNG, ClusterAt->X - ClusterAt->Width, ClusterAt->X + ClusterAt->Width);
                    f64 Y1 = RandomBetween(&RNG, ClusterAt->Y - ClusterAt->Height, ClusterAt->Y + ClusterAt->Height);
                    
                    f64 Sum = ReferenceHaversine(X0, Y0, X1, Y1, 6372.8);
                    TotalSum += Sum;
                    
                    *(f64 *)BinOut = Sum;
                    BinOut += sizeof(Sum);
                    
                    JsonOut += stbsp_sprintf((char *)JsonOut, "    " PointJsonFormat, X0, Y0, X1, Y1);
                    
                    ClusterIndex += 1;
                    if(ClusterIndex == ClusterCount)
                    {
                        ClusterIndex -= ClusterCount;
                    }
                    
                }
                AverageSum = TotalSum / (f64)PairCount;
                
                *(f64 *)BinOut = AverageSum;
                BinOut += sizeof(AverageSum);
                
                printf("Average sum: %f\n", AverageSum);
            }
            else
            {
                Assert(0);
            }
            
            JsonOut += stbsp_sprintf((char *)JsonOut, "%s", JsonFooter);
            
            // Write memory to json file
            {            
                char JsonFileName[256] = {};
                stbsp_sprintf(JsonFileName, "data_%lu.json", PairCount);
                
                int File = open(JsonFileName, O_RDWR|O_CREAT|O_TRUNC, 0600);
                Assert(File != -1);
                smm Result = write(File, JsonMemory, JsonOut - JsonMemory);
                Assert(Result == JsonOut - JsonMemory);
            }
            
            // Write memory to binary answer file
            char BinFileName[256] = {};
            {
                stbsp_sprintf(BinFileName, "data_%lu_haveranswer.f64", PairCount);
                int File = open(BinFileName, O_RDWR|O_CREAT|O_TRUNC, 0600);
                Assert(File != -1);
                smm Result = write(File, BinMemory, BinOut - BinMemory);
                Assert(Result == BinOut - BinMemory);
            }
        }
        else
        {
            printf("Usage: %s [uniform/cluster] [random seed] [number of pairs to generate]\n", 
                   Args[0]);
        }
    }
    else
    {
        printf("Usage: %s [uniform/cluster] [random seed] [number of pairs to generate]\n", 
               Args[0]);
    }
    
    return 0;
}