Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DLR-AMR
GitHub Repository: DLR-AMR/t8code
Path: blob/main/src/t8.h
898 views
1
/*
2
This file is part of t8code.
3
t8code is a C library to manage a collection (a forest) of multiple
4
connected adaptive space-trees of general element classes in parallel.
5
6
Copyright (C) 2015 the developers
7
8
t8code is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
13
t8code is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with t8code; if not, write to the Free Software Foundation, Inc.,
20
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
*/
22
23
/** \file t8.h
24
* This is the administrative header file for t8code.
25
* It includes standard C headers via subpackages.
26
* It also provides application-level convenience code such as logging functions.
27
*/
28
29
#ifndef T8_H
30
#define T8_H
31
32
#include <t8_with_macro_error.h>
33
#ifdef __cplusplus
34
#include <cinttypes>
35
#else
36
#include <inttypes.h>
37
#endif
38
39
#include <sc_config.h>
40
#if (defined(T8_ENABLE_MPI) && !defined(SC_ENABLE_MPI)) || (!defined(T8_ENABLE_MPI) && defined(SC_ENABLE_MPI))
41
#error "MPI configured differently in t8code and libsc"
42
#endif
43
#if (defined(T8_ENABLE_MPIIO) && !defined(SC_ENABLE_MPIIO)) || (!defined(T8_ENABLE_MPIIO) && defined(SC_ENABLE_MPIIO))
44
#error "MPI I/O configured differently in t8code and libsc"
45
#endif
46
47
/* indirectly also include sc.h */
48
#include <sc_containers.h>
49
50
/** This macro opens the extern "C" brace needed in C headers.
51
* It needs to be followed by a semicolon to look like a statement. */
52
#define T8_EXTERN_C_BEGIN() SC_EXTERN_C_BEGIN
53
54
/** This macro closes the extern "C" brace needed in C headers.
55
* It needs to be followed by a semicolon to look like a statement. */
56
#define T8_EXTERN_C_END() SC_EXTERN_C_END
57
58
/** Call this after including all headers */
59
T8_EXTERN_C_BEGIN ();
60
61
/** Portable way to use the const keyword determined by configure. */
62
#define t8_const _sc_const
63
64
/** Portable way to use the const keyword determined by configure. */
65
#define t8_restrict _sc_restrict
66
67
/** Widely used assertion. Only active in debug-mode. */
68
/* Note that we use the same definition as in sc.h. We are deliberately not using
69
* #define T8_ASSERT SC_ASSERT
70
* since then the assertion would not trigger if sc is not configured in debugging mode.
71
* However, we want it to trigger any time t8code is in debugging mode, independent of sc.
72
*/
73
#if T8_ENABLE_DEBUG
74
#define T8_ASSERT(c) SC_CHECK_ABORT ((c), "Assertion '" #c "'")
75
#else
76
#define T8_ASSERT(c) SC_NOOP ()
77
#endif
78
79
/**Extended T8_ASSERT assertion with custom error message. Only active in debug-mode. */
80
#if T8_ENABLE_DEBUG
81
#define T8_ASSERTF(c, msg) SC_CHECK_ABORT ((c), "Assertion '" #c "': " msg)
82
#else
83
#define T8_ASSERTF(c, msg) SC_NOOP ()
84
#endif
85
86
/** Allocate a \a t-array with \a n elements. */
87
#define T8_ALLOC(t, n) (t *) sc_malloc (t8_get_package_id (), (n) * sizeof (t))
88
89
/** Allocate a \a t-array with \a n elements and init to zero. */
90
#define T8_ALLOC_ZERO(t, n) (t *) sc_calloc (t8_get_package_id (), (size_t) (n), sizeof (t))
91
92
/** Deallocate a \a t-array. */
93
#define T8_FREE(p) sc_free (t8_get_package_id (), (p))
94
95
/** Reallocate the \a t-array \a p with \a n elements. */
96
#define T8_REALLOC(p, t, n) (t *) sc_realloc (t8_get_package_id (), (p), (n) * sizeof (t))
97
98
/** A type for processor-local indexing. */
99
typedef int32_t t8_locidx_t;
100
/** The format specifier for t8_locidx_t */
101
#define T8_LOCIDX_FORMAT PRId32
102
/** The MPI Datatype of t8_locidx_t */
103
#define T8_MPI_LOCIDX sc_MPI_INT
104
/** Macro to get the absolute value of a t8_locidx_t */
105
#define T8_LOCIDX_ABS(x) ((t8_locidx_t) labs ((long) (x)))
106
/** Maximum possible value of a t8_locidx_t */
107
#define T8_LOCIDX_MAX INT32_MAX
108
/** Comparison function for t8_locidx_t */
109
#define t8_compare_locidx(v, w) sc_int32_compare (v, w)
110
/** A type for holding process ids. */
111
typedef int t8_procidx_t;
112
/** A type for global indexing that holds really big numbers. */
113
typedef int64_t t8_gloidx_t;
114
/** The format specifier for t8_gloidx_t */
115
#define T8_GLOIDX_FORMAT PRId64
116
/** The MPI Datatype of t8_gloidx_t */
117
#define T8_MPI_GLOIDX sc_MPI_LONG_LONG_INT
118
/** Macro to get the absolute value of a t8_gloidx_t */
119
#define T8_GLOIDX_ABS(x) ((t8_gloidx_t) llabs ((long long) (x)))
120
/** Maximum possible value of a t8_gloidx_t*/
121
#define T8_GLOIDX_MAX INT64_MAX
122
/** Comparison function for t8_gloidx_t */
123
#define t8_compare_gloidx(v, w) sc_int64_compare (v, w)
124
125
/** A type for storing SFC indices */
126
typedef uint64_t t8_linearidx_t;
127
/** The format specifier for t8_linearidx_t */
128
#define T8_LINEARIDX_FORMAT PRIu64
129
/** The MPI datatype of t8_linearidx_t */
130
#define T8_MPI_LINEARIDX sc_MPI_UNSIGNED_LONG_LONG
131
132
/** The padding size is the size of a void pointer*/
133
#define T8_PADDING_SIZE (sizeof (void *))
134
/** Compute the number of bytes that have to be added to a given byte_count
135
* such that it is a multiple of the padding size */
136
#define T8_ADD_PADDING(_x) ((T8_PADDING_SIZE - ((_x) % T8_PADDING_SIZE)) % T8_PADDING_SIZE)
137
138
/** Define machine precision for computations */
139
#define T8_PRECISION_EPS SC_EPS
140
/** Define square root of machine precision for computations */
141
#define T8_PRECISION_SQRT_EPS sqrt (T8_PRECISION_EPS)
142
143
/** Access multidimensional data on one-dimensional C arrays. */
144
/** Access onedimensional data on one-dimensional C arrays. */
145
#define T8_1D_TO_1D(nx, i) (i)
146
/** Access twodimensional data on one-dimensional C arrays. */
147
#define T8_2D_TO_1D(nx, ny, i, j) ((i) * (ny) + (j))
148
/** Access threedimensional data on one-dimensional C arrays. */
149
#define T8_3D_TO_1D(nx, ny, nz, i, j, k) (((i) * (ny) + (j)) * (nz) + (k))
150
/** Access fourdimensional data on one-dimensional C arrays. */
151
#define T8_4D_TO_1D(nx, ny, nz, nl, i, j, k, l) ((((i) * (ny) + (j)) * (nz) + (k)) * (nl) + (l))
152
153
/** Communication tags used internal to t8code. */
154
typedef enum {
155
T8_MPI_TAG_FIRST = SC_TAG_FIRST, /**< Dummy first MPT tag. */
156
T8_MPI_PARTITION_CMESH = SC_TAG_LAST, /**< Used for coarse mesh partitioning */
157
T8_MPI_PARTITION_FOREST, /**< Used for forest partitioning */
158
T8_MPI_GHOST_FOREST, /**< Used for for ghost layer creation */
159
T8_MPI_GHOST_EXC_FOREST, /**< Used for ghost data exchange */
160
T8_MPI_CMESH_UNIFORM_BOUNDS_START, /**< Used for cmesh uniform bounds computation. */
161
T8_MPI_CMESH_UNIFORM_BOUNDS_END, /**< Used for cmesh uniform bounds computation. */
162
T8_MPI_TEST_ELEMENT_PACK_TAG, /**< Used for testing mpi pack and unpack functionality */
163
T8_MPI_PFC_TAG, /**< Used for data exchange during partition for coarsening. */
164
T8_MPI_TAG_LAST /**< Dummy last MPI tag. */
165
} t8_MPI_tag_t;
166
167
/** Query the package identity as registered in libsc.
168
* \return This is -1 before \ref t8_init has been called
169
* and a proper package identifier afterwards.
170
*/
171
int
172
t8_get_package_id (void);
173
174
/** Logging function parameterized by local/global category and priority.
175
* \param [in] category Either SC_LC_NORMAL for outputting on every rank
176
* or SC_LC_GLOBAL for outputting on the root rank.
177
* \param [in] priority Please see sc.h for legal log priorities.
178
* \param [in] fmt Printf-style format string.
179
* \param [in] ap Argument list; see stdarg.h.
180
*/
181
void
182
t8_logv (int category, int priority, const char *fmt, va_list ap);
183
184
/** Logging function parameterized by local/global category and priority.
185
* \param [in] category Either SC_LC_NORMAL for outputting on every rank
186
* or SC_LC_GLOBAL for outputting on the root rank.
187
* \param [in] priority Please see sc.h for legal log priorities.
188
* \param [in] fmt Printf-style format string.
189
*/
190
void
191
t8_logf (int category, int priority, const char *fmt, ...)
192
#ifndef T8_DOXYGEN
193
__attribute__ ((format (printf, 3, 4)))
194
#endif
195
;
196
197
/** Add one space to the start of t8's default log format. */
198
void
199
t8_log_indent_push (void);
200
201
/** Remove one space from the start of a t8's default log format. */
202
void
203
t8_log_indent_pop (void);
204
205
/** Log a message on the root rank with priority SC_LP_ERROR.
206
* \param [in] fmt Printf-style format string.
207
*/
208
void
209
t8_global_errorf (const char *fmt, ...)
210
#ifndef T8_DOXYGEN
211
__attribute__ ((format (printf, 1, 2)))
212
#endif
213
;
214
215
/** Log a message on the root rank with priority SC_LP_ESSENTIAL.
216
* \param [in] fmt Printf-style format string.
217
*/
218
void
219
t8_global_essentialf (const char *fmt, ...)
220
#ifndef T8_DOXYGEN
221
__attribute__ ((format (printf, 1, 2)))
222
#endif
223
;
224
225
/** Log a message on the root rank with priority SC_LP_PRODUCTION.
226
* \param [in] fmt Printf-style format string.
227
*/
228
void
229
t8_global_productionf (const char *fmt, ...)
230
#ifndef T8_DOXYGEN
231
__attribute__ ((format (printf, 1, 2)))
232
#endif
233
;
234
235
/** Log a message on the root rank with priority SC_LP_INFO.
236
* \param [in] fmt Printf-style format string.
237
*/
238
void
239
t8_global_infof (const char *fmt, ...)
240
#ifndef T8_DOXYGEN
241
__attribute__ ((format (printf, 1, 2)))
242
#endif
243
;
244
245
/** Log a message, no matter what rank, with priority SC_LP_INFO.
246
* \param [in] fmt Printf-style format string.
247
*/
248
void
249
t8_infof (const char *fmt, ...)
250
#ifndef T8_DOXYGEN
251
__attribute__ ((format (printf, 1, 2)))
252
#endif
253
;
254
255
/** Log a message, no matter what rank, with priority SC_LP_PRODUCTION.
256
* \param [in] fmt Printf-style format string.
257
*/
258
void
259
t8_productionf (const char *fmt, ...)
260
#ifndef T8_DOXYGEN
261
__attribute__ ((format (printf, 1, 2)))
262
#endif
263
;
264
265
/** Log a message, no matter what rank, with priority SC_LP_DEBUG.
266
* \param [in] fmt Printf-style format string.
267
* \note This function does not print anything unless t8code was compiled
268
* in debug mode (using -DCMAKE_BUILD_TYPE=Debug, so T8_ENABLE_DEBUG is defined).
269
*/
270
void
271
t8_debugf (const char *fmt, ...)
272
#ifndef T8_DOXYGEN
273
__attribute__ ((format (printf, 1, 2)))
274
#endif
275
;
276
277
/** Log a message, no matter what rank, with priority SC_LP_ERROR.
278
* \param [in] fmt Printf-style format string.
279
*/
280
void
281
t8_errorf (const char *fmt, ...)
282
#ifndef T8_DOXYGEN
283
__attribute__ ((format (printf, 1, 2)))
284
#endif
285
;
286
287
/**
288
* Set a custom logging function to be used by t8code.
289
* When setting a custom logging function, the t8code internal logging function will be ignored.
290
* \param [in] log_fcn A function pointer to a logging function
291
*/
292
void
293
t8_set_external_log_fcn (void (*log_fcn) (int category, int priority, const char *msg));
294
295
/** Register t8code with libsc and print version and variable information.
296
* \param [in] log_threshold Declared in sc.h. SC_LP_DEFAULT is fine.
297
* You can also choose from log levels SC_LP_*.
298
*/
299
void
300
t8_init (int log_threshold);
301
302
/** Return a pointer to an array element indexed by a t8_locidx_t.
303
* \param [in] array The array of elements.
304
* \param [in] index needs to be in [0]..[elem_count-1].
305
* \return A void * pointing to entry \a index in \a array.
306
*/
307
void *
308
t8_sc_array_index_locidx (const sc_array_t *array, const t8_locidx_t index);
309
310
/** Call this at the end of a header file to match T8_EXTERN_C_BEGIN (). */
311
T8_EXTERN_C_END ();
312
313
#endif /* !T8_H */
314
315