diff options
| author | Raymaekers Luca <luca@spacehb.net> | 2025-11-13 00:22:11 +0100 |
|---|---|---|
| committer | Raymaekers Luca <luca@spacehb.net> | 2025-11-13 00:22:11 +0100 |
| commit | 7c2d674835f78c3af9b4f7c1d75f29b6b5a10f6e (patch) | |
| tree | b8a2b7652bdf2536e6b178a4719327ca677a25db /src | |
| parent | d4f6774c172ac1e7c193fc4e89230c873d179c2b (diff) | |
Added clustered approach
Diffstat (limited to 'src')
| -rwxr-xr-x | src/haversine/build.sh | 77 | ||||
| -rw-r--r-- | src/haversine/haversine_generator.cpp | 127 | ||||
| -rw-r--r-- | src/haversine/haversine_random.h | 4 |
3 files changed, 150 insertions, 58 deletions
diff --git a/src/haversine/build.sh b/src/haversine/build.sh index 38170c0..54fd9f2 100755 --- a/src/haversine/build.sh +++ b/src/haversine/build.sh @@ -1,36 +1,51 @@ #!/bin/sh -cd "$(dirname "$(readlink -f "$0")")" - -Build="../../build" -mkdir -p "$Build" -mkdir -p generated - -Compiler="clang" - -CompilerFlags=" --g --fdiagnostics-absolute-paths --nostdinc++ -" - -WarningFlags=" --Wall --Wextra --Wno-unused-label --Wno-unused-variable --Wno-unused-function --Wno-unused-value --Wno-unused-but-set-variable --Wno-missing-field-initializers --Wno-write-strings --Wno-unused-parameter -" +set -eu +ScriptDirectory="$(dirname "$(readlink -f "$0")")" +cd "$ScriptDirectory" + +#- Globals +CommonCompilerFlags="-DOS_LINUX=1 -fsanitize-trap -nostdinc++" +CommonWarningFlags="-Wall -Wextra -Wconversion -Wdouble-promotion -Wno-sign-conversion -Wno-sign-compare -Wno-double-promotion -Wno-unused-but-set-variable -Wno-unused-variable -Wno-write-strings -Wno-pointer-arith -Wno-unused-parameter -Wno-unused-function" LinkerFlags="-lm" -printf '[debug mode]\n' -printf '[%s build]\n' "$Compiler" -$Compiler $CompilerFlags $WarningFlags $LinkerFlags \ - -o "$Build"/haversine_generator \ - haversine_generator.cpp
\ No newline at end of file +DebugFlags="-g -ggdb -g3" +ReleaseFlags="-O3" + +ClangFlags="-fdiagnostics-absolute-paths -ftime-trace +-Wno-null-dereference -Wno-missing-braces -Wno-vla-extension -Wno-writable-strings -Wno-missing-field-initializers -Wno-address-of-temporary -Wno-int-to-void-pointer-cast" + +GCCFlags="-Wno-cast-function-type -Wno-missing-field-initializers -Wno-int-to-pointer-cast" + + +#- Main + +clang=1 +gcc=0 +debug=1 +release=0 +for Arg in "$@"; do eval "$Arg=1"; done +# Exclusive flags +[ "$release" = 1 ] && debug=0 +[ "$gcc" = 1 ] && clang=0 + +[ "$gcc" = 1 ] && Compiler="g++" +[ "$clang" = 1 ] && Compiler="clang" + +Flags="$CommonCompilerFlags" +[ "$debug" = 1 ] && Flags="$Flags $DebugFlags" +[ "$release" = 1 ] && Flags="$Flags $ReleaseFlags" +Flags="$Flags $CommonCompilerFlags" +Flags="$Flags $CommonWarningFlags" +[ "$clang" = 1 ] && Flags="$Flags $ClangFlags" +[ "$gcc" = 1 ] && Flags="$Flags $GCCFlags" +Flags="$Flags $LinkerFlags" + +[ "$debug" = 1 ] && printf '[debug mode]\n' +[ "$release" = 1 ] && printf '[release mode]\n' +printf '[%s compile]\n' "$Compiler" + +mkdir -p ../../build + +$Compiler $Flags -o ../../build/haversine_generator haversine_generator.cpp diff --git a/src/haversine/haversine_generator.cpp b/src/haversine/haversine_generator.cpp index c3f7561..afc25e5 100644 --- a/src/haversine/haversine_generator.cpp +++ b/src/haversine/haversine_generator.cpp @@ -1,3 +1,4 @@ +//~ Libraries #include "libs/lr/lr.h" #include <stdio.h> @@ -5,18 +6,35 @@ #include <string.h> #include <inttypes.h> -#define MemoryCopy memcpy +#include <sys/mman.h> #include "listing_065.cpp" #include "haversine_random.h" +//~ Macro's +#define MemoryCopy memcpy + + +//~ Constants +#define ClusterCount 64 + +//~ Types enum generation_method : u32 { Method_Uniform, Method_Cluster }; +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] @@ -25,7 +43,7 @@ int main(int ArgsCount, char *Args[], char *Env[]) { u32 Method = 0; u64 RandomSeed = 0;; - u64 PairsCount = 0; + u64 PairCount = 0; b32 Error = false; if(!strcmp(Args[1], "uniform")) @@ -55,8 +73,8 @@ int main(int ArgsCount, char *Args[], char *Env[]) } } - PairsCount = atoll(Args[3]); - if(PairsCount == 0) + PairCount = atoll(Args[3]); + if(PairCount == 0) { Error = true; } @@ -66,7 +84,10 @@ int main(int ArgsCount, char *Args[], char *Env[]) printf("Method: %s\n" "Random seed: %lu\n" "Pairs count: %lu\n" - , Args[1], RandomSeed, PairsCount); + , Args[1], RandomSeed, PairCount); + + umm MemorySize = Gigabytes(4); + u8 *Memory = (u8 *)mmap(0, MemorySize, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED|MAP_POPULATE, -1, 0); // Generate pairs in the following format. // @@ -93,34 +114,90 @@ int main(int ArgsCount, char *Args[], char *Env[]) pcg64_random_t RNG = {}; pcg64_srandom_r(&RNG, RandomSeed, RandomSeed); - f64 AverageSum = 0; - f64 TotalSum = 0; - for(u64 PairsIndex = 0; - PairsIndex < PairsCount; - PairsIndex++) + if(0) {} + else if(Method_Uniform) { - b32 LastPair = false; - - f64 X0 = RandomBetween(&RNG, -180.0, 180.0); - f64 Y0 = RandomBetween(&RNG, -360.0, 360.0); - f64 X1 = RandomBetween(&RNG, -180.0, 180.0); - f64 Y1 = RandomBetween(&RNG, -360.0, 360.0); + 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; + + // NOTE(luca): A double's mantissa is 52 bits. 2^52 - 1 is 4503599627370495 which has + // 16 digits. + fprintf(Out, + " { \"x0\": %.15f, \"y0\": %.15f, \"x1\": %.15f, \"y1\": %.15f }\n", + X0, Y0, X1, Y1); + + } + AverageSum = TotalSum / (f64)PairCount; - f64 Sum = ReferenceHaversine(X0, Y0, X1, Y1, 6372.8); - TotalSum += Sum; + printf("Average sum: %f\n", AverageSum); + } + else if(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); + } - // NOTE(luca): A double's mantissa is 52 bits. 2^52 - 1 is 4503599627370495 which has - // 16 digits. - fprintf(Out, - " { \"x0\": %.15f, \"y0\": %.15f, \"x1\": %.15f, \"y1\": %.15f }\n", - X0, Y0, X1, Y1); + 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; + + // NOTE(luca): A double's mantissa is 52 bits. 2^52 - 1 is 4503599627370495 which has + // 16 digits. + fprintf(Out, + " { \"x0\": %.15f, \"y0\": %.15f, \"x1\": %.15f, \"y1\": %.15f }\n", + X0, Y0, X1, Y1); + + ClusterIndex += 1; + if(ClusterIndex == ClusterCount) + { + ClusterIndex -= ClusterCount; + } + + } + AverageSum = TotalSum / (f64)PairCount; + printf("Average sum: %f\n", AverageSum); + } + else + { + Assert(0); } - AverageSum = TotalSum / (f64)PairsCount; fprintf(Out, "%s", JsonFooter); - printf("Average sum: %f\n", AverageSum); } else { diff --git a/src/haversine/haversine_random.h b/src/haversine/haversine_random.h index b7c545e..a9ec221 100644 --- a/src/haversine/haversine_random.h +++ b/src/haversine/haversine_random.h @@ -1,7 +1,7 @@ - #include <math.h> - +PUSH_WARNINGS #include "libs/pcg/pcg.c" +POP_WARNINGS #define CountLeadingZeroes64(Value) __builtin_clzll(Value) |
