egsl.c
Go to the documentation of this file.
1 #include <gsl/gsl_matrix.h>
2 #include <gsl/gsl_blas.h>
3 #include <gsl/gsl_linalg.h>
4 
5 #include <assert.h>
6 #include <math.h>
7 #include <string.h>
8 
9 #include "egsl.h"
10 #include "egsl_imp.h"
11 #define MAX_VALS 1024
12 #define MAX_CONTEXTS 1024
13 
14 /*#define INVALID (val_from_context_and_index(1000,1000))*/
15 
16 
17 struct egsl_variable {
18  gsl_matrix * gsl_m;
19 };
20 
21 struct egsl_context {
22  char name[256];
24  int nvars;
25  struct egsl_variable vars[MAX_VALS];
26 };
27 
28 /* Current context */
29 int cid=0;
30 /* Maximum number of contexts */
31 int max_cid = 0;
33 
34 
36 
39 
40 void egsl_error(void) {
41  /* TODO: better handling of errors */
42 
44 
45  assert(0);
46 }
47 
48 val assemble_val(int cid, int index, gsl_matrix*m) {
49  val v;
50  v.cid=cid;
51  v.index=index;
52  v.gslm = m;
53  return v;
54 }
55 
56 int its_context(val v) {
57  return v.cid;
58 }
59 
61  return v.index;
62 }
63 
64 
65 #if 0
66 inline void check_valid_val(val v) { int i = v.cid; v.cid=i;}
67 #else
69  int context = its_context(v);
70  if(context>cid) {
71  fprintf(stderr, "Val is from invalid context (%d>%d)\n",context,cid);
72  egsl_error();
73  }
74  int var_index = its_var_index(v);
75  if(var_index >= egsl_contexts[context].nvars) {
76  fprintf(stderr, "Val is invalid (%d>%d)\n",var_index,
77  egsl_contexts[context].nvars);
78  egsl_error();
79  }
80 }
81 #endif
82 
83 gsl_matrix * egsl_gslm(val v) {
84  check_valid_val(v);
85  return v.gslm;
86 }
87 
88 void egsl_push() { egsl_push_named("unnamed context"); }
89 void egsl_pop() { egsl_pop_named("unnamed context"); }
90 
91 void egsl_push_named(const char*name) {
92  if(egsl_first_time) {
93  int c;
94  for(c=0;c<MAX_CONTEXTS;c++) {
96  egsl_contexts[c].nvars = 0;
97  sprintf(egsl_contexts[c].name, "not yet used");
98  }
99  egsl_first_time = 0;
100  }
101  cid++;
102 
103  if(cid >= MAX_CONTEXTS) {
104  fprintf(stderr, "egsl: maximum number of contexts reached \n");
106  assert(0);
107  }
108 
109  if(max_cid < cid) max_cid = cid;
110 
111  if(name != 0)
112  sprintf(egsl_contexts[cid].name, "%s", name);
113  else
114  sprintf(egsl_contexts[cid].name, "Unnamed context");
115 }
116 
117 void egsl_pop_named(const char*name) {
118  assert(cid>=0);/*, "No egsl_push before?"); */
119  if(name != 0) {
120  if(strcmp(name, egsl_contexts[cid].name)) {
121  fprintf(stderr, "egsl: context mismatch. You want to pop '%s', you are still at '%s'\n",
122  name, egsl_contexts[cid].name);
124  assert(0);
125  }
126  }
127 
128  egsl_contexts[cid].nvars = 0;
129  sprintf(egsl_contexts[cid].name, "Popped");
130  cid--;
131 }
132 /*
133 void egsl_pop_check(int assumed) {
134  if(assumed != cid) {
135  fprintf(stderr, "egsl: You think you are in context %d while you are %d. \n", assumed, cid);
136  if(assumed < cid)
137  fprintf(stderr, " It seems you miss %d egsl_pop() somewhere.\n", - assumed + cid);
138  else
139  fprintf(stderr, " It seems you did %d egsl_pop() more.\n", + assumed - cid);
140  egsl_print_stats();
141  }
142  assert(cid>=0);
143  egsl_contexts[cid].nvars = 0;
144  cid--;
145 }*/
146 
148  fprintf(stderr, "egsl: total allocations: %d cache hits: %d\n",
150 /* printf("egsl: sizeof(val) = %d\n",(int)sizeof(val)); */
151  int c; for(c=0;c<=max_cid&&c<MAX_CONTEXTS;c++) {
152  /* printf("egsl: context #%d\n ",c); */
153 /* if(0==egsl_contexts[c].nallocated) break; */
154  fprintf(stderr, "egsl: context #%d allocations: %d active: %d name: '%s' \n",
156  }
157 }
158 
159 val egsl_alloc(size_t rows, size_t columns) {
160  struct egsl_context*c = egsl_contexts+cid;
161 
162 /* if(cid<3)
163  printf("Alloc cid=%d nvars=%d nalloc=%d\n",cid,c->nvars,c->nallocated); */
164 
165  if(c->nvars>=MAX_VALS) {
166  fprintf(stderr,"Limit reached, in context %d, nvars is %d\n",cid,c->nvars);
167  egsl_error();
168  }
169  int index = c->nvars;
170  if(index<c->nallocated) {
171  gsl_matrix*m = c->vars[index].gsl_m;
172  if(m->size1 == rows && m->size2 == columns) {
173  egsl_cache_hits++;
174  c->nvars++;
175  return assemble_val(cid,index,c->vars[index].gsl_m);
176  } else {
177  gsl_matrix_free(m);
179  c->vars[index].gsl_m = gsl_matrix_alloc((size_t)rows,(size_t)columns);
180  c->nvars++;
181  return assemble_val(cid,index,c->vars[index].gsl_m);
182  }
183  } else {
185  c->vars[index].gsl_m = gsl_matrix_alloc((size_t)rows,(size_t)columns);
186  c->nvars++;
187  c->nallocated++;
188  return assemble_val(cid,index,c->vars[index].gsl_m);
189  }
190 }
191 
192 val egsl_alloc_in_context(int context, size_t rows, size_t columns) {
193  struct egsl_context*c = egsl_contexts+context;
194 
195  if(c->nvars>=MAX_VALS) {
196  fprintf(stderr,"Limit reached, in context %d, nvars is %d\n",context,c->nvars);
197  egsl_error();
198  }
199  int index = c->nvars;
200  if(index<c->nallocated) {
201  gsl_matrix*m = c->vars[index].gsl_m;
202  if(m->size1 == rows && m->size2 == columns) {
203  egsl_cache_hits++;
204  c->nvars++;
205  return assemble_val(context,index,c->vars[index].gsl_m);
206  } else {
207  gsl_matrix_free(m);
209  c->vars[index].gsl_m = gsl_matrix_alloc((size_t)rows,(size_t)columns);
210  c->nvars++;
211  return assemble_val(context,index,c->vars[index].gsl_m);
212  }
213  } else {
215  c->vars[index].gsl_m = gsl_matrix_alloc((size_t)rows,(size_t)columns);
216  c->nvars++;
217  c->nallocated++;
218  return assemble_val(context,index,c->vars[index].gsl_m);
219  }
220 }
221 
224  if(cid==0) {
225  egsl_error();
226  }
227 
228  gsl_matrix * m = egsl_gslm(v);
229  val v2 = egsl_alloc_in_context(cid-1, m->size1, m->size2);
230  gsl_matrix * m2 = egsl_gslm(v2);
231  gsl_matrix_memcpy(m2, m);
232  return v2;
233 }
234 
235 
236 
237 
238 void egsl_expect_size(val v, size_t rows, size_t cols) {
239  gsl_matrix * m = egsl_gslm(v);
240 
241  int bad = (rows && (m->size1!=rows)) || (cols && (m->size2!=cols));
242  if(bad) {
243  fprintf(stderr, "Matrix size is %d,%d while I expect %d,%d",
244  (int)m->size1,(int)m->size2,(int)rows,(int)cols);
245 
246  egsl_error();
247  }
248 }
249 
250 
251 void egsl_print(const char*str, val v) {
252  gsl_matrix * m = egsl_gslm(v);
253  size_t i,j;
254  int context = its_context(v);
255  int var_index = its_var_index(v);
256  fprintf(stderr, "%s = (%d x %d) context=%d index=%d\n",
257  str,(int)m->size1,(int)m->size2, context, var_index);
258 
259  for(i=0;i<m->size1;i++) {
260  if(i==0)
261  fprintf(stderr, " [ ");
262  else
263  fprintf(stderr, " ");
264 
265  for(j=0;j<m->size2;j++)
266  fprintf(stderr, "%f ", gsl_matrix_get(m,i,j));
267 
268 
269  if(i==m->size1-1)
270  fprintf(stderr, "] \n");
271  else
272  fprintf(stderr, "; \n");
273  }
274 }
275 
276 double* egsl_atmp(val v, size_t i, size_t j) {
277  gsl_matrix * m = egsl_gslm(v);
278  return gsl_matrix_ptr(m,(size_t)i,(size_t)j);
279 }
280 
281 
282 double egsl_norm(val v1){
283  egsl_expect_size(v1, 0, 1);
284  double n=0;
285  size_t i;
286  gsl_matrix * m = egsl_gslm(v1);
287  for(i=0;i<m->size1;i++) {
288  double v = gsl_matrix_get(m,i,0);
289  n += v * v;
290  }
291  return sqrt(n);
292 }
293 
294 double egsl_atv(val v1, size_t i){
295  return *egsl_atmp(v1, i, 0);
296 }
297 
298 double egsl_atm(val v1, size_t i, size_t j){
299  return *egsl_atmp(v1, i, j);
300 }
301 
302 void egsl_free(void){
303  int c;
304  for(c=0;c<=max_cid;c++) {
305  for(int i=egsl_contexts[c].nvars; i<egsl_contexts[c].nallocated; i++){
306  gsl_matrix_free(egsl_contexts[c].vars[i].gsl_m);
307  }
309  }
310 }
311 
static struct egsl_context egsl_contexts[MAX_CONTEXTS]
Definition: egsl.c:32
int nallocated
Definition: egsl.c:23
void egsl_pop()
Definition: egsl.c:89
double egsl_norm(val v1)
Definition: egsl.c:282
Definition: egsl.h:12
gsl_matrix * gslm
Definition: egsl.h:13
void egsl_error(void)
Definition: egsl.c:40
void egsl_free(void)
Definition: egsl.c:302
struct egsl_variable vars[MAX_VALS]
Definition: egsl.c:25
void egsl_push()
Definition: egsl.c:88
int egsl_total_allocations
Definition: egsl.c:37
int egsl_first_time
Definition: egsl.c:35
int nvars
Definition: egsl.c:24
val egsl_alloc(size_t rows, size_t columns)
Definition: egsl.c:159
void check_valid_val(val v)
Definition: egsl.c:68
#define MAX_CONTEXTS
Definition: egsl.c:12
val egsl_alloc_in_context(int context, size_t rows, size_t columns)
Definition: egsl.c:192
#define m(v1, v2)
Definition: egsl_macros.h:13
double egsl_atm(val v1, size_t i, size_t j)
Definition: egsl.c:298
void egsl_print_stats()
Definition: egsl.c:147
int index
Definition: egsl.h:15
val egsl_promote(val v)
Definition: egsl.c:223
gsl_matrix * gsl_m
Definition: egsl.c:18
void egsl_expect_size(val v, size_t rows, size_t cols)
Definition: egsl.c:238
#define MAX_VALS
Definition: egsl.c:11
char name[256]
Definition: egsl.c:22
int its_var_index(val v)
Definition: egsl.c:60
int egsl_cache_hits
Definition: egsl.c:38
gsl_matrix * egsl_gslm(val v)
Definition: egsl.c:83
void egsl_push_named(const char *name)
Definition: egsl.c:91
double * egsl_atmp(val v, size_t i, size_t j)
Definition: egsl.c:276
double egsl_atv(val v1, size_t i)
Definition: egsl.c:294
int cid
Definition: egsl.h:14
val assemble_val(int cid, int index, gsl_matrix *m)
Definition: egsl.c:48
int cid
Definition: egsl.c:29
int max_cid
Definition: egsl.c:31
void egsl_pop_named(const char *name)
Definition: egsl.c:117
void egsl_print(const char *str, val v)
Definition: egsl.c:251
int its_context(val v)
Definition: egsl.c:56


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23