openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
basics.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // openCARP is an open cardiac electrophysiology simulator.
3 //
4 // Copyright (C) 2020 openCARP project
5 //
6 // This program is licensed under the openCARP Academic Public License (APL)
7 // v1.0: You can use and redistribute it and/or modify it in non-commercial
8 // academic environments under the terms of APL as published by the openCARP
9 // project v1.0, or (at your option) any later version. Commercial use requires
10 // a commercial license (info@opencarp.org).
11 //
12 // This program is distributed without any warranty; see the openCARP APL for
13 // more details.
14 //
15 // You should have received a copy of the openCARP APL along with this program
16 // and can find it online: http://www.opencarp.org/license
17 // ----------------------------------------------------------------------------
18 
27 #ifndef _BASICS_H
28 #define _BASICS_H
29 
30 #include <cstdio>
31 #include <cstdlib>
32 #include <cstdarg>
33 #include <cstring>
34 #include <cassert>
35 #include <string>
36 
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <mpi.h>
40 #include <petscsys.h> // TODO: review this (needed for PETSC_COMM_WORLD)
41 
42 #include "mpi_utils.h"
43 #include "opencarp_types.h"
44 #include "vect.h"
45 #include "SF_vector.h"
46 
47 #ifndef CARP_PARAMS
48 #define CARP_PARAMS
49 #include <openCARP_p.h>
50 #include <openCARP_d.h>
51 #endif
52 
53 #define STRUCT_ZERO(S) memset(&(S), 0, sizeof(S))
54 #define SLIST_APPEND( S, P ) sltlst_append( S, &(P), sizeof(P) )
55 
56 namespace opencarp {
57 
59 struct Salt_list {
60  void *data;
61  int chunk;
62  int nitems;
63  int size;
64 };
65 
75 void sltlst_append(Salt_list* sl, void *p, int quantum);
76 
83 char* dupstr(const char* old_str);
84 
91 char *stringify(double r);
92 
93 std::string get_basename(const std::string & path);
94 
104 template<class STRVEC>
105 void split_string(const std::string & input, const char s, STRVEC & list)
106 {
107  size_t fnd = 0, ofnd = 0;
108  int num_parts = 1, idx = 0;
109 
110  fnd = input.find(s);
111  while (fnd != std::string::npos)
112  {
113  num_parts++;
114  ofnd = fnd+1;
115  fnd = input.find(s, ofnd);
116  }
117 
118  list.resize(num_parts);
119 
120  fnd = input.find(s); ofnd = 0;
121  while (fnd != std::string::npos)
122  {
123  list[idx++].assign(input.begin() + ofnd, input.begin() + fnd);
124  ofnd = fnd+1;
125  fnd = input.find(s, ofnd);
126  }
127 
128  if(ofnd < input.size())
129  list[idx].assign(input.begin() + ofnd, input.end());
130 }
131 
135 struct file_desc {
136  FILE* fd;
137  const char* name;
138 };
139 
141 
149 bool f_exist(const char *fname);
150 
159 FILE_SPEC f_open(const char *fname, const char *mode);
160 
166 void f_close(FILE_SPEC & f);
167 
177 void f_read_par(void *ptr, size_t size, size_t nmemb, FILE_SPEC stream, MPI_Comm comm = PETSC_COMM_WORLD);
178 
189 void f_write_par(void* ptr, size_t size, size_t nmemb, int source_pid, FILE_SPEC stream,
190  MPI_Comm comm = PETSC_COMM_WORLD);
191 
200 char *read_bin_string(FILE_SPEC in);
201 
213 
214 char* f_gets_par(char* s, int size, FILE_SPEC stream, MPI_Comm comm = PETSC_COMM_WORLD);
215 
221 void write_bin_string(FILE_SPEC out, const char *s);
222 
232 template<typename T> inline
233 T get_global(T in, MPI_Op OP, MPI_Comm comm = PETSC_COMM_WORLD)
234 {
235  T dat = in;
236  MPI_Allreduce(MPI_IN_PLACE, &dat, 1, mpi_datatype<T>(), OP, comm);
237  return dat;
238 }
239 
240 template<> inline
241 size_t get_global<size_t>(size_t in, MPI_Op OP, MPI_Comm comm)
242 {
243  size_t dat = in;
244  MPI_Allreduce(MPI_IN_PLACE, &dat, 1, mpi_datatype<size_t>(), OP, comm);
245  return dat;
246 }
247 
255 template<typename T> inline
256 void get_global(SF::vector<T> & vec, int sender = 0, MPI_Comm comm = PETSC_COMM_WORLD)
257 {
258  size_t sz = vec.size();
259  MPI_Bcast(&sz, sizeof(size_t), MPI_BYTE, sender, comm);
260  vec.resize(sz);
261  MPI_Bcast(vec.data(), sz*sizeof(T), MPI_BYTE, sender, comm);
262 }
263 
268 inline bool mpi_runtime_ready()
269 {
270  int initialized = 0;
271  MPI_Initialized(&initialized);
272  if(!initialized)
273  return false;
274 
275  int finalized = 0;
276  MPI_Finalized(&finalized);
277  return !finalized;
278 }
279 
284 inline int get_rank(MPI_Comm comm = PETSC_COMM_WORLD)
285 {
286  int rank;
287  MPI_Comm_rank(comm, &rank);
288 
289  return rank;
290 }
291 
298 inline int get_size(MPI_Comm comm = PETSC_COMM_WORLD)
299 {
300  int size;
301 
302  MPI_Comm_size(comm, &size);
303 
304  return size;
305 }
306 
307 inline int get_remote_size(MPI_Comm intercomm)
308 {
309  int size;
310 
311  MPI_Comm_remote_size(intercomm, &size);
312  return size;
313 }
314 
315 // flags for log messages
316 #define ECHO 1 // screen output
317 #define LOCAL 2 // output all cores
318 #define SYNCED 4 // synchronized
319 #define FLUSH 8 // flush screen output
320 #define NONL 16 // no new line
321 
322 #define MAX_MESG_LEN 2048
323 #define MAX_LOG_LEVEL 5
324 
348 void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt, ...);
349 
351 void init_iterations_logger(FILE_SPEC* & logger, const char* filename);
352 
353 inline void remove_preceding_char(char* buff, const int buffsize, const char c)
354 {
355  int ridx = 0, widx=0;
356  while(ridx < buffsize && buff[ridx] == c) ridx++;
357 
358  while(ridx < (buffsize - 1))
359  buff[widx++] = buff[ridx++];
360 
361  buff[widx] = '\0';
362 }
363 
364 inline void remove_char(char* buff, const int buffsize, const char c)
365 {
366  int ridx = 0, widx=0;
367  while(ridx < buffsize) {
368  if(buff[ridx] != c)
369  buff[widx++] = buff[ridx];
370  ridx++;
371  }
372 
373  buff[widx] = '\0';
374 }
375 
376 inline bool has_char(char* buff, const int buffsize, const char c)
377 {
378  int ridx = 0;
379  while(ridx < buffsize) {
380  if(buff[ridx] == '\0') return false;
381  if(buff[ridx] == c) return true;
382  ridx++;
383  }
384 
385  return false;
386 }
387 
389 class geom_shape {
390  public:
391  enum shape_t {sphere = 1, block = 2, cylinder = 3};
395  double radius;
396 };
397 
399 bool point_in_shape(const Point & p, const geom_shape & shape);
400 
406 bool is_big_endian();
407 
415 bool file_can_be_opened(const char* file);
416 
417 
419 bool path_is_absolute(const char* path);
420 
421 /* fmemopen for OSX / BSD compilations */
422 #ifdef __APPLE__
423 #define USE_FMEM_WRAPPER 1
424 #endif
425 
426 #ifdef OS_FREEBSD
427 #define USE_FMEM_WRAPPER 1
428 #endif
429 
430 #ifdef USE_FMEM_WRAPPER
431 struct fmem {
432  size_t pos;
433  size_t size;
434  char *buffer;
435 };
436 typedef struct fmem fmem_t;
437 
438 FILE *fmemopen_(void *, size_t, const char *);
439 #else
440 /* Else use the normal fmemopen */
441 #define fmemopen_ fmemopen
442 #endif
443 
444 inline void get_time(double & tm)
445 {
446  tm = MPI_Wtime();
447 }
448 
449 inline double get_time()
450 {
451  double tm = MPI_Wtime();
452  return tm;
453 }
454 
455 template<typename V> inline
456 V timing(V & t2, const V & t1)
457 {
458  t2 = get_time();
459  return t2 - t1;
460 }
461 
462 } // namespace opencarp
463 
464 #if OPENCARP_GLOBAL_INDEX_BITS == 64
465 #define OPENCARP_INSTANTIATE_INDEX_TYPE std::int64_t
466 #else
467 #define OPENCARP_INSTANTIATE_INDEX_TYPE std::int32_t
468 #endif
469 
470 #if OPENCARP_REAL_BITS == 32
471 #define OPENCARP_INSTANTIATE_VALUE_TYPE float
472 #else
473 #define OPENCARP_INSTANTIATE_VALUE_TYPE double
474 #endif
475 
476 #define OPENCARP_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
477  template _macro(OPENCARP_INSTANTIATE_INDEX_TYPE, OPENCARP_INSTANTIATE_VALUE_TYPE)
478 
479 #endif
The vector class and related algorithms.
#define fmemopen_
Definition: basics.h:441
A vector storing arbitrary data.
Definition: SF_vector.h:43
size_t size() const
The current size of the vector.
Definition: SF_vector.h:104
void resize(size_t n)
Resize a vector.
Definition: SF_vector.h:209
T * data()
Pointer to the vector's start.
Definition: SF_vector.h:91
class to store shape definitions
Definition: basics.h:389
bool is_big_endian()
Definition: basics.cc:293
void sltlst_append(Salt_list *sl, void *p, int quantum)
Definition: basics.cc:33
bool has_char(char *buff, const int buffsize, const char c)
Definition: basics.h:376
char * f_gets_par(char *s, int size, FILE_SPEC stream, MPI_Comm comm)
Definition: basics.cc:202
bool point_in_shape(const Point &p, const geom_shape &shape)
test if a point is inside a simple geometric shape
Definition: basics.cc:253
char * stringify(double r)
Definition: basics.cc:54
int get_remote_size(MPI_Comm intercomm)
Definition: basics.h:307
char * read_bin_string(FILE_SPEC in)
Definition: basics.cc:231
size_t get_global< size_t >(size_t in, MPI_Op OP, MPI_Comm comm)
Definition: basics.h:241
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:174
bool file_can_be_opened(const char *file)
Check wheterh a file can be opened for reading.
Definition: basics.cc:304
int get_rank(MPI_Comm comm=PETSC_COMM_WORLD)
Definition: basics.h:284
T get_global(T in, MPI_Op OP, MPI_Comm comm=PETSC_COMM_WORLD)
Do a global reduction on a variable.
Definition: basics.h:233
void write_bin_string(FILE_SPEC out, const char *s)
Definition: basics.cc:222
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:182
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:138
bool path_is_absolute(const char *path)
check whether path is absolute
Definition: basics.cc:322
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:105
char * read_bin_string_par(FILE_SPEC in)
Definition: basics.cc:242
bool mpi_runtime_ready()
Definition: basics.h:268
char * dupstr(const char *old_str)
Definition: basics.cc:44
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:72
void get_time(double &tm)
Definition: basics.h:444
int get_size(MPI_Comm comm=PETSC_COMM_WORLD)
Definition: basics.h:298
void remove_char(char *buff, const int buffsize, const char c)
Definition: basics.h:364
V timing(V &t2, const V &t1)
Definition: basics.h:456
bool f_exist(const char *fname)
Definition: basics.cc:133
std::string get_basename(const std::string &path)
Definition: basics.cc:61
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:165
void remove_preceding_char(char *buff, const int buffsize, const char c)
Definition: basics.h:353
file_desc * FILE_SPEC
Definition: basics.h:140
saltatory list – memory is allocated in chunks
Definition: basics.h:59
int chunk
allocate memory to hold this many items
Definition: basics.h:61
void * data
the buffer
Definition: basics.h:60
int size
size of list items
Definition: basics.h:63
int nitems
number of items
Definition: basics.h:62
File descriptor struct.
Definition: basics.h:135
const char * name
Definition: basics.h:137