c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

commit b91752a2c0e1d470c8c43d790b029f7642431133
parent cb92648ce2739c78723de76de22059637cf52dc7
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 18 Oct 2026 21:29:41 -0500

RDTSC calibration and instrumentation. Added haversine calculation, fixed bug w/ python gen

Diffstat:
Mperf-aware-programming/haversine/Makefile | 5++++-
Aperf-aware-programming/haversine/cpu-info/cpu-info.c | 21+++++++++++++++++++++
Aperf-aware-programming/haversine/cpu.c | 22++++++++++++++++++++++
Mperf-aware-programming/haversine/gen.py | 13++++---------
Mperf-aware-programming/haversine/json.c | 58+++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mperf-aware-programming/haversine/main.c | 19++++++++++++++-----
6 files changed, 122 insertions(+), 16 deletions(-)

diff --git a/perf-aware-programming/haversine/Makefile b/perf-aware-programming/haversine/Makefile @@ -5,7 +5,10 @@ clean: rm -rf out.json build: - gcc main.c + gcc main.c -lm run: build ./a.out artifacts/out.json +cpu: + gcc cpu-info/cpu-info.c + ./a.out diff --git a/perf-aware-programming/haversine/cpu-info/cpu-info.c b/perf-aware-programming/haversine/cpu-info/cpu-info.c @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <stdint.h> +#include <x86intrin.h> +#include <unistd.h> + +uint64_t rdtsc() { + uint64_t t = __rdtsc(); + return t; +} + +int main() { + int avgDelta = 0; + for(int i = 0; i < 5; ++i) { + uint64_t current = rdtsc(); + usleep(1000000); + uint64_t after = rdtsc(); + int delta = after - current; + avgDelta += delta / 5; + } + printf("Avg rdtsc delta: %lld\n", avgDelta); +} diff --git a/perf-aware-programming/haversine/cpu.c b/perf-aware-programming/haversine/cpu.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <inttypes.h> +#include <stdint.h> +#include <x86intrin.h> +#include <unistd.h> + +uint64_t rdtsc() { + uint64_t t = __rdtsc(); + return t; +} + +uint tsPerSecond(){ + int avgDelta = 0; + for(uint i = 0; i < 5; ++i) { + uint current = rdtsc(); + usleep(1000000); + uint after = rdtsc(); + uint delta = after - current; + avgDelta += delta / 5; + } + return avgDelta; +} diff --git a/perf-aware-programming/haversine/gen.py b/perf-aware-programming/haversine/gen.py @@ -61,17 +61,12 @@ def genJson(): return st with open(args.filepath, 'w') as f: - genCount = int(args.num) - opening = "{\"pairs\": [" - lns = [opening] - for i in range(0,genCount-1): - first = genJson() + "," - lns.append(first) + lns = ["{\"pairs\": ["] + for i in range(genCount - 1): + lns.append(genJson() + ",") lns.append(genJson()) lns.append("]}") - final = genJson() - closing = "]}" f.writelines(lns) print("Avg x: " + str(sum(rndx) / len(rndx))) @@ -85,5 +80,5 @@ with open(args.filepath + ".txt", 'w') as f: h = hashlib.new('sha256') h.update(hvS.encode()) print(h.hexdigest()) -with open(args.filepath + ".sha.txt", 'w') as f: +with open('artifacts/sha.txt', 'w') as f: f.writelines([str(h.hexdigest())]) diff --git a/perf-aware-programming/haversine/json.c b/perf-aware-programming/haversine/json.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <math.h> #include <string.h> #include <stdlib.h> @@ -130,17 +131,72 @@ struct Coordinates* parseCoordinates(char* jsonData, int* sz) { return coordinates; } -struct Coordinates* loadJson(char* filename, int* size) { +double square(double x) { + return x*x; +} + +double radiansFromDegrees (double deg) { + return 0.01745329251994329577 * deg; +} + +double* computeHaversines(struct Coordinates* coords, int size, double radius) { + double* res = malloc(sizeof(double)*size); + + for(int i = 0; i < size; ++i) { + struct Coordinates* current = &coords[i]; + + double lat1 = current->y0; + double lat2 = current->y1; + double lon1 = current->x0; + double lon2 = current->x1; + + double dLat = radiansFromDegrees(lat2 - lat1); + double dLon = radiansFromDegrees(lon2 - lon1); + lat1 = radiansFromDegrees(lat1); + lat2 = radiansFromDegrees(lat2); + + double a = square(sin(dLat/2.0)) + cos(lat1)*cos(lat2)*square(sin(dLon/2)); + double c = 2.0*asin(sqrt(a)); + res[i] = radius * c; + } + return res; +} + +struct Coordinates* loadJson(char* filename, int* size, uint tsPerSecond) { + + uint64_t startLf = rdtsc(); char* line = loadFileToString(filename); + uint64_t endLf = rdtsc(); + printf("Load File TS\t\t%lld\n", endLf - startLf); if(line == NULL) { return NULL; } int sz = 0; + + uint64_t startParse = rdtsc(); struct Coordinates* res = parseCoordinates(line, &sz); + uint64_t endParse = rdtsc(); + printf("Parse File\t\t%lld\n", endParse - startParse); + size[0] = sz; + uint64_t startFree = rdtsc(); free(line); + uint64_t endFree = rdtsc(); + printf("Free line\t\t%lld\n", endFree - startFree); + + + uint64_t startCompute = rdtsc(); + double* havs = computeHaversines(res,sz, 6372.8); + uint64_t endCompute = rdtsc(); + printf("Compute\t\t\t%lld\n", endCompute - startCompute); + + uint64_t startFreeH = rdtsc(); + free(havs); + uint64_t endFreeH = rdtsc(); + printf("Free havs\t\t%lld\n", endFreeH - startFreeH); + return res; } diff --git a/perf-aware-programming/haversine/main.c b/perf-aware-programming/haversine/main.c @@ -1,3 +1,5 @@ +#include "cpu.c" +#include <inttypes.h> #include "json.c" int main(int argc, char** argv) { @@ -7,17 +9,24 @@ int main(int argc, char** argv) { return -1; } + uint ts = tsPerSecond(); + printf("\nAvg TS / second\t\t%u\n", ts); + printf("======================================================\n"); + int size = 0; - struct Coordinates* coords = loadJson(argv[1], &size); + struct Coordinates* coords = loadJson(argv[1], &size, ts); if(coords == NULL) { return -1; } - for(int i = 0; i < size; ++i) { - struct Coordinates* current = &coords[i]; - printf("x0: %.10f, x1: %.10f, y0: %.10f, y1: %.10f\n", current->x0, current->x1, current->y0, current->y1); - } + // for(int i = 0; i < size; ++i) { + // struct Coordinates* current = &coords[i]; + // printf("x0: %.10f, x1: %.10f, y0: %.10f, y1: %.10f\n", current->x0, current->x1, current->y0, current->y1); + // } + uint64_t startFree = rdtsc(); free(coords); + uint64_t endFree = rdtsc(); + printf("Free coords\t\t%lld\n", endFree - startFree); }