logger.h 9.09 KB
Newer Older
1
2
3
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2017 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
Loic Hausammann's avatar
Loic Hausammann committed
4
 *               2018 Loic Hausammann (loic.hausammann@epfl.ch)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/
#ifndef SWIFT_LOGGER_H
#define SWIFT_LOGGER_H

Loic Hausammann's avatar
Loic Hausammann committed
23
24
#include "../config.h"

lhausamm's avatar
lhausamm committed
25
26
#ifdef WITH_LOGGER

27
/* Includes. */
lhausamm's avatar
lhausamm committed
28
#include "common_io.h"
29
#include "dump.h"
Loic Hausammann's avatar
Loic Hausammann committed
30
31
#include "inline.h"
#include "timeline.h"
lhausamm's avatar
lhausamm committed
32
#include "units.h"
33

Loic Hausammann's avatar
Loic Hausammann committed
34
/* Forward declaration. */
35
struct dump;
Loic Hausammann's avatar
Loic Hausammann committed
36
struct gpart;
37
struct part;
Loic Hausammann's avatar
Loic Hausammann committed
38
struct engine;
39

Loic Hausammann's avatar
Loic Hausammann committed
40
#define logger_major_version 0
Loic Hausammann's avatar
Loic Hausammann committed
41
#define logger_minor_version 3
Loic Hausammann's avatar
Loic Hausammann committed
42

43
44
/**
 * Logger entries contain messages representing the particle data at a given
45
 * point in time during the simulation.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 *
 * The logger messages always start with an 8-byte header structured as
 * follows:
 *
 *   data: [ mask |                     offset                     ]
 *   byte: [  01  |  02  |  03  |  04  |  05  |  06  |  07  |  08  ]
 *
 * I.e. a first "mask" byte followed by 7 "offset" bytes. The mask contains
 * information on what kind of data is packed after the header. The mask
 * bits correspond to the following data:
 *
 *   bit | name   | size | comment
 *   -------------------------------------------------------------------------
 *   0   | x      | 24   | The particle position, in absolute coordinates,
 *       |        |      | stored as three doubles.
 *   1   | v      | 12   | Particle velocity, stored as three floats.
 *   2   | a      | 12   | Particle acceleration, stored as three floats.
63
64
 *   3   | u      | 4    | Particle internal energy (or entropy, if Gadget-SPH
 *       |        |      | is used), stored as a single float.
65
66
 *   4   | h      | 4    | Particle smoothing length (or epsilon, if a gpart),
 *       |        |      | stored as a single float.
67
 *   5   | rho    | 4    | Particle density, stored as a single float.
68
69
 *   6   | consts | 12   | Particle constants, i.e. mass and ID.
 *   7   | time   | 8    | Timestamp, not associated with a particle, just
70
71
72
73
74
75
76
 *       |        |      | marks the transitions from one timestep to another.
 *
 * There is no distinction between gravity and SPH particles.
 *
 * The offset refers to the relative location of the previous message for the
 * same particle or for the previous timestamp (if mask bit 7 is set). I.e.
 * the previous log entry will be at the address of the current mask byte minus
lhausamm's avatar
lhausamm committed
77
78
 * the unsigned value stored in the offset. An offset equal to the chunk offset
 * indicated that this is the first message for the given particle/timestamp.
79
80
81
 */

/* Some constants. */
82
83
84
85
86
87
88
89
enum logger_masks_number {
  logger_x = 0,
  logger_v = 1,
  logger_a = 2,
  logger_u = 3,
  logger_h = 4,
  logger_rho = 5,
  logger_consts = 6,
Loic Hausammann's avatar
Loic Hausammann committed
90
  logger_special_flags = 7, /* Flag for special cases */
Loic Hausammann's avatar
Format    
Loic Hausammann committed
91
92
  logger_timestamp = 8,     /* expect it to be before count. */
  logger_count_mask = 9,    /* Need to be the last. */
Loic Hausammann's avatar
Loic Hausammann committed
93
} __attribute__((packed));
94

Loic Hausammann's avatar
Loic Hausammann committed
95
96
/* Defines some mask for logging all the fields */
enum logger_masks_all {
Loic Hausammann's avatar
Format    
Loic Hausammann committed
97
98
99
100
101
102
103
  logger_masks_all_part = (1 << logger_x) | (1 << logger_v) | (1 << logger_a) |
                          (1 << logger_u) | (1 << logger_h) |
                          (1 << logger_rho) | (1 << logger_consts),
  logger_masks_all_gpart = (1 << logger_x) | (1 << logger_v) | (1 << logger_a) |
                           (1 << logger_consts),
  logger_masks_all_spart =
      (1 << logger_x) | (1 << logger_v) | (1 << logger_consts),
Loic Hausammann's avatar
Loic Hausammann committed
104
105
} __attribute__((packed));

106
enum logger_special_flags {
Loic Hausammann's avatar
Loic Hausammann committed
107
  logger_flag_change_type = 1, /* Flag for a change of particle type */
Loic Hausammann's avatar
Format    
Loic Hausammann committed
108
109
110
111
112
  logger_flag_mpi_enter, /* Flag for a particle received from another  MPI rank
                          */
  logger_flag_mpi_exit,  /* Flag for a particle sent to another MPI rank */
  logger_flag_delete,    /* Flag for a deleted particle */
  logger_flag_create,    /* Flag for a created particle */
113
114
} __attribute__((packed));

115
struct mask_data {
Loic Hausammann's avatar
Loic Hausammann committed
116
  /* Number of bytes for a mask. */
117
  int size;
118

Loic Hausammann's avatar
Loic Hausammann committed
119
  /* Mask value. */
120
  unsigned int mask;
121

Loic Hausammann's avatar
Loic Hausammann committed
122
  /* Name of the mask. */
123
124
125
126
127
  char name[100];
};

extern const struct mask_data logger_mask_data[logger_count_mask];

128
129
/* Size of the strings. */
#define logger_string_length 200
Loic Hausammann's avatar
Loic Hausammann committed
130

Loic Hausammann's avatar
Loic Hausammann committed
131
132
133
/**
 * @brief structure containing global data for the particle logger.
 */
Loic Hausammann's avatar
Loic Hausammann committed
134
135
struct logger_writer {
  /* Number of particle steps between dumping a chunk of data. */
Loic Hausammann's avatar
Loic Hausammann committed
136
137
  short int delta_step;

Loic Hausammann's avatar
Loic Hausammann committed
138
  /* Logger basename. */
139
  char base_name[logger_string_length];
Loic Hausammann's avatar
Loic Hausammann committed
140

Loic Hausammann's avatar
Loic Hausammann committed
141
142
143
144
145
146
147
148
  struct {
    /* The total memory fraction reserved for the index files. */
    float mem_frac;

    /* Size of the dump since at the last output */
    size_t dump_size_last_output;
  } index;

Loic Hausammann's avatar
Loic Hausammann committed
149
150
  /*  Dump file (In the reader, the dump is cleaned, therefore it is renamed
   * logfile). */
151
  struct dump dump;
Loic Hausammann's avatar
Loic Hausammann committed
152

Loic Hausammann's avatar
Loic Hausammann committed
153
  /* timestamp offset for logger. */
Loic Hausammann's avatar
Loic Hausammann committed
154
155
  size_t timestamp_offset;

Loic Hausammann's avatar
Loic Hausammann committed
156
  /* scaling factor when buffer is too small. */
Loic Hausammann's avatar
Loic Hausammann committed
157
158
  float buffer_scale;

Loic Hausammann's avatar
Loic Hausammann committed
159
  /* Size of a chunk if every mask are activated. */
160
161
  int max_chunk_size;

Loic Hausammann's avatar
Loic Hausammann committed
162
163
} SWIFT_STRUCT_ALIGN;

Loic Hausammann's avatar
Loic Hausammann committed
164
/* required structure for each particle type. */
Loic Hausammann's avatar
Loic Hausammann committed
165
struct logger_part_data {
Loic Hausammann's avatar
Loic Hausammann committed
166
  /* Number of particle updates since last output. */
167
  int steps_since_last_output;
Loic Hausammann's avatar
Loic Hausammann committed
168

Loic Hausammann's avatar
Loic Hausammann committed
169
  /* offset of last particle log entry. */
Loic Hausammann's avatar
Loic Hausammann committed
170
  uint64_t last_offset;
Loic Hausammann's avatar
Loic Hausammann committed
171
172
};

173
/* Function prototypes. */
lhausamm's avatar
lhausamm committed
174
int logger_compute_chunk_size(unsigned int mask);
Loic Hausammann's avatar
Loic Hausammann committed
175
176
void logger_log_all(struct logger_writer *log, const struct engine *e);
void logger_log_part(struct logger_writer *log, const struct part *p,
177
                     struct xpart *xp, unsigned int mask,
178
                     const uint32_t special_flags);
Loic Hausammann's avatar
Loic Hausammann committed
179
void logger_log_parts(struct logger_writer *log, const struct part *p,
Loic Hausammann's avatar
Format    
Loic Hausammann committed
180
181
                      struct xpart *xp, int count, unsigned int mask,
                      const uint32_t special_flags);
182
183
void logger_log_spart(struct logger_writer *log, struct spart *p,
                      unsigned int mask, const uint32_t special_flags);
Loic Hausammann's avatar
Format    
Loic Hausammann committed
184
185
void logger_log_sparts(struct logger_writer *log, struct spart *sp, int count,
                       unsigned int mask, const uint32_t special_flags);
186
187
void logger_log_gpart(struct logger_writer *log, struct gpart *p,
                      unsigned int mask, const uint32_t special_flags);
Loic Hausammann's avatar
Format    
Loic Hausammann committed
188
189
void logger_log_gparts(struct logger_writer *log, struct gpart *gp, int count,
                       unsigned int mask, const uint32_t special_flags);
Loic Hausammann's avatar
Loic Hausammann committed
190
191
void logger_init(struct logger_writer *log, struct swift_params *params);
void logger_free(struct logger_writer *log);
Loic Hausammann's avatar
Loic Hausammann committed
192
193
void logger_log_timestamp(struct logger_writer *log, integertime_t t,
                          double time, size_t *offset);
Loic Hausammann's avatar
Loic Hausammann committed
194
void logger_ensure_size(struct logger_writer *log, size_t total_nr_parts,
Loic Hausammann's avatar
Format    
Loic Hausammann committed
195
                        size_t total_nr_gparts, size_t total_nr_sparts);
Loic Hausammann's avatar
Loic Hausammann committed
196
void logger_write_file_header(struct logger_writer *log);
lhausamm's avatar
lhausamm committed
197

Pedro Gonnet's avatar
Pedro Gonnet committed
198
199
int logger_read_part(struct part *p, size_t *offset, const char *buff);
int logger_read_gpart(struct gpart *p, size_t *offset, const char *buff);
200
201
int logger_read_timestamp(unsigned long long int *t, double *time,
                          size_t *offset, const char *buff);
Loic Hausammann's avatar
Loic Hausammann committed
202
203
void logger_struct_dump(const struct logger_writer *log, FILE *stream);
void logger_struct_restore(struct logger_writer *log, FILE *stream);
lhausamm's avatar
lhausamm committed
204

Loic Hausammann's avatar
Loic Hausammann committed
205
206
207
208
209
210
/**
 * @brief Generate the data for the special flags.
 *
 * @param flag The special flag to use.
 * @param data The data to write in the .
 */
Loic Hausammann's avatar
Format    
Loic Hausammann committed
211
212
INLINE static uint32_t logger_pack_flags_and_data(
    enum logger_special_flags flag, int data) {
213
214
#ifdef SWIFT_DEBUG_CHECKS
  if (flag & 0xFFFFFF00) {
Loic Hausammann's avatar
Format    
Loic Hausammann committed
215
216
217
    error(
        "The special flag in the particle logger cannot be larger than 1 "
        "byte.");
218
  }
Loic Hausammann's avatar
Loic Hausammann committed
219
  if (data & ~0xFFFFFF) {
Loic Hausammann's avatar
Format    
Loic Hausammann committed
220
221
222
    error(
        "The data for the special flag in the particle logger cannot be larger "
        "than 3 bytes.");
Loic Hausammann's avatar
Loic Hausammann committed
223
  }
224
#endif
Loic Hausammann's avatar
Format    
Loic Hausammann committed
225
  return ((uint32_t)flag << (3 * 8)) | (data & 0xFFFFFF);
226
227
}

228
229
230
231
232
233
234
/**
 * @brief Initialize the logger data for a particle.
 *
 * @param logger The #logger_part_data.
 */
INLINE static void logger_part_data_init(struct logger_part_data *logger) {
  logger->last_offset = 0;
235
  logger->steps_since_last_output = INT_MAX;
236
237
238
239
240
}

/**
 * @brief Should this particle write its data now ?
 *
Loic Hausammann's avatar
Loic Hausammann committed
241
 * @param logger_data The #logger_part_data of a particle.
Loic Hausammann's avatar
Loic Hausammann committed
242
 * @param log The #logger_writer.
Loic Hausammann's avatar
Loic Hausammann committed
243
 *
Loic Hausammann's avatar
Loic Hausammann committed
244
 * @return 1 if the particle should be writen, 0 otherwise.
245
246
 */
__attribute__((always_inline)) INLINE static int logger_should_write(
Loic Hausammann's avatar
Loic Hausammann committed
247
248
    const struct logger_part_data *logger_data,
    const struct logger_writer *log) {
249
250
251
252

  return (logger_data->steps_since_last_output > log->delta_step);
}

lhausamm's avatar
lhausamm committed
253
254
#endif /* WITH_LOGGER */

255
#endif /* SWIFT_LOGGER_H */