#define _POSIX_C_SOURCE 199309L #include #include #include #include "common.h" double monotonic_seconds() { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ts.tv_sec + ts.tv_nsec * 1e-9; } /** * @brief Output the seconds elapsed while execution. * * @param seconds Seconds spent on execution, excluding IO. */ void print_time(double const seconds) { printf("Execution time: %0.04fs\n", seconds); } struct data *read_data(char *path) { struct data *result = malloc(sizeof(struct data)); // Open file FILE *file; { file = fopen(path, "r"); if (!file) perror("failed to open file"); fscanf(file, "%u", &result->rows); fscanf(file, "%u", &result->dimensions); // Allocate result->buf = malloc(result->dimensions * result->rows * sizeof(double)); // Read into buffer for (uint32_t i = 0; i < result->dimensions; i++) { fscanf(file, "%lf", &result->buf[i]); } fclose(file); } return result; } struct labels *read_labels(char *path) { struct labels *result = malloc(sizeof(struct labels)); // Open file FILE *file; { file = fopen(path, "r"); if (!file) perror("failed to open file"); fscanf(file, "%u", &result->rows); // Allocate result->buf = malloc(result->rows * sizeof(double)); // Read into buffer for (uint32_t i = 0; i < result->rows; i++) { fscanf(file, "%lf", &result->buf[i]); } fclose(file); } return result; }