Commit a74623cd authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Logger: cleanup

parent edc3e6d5
......@@ -89,7 +89,7 @@ int header_get_field_index(const struct header *h, const char *field) {
*/
void header_change_offset_direction(struct header *h, enum logger_offset_direction new_value) {
h->offset_direction = new_value;
/* Skip file format and version numbers */
/* Skip file format and version numbers. */
size_t offset = LOGGER_VERSION_SIZE + 2 * sizeof(int);
logger_loader_io_write_data(h->log->log.map + offset, sizeof(unsigned int), &new_value);
......@@ -104,19 +104,19 @@ void header_change_offset_direction(struct header *h, enum logger_offset_directi
void header_read(struct header *h, struct logger_logfile *log) {
void *map = log->log.map;
/* Set pointer to log */
/* Set pointer to log. */
h->log = log;
/* read file format */
/* read the file format. */
char file_format[STRING_SIZE];
map = logger_loader_io_read_data(map, LOGGER_VERSION_SIZE, &file_format);
if (strcmp(file_format, "SWIFT_LOGGER"))
error("Wrong file format (%s)", file_format);
/* Read major version number */
/* Read the major version number. */
map = logger_loader_io_read_data(map, sizeof(int), &h->major_version);
/* Read minor version number */
/* Read the minor version number. */
map = logger_loader_io_read_data(map, sizeof(int), &h->minor_version);
struct logger_reader *reader = log->reader;
......@@ -126,43 +126,43 @@ void header_read(struct header *h, struct logger_logfile *log) {
if (reader->verbose > 0)
message("File version %i.%i", h->major_version, h->minor_version);
/* read the offset directions */
/* Read the offset directions. */
map = logger_loader_io_read_data(map, sizeof(int), &h->offset_direction);
if (!header_is_forward(h) && !header_is_backward(h) &&
!header_is_corrupted(h))
error("Wrong offset value in the header (%i)", h->offset_direction);
/* read offset to first record */
/* Read offset to first record. */
map = logger_loader_io_read_data(map, LOGGER_OFFSET_SIZE, &h->offset_first_record);
/* read the size of the strings */
/* Read the size of the strings. */
map = logger_loader_io_read_data(map, sizeof(unsigned int), &h->string_length);
/* check if value defined in this file is large enough. */
/* Check if value defined in this file is large enough. */
if (STRING_SIZE < h->string_length) {
error("Name too large in log file %i", h->string_length);
}
/* read the number of masks. */
/* Read the number of masks. */
map = logger_loader_io_read_data(map, sizeof(unsigned int), &h->number_mask);
/* allocate the masks memory. */
/* Allocate the masks memory. */
h->masks = malloc(sizeof(struct mask_data) * h->number_mask);
/* loop over all masks. */
/* Loop over all masks. */
for (size_t i = 0; i < h->number_mask; i++) {
/* read the mask name. */
/* Read the mask name. */
map = logger_loader_io_read_data(map, h->string_length, h->masks[i].name);
/* set the mask value. */
/* Set the mask value. */
h->masks[i].mask = 1 << i;
/* read the mask data size. */
/* Read the mask data size. */
map = logger_loader_io_read_data(map, sizeof(unsigned int), &h->masks[i].size);
}
/* Check the logfile header's size */
/* Check the logfile header's size. */
if (map != log->log.map + h->offset_first_record) {
header_print(h);
size_t offset = map - log->log.map;
......@@ -181,7 +181,7 @@ void header_read(struct header *h, struct logger_logfile *log) {
*/
size_t header_get_record_size_from_mask(const struct header *h, const size_t mask) {
size_t count = 0;
/* Loop over each masks */
/* Loop over each masks. */
for (size_t i = 0; i < h->number_mask; i++) {
if (mask & h->masks[i].mask) {
count += h->masks[i].size;
......
......@@ -32,7 +32,7 @@ enum logger_offset_direction {
logger_offset_backward = 0,
logger_offset_forward,
logger_offset_corrupted,
/* Number of offset type */
/* Number of offset type. */
logger_offset_count,
};
......@@ -56,10 +56,10 @@ struct logger_logfile;
* The only function that modify the file is #header_change_offset_direction.
*/
struct header {
/* Dump's major version */
/* Dump's major version. */
int major_version;
/* Dump's minor version */
/* Dump's minor version. */
int minor_version;
/* Offset of the first record. */
......@@ -77,7 +77,7 @@ struct header {
/* Direction of the offset in the records. */
enum logger_offset_direction offset_direction;
/* The corresponding log */
/* The corresponding log. */
struct logger_logfile *log;
};
......
......@@ -70,7 +70,7 @@ void logger_index_init(struct logger_index *index, struct logger_reader *reader,
*/
void logger_index_free(struct logger_index *index) {
/* unmap file */
/* unmap file. */
logger_loader_io_munmap_file(index->data, index->file_size);
/* Set variables to default value. */
......
......@@ -30,10 +30,10 @@ struct logger_reader;
struct logger_index_data {
/* The particle's id */
/* The particle's id. */
uint64_t id;
/* The particle's offset */
/* The particle's offset. */
uint64_t offset;
};
......@@ -45,25 +45,25 @@ struct logger_index_data {
* #logger_index_free.
*/
struct logger_index {
/* The reader */
/* The reader. */
struct logger_reader *reader;
/* memory map of the index file */
/* memory map of the index file. */
void *data;
/* Index file size */
/* Index file size. */
size_t file_size;
/* Number of particles */
/* Number of particles. */
size_t total_number_particles;
/* Number of particles per type */
/* Number of particles per type. */
long long number_particles[swift_type_count];
/* Time of the index file */
/* Time of the index file. */
double time;
/* Integer time of the index file */
/* Integer time of the index file. */
integertime_t int_time;
};
......
......@@ -88,7 +88,7 @@ void *logger_loader_io_mmap_file(char *filename, size_t *file_size, int read_onl
*
*/
void logger_loader_io_munmap_file(void *map, size_t file_size) {
/* unmap */
/* unmap the file. */
if (munmap(map, file_size) != 0) {
error("Unable to unmap the file (%s)", strerror(errno));
}
......
......@@ -38,7 +38,7 @@ void logger_logfile_init_from_file(
if (&reader->log != log)
error("Wrong link to the reader.");
/* Set pointers to zero */
/* Set pointers to zero. */
time_array_init(&log->times);
/* Open file, map it and get its size. */
......@@ -58,26 +58,26 @@ void logger_logfile_init_from_file(
}
/* No need to continue if only the
header is required */
header is required. */
if (only_header)
return;
/* Check if the offset are corrupted */
/* Check if the offset are corrupted. */
if (header_is_corrupted(&log->header)) {
error("The offsets have been corrupted");
}
/* Reverse the offsets direction */
/* Reverse the offsets direction. */
if (header_is_backward(&log->header)) {
logger_logfile_reverse_offset(log, filename);
}
/* Initialize the time array */
/* Initialize the time array. */
if (reader->verbose > 1)
message("Reading the time stamps.");
time_array_populate(&log->times, log);
/* Print the time array */
/* Print the time array. */
if (reader->verbose > 0) {
time_array_print(&log->times);
}
......@@ -104,7 +104,7 @@ void logger_logfile_free(struct logger_logfile *log) {
*/
void logger_logfile_reverse_offset(struct logger_logfile *log, char *filename) {
/* Close and reopen the file in write mode */
/* Close and reopen the file in write mode. */
logger_loader_io_munmap_file(log->log.map, log->log.file_size);
log->log.map = logger_loader_io_mmap_file(
filename, &log->log.file_size, /* read_only */ 0);
......@@ -115,7 +115,7 @@ void logger_logfile_reverse_offset(struct logger_logfile *log, char *filename) {
if (&reader->log != log)
error("Wrong link to the reader.");
/* Check if the offsets need to be reversed */
/* Check if the offsets need to be reversed. */
if (!header_is_backward(header)) {
error("The offsets are already reversed.");
}
......@@ -175,7 +175,7 @@ void logger_logfile_reverse_offset(struct logger_logfile *log, char *filename) {
}
#endif
/* Close and reopen the file in read mode */
/* Close and reopen the file in read mode. */
logger_loader_io_munmap_file(log->log.map, log->log.file_size);
log->log.map = logger_loader_io_mmap_file(
filename, &log->log.file_size, /* read_only */ 1);
......
......@@ -46,15 +46,15 @@ struct logger_logfile {
/* The reader that is using this log file. */
struct logger_reader *reader;
/* Information about the time records */
/* Information about the time records. */
struct time_array times;
/* The log's variables. */
struct {
/* Mapped data */
/* Mapped data. */
void *map;
/* File size */
/* File size. */
size_t file_size;
} log;
......
......@@ -77,7 +77,7 @@ void* logger_particle_read_field(struct logger_particle *part, void *map,
const char *field, const size_t size) {
void *p = NULL;
/* Get the correct pointer */
/* Get the correct pointer. */
if (strcmp("positions", field) == 0) {
p = &part->pos;
} else if (strcmp("velocities", field) == 0) {
......@@ -96,10 +96,10 @@ void* logger_particle_read_field(struct logger_particle *part, void *map,
error("Type %s not defined", field);
}
/* read the data */
/* read the data. */
map = logger_loader_io_read_data(map, size, p);
/* Split the required fields */
/* Split the required fields. */
if (strcmp("consts", field) == 0) {
part->mass = 0;
part->id = 0;
......@@ -128,7 +128,7 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
size_t offset, const double time,
const enum logger_reader_type reader_type) {
/* Get a few pointers */
/* Get a few pointers. */
const struct header *h = &reader->log.header;
void *map = reader->log.log.map;
......@@ -139,13 +139,13 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
logger_particle_init(part);
/* Read the record mask */
/* Read the record's mask. */
map = logger_loader_io_read_mask(h, map + offset, &mask, &h_offset);
/* Check if it is not a time record */
if (mask != 127) error("Unexpected mask: %lu", mask);
/* Check if it is not a time record. */
if (mask == 128) error("Unexpected mask: %lu", mask);
/* Read all the fields */
/* Read all the fields. */
for (size_t i = 0; i < h->number_mask; i++) {
if (mask & h->masks[i].mask) {
map = logger_particle_read_field(part, map, h->masks[i].name,
......@@ -160,7 +160,7 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
else
part->time = -1;
/* update the offset */
/* update the offset. */
offset = (size_t) (map - reader->log.log.map);
/* Check if an interpolation is required. */
......@@ -183,7 +183,7 @@ size_t logger_particle_read(struct logger_particle *part, const struct logger_re
h_offset += offset - header_get_record_size_from_mask(h, mask) - LOGGER_MASK_SIZE -
LOGGER_OFFSET_SIZE;
/* Get time of next record */
/* Get time of next record. */
part_next.time = time_array_get_time(times, h_offset);
/* Read next record. */
......@@ -232,7 +232,7 @@ void logger_particle_interpolate(
double tmp;
float ftmp;
/* interpolate vectors */
/* interpolate vectors. */
for (size_t i = 0; i < DIM; i++) {
tmp = (part_next->pos[i] - part_curr->pos[i]);
part_curr->pos[i] += tmp * scaling;
......@@ -244,10 +244,10 @@ void logger_particle_interpolate(
part_curr->acc[i] += ftmp * scaling;
}
/* interpolate scalars */
/* interpolate scalars. */
ftmp = (part_next->entropy - part_curr->entropy);
part_curr->entropy += ftmp * scaling;
/* set time */
/* set time. */
part_curr->time = time;
}
......@@ -52,31 +52,31 @@ struct logger_reader;
* interpolation is required.
*/
struct logger_particle {
/* position */
/* position. */
double pos[DIM];
/* velocity */
/* velocity. */
float vel[DIM];
/* acceleration */
/* acceleration. */
float acc[DIM];
/* entropy */
/* entropy. */
float entropy;
/* smoothing length */
/* smoothing length. */
float h;
/* density */
/* density. */
float density;
/* mass */
/* mass. */
float mass;
/* id */
/* unique id. */
size_t id;
/* time */
/* time of the record. */
double time;
};
......@@ -84,8 +84,8 @@ struct logger_particle {
* @brief Defines the type of interpolation
*/
enum logger_reader_type {
logger_reader_const, /* Constant interpolation */
logger_reader_lin, /* Linear interpolation */
logger_reader_const, /* Constant interpolation. */
logger_reader_lin, /* Linear interpolation. */
};
void logger_particle_print(const struct logger_particle *p);
......
......@@ -44,11 +44,11 @@
static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
PyObject *args) {
/* input */
/* input variables. */
PyArrayObject *offset = NULL;
char *filename = NULL;
/* output */
/* output variables. */
PyArrayObject *pos = NULL;
PyArrayObject *vel = NULL;
PyArrayObject *acc = NULL;
......@@ -61,8 +61,7 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
size_t time_offset;
int verbose = 2;
/* parse arguments */
/* parse arguments. */
if (!PyArg_ParseTuple(args, "OsL|i", &offset, &filename, &time_offset,
&verbose))
return NULL;
......@@ -77,12 +76,12 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
error("Offset does not contain unsigned int");
}
/* initialize the reader */
/* initialize the reader. */
struct logger_reader reader;
logger_reader_init(&reader, filename, verbose);
struct header *h = &reader.log.header;
/* init array */
/* init array. */
npy_intp dim[2];
dim[0] = PyArray_DIMS(offset)[0];
dim[1] = DIM;
......@@ -90,7 +89,7 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
/* Get required time. */
double time = time_array_get_time(&reader.log.times, time_offset);
/* init output */
/* init output. */
if (header_get_field_index(h, "positions") != -1) {
pos = (PyArrayObject *)PyArray_SimpleNew(2, dim, NPY_DOUBLE);
}
......@@ -127,19 +126,21 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
if (verbose > 1)
message("Reading particles.");
/* loop over all particles */
/* loop over all particles. */
for (npy_intp i = 0; i < PyArray_DIMS(offset)[0]; i++) {
struct logger_particle part;
/* Get the offset. */
size_t offset_particle = *(size_t *)PyArray_GETPTR1(offset, i);
/* Read the particle. */
logger_particle_read(&part, &reader, offset_particle, time, logger_reader_lin);
double *dtmp;
float *ftmp;
size_t *stmp;
/* copy data */
/* copy the data. */
for (size_t k = 0; k < DIM; k++) {
if (pos) {
dtmp = PyArray_GETPTR2(pos, i, k);
......@@ -183,10 +184,10 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
}
}
/* Free the memory */
/* Free the memory. */
logger_reader_free(&reader);
/* construct return value */
/* construct return value. */
PyObject *dict = PyDict_New();
PyObject *key = PyUnicode_FromString("positions");
PyDict_SetItem(dict, key, PyArray_Return(pos));
......@@ -237,26 +238,25 @@ static PyObject *loadFromIndex(__attribute__((unused)) PyObject *self,
*/
static PyObject *pyReverseOffset(__attribute__((unused)) PyObject *self,
PyObject *args) {
/* input */
/* input variables. */
char *filename = NULL;
int verbose = 0;
/* parse arguments */
/* parse the arguments. */
if (!PyArg_ParseTuple(args, "s|i", &filename, &verbose)) return NULL;
/* initialize the reader (and reverse the offset if necessary) */
/* initialize the reader which reverse the offset if necessary. */
struct logger_reader reader;
logger_reader_init(&reader, filename, verbose);
/* Free the reader */
/* Free the reader. */
logger_reader_free(&reader);
return Py_BuildValue("");
}
/* definition of the method table */
/* definition of the method table. */
static PyMethodDef libloggerMethods[] = {
{"loadFromIndex", loadFromIndex, METH_VARARGS,
......
......@@ -30,10 +30,10 @@ void logger_reader_init(struct logger_reader *reader, char *filename, int verbos
if (verbose > 1)
message("Initializing the reader");
/* Initialize the reader variables */
/* Initialize the reader variables. */
reader->verbose = verbose;
/* Initialize the log file */
/* Initialize the log file. */
logger_logfile_init_from_file(&reader->log, filename, reader,
/* only_header */ 0);
......@@ -47,6 +47,44 @@ void logger_reader_init(struct logger_reader *reader, char *filename, int verbos
* @param reader The #logger_reader.
*/
void logger_reader_free(struct logger_reader *reader) {
/* Free the log */
/* Free the log. */
logger_logfile_free(&reader->log);
}
/**
* @brief Read a record (timestamp or particle)
*
* @param reader The #reader.
* @param lp (out) The #logger_particle (if the record is a particle).
* @param time (out) The time read (if the record is a timestamp).
* @param is_particle Is the record a particle (or a timestamp)?
* @param offset The offset in the file.
*
* @return The offset after this record.
*/
size_t reader_read_record(struct logger_reader *reader, struct logger_particle *lp,
double *time, int *is_particle, size_t offset) {
struct logger_logfile *log = &reader->log;
/* Read mask to find out if timestamp or particle. */
size_t mask = 0;
logger_loader_io_read_mask(&log->header, log->log.map + offset, &mask, NULL);
/* Check if timestamp or not. */
int ind = header_get_field_index(&log->header, "timestamp");
if (ind == -1) {
error("File header does not contain a mask for time");
}
if (log->header.masks[ind].mask == mask) {
*is_particle = 0;
integertime_t int_time = 0;
offset = time_read(&int_time, time, reader, offset);
}
else {
*is_particle = 1;
offset = logger_particle_read(lp, reader, offset, *time, logger_reader_const);
}
return offset;
}
......@@ -41,8 +41,10 @@
#ifndef __LOGGER_LOGGER_READER_H__
#define __LOGGER_LOGGER_READER_H__
#include "logger_loader_io.h"
#include "logger_logfile.h"
#include "logger_index.h"
#include "logger_particle.h"
/**
* @brief Main structure of the logger.
......@@ -55,20 +57,21 @@
*/
struct logger_reader {
/* Time of each index file #TODO */
/* Time of each index file. #TODO */
double *times;
/* Information contained in the index file */
/* Information contained in the index file. */
struct logger_index index;
/* Informations contained in the file header */
/* Informations contained in the file header. */
struct logger_logfile log;
/* Level of verbosity */
/* Level of verbosity. */
int verbose;
};
void logger_reader_init(struct logger_reader *reader, char *filename, int verbose);
void logger_reader_free(struct logger_reader *reader);
size_t reader_read_record(struct logger_reader *reader, struct logger_particle *lp,
double *time, int *is_particle, size_t offset);
#endif // __LOGGER_LOGGER_READER_H__
......@@ -42,7 +42,7 @@ size_t time_read(integertime_t *int_time, double *time, const struct logger_read
*int_time = 0;