openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
basics.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
12 #ifndef _BASICS_H
13 #define _BASICS_H
14 
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cstdarg>
18 #include <cstring>
19 #include <cassert>
20 #include <string>
21 
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <mpi.h>
25 #include <petscsys.h> // TODO: review this (needed for PETSC_COMM_WORLD)
26 
27 #include "mpi_utils.h"
28 #include "opencarp_types.h"
29 #include "vect.h"
30 #include "SF_vector.h"
31 
32 #ifndef CARP_PARAMS
33 #define CARP_PARAMS
34 #include <openCARP_p.h>
35 #include <openCARP_d.h>
36 #endif
37 
38 #define STRUCT_ZERO(S) memset(&(S), 0, sizeof(S))
39 #define SLIST_APPEND( S, P ) sltlst_append( S, &(P), sizeof(P) )
40 
41 namespace opencarp {
42 
44 struct Salt_list {
45  void *data;
46  int chunk;
47  int nitems;
48  int size;
49 };
50 
60 void sltlst_append(Salt_list* sl, void *p, int quantum);
61 
68 char* dupstr(const char* old_str);
69 
76 char *stringify(double r);
77 
78 std::string get_basename(const std::string & path);
79 
89 template<class STRVEC>
90 void split_string(const std::string & input, const char s, STRVEC & list)
91 {
92  size_t fnd = 0, ofnd = 0;
93  int num_parts = 1, idx = 0;
94 
95  fnd = input.find(s);
96  while (fnd != std::string::npos)
97  {
98  num_parts++;
99  ofnd = fnd+1;
100  fnd = input.find(s, ofnd);
101  }
102 
103  list.resize(num_parts);
104 
105  fnd = input.find(s); ofnd = 0;
106  while (fnd != std::string::npos)
107  {
108  list[idx++].assign(input.begin() + ofnd, input.begin() + fnd);
109  ofnd = fnd+1;
110  fnd = input.find(s, ofnd);
111  }
112 
113  if(ofnd < input.size())
114  list[idx].assign(input.begin() + ofnd, input.end());
115 }
116 
120 struct file_desc {
121  FILE* fd;
122  const char* name;
123 };
124 
126 
134 bool f_exist(const char *fname);
135 
144 FILE_SPEC f_open(const char *fname, const char *mode);
145 
151 void f_close(FILE_SPEC & f);
152 
162 void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm = PETSC_COMM_WORLD);
163 
174 void f_write_par(void* ptr, size_t size, size_t nmemb, int source_pid, FILE_SPEC stream,
175  MPI_Comm comm = PETSC_COMM_WORLD);
176 
185 char *read_bin_string(FILE_SPEC in);
186 
198 
199 char* f_gets_par(char* s, int size, FILE_SPEC stream, MPI_Comm comm = PETSC_COMM_WORLD);
200 
206 void write_bin_string(FILE_SPEC out, const char *s);
207 
217 template<typename T> inline
218 T get_global(T in, MPI_Op OP, MPI_Comm comm = PETSC_COMM_WORLD)
219 {
220  T dat = in;
221  MPI_Allreduce(MPI_IN_PLACE, &dat, 1, mpi_datatype<T>(), OP, comm);
222  return dat;
223 }
224 
225 template<> inline
226 size_t get_global<size_t>(size_t in, MPI_Op OP, MPI_Comm comm)
227 {
228  size_t dat = in;
229  MPI_Allreduce(MPI_IN_PLACE, &dat, 1, mpi_datatype<size_t>(), OP, comm);
230  return dat;
231 }
232 
240 template<typename T> inline
241 void get_global(SF::vector<T> & vec, int sender = 0, MPI_Comm comm = PETSC_COMM_WORLD)
242 {
243  size_t sz = vec.size();
244  MPI_Bcast(&sz, sizeof(size_t), MPI_BYTE, sender, comm);
245  vec.resize(sz);
246  MPI_Bcast(vec.data(), sz*sizeof(T), MPI_BYTE, sender, comm);
247 }
248 
253 inline bool mpi_runtime_ready()
254 {
255  int initialized = 0;
256  MPI_Initialized(&initialized);
257  if(!initialized)
258  return false;
259 
260  int finalized = 0;
261  MPI_Finalized(&finalized);
262  return !finalized;
263 }
264 
269 inline int get_rank(MPI_Comm comm = PETSC_COMM_WORLD)
270 {
271  int rank;
272  MPI_Comm_rank(comm, &rank);
273 
274  return rank;
275 }
276 
283 inline int get_size(MPI_Comm comm = PETSC_COMM_WORLD)
284 {
285  int size;
286 
287  MPI_Comm_size(comm, &size);
288 
289  return size;
290 }
291 
292 inline int get_remote_size(MPI_Comm intercomm)
293 {
294  int size;
295 
296  MPI_Comm_remote_size(intercomm, &size);
297  return size;
298 }
299 
300 // flags for log messages
301 #define ECHO 1 // screen output
302 #define LOCAL 2 // output all cores
303 #define SYNCED 4 // synchronized
304 #define FLUSH 8 // flush screen output
305 #define NONL 16 // no new line
306 
307 #define MAX_MESG_LEN 2048
308 #define MAX_LOG_LEVEL 5
309 
333 void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt, ...);
334 
336 void init_iterations_logger(FILE_SPEC* & logger, const char* filename);
337 
338 inline void remove_preceding_char(char* buff, const int buffsize, const char c)
339 {
340  int ridx = 0, widx=0;
341  while(ridx < buffsize && buff[ridx] == c) ridx++;
342 
343  while(ridx < (buffsize - 1))
344  buff[widx++] = buff[ridx++];
345 
346  buff[widx] = '\0';
347 }
348 
349 inline void remove_char(char* buff, const int buffsize, const char c)
350 {
351  int ridx = 0, widx=0;
352  while(ridx < buffsize) {
353  if(buff[ridx] != c)
354  buff[widx++] = buff[ridx];
355  ridx++;
356  }
357 
358  buff[widx] = '\0';
359 }
360 
361 inline bool has_char(char* buff, const int buffsize, const char c)
362 {
363  int ridx = 0;
364  while(ridx < buffsize) {
365  if(buff[ridx] == '\0') return false;
366  if(buff[ridx] == c) return true;
367  ridx++;
368  }
369 
370  return false;
371 }
372 
374 class geom_shape {
375  public:
376  enum shape_t {sphere = 1, block = 2, cylinder = 3};
380  double radius;
381 };
382 
384 bool point_in_shape(const Point & p, const geom_shape & shape);
385 
391 bool is_big_endian();
392 
400 bool file_can_be_opened(const char* file);
401 
402 
404 bool path_is_absolute(const char* path);
405 
406 /* fmemopen for OSX / BSD compilations */
407 #ifdef __APPLE__
408 #define USE_FMEM_WRAPPER 1
409 #endif
410 
411 #ifdef OS_FREEBSD
412 #define USE_FMEM_WRAPPER 1
413 #endif
414 
415 #ifdef USE_FMEM_WRAPPER
416 struct fmem {
417  size_t pos;
418  size_t size;
419  char *buffer;
420 };
421 typedef struct fmem fmem_t;
422 
423 FILE *fmemopen_(void *, size_t, const char *);
424 #else
425 /* Else use the normal fmemopen */
426 #define fmemopen_ fmemopen
427 #endif
428 
429 inline void get_time(double & tm)
430 {
431  tm = MPI_Wtime();
432 }
433 
434 inline double get_time()
435 {
436  double tm = MPI_Wtime();
437  return tm;
438 }
439 
440 template<typename V> inline
441 V timing(V & t2, const V & t1)
442 {
443  t2 = get_time();
444  return t2 - t1;
445 }
446 
447 } // namespace opencarp
448 
449 #if OPENCARP_GLOBAL_INDEX_BITS == 64
450 #define OPENCARP_INSTANTIATE_INDEX_TYPE std::int64_t
451 #else
452 #define OPENCARP_INSTANTIATE_INDEX_TYPE std::int32_t
453 #endif
454 
455 #if OPENCARP_REAL_BITS == 32
456 #define OPENCARP_INSTANTIATE_VALUE_TYPE float
457 #else
458 #define OPENCARP_INSTANTIATE_VALUE_TYPE double
459 #endif
460 
461 #define OPENCARP_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
462  template _macro(OPENCARP_INSTANTIATE_INDEX_TYPE, OPENCARP_INSTANTIATE_VALUE_TYPE)
463 
464 #endif
The vector class and related algorithms.
#define fmemopen_
Definition: basics.h:426
A vector storing arbitrary data.
Definition: SF_vector.h:28
size_t size() const
The current size of the vector.
Definition: SF_vector.h:89
void resize(size_t n)
Resize a vector.
Definition: SF_vector.h:194
T * data()
Pointer to the vector's start.
Definition: SF_vector.h:76
class to store shape definitions
Definition: basics.h:374
bool is_big_endian()
Definition: basics.cc:278
void sltlst_append(Salt_list *sl, void *p, int quantum)
Definition: basics.cc:18
bool has_char(char *buff, const int buffsize, const char c)
Definition: basics.h:361
char * f_gets_par(char *s, int size, FILE_SPEC stream, MPI_Comm comm)
Definition: basics.cc:187
bool point_in_shape(const Point &p, const geom_shape &shape)
test if a point is inside a simple geometric shape
Definition: basics.cc:238
char * stringify(double r)
Definition: basics.cc:39
int get_remote_size(MPI_Comm intercomm)
Definition: basics.h:292
char * read_bin_string(FILE_SPEC in)
Definition: basics.cc:216
size_t get_global< size_t >(size_t in, MPI_Op OP, MPI_Comm comm)
Definition: basics.h:226
void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm)
Parallel fread. Root reads, then broadcasts.
Definition: basics.cc:159
bool file_can_be_opened(const char *file)
Check wheterh a file can be opened for reading.
Definition: basics.cc:289
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
Definition: basics.h:269
T get_global(T in, MPI_Op OP, MPI_Comm comm=PETSC_COMM_WORLD)
Do a global reduction on a variable.
Definition: basics.h:218
void write_bin_string(FILE_SPEC out, const char *s)
Definition: basics.cc:207
void f_write_par(void *ptr, size_t size, size_t nmemb, int source_pid, FILE_SPEC stream, MPI_Comm comm)
Write in parallel. Data comes from one rank, rank 0 writes.
Definition: basics.cc:167
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:123
bool path_is_absolute(const char *path)
check whether path is absolute
Definition: basics.cc:307
void split_string(const std::string &input, const char s, STRVEC &list)
Split a string holding a character-seperated list into a vector of strings.
Definition: basics.h:90
char * read_bin_string_par(FILE_SPEC in)
Definition: basics.cc:227
bool mpi_runtime_ready()
Definition: basics.h:253
char * dupstr(const char *old_str)
Definition: basics.cc:29
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:57
void get_time(double &tm)
Definition: basics.h:429
int get_size(MPI_Comm comm=PETSC_COMM_WORLD)
Definition: basics.h:283
void remove_char(char *buff, const int buffsize, const char c)
Definition: basics.h:349
V timing(V &t2, const V &t1)
Definition: basics.h:441
bool f_exist(const char *fname)
Definition: basics.cc:118
std::string get_basename(const std::string &path)
Definition: basics.cc:46
void init_iterations_logger(FILE_SPEC *&logger, const char *filename)
init a logger for solver iterations
void f_close(FILE_SPEC &f)
Close a FILE_SPEC.
Definition: basics.cc:150
void remove_preceding_char(char *buff, const int buffsize, const char c)
Definition: basics.h:338
file_desc * FILE_SPEC
Definition: basics.h:125
saltatory list – memory is allocated in chunks
Definition: basics.h:44
int chunk
allocate memory to hold this many items
Definition: basics.h:46
void * data
the buffer
Definition: basics.h:45
int size
size of list items
Definition: basics.h:48
int nitems
number of items
Definition: basics.h:47
File descriptor struct.
Definition: basics.h:120
const char * name
Definition: basics.h:122