Commit 012f1222 authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Logger: Remove error codes and move toward the swift way

parent 35b4de8b
......@@ -9,6 +9,8 @@
/**
* @brief print a header struct
*
* @param h The #header
*/
void header_print(const struct header *h) {
#ifdef SWIFT_DEBUG_CHECKS
......@@ -30,12 +32,12 @@ void header_print(const struct header *h) {
printf("\tSize: %lu\n", h->masks_size[i]);
printf("\n");
}
/* mask contains... TODO */
};
/**
* @brief free allocated memory
*
* @param h The #header
*/
void header_free(struct header *h) {
for (size_t i = 0; i < h->nber_mask; i++) {
......@@ -49,11 +51,12 @@ void header_free(struct header *h) {
/**
* @brief check if field is present in header
*
* @param h The #header
* @param field name of the requested field
* @param ind (return value) indice of the requested field
*/
int header_is_present_and_get_index(const struct header *h, const char *field,
size_t *ind) {
int header_field_is_present(const struct header *h, const char *field,
size_t *ind) {
for (size_t i = 0; i < h->nber_mask; i++) {
if (strcmp(h->masks_name[i], field) == 0) {
if (ind != NULL) {
......@@ -66,28 +69,18 @@ int header_is_present_and_get_index(const struct header *h, const char *field,
return 0;
};
/**
* @brief check if field is present in header
*
* @param field name of the requested field
*/
int header_is_present(const struct header *h, const char *field) {
return header_is_present_and_get_index(h, field, NULL);
};
/**
* @brief Inverse the offset direction
*
* @param h #header file structure
* @param map file mapping
*
* @return error code
*/
int header_change_offset_direction(struct header *h, void *map) {
void header_change_offset_direction(struct header *h, void *map) {
h->forward_offset = !h->forward_offset;
size_t offset = LOGGER_VERSION_SIZE;
return io_write_data(map, LOGGER_NBER_SIZE, &h->forward_offset, &offset);
io_write_data(map, LOGGER_NBER_SIZE, &h->forward_offset, &offset);
}
/**
......@@ -96,7 +89,7 @@ int header_change_offset_direction(struct header *h, void *map) {
* @param h out: header
* @param map file mapping
*/
int header_read(struct header *h, void *map) {
void header_read(struct header *h, void *map) {
size_t offset = 0;
/* read version */
......@@ -107,7 +100,7 @@ int header_read(struct header *h, void *map) {
io_read_data(map, LOGGER_NBER_SIZE, &h->forward_offset, &offset);
if (h->forward_offset != 0 && h->forward_offset != 1)
error(EIO, "Non boolean value for the offset direction (%i)",
error("Non boolean value for the offset direction (%i)",
h->forward_offset);
/* read offset to first data */
......@@ -120,7 +113,7 @@ int header_read(struct header *h, void *map) {
/* check if value defined in this file is large enough */
if (STRING_SIZE < h->name) {
error(EOVERFLOW, "Name too large in dump file");
error("Name too large in dump file");
}
/* read number of masks */
......@@ -150,11 +143,10 @@ int header_read(struct header *h, void *map) {
#ifdef SWIFT_DEBUG_CHECKS
header_print(h);
#endif
error(EIO, "Wrong header size (in header %li, current %li)",
error("Wrong header size (in header %li, current %li)",
h->offset_first, offset);
}
return 0;
};
/**
......
......@@ -39,11 +39,10 @@ struct header {
void header_print(const struct header *h);
void header_free(struct header *h);
int header_is_present_and_get_index(const struct header *h, const char *field,
size_t *ind);
int header_is_present(const struct header *h, const char *field);
int header_read(struct header *h, void *map);
int header_field_is_present(const struct header *h, const char *field,
size_t *ind);
void header_read(struct header *h, void *map);
size_t header_get_mask_size(const struct header *h, const size_t mask);
int header_change_offset_direction(struct header *h, void *map);
void header_change_offset_direction(struct header *h, void *map);
#endif // __LOGGER_HEADER_H__
......@@ -13,15 +13,12 @@
* @param fd file id
* @param size out: file size
*
* @return error code
*/
int io_get_file_size(int fd, size_t *size) {
void io_get_file_size(int fd, size_t *size) {
struct stat s;
int status = fstat(fd, &s);
if (status != 0) error(errno, "Unable to get file size");
if (status != 0) error("Unable to get file size (%s)", strerror(errno));
*size = s.st_size;
return 0;
}
/**
......@@ -31,24 +28,20 @@ int io_get_file_size(int fd, size_t *size) {
* @param fd out: file id
* @param map out: file mapping
*
* @return error code
*/
int io_open_file(char *filename, int *fd, void **map) {
void io_open_file(char *filename, int *fd, void **map) {
/* open file */
*fd = open(filename, O_RDWR);
if (*fd == -1) error(errno, "Unable to open file %s", filename);
if (*fd == -1) error("Unable to open file %s (%s)", filename, strerror(errno));
/* get file size */
size_t size = 0;
int status = io_get_file_size(*fd, &size);
if (status != 0) return status;
io_get_file_size(*fd, &size);
/* map memory */
*map = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, *fd, 0);
if (map == MAP_FAILED)
error(errno, "Failed to allocate map of size %zi bytes.", size);
return 0;
error("Failed to allocate map of size %zi bytes. (%s)", size, strerror(errno));
}
/**
......@@ -57,20 +50,18 @@ int io_open_file(char *filename, int *fd, void **map) {
* @param fd file id
* @param map file mapping
*
* @return error code
*/
int io_close_file(int *fd, void **map) {
void io_close_file(int *fd, void **map) {
/* get file size */
size_t size = 0;
int status = io_get_file_size(*fd, &size);
if (status != 0) return status;
io_get_file_size(*fd, &size);
/* unmap */
if (munmap(*map, size) != 0) error(errno, "Unable to unmap the file");
if (munmap(*map, size) != 0) {
error("Unable to unmap the file (%s)", strerror(errno));
}
close(*fd);
return 0;
}
/**
......@@ -83,10 +74,9 @@ int io_close_file(int *fd, void **map) {
* @param mask mask read
* @param diff_offset offset difference to previous/next corresponding chunk
*
* @return error code
*/
int io_read_mask(const struct header *h, void *map, size_t *offset,
size_t *mask, size_t *diff_offset) {
void io_read_mask(const struct header *h, void *map, size_t *offset,
size_t *mask, size_t *diff_offset) {
/* read mask */
if (mask) {
*mask = 0;
......@@ -100,8 +90,6 @@ int io_read_mask(const struct header *h, void *map, size_t *offset,
memcpy(diff_offset, map + *offset, LOGGER_OFFSET_SIZE);
}
*offset += LOGGER_OFFSET_SIZE;
return 0;
}
/**
......@@ -111,14 +99,10 @@ int io_read_mask(const struct header *h, void *map, size_t *offset,
* @param size size of the chunk to read
* @param p pointer where to store the data
* @param offset In: position to read, Out: shifted by size
*
* @return error code
*/
int io_read_data(void *map, const size_t size, void *p, size_t *offset) {
void io_read_data(void *map, const size_t size, void *p, size_t *offset) {
memcpy(p, map + *offset, size);
*offset += size;
return 0;
};
/**
......@@ -129,11 +113,8 @@ int io_read_data(void *map, const size_t size, void *p, size_t *offset) {
* @param p pointer to the data
* @param offset In: position to write, Out: shifted by size
*
* @return error code
*/
int io_write_data(void *map, const size_t size, const void *p, size_t *offset) {
void io_write_data(void *map, const size_t size, const void *p, size_t *offset) {
memcpy(map + *offset, p, size);
*offset += size;
return 0;
};
......@@ -7,12 +7,12 @@
#include <stdio.h>
#include <stdlib.h>
int io_get_file_size(int fd, size_t *size);
int io_open_file(char *filename, int *fd, void **map);
int io_close_file(int *fd, void **map);
int io_read_data(void *map, const size_t size, void *p, size_t *offset);
int io_write_data(void *map, const size_t size, const void *p, size_t *offset);
int io_read_mask(const struct header *h, void *map, size_t *offset,
void io_get_file_size(int fd, size_t *size);
void io_open_file(char *filename, int *fd, void **map);
void io_close_file(int *fd, void **map);
void io_read_data(void *map, const size_t size, void *p, size_t *offset);
void io_write_data(void *map, const size_t size, const void *p, size_t *offset);
void io_read_mask(const struct header *h, void *map, size_t *offset,
size_t *mask, size_t *diff_offset);
#endif // __SWIFT_LOGGER_IO_H__
......@@ -25,18 +25,6 @@ void particle_print(const struct particle *p) {
printf("Density: %g\n", p->density);
}
/**
* @brief Check if dump data type are compatible with the particle type
*
* @param h #header structure of the file
*
* @return error code
*/
int particle_check_data_type(__attribute__((unused)) const struct header *h) {
printf("TODO check_data_type\n");
return 1;
}
/**
* @brief initialize a particle
*
......@@ -66,10 +54,9 @@ void particle_init(struct particle *part) {
* @param field field to read
* @param size number of bits to read
*
* @return error code
*/
int particle_read_field(struct particle *part, void *map, size_t *offset,
const char *field, const size_t size) {
void particle_read_field(struct particle *part, void *map, size_t *offset,
const char *field, const size_t size) {
void *p = NULL;
if (strcmp("positions", field) == 0) {
......@@ -87,12 +74,10 @@ int particle_read_field(struct particle *part, void *map, size_t *offset,
} else if (strcmp("consts", field) == 0) {
p = malloc(size);
} else {
error(ENOTSUP, "Type %s not defined", field);
error("Type %s not defined", field);
}
int error_code = io_read_data(map, size, p, offset);
if (error_code != 0) return error_code;
io_read_data(map, size, p, offset);
if (strcmp("consts", field) == 0) {
part->mass = 0;
......@@ -103,7 +88,6 @@ int particle_read_field(struct particle *part, void *map, size_t *offset,
p -= sizeof(float);
free(p);
}
return error_code;
}
/**
......@@ -117,27 +101,23 @@ int particle_read_field(struct particle *part, void *map, size_t *offset,
* @param reader #reader_type
* @param times #time_array times in the dump
*
* @return error code
*/
int particle_read(struct particle *part, const struct header *h, void *map,
size_t *offset, const double time, const int reader,
struct time_array *times) {
int error_code = 0;
void particle_read(struct particle *part, const struct header *h, void *map,
size_t *offset, const double time, const int reader,
struct time_array *times) {
size_t mask = 0;
size_t h_offset = 0;
particle_init(part);
error_code = io_read_mask(h, map, offset, &mask, &h_offset);
if (error_code != 0) return error_code;
if (mask != 127) error(EIO, "Unexpected mask: %lu", mask);
io_read_mask(h, map, offset, &mask, &h_offset);
if (mask != 127) error("Unexpected mask: %lu", mask);
for (size_t i = 0; i < h->nber_mask; i++) {
if (mask & h->masks[i]) {
error_code = particle_read_field(part, map, offset, h->masks_name[i],
particle_read_field(part, map, offset, h->masks_name[i],
h->masks_size[i]);
if (error_code != 0) return error_code;
}
}
......@@ -147,14 +127,14 @@ int particle_read(struct particle *part, const struct header *h, void *map,
part->time = -1;
/* end of const case */
if (reader == reader_const) return 0;
if (reader == reader_const) return;
/* read next particle */
struct particle part_next;
if (!h->forward_offset) error(ENOSYS, "TODO");
if (!h->forward_offset) error("TODO");
if (h_offset == 0) return 0;
if (h_offset == 0) return;
/* get absolute offset of next particle */
h_offset += *offset - header_get_mask_size(h, mask) - LOGGER_MASK_SIZE -
LOGGER_OFFSET_SIZE;
......@@ -162,14 +142,10 @@ int particle_read(struct particle *part, const struct header *h, void *map,
part_next.time = time_array_get_time(times, h_offset);
/* previous part exists */
error_code = particle_read(&part_next, h, map, &h_offset, part_next.time,
reader_const, times);
if (error_code != 0) return error_code;
particle_read(&part_next, h, map, &h_offset, part_next.time,
reader_const, times);
error_code = particle_interpolate(part, &part_next, time);
if (error_code != 0) return error_code;
return 0;
particle_interpolate(part, &part_next, time);
}
/**
......@@ -180,20 +156,18 @@ int particle_read(struct particle *part, const struct header *h, void *map,
* @param part_next #particle next particle (after time)
* @param time interpolation time
*
* @return error code
*/
int particle_interpolate(struct particle *part_curr,
const struct particle *part_next, const double time) {
void particle_interpolate(struct particle *part_curr,
const struct particle *part_next, const double time) {
if (!part_curr) error(EFAULT, "part_curr is NULL");
if (!part_next) error(EFAULT, "part_next is NULL");
if (!part_curr) error("part_curr is NULL");
if (!part_next) error("part_next is NULL");
#ifdef SWIFT_DEBUG_CHECKS
if (part_next->time <= part_curr->time)
error(EIO, "Wrong particle order (next before current)");
error("Wrong particle order (next before current)");
if ((time < part_curr->time) || (part_next->time < time))
error(EIO,
"Interpolating, not extrapolating (particle time: %f, "
error("Interpolating, not extrapolating (particle time: %f, "
"interpolating time: %f, next particle time: %f)",
part_curr->time, time, part_next->time);
#endif
......@@ -223,6 +197,4 @@ int particle_interpolate(struct particle *part_curr,
/* set time */
part_curr->time = time;
return 0;
}
......@@ -46,17 +46,16 @@ enum reader_type {
void particle_print(const struct particle *p);
int particle_read(struct particle *part, const struct header *h, void *map,
void particle_read(struct particle *part, const struct header *h, void *map,
size_t *offset, const double time, const int reader,
struct time_array *times);
int particle_check_data_type(const struct header *h);
void particle_init(struct particle *part);
int particle_read_field(struct particle *part, void *map, size_t *offset,
void particle_read_field(struct particle *part, void *map, size_t *offset,
const char *field, const size_t size);
int particle_interpolate(struct particle *part_curr,
void particle_interpolate(struct particle *part_curr,
const struct particle *part_next, const double time);
#endif //__PARTICLE_H__
......@@ -44,44 +44,41 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
return NULL;
if (!PyArray_Check(offset)) {
error_no_return(ENOTSUP, "Offset is not a numpy array");
return NULL;
error("Offset is not a numpy array");
}
if (PyArray_NDIM(offset) != 1) {
error_no_return(ENOTSUP, "Offset is not a 1 dimensional array");
return NULL;
error("Offset is not a 1 dimensional array");
}
if (PyArray_TYPE(offset) != NPY_UINT64) {
error_no_return(ENOTSUP, "Offset does not contain unsigned int");
return NULL;
error("Offset does not contain unsigned int");
}
/* open file */
int fd;
void *map;
if (io_open_file(filename, &fd, &map) != 0) return NULL;
io_open_file(filename, &fd, &map);
/* read header */
if (header_read(&h, map) != 0) return NULL;
header_read(&h, map);
/* reverse offset if needed */
if (!h.forward_offset) {
if (io_close_file(&fd, &map) != 0) return NULL;
io_close_file(&fd, &map);
if (reverse_offset(filename) != 0) return NULL;
reverse_offset(filename);
if (io_open_file(filename, &fd, &map) != 0) return NULL;
io_open_file(filename, &fd, &map);
/* Reset header */
header_free(&h);
if (header_read(&h, map) != 0) return NULL;
header_read(&h, map);
}
/* read timestamps */
struct time_array times;
if (time_array_init(&times, &h, map, fd) != 0) return NULL;
time_array_init(&times, &h, map, fd);
time_array_print(&times);
/* get required time */
......@@ -93,56 +90,47 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
dim[1] = DIM;
/* init output */
if (header_is_present(&h, "positions")) {
if (header_field_is_present(&h, "positions", /* ind */ NULL)) {
pos = (PyArrayObject *)PyArray_SimpleNew(2, dim, NPY_DOUBLE);
}
if (header_is_present(&h, "velocities")) {
if (header_field_is_present(&h, "velocities", /* ind */ NULL)) {
vel = (PyArrayObject *)PyArray_SimpleNew(2, dim, NPY_FLOAT);
}
if (header_is_present(&h, "accelerations")) {
if (header_field_is_present(&h, "accelerations", /* ind */ NULL)) {
acc = (PyArrayObject *)PyArray_SimpleNew(2, dim, NPY_FLOAT);
}
if (header_is_present(&h, "entropy")) {
if (header_field_is_present(&h, "entropy", /* ind */ NULL)) {
entropy =
(PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_FLOAT);
}
if (header_is_present(&h, "smoothing length")) {
if (header_field_is_present(&h, "smoothing length", /* ind */ NULL)) {
h_sph =
(PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_FLOAT);
}
if (header_is_present(&h, "density")) {
if (header_field_is_present(&h, "density", /* ind */ NULL)) {
rho =
(PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_FLOAT);
}
if (header_is_present(&h, "consts")) {
if (header_field_is_present(&h, "consts", /* ind */ NULL)) {
mass =
(PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_FLOAT);
id = (PyArrayObject *)PyArray_SimpleNew(1, PyArray_DIMS(offset), NPY_ULONG);
}
int error_code = 0;
/* check data type in particles */
if (!particle_check_data_type(&h)) {
error_no_return(ENOTSUP, "Particle data type are not compatible");
return NULL;
}
/* loop over all particles */
for (npy_intp i = 0; i < PyArray_DIMS(offset)[0]; i++) {
struct particle part;
size_t *offset_particle = (size_t *)PyArray_GETPTR1(offset, i);
error_code = particle_read(&part, &h, map, offset_particle, time,
reader_lin, &times);
if (error_code != 0) return NULL;
particle_read(&part, &h, map, offset_particle, time,
reader_lin, &times);
double *dtmp;
float *ftmp;
......@@ -253,8 +241,8 @@ static PyObject *pyReverseOffset(__attribute__((unused)) PyObject *self,
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
if (reverse_offset(filename) != 0) return NULL;
reverse_offset(filename);
return Py_BuildValue("");
}
......
#include "logger_header.h"
#include "logger_io.h"
/**
* @brief Reverse offset in dump file
*
* @param filename string filename of the dump file
*/
void reverse_offset(char *filename) {
struct header h;
/* open file */
int fd;
void *map;
io_open_file(filename, &fd, &map);
/* read header */
header_read(&h, map);
header_print(&h);
/* check offset direction */
if (h.forward_offset) {
error("Offset are already reversed");
}
/* compute file size */
size_t sz;
io_get_file_size(fd, &sz);
size_t offset;
#ifdef SWIFT_DEBUG_CHECKS
/* check offset */
printf("Check offsets...\n");
offset = h.offset_first;
while (offset < sz) {
tools_check_offset(&h, map, &offset);
}