2023-09-23 05:04:06 +00:00
|
|
|
#define _POSIX_C_SOURCE 199309L
|
2023-10-08 00:49:58 +00:00
|
|
|
|
2023-09-23 05:04:06 +00:00
|
|
|
#include <stdio.h>
|
2023-10-06 06:21:15 +00:00
|
|
|
#include <stdlib.h>
|
2023-09-23 05:04:06 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2023-10-08 01:37:18 +00:00
|
|
|
double monotonic_seconds() {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return ts.tv_sec + ts.tv_nsec * 1e-9;
|
|
|
|
}
|
|
|
|
|
2023-09-23 05:04:06 +00:00
|
|
|
/**
|
|
|
|
* @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);
|
2023-10-06 06:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2023-10-08 01:37:18 +00:00
|
|
|
if (!fscanf(file, "%u", &result->rows))
|
|
|
|
perror("failed to read num rows");
|
|
|
|
if (!fscanf(file, "%u", &result->dimensions))
|
|
|
|
perror("failed to read num dimensions");
|
2023-10-06 06:21:15 +00:00
|
|
|
|
|
|
|
// Allocate
|
2023-10-08 00:49:58 +00:00
|
|
|
result->buf = malloc(result->dimensions * result->rows * sizeof(FLOAT));
|
2023-10-06 06:21:15 +00:00
|
|
|
|
|
|
|
// Read into buffer
|
2023-10-08 00:49:58 +00:00
|
|
|
for (uint32_t j = 0; j < result->rows; j++) {
|
|
|
|
for (uint32_t i = 0; i < result->dimensions; i++) {
|
2023-10-08 01:37:18 +00:00
|
|
|
if (!fscanf(file, FLOAT_FORMAT, &result->buf[result->rows * i + j]))
|
|
|
|
perror("failed to read data");
|
2023-10-08 00:49:58 +00:00
|
|
|
}
|
2023-10-06 06:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2023-10-08 01:37:18 +00:00
|
|
|
if (!fscanf(file, "%u", &result->rows))
|
|
|
|
perror("failed to read num rows");
|
2023-10-06 06:21:15 +00:00
|
|
|
|
|
|
|
// Allocate
|
2023-10-08 00:49:58 +00:00
|
|
|
result->buf = malloc(result->rows * sizeof(FLOAT));
|
2023-10-06 06:21:15 +00:00
|
|
|
|
|
|
|
// Read into buffer
|
|
|
|
for (uint32_t i = 0; i < result->rows; i++) {
|
2023-10-08 01:37:18 +00:00
|
|
|
if (!fscanf(file, FLOAT_FORMAT, &result->buf[i]))
|
|
|
|
perror("failed to read label");
|
2023-10-06 06:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2023-09-23 05:04:06 +00:00
|
|
|
}
|