performance-aware-programming/haversine_02/src/processor/main.cpp

125 lines
3.1 KiB
C++

#include "haversine.h"
#include "point_types.h"
#include "processor/proc_argparser.h"
#include "profiler/ids.h"
#include "profiler/timer.h"
#include "json/dstring.h"
#include "json/json_entities.h"
#include "json/parser.h"
#include <aliases.h>
#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
PROFILE_START(COUNT_PROFILER_IDS);
SAMPLE_START(PROFILER_ID_CLI_PARSE, "CLI PARSING");
ProcessorArgs args = parse_args(argc, argv);
SAMPLE_END(PROFILER_ID_CLI_PARSE);
SAMPLE_START(PROFILER_ID_JSON_PARSE, "JSON PARSING");
jentity_t *root = load_json(args.filepath);
assert(root->type == JENTITY_SINGLE && root->value.type == JVAL_COLLECTION);
SAMPLE_END(PROFILER_ID_JSON_PARSE);
SAMPLE_START(PROFILER_ID_LOAD_JSON_PAIRS, "LOAD JSON PAIRS");
jentity_t *pairs = root->value.collection->begin;
assert(pairs->type == JENTITY_PAIR &&
pairs->pair.value.type == JVAL_COLLECTION);
u64 pair_count = pairs->pair.value.collection->size;
PointPair *point_pairs = (PointPair *)malloc(sizeof(PointPair) * pair_count);
memset(point_pairs, 0, pair_count);
u64 index = 0;
for (jentity_t *pair = pairs->pair.value.collection->begin; pair != NULL;
pair = pair->next) {
assert(index < pair_count && pair->type == JENTITY_SINGLE &&
pair->value.type == JVAL_COLLECTION &&
pair->value.collection->size == 4);
jentity_t *x0 = pair->value.collection->begin;
jentity_t *y0 = x0->next;
jentity_t *x1 = y0->next;
jentity_t *y1 = x1->next;
PointPair p = ((PointPair){
{x0->pair.value.num_dbl, y0->pair.value.num_dbl},
{x1->pair.value.num_dbl, y1->pair.value.num_dbl},
});
point_pairs[index++] = p;
}
SAMPLE_END(PROFILER_ID_LOAD_JSON_PAIRS);
SAMPLE_START(PROFILER_ID_READ_BINARY, "BINARY READ");
const char *filename = "count_and_distances";
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Failed to open the %s file", filename);
} else {
// Skip the count
fseek(fp, sizeof(u64), SEEK_SET);
}
SAMPLE_END(PROFILER_ID_READ_BINARY);
SAMPLE_START(PROFILER_ID_HAVERSINE_SUM, "HAVERSINE SUM");
f64 sum = 0.0;
f64 distance = 0.0;
f64 saved_distance = 0.0;
for (u64 i = 0; i < pair_count; ++i) {
SAMPLE_START(PROFILER_ID_HAVERSINE_DISTANCE, "HAVERSINE DISTANCE");
distance = haversine_of_degrees(point_pairs[i], EARTH_RADIUS_KM);
SAMPLE_END(PROFILER_ID_HAVERSINE_DISTANCE);
if (fp) {
fread(&saved_distance, sizeof(f64), 1, fp);
if (fabs(distance - saved_distance) > FLT_EPSILON) {
printf("%llu: %.16f does not equal %.16f\n", (unsigned long long)i,
distance, saved_distance);
}
}
sum += distance;
}
SAMPLE_END(PROFILER_ID_HAVERSINE_SUM);
SAMPLE_START(PROFILER_ID_HAVERSINE_AVG, "HAVERSINE AVERAGE");
printf("\nAVERAGE DISTANCE: %f\n", sum / pair_count);
SAMPLE_END(PROFILER_ID_HAVERSINE_AVG);
SAMPLE_START(PROFILER_ID_TEAR_DOWN, "TEAR DOWN");
if (fp) {
fclose(fp);
}
free(point_pairs);
free_json(&root);
SAMPLE_END(PROFILER_ID_TEAR_DOWN);
PROFILE_END;
return 0;
}