openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
LUT.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 
19 #ifndef LUT_H
20 #define LUT_H
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #include <stdbool.h>
26 #include "limpet_types.h"
27 #include "target.h"
28 
29 // define so that LUT exist only in device memory
30 #define LUT_DEV_ONLY
31 
32 namespace limpet {
33 
34 typedef double LUT_data_t;
35 
46 struct LUT {
47  char *name;
48  int rows;
49  int cols;
50  float mn;
51  float mx;
52  float res;
53  float step;
54  int mn_ind;
55  int mx_ind;
56  float dt;
57  LUT_data_t **tab;
58 };
59 
60 
61 void LUT_alloc( LUT *psv, int cols, float mn, float mx, float res,
62  const char*, Target target );
63 int LUT_dump( LUT *plut, const char * );
64 int check_LUT( LUT* );
65 LUT_data_t LUT_interp( LUT *t, int i, int j, GlobalData_t x );
66 LUT_data_t LUT_derror( LUT *t, int idx, GlobalData_t x );
67 #if defined MLIR_CODEGEN && (defined __CUDA__ || defined __HIP__)
68 #ifdef __cplusplus
69 extern "C"
70 {
71 #endif // ifdef __cplusplus
72 __host__ __device__
73 #endif
74 LUT_data_t LUT_interpRow( LUT *tab, GlobalData_t val, int i, LUT_data_t*);
75 #if defined MLIR_CODEGEN && (defined __CUDA__ || defined __HIP__)
76 #ifdef __cplusplus
77 }
78 #endif // ifdef __cplusplus
79 #endif
80 void LUT_problem( LUT *, double, int, const char * );
81 void IIF_warn(const int wv, const char* error);
82 void destroy_lut( LUT *plut, Target target );
83 
89 
98 template<typename T>
99 T** build_matrix_ns(int m, int n, int size, Target target)
100 {
101  T **ptr = NULL;
102  T *buf;
103 
104  buf = allocate_on_target<T>(target, m * n);
105  if( buf==NULL ) {
106  log_msg(0, 5, 0, "Not enough memory for matrix\n" );
107  exit(1);
108  }
109 
110  ptr = allocate_on_target<T*>(target, m);
111  for ( int i=0; i<m; i++ )
112  ptr[i] = buf + i*n;
113 
114  return ptr;
115 }
116 
117 LUT_data_t* LUT_row( LUT *lut, GlobalData_t val, int locind );
118 
119 #ifdef __cplusplus
120 extern "C"
121 {
122 #endif // ifdef __cplusplus
123 // LUT functions for MLIR models
124 // Implements LUT_interpRow for multiple elements to be placed in a vector
125 void
126 LUT_interpRow_n_elements(char *tab, char *val_ptr, int offset, int distance,
127  int index, int n, char* row_ptr, int lut_numelements);
128 
129 void LUT_interpRow_mlir(char *table, int i, char* row_ptr,
130  int lut_num_elements, int vector_size);
131 
132 void LUT_problem_mlir( char *tab, GlobalData_t val, int locind);
133 
134 #ifdef __cplusplus
135 }
136 #endif // ifdef __cplusplus
137 
143 #define LUT_entry(A, I, J) (A)->tab[I][J]
144 
145 #ifdef IMP_FAST
146 
152 #define LUT_index( T, V, W ) (int)((V)*T->step)
153 #define LUT_constrain( T, V, W )
154 
155 #else
156 
168 int LUT_index( LUT *tab, GlobalData_t val, int locind );
169 
182 #define LUT_constrain(T, V, W, N ) \
183  if( V>=T->mx){/*fprintf(stderr, "table overbound: %g\n", V );*/V=T->mx;}\
184  if( V<=T->mn){/*fprintf(stderr, "table underbound: %g\n", V );*/V=T->mn;}
185 #endif
186 
187 } // namespace limpet
188 
189 #endif
char * dupstr(const char *old_str)
Definition: basics.cc:44
float dt
Definition: LUT.h:56
int rows
Definition: LUT.h:48
bool is_big_endian()
Definition: basics.cc:290
void LUT_interpRow_n_elements(char *table, char *val_ptr, int offset, int distance, int index, int n, char *row_ptr, int lut_numelements)
Definition: LUT.cc:304
LUT_data_t * LUT_row(LUT *lut, GlobalData_t val, int locind)
Definition: LUT.cc:280
void destroy_lut(LUT *plut, Target target)
Definition: LUT.cc:101
double LUT_data_t
Definition: LUT.h:34
void LUT_problem_mlir(char *tab, GlobalData_t val, int locind)
int check_LUT(LUT *lut)
Definition: LUT.cc:129
void IIF_warn(const int wv, const char error[])
Definition: LUT.cc:155
void LUT_alloc(LUT *plut, int cols, float mn, float mx, float res, const char *name, Target target)
Definition: LUT.cc:45
LUT_data_t LUT_interpRow(LUT *const tab, GlobalData_t val, int i, LUT_data_t *row)
Definition: LUT.cc:258
int cols
Definition: LUT.h:49
LUT_data_t LUT_derror(LUT *t, int idx, GlobalData_t x)
Definition: LUT.cc:242
int LUT_index(LUT *tab, GlobalData_t val, int locind)
Definition: LUT.cc:193
int mn_ind
Definition: LUT.h:54
float res
Definition: LUT.h:52
int mx_ind
Definition: LUT.h:55
double distance(const Point &a, const Point &b)
Definition: SF_container.h:149
LUT_data_t ** tab
Definition: LUT.h:57
int LUT_dump(LUT *plut, const char *fname)
Definition: LUT.cc:74
float mn
Definition: LUT.h:50
void LUT_problem(LUT *lt, double val, int wv, const char *tabname)
Definition: LUT.cc:171
LUT_data_t LUT_interp(LUT *t, int i, int j, GlobalData_t x)
Definition: LUT.cc:223
void LUT_interpRow_mlir(char *table, int i, char *row_ptr, int lut_num_elements, int vector_size)
Defines valid targets for an ionic model to run on and an allocator for allocating memory on a specif...
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:72
char * name
Definition: LUT.h:47
Target
enum that represents different targets to run ionic models on.
Definition: target.h:45
float mx
Definition: LUT.h:51
lookup table structure
Definition: LUT.h:46
file_desc * FILE_SPEC
Definition: basics.h:138
T ** build_matrix_ns(int m, int n, int size, Target target)
Definition: LUT.h:99
float step
Definition: LUT.h:53
SF_real GlobalData_t
Definition: limpet_types.h:27
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:135