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 
26 #include "backend.h"
27 #include "mpi_utils.h"
28 #include "opencarp_types.h"
29 #include "SF_globals.h"
30 #include "vect.h"
31 #include "SF_vector.h"
32 
33 #ifndef CARP_PARAMS
34 #define CARP_PARAMS
35 #include <openCARP_p.h>
36 #include <openCARP_d.h>
37 #endif
38 
39 #define STRUCT_ZERO(S) memset(&(S), 0, sizeof(S))
40 #define SLIST_APPEND( S, P ) sltlst_append( S, &(P), sizeof(P) )
41 
42 namespace opencarp {
43 
45 struct Salt_list {
46  void *data;
47  int chunk;
48  int nitems;
49  int size;
50 };
51 
61 void sltlst_append(Salt_list* sl, void *p, int quantum);
62 
69 char* dupstr(const char* old_str);
70 
77 char *stringify(double r);
78 
79 std::string get_basename(const std::string & path);
80 
90 template<class STRVEC>
91 void split_string(const std::string & input, const char s, STRVEC & list)
92 {
93  size_t fnd = 0, ofnd = 0;
94  int num_parts = 1, idx = 0;
95 
96  fnd = input.find(s);
97  while (fnd != std::string::npos)
98  {
99  num_parts++;
100  ofnd = fnd+1;
101  fnd = input.find(s, ofnd);
102  }
103 
104  list.resize(num_parts);
105 
106  fnd = input.find(s); ofnd = 0;
107  while (fnd != std::string::npos)
108  {
109  list[idx++].assign(input.begin() + ofnd, input.begin() + fnd);
110  ofnd = fnd+1;
111  fnd = input.find(s, ofnd);
112  }
113 
114  if(ofnd < input.size())
115  list[idx].assign(input.begin() + ofnd, input.end());
116 }
117 
121 struct file_desc {
122  FILE* fd;
123  const char* name;
124 };
125 
127 
135 bool f_exist(const char *fname);
136 
145 FILE_SPEC f_open(const char *fname, const char *mode);
146 
152 void f_close(FILE_SPEC & f);
153 
163 void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm = SF_COMM);
164 
175 void f_write_par(void* ptr, size_t size, size_t nmemb, int source_pid, FILE_SPEC stream,
176  MPI_Comm comm = SF_COMM);
177 
186 char *read_bin_string(FILE_SPEC in);
187 
199 
200 char* f_gets_par(char* s, int size, FILE_SPEC stream, MPI_Comm comm = SF_COMM);
201 
207 void write_bin_string(FILE_SPEC out, const char *s);
208 
218 template<typename T> inline
219 T get_global(T in, MPI_Op OP, MPI_Comm comm = SF_COMM)
220 {
221  T dat = in;
222  MPI_Allreduce(MPI_IN_PLACE, &dat, 1, mpi_datatype<T>(), OP, comm);
223  return dat;
224 }
225 
226 template<> inline
227 size_t get_global<size_t>(size_t in, MPI_Op OP, MPI_Comm comm)
228 {
229  size_t dat = in;
230  MPI_Allreduce(MPI_IN_PLACE, &dat, 1, mpi_datatype<size_t>(), OP, comm);
231  return dat;
232 }
233 
241 template<typename T> inline
242 void get_global(SF::vector<T> & vec, int sender = 0, MPI_Comm comm = SF_COMM)
243 {
244  size_t sz = vec.size();
245  MPI_Bcast(&sz, sizeof(size_t), MPI_BYTE, sender, comm);
246  vec.resize(sz);
247  MPI_Bcast(vec.data(), sz*sizeof(T), MPI_BYTE, sender, comm);
248 }
249 
254 inline bool mpi_runtime_ready()
255 {
256  int initialized = 0;
257  MPI_Initialized(&initialized);
258  if(!initialized)
259  return false;
260 
261  int finalized = 0;
262  MPI_Finalized(&finalized);
263  return !finalized;
264 }
265 
270 inline int get_rank(MPI_Comm comm = SF_COMM)
271 {
272  int rank;
273  MPI_Comm_rank(comm, &rank);
274 
275  return rank;
276 }
277 
284 inline int get_size(MPI_Comm comm = SF_COMM)
285 {
286  int size;
287 
288  MPI_Comm_size(comm, &size);
289 
290  return size;
291 }
292 
293 inline int get_remote_size(MPI_Comm intercomm)
294 {
295  int size;
296 
297  MPI_Comm_remote_size(intercomm, &size);
298  return size;
299 }
300 
301 // flags for log messages
302 #define ECHO 1 // screen output
303 #define LOCAL 2 // output all cores
304 #define SYNCED 4 // synchronized
305 #define FLUSH 8 // flush screen output
306 #define NONL 16 // no new line
307 
308 #define MAX_MESG_LEN 2048
309 #define MAX_LOG_LEVEL 5
310 
334 void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt, ...);
335 
337 void init_iterations_logger(FILE_SPEC* & logger, const char* filename);
338 
339 inline void remove_preceding_char(char* buff, const int buffsize, const char c)
340 {
341  int ridx = 0, widx=0;
342  while(ridx < buffsize && buff[ridx] == c) ridx++;
343 
344  while(ridx < (buffsize - 1))
345  buff[widx++] = buff[ridx++];
346 
347  buff[widx] = '\0';
348 }
349 
350 inline void remove_char(char* buff, const int buffsize, const char c)
351 {
352  int ridx = 0, widx=0;
353  while(ridx < buffsize) {
354  if(buff[ridx] != c)
355  buff[widx++] = buff[ridx];
356  ridx++;
357  }
358 
359  buff[widx] = '\0';
360 }
361 
362 inline bool has_char(char* buff, const int buffsize, const char c)
363 {
364  int ridx = 0;
365  while(ridx < buffsize) {
366  if(buff[ridx] == '\0') return false;
367  if(buff[ridx] == c) return true;
368  ridx++;
369  }
370 
371  return false;
372 }
373 
375 class geom_shape {
376  public:
377  enum shape_t {sphere = 1, block = 2, cylinder = 3};
381  double radius;
382 };
383 
385 bool point_in_shape(const Point & p, const geom_shape & shape);
386 
392 bool is_big_endian();
393 
401 bool file_can_be_opened(const char* file);
402 
403 
405 bool path_is_absolute(const char* path);
406 
407 /* fmemopen for OSX / BSD compilations */
408 #ifdef __APPLE__
409 #define USE_FMEM_WRAPPER 1
410 #endif
411 
412 #ifdef OS_FREEBSD
413 #define USE_FMEM_WRAPPER 1
414 #endif
415 
416 #ifdef USE_FMEM_WRAPPER
417 struct fmem {
418  size_t pos;
419  size_t size;
420  char *buffer;
421 };
422 typedef struct fmem fmem_t;
423 
424 FILE *fmemopen_(void *, size_t, const char *);
425 #else
426 /* Else use the normal fmemopen */
427 #define fmemopen_ fmemopen
428 #endif
429 
430 inline void get_time(double & tm)
431 {
432  tm = MPI_Wtime();
433 }
434 
435 inline double get_time()
436 {
437  double tm = MPI_Wtime();
438  return tm;
439 }
440 
441 template<typename V> inline
442 V timing(V & t2, const V & t1)
443 {
444  t2 = get_time();
445  return t2 - t1;
446 }
447 
448 } // namespace opencarp
449 
450 #if OPENCARP_GLOBAL_INDEX_BITS == 64
451 #define OPENCARP_INSTANTIATE_INDEX_TYPE std::int64_t
452 #else
453 #define OPENCARP_INSTANTIATE_INDEX_TYPE std::int32_t
454 #endif
455 
456 #if OPENCARP_REAL_BITS == 32
457 #define OPENCARP_INSTANTIATE_VALUE_TYPE float
458 #else
459 #define OPENCARP_INSTANTIATE_VALUE_TYPE double
460 #endif
461 
462 #define OPENCARP_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
463  template _macro(OPENCARP_INSTANTIATE_INDEX_TYPE, OPENCARP_INSTANTIATE_VALUE_TYPE)
464 
465 #endif
#define SF_COMM
the default SlimFem MPI communicator
Definition: SF_globals.h:25
The vector class and related algorithms.
Starts and stops the linear algebra backend the flavor parameter selects.
#define fmemopen_
Definition: basics.h:427
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:375
bool is_big_endian()
Definition: basics.cc:266
void sltlst_append(Salt_list *sl, void *p, int quantum)
Definition: basics.cc:16
bool has_char(char *buff, const int buffsize, const char c)
Definition: basics.h:362
char * f_gets_par(char *s, int size, FILE_SPEC stream, MPI_Comm comm)
Definition: basics.cc:175
bool point_in_shape(const Point &p, const geom_shape &shape)
test if a point is inside a simple geometric shape
Definition: basics.cc:226
char * stringify(double r)
Definition: basics.cc:37
int get_size(MPI_Comm comm=SF_COMM)
Definition: basics.h:284
int get_remote_size(MPI_Comm intercomm)
Definition: basics.h:293
char * read_bin_string(FILE_SPEC in)
Definition: basics.cc:204
size_t get_global< size_t >(size_t in, MPI_Op OP, MPI_Comm comm)
Definition: basics.h:227
bool file_can_be_opened(const char *file)
Check wheterh a file can be opened for reading.
Definition: basics.cc:277
T get_global(T in, MPI_Op OP, MPI_Comm comm=SF_COMM)
Do a global reduction on a variable.
Definition: basics.h:219
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:155
void write_bin_string(FILE_SPEC out, const char *s)
Definition: basics.cc:195
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:111
bool path_is_absolute(const char *path)
check whether path is absolute
Definition: basics.cc:295
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:91
char * read_bin_string_par(FILE_SPEC in)
Definition: basics.cc:215
bool mpi_runtime_ready()
Definition: basics.h:254
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:147
char * dupstr(const char *old_str)
Definition: basics.cc:27
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:55
void get_time(double &tm)
Definition: basics.h:430
int get_rank(MPI_Comm comm=SF_COMM)
Definition: basics.h:270
void remove_char(char *buff, const int buffsize, const char c)
Definition: basics.h:350
V timing(V &t2, const V &t1)
Definition: basics.h:442
bool f_exist(const char *fname)
Definition: basics.cc:106
std::string get_basename(const std::string &path)
Definition: basics.cc:44
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:138
void remove_preceding_char(char *buff, const int buffsize, const char c)
Definition: basics.h:339
file_desc * FILE_SPEC
Definition: basics.h:126
saltatory list – memory is allocated in chunks
Definition: basics.h:45
int chunk
allocate memory to hold this many items
Definition: basics.h:47
void * data
the buffer
Definition: basics.h:46
int size
size of list items
Definition: basics.h:49
int nitems
number of items
Definition: basics.h:48
File descriptor struct.
Definition: basics.h:121
const char * name
Definition: basics.h:123