ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
newdar.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
7 #include "fvar.hpp"
8 #ifdef __ZTC__
9  #include <iostream.hpp>
10 #endif
11 
12 #ifdef __TURBOC__
13  #pragma hdrstop
14  #pragma options -h-
15  #include <iostream.h>
16 #endif
17 #include <stdlib.h>
18 #ifndef OPT_LIB
19  #include <cassert>
20 #endif
21 
24 
29 {
30  prev = NULL;
31  next = NULL;
32  free_prev = NULL;
33  free_next = NULL;
34  status = 0;
35  // free_list_status=0;
36  size = 0;
37  offset = 0;
38 }
39 
45 {
46  num_free_obj--;
47  // if this is the last free object reset list pointer
48  if (!tmp->free_next)
49  {
50  free_last=tmp->free_prev;
51  }
52  // This routine removes the link pointed to by tmp
53  if (tmp->free_next) // Make the one after it point to tmp->prev
54  {
55  tmp->free_next->free_prev = tmp->free_prev;
56  }
57 
58  if (tmp->free_prev) // Make the one before it point to tmp->next
59  {
60  tmp->free_prev->free_next = tmp->free_next;
61  }
62 }
63 
69 {
70  num_free_obj++;
71  // This routine adds the link pointed to by tmp to the end of the free list
72  tmp->free_prev = free_last;
73  free_last=tmp;
74  tmp->free_next = NULL;
75  if (tmp->free_prev) tmp->free_prev->free_next = tmp;
76 }
77 
79 {
81  {
82  cerr << "Error -- you are trying to create a dvar_vector object"
83  " when there is " << endl << "no object of type"
84  " gradient_structure in scope " << endl;
85  ad_exit(1);
86  }
87 
88  char* temp_ptr = nullptr;
89 
90  // this routine allocated a block of memory of sizeof(double)*sz bytes
91  // for the gradients of an array or matrix of prevariables
92 
93  arr_link * tmp = free_last;
94 
95  unsigned int bytes_needed = sz * (unsigned int)sizeof(double_and_int);
96 
97 #ifdef DIAG
98  int ss=0;
99  if (ss)
100  {
101  double_and_int* tt=0;
102  return tt;
103  }
104 #endif
105 
106  while (tmp)
107  {
108  if (tmp->size >= bytes_needed)
109  {
110  // if the free block within 20 bytes of the size you want
111  // simply mark it occupied and return it
112 
113  if (tmp->size <= bytes_needed + 50)
114  {
115  tmp->status = 1;
116  // remove tmp from the free list
117  arr_free_remove(tmp);
118 
119  temp_ptr = (char*)ARRAY_MEMBLOCK_BASE + tmp->offset;
120 
121  //put the address tmp into the location pointed to by temp_ptr
122  (* (arr_link **) (temp_ptr)) = tmp;
123 
124  return (double_and_int*)temp_ptr;
125  }
126  else
127  {
128  // otherwise split up this memory block and return
129  // the part you need
130 
131  arr_link* tmp1 = new arr_link;
132  number_arr_links += 1;
133 
134  // put the new link tmp1-> into the list BEFORE tmp->
135 
136  tmp1->prev=tmp->prev;
137 
138  if(tmp1->prev)
139  {
140  tmp1->prev->next=tmp1;
141  }
142 
143  tmp1->next=tmp;
144  tmp->prev=tmp1;
145 
146  // get the size of the new link and mark it free
147 
148  tmp1->size=bytes_needed;
149  tmp1->status=1;
150  tmp1->offset=tmp->offset;
151 
152  tmp->offset+=bytes_needed;
153  tmp->size-=bytes_needed;
154 
155  temp_ptr = (char*)(ARRAY_MEMBLOCK_BASE) + tmp1->offset;
156 
157  //put the address pointed to by tmp1 into the location pointed to by temp_ptr
158  (*(arr_link**)(temp_ptr)) = tmp1;
159 
160  return (double_and_int*)temp_ptr;
161  }
162  }
163  tmp=tmp->free_prev;
164  }
165  // couldn't find a free block large enough
166  // make a new block
167 
168  tmp = new arr_link;
169  if (tmp==0)
170  {
171  cerr << "Error allocating new arr_link" << endl;
172  ad_exit(1);
173  }
174 
175  number_arr_links += 1;
176 
177  tmp->prev = last; // the new block point back
178  // at the previous last block
179 
180  if (last)
181  {
182  last->next = tmp; // the previous last
183  // block point forward to tmp
184  }
185  last = tmp; // tmp is the new last block
186 
187  tmp->next = 0;
188 
189  tmp->status = 1;
190 
191  tmp->offset = last_offset;
192 
193  last_offset += bytes_needed;
194 
196  {
198  }
199 
201  {
202  cout << last_offset <<" > "
204  cout << " No memory for dvar_vectors\n"
205  << " Need to increase ARRAY_MEMBLOCK_SIZE parameter\n"
206  "In gradient_structure declaration\n";
207  //throw gradient_structure::arrmemblerr();
208  ad_exit(1);
209  }
210 
211  tmp->size = bytes_needed;
212 
213  temp_ptr = (char*)(ARRAY_MEMBLOCK_BASE) + tmp->offset;
214 
215  (*(arr_link **) (temp_ptr )) = tmp; //put the address
216  // tmp into the location pointed to
217  //by temp_ptr
218 
219 // return (double_and_int *) (temp_ptr+sizeof(double_and_int));
220  return (double_and_int *) (temp_ptr);
221 }
222 
228 {
229  // This routines frees up a memory block and
230  // consolidates the free blocks if possible
231  //cout<< "calling arr_free\n";
232  char * temp_ptr;
233  arr_link * ptr;
234 
235  temp_ptr = (char *) varr;
236 
237  //temp=sizeof(double_and_int);
238 // ptr = *(arr_link **) (temp_ptr-temp);
239  ptr = *(arr_link **) (temp_ptr);
240 
241  //mark this block free
242 
243  ptr->status = 0;
244  // cout <<"Marking arr_link with adress "<<farptr_tolong(ptr)<<"free\n";
245 
246  // if there is a block after this add this one to the free list
247  if (ptr->next) arr_free_add(ptr);
248 
249  if (!ptr->next) // Is this the last link?
250  {
251  // Check to see if ptr->prev is free and should be deleted as well
252  // ... but first check to see if ptr is first block in list
253  // which will be indicated by ptr->prev being a NULL pointer
254  if (ptr->prev && !ptr->prev->status)
255  {
256  // delete ptr->prev
257  last = ptr->prev->prev;
258  //if (gradient_structure::get()->ARR_LIST1->last ==0)
259  // cout << "gradient_structure::get()->ARR_LIST1->last =0 " << endl;
260 
261  last_offset -= ptr->size + ptr->prev->size;
262  arr_free_remove(ptr->prev);
263  arr_remove(&(ptr->prev));
264  }
265  else
266  {
267  last = ptr->prev;
268  //if (gradient_structure::ARR_LIST1->last ==0)
269  // cout << "gradient_structure::ARR_LIST1->last =0 " << endl;
270  last_offset -= ptr->size;
271  }
272  arr_remove(&ptr);
273  }
274  else
275  {
276  // There is another link after this one?
277 
278  if (!ptr->next->status) // If yes is it free?
279  {
280  // add its memory capacity to the present one and delete it
281 
282  ptr->size += ptr->next->size;
283 
284  arr_free_remove(ptr->next);
285  arr_remove(&ptr->next);
286  }
287 
288  if (ptr->prev) // Is there another link before this one?
289  {
290  if (!ptr->prev->status) // If yes is it free?
291  {
292  // we will keep ptr->prev and add ptr to it
293 
294  ptr->prev->size += ptr->size;
295  arr_free_remove(ptr);
296  arr_remove(&ptr);
297  }
298  }
299  }
300 }
301 
306 void check_derivative_values(const char * _s)
307 {
308  char * s = (char *) _s;
309  //char label[20];
312  set_gradient_stack(df_check_derivative_values);
313 }
314 
319 void check_derivative_values(const char * _s,int i)
320 {
322  DF_FILE* fp = gs->fp;
323  char * s = (char *) _s;
324  //char label[20];
326  fp->save_int_value(i);
328 }
329 
330 void df_print_identifier_string(void);
331 
336 void insert_identifier_string(const char * _s)
337 {
338  char * s = (char *) _s;
341  set_gradient_stack(df_print_identifier_string);
342 }
343 
348 void check_derivative_values_break(const char * _s,int i,int b)
349 {
351  DF_FILE* fp = gs->fp;
352  char * s = (char *) _s;
353  //char label[20];
355  fp->save_int_value(i);
356  fp->save_int_value(b);
358 }
359 
365 {
366  //char label[20];
368  double* temp_ptr = (double*)(gradient_structure::_instance->ARR_LIST1->ARRAY_MEMBLOCK_BASE);
369  unsigned long int max_last_offset = gradient_structure::_instance->ARR_LIST1->get_max_last_offset();
370  size_t size = sizeof(double_and_int);
371 
372  int icount=0;
373  int exit_flag=0;
374  cout << str << endl;
375  unsigned int i;
376  for (i=0 ; i< (max_last_offset/size) ; i++ )
377  {
378  if (fabs(temp_ptr[i])>1.e+8)
379  {
380  if (ad_kill_flag) exit_flag=1;
381  icount++;
382  cout << i << " " << temp_ptr[i] << endl;
383  if (icount>10)
384  {
385  break;
386  }
387  }
388  }
389 
390  icount=0;
392  {
394  > 1.e+8)
395  {
396  icount++;
397  cout << "dlist " << i << " " << setscientific() <<
398  * (double*) (gradient_structure::_instance->GRAD_LIST->dlink_addresses[i])
399  << endl;
400  if (icount>10)
401  {
402  if (ad_kill_flag) exit_flag=1;
403  break;
404  }
405  }
406  }
407  if (exit_flag) ad_exit(1);
408 }
409 
410 //extern ofstream gradlog;
411 
417 {
419  cout << "GS = " << str << endl;
420  //gradlog << "GS = " << str << endl;
421 }
422 
428 {
430  DF_FILE* fp = gs->fp;
431 
432  //char label[20];
433  int index=fp->restore_int_value();
435  double* temp_ptr = (double*)(gradient_structure::_instance->ARR_LIST1->ARRAY_MEMBLOCK_BASE);
436  unsigned long int max_last_offset =
438  size_t size = sizeof(double_and_int);
439 
440  int icount=0;
441  int exit_flag=0;
442  cout << str << " index = " << index << endl;
443  unsigned int i;
444  for (i=0 ; i< (max_last_offset/size) ; i++ )
445  {
446  if (fabs(temp_ptr[i])>1.e+8)
447  {
448  if (ad_kill_flag) exit_flag=1;
449  icount++;
450  cout << i << " " << setscientific() << temp_ptr[i] << endl;
451  if (icount>10) break;
452  }
453  }
454 
455  icount=0;
457  {
458  if (* (double*) (gradient_structure::_instance->GRAD_LIST->dlink_addresses[i])
459  > 1.e+8)
460  {
461  icount++;
462  if (ad_kill_flag) exit_flag=1;
463  cout << "dlist " << i << " " << setscientific() <<
464  * (double*) (gradient_structure::_instance->GRAD_LIST->dlink_addresses[i])
465  << endl;
466  if (icount>10)
467  {
468  break;
469  }
470  }
471  }
472  if (exit_flag) ad_exit(1);
473 }
474 
480 {
482  DF_FILE* fp = gs->fp;
483 
484  //char label[20];
485  int b=fp->restore_int_value();
486  int index=fp->restore_int_value();
488  double* temp_ptr = (double*)(gradient_structure::_instance->ARR_LIST1->ARRAY_MEMBLOCK_BASE);
489  unsigned long int max_last_offset =
491  size_t size = sizeof(double_and_int);
492 
493  if (index<=b)
494  {
495  cout << "break condition" << endl;
496  }
497  int icount=0;
498  int exit_flag=0;
499  cout << str << " index = " << index << endl;
500  unsigned int i;
501  for (i=0 ; i< (max_last_offset/size) ; i++ )
502  {
503  if (fabs(temp_ptr[i])>1.e+8)
504  {
505  if (ad_kill_flag) exit_flag=1;
506  icount++;
507  cout << i << " " << temp_ptr[i] << endl;
508  if (icount>10) break;
509  }
510  }
511 
512  icount=0;
514  {
516  > 1.e+8)
517  {
518  icount++;
519  if (ad_kill_flag) exit_flag=1;
520  cout << "dlist " << i << " " <<
521  * (double*) (gradient_structure::_instance->GRAD_LIST->dlink_addresses[i])
522  << endl;
523  if (icount>10)
524  {
525  break;
526  }
527  }
528  }
529  if (exit_flag) ad_exit(1);
530 }
531 
536 {
537 #ifndef OPT_LIB
538  assert(pptr != NULL);
539 #endif
540 
541  arr_link* tmp = *pptr;
542 
543  if (tmp == NULL)
544  {
545  cout <<" Error -- tried to delete NULL arr_link in arr_remove\n";
546  ad_exit(23);
547  }
548  else
549  {
550  // This routine removes the link pointed to by tmp
551  if (tmp->next) // Make the one after it point to tmp->prev
552  {
553  tmp->next->prev = tmp->prev;
554  }
555  if (tmp->prev) // Make the one before it point to tmp->next
556  {
557  tmp->prev->next = tmp->next;
558  }
559 
560 #ifdef DIAG
561  cout << "Deleting an arr_link with adress "
562  #ifdef __ZTC__
563  << _farptr_tolong(tmp)
564  #else
565  << farptr_tolong(tmp)
566  #endif
567  << "\n";
568 #endif
569 
570  delete tmp;
571  tmp = NULL;
572  }
573  number_arr_links -= 1;
574  //cout << "after delete number_arr_links = "
575  //<< gradient_structure::get()->ARR_LIST1->number_arr_links <<"\n";
576 }
double_and_int * arr_new(unsigned int sz)
Definition: newdar.cpp:78
void arr_remove(arr_link **pptr)
Remove and delete arr_link node pptr from gradient_structure::ARR_LIST1.
Definition: newdar.cpp:535
static unsigned long ARRAY_MEMBLOCK_SIZE
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
void df_check_derivative_values_indexed(void)
Description not yet available.
Definition: newdar.cpp:427
adstring get_string_marker(void)
Description not yet available.
Definition: cmpdif3.cpp:171
void save_int_value(int x)
Definition: cmpdif8.cpp:108
arr_link * last
Definition: fvar.hpp:2066
Holds the data for the prevariable class.
Definition: fvar.hpp:191
void set_gradient_stack(void(*func)(void), double *dep_addr, double *ind_addr1=NULL, double mult1=0, double *ind_addr2=NULL, double mult2=0)
Description not yet available.
Definition: fvar.hpp:1045
unsigned long int max_last_offset
Definition: fvar.hpp:2069
humungous_pointer ARRAY_MEMBLOCK_BASE
Definition: fvar.hpp:2074
prescientific setscientific(void)
Description not yet available.
Definition: admanip.cpp:80
void df_check_derivative_values(void)
Description not yet available.
Definition: newdar.cpp:364
unsigned long int last_offset
Definition: fvar.hpp:2068
void arr_free(double_and_int *varr)
Description not yet available.
Definition: newdar.cpp:227
int num_free_obj
Definition: newdar.cpp:22
prnstream & endl(prnstream &)
unsigned long int get_max_last_offset()
Definition: fvar.hpp:2103
void arr_free_remove(arr_link *tmp)
Description not yet available.
Definition: newdar.cpp:44
unsigned int nlinks
Definition: fvar.hpp:837
dlink ** dlink_addresses
Definition: fvar.hpp:838
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
arr_link * free_last
Definition: fvar.hpp:2067
static _THREAD gradient_structure * _instance
void insert_identifier_string(const char *s)
Description not yet available.
Definition: newdar.cpp:336
int restore_int_value()
Definition: cmpdif8.cpp:201
void df_check_derivative_values_indexed_break(void)
Description not yet available.
Definition: newdar.cpp:479
int save_identifier_string(const char *)
Writes a gradient stack verification string.
Definition: cmpdif2.cpp:315
adstring str(double x, int minwidth=17, int decplaces=-1)
Convert x to adstring with minimum width and total number of decimal places.
Definition: str.cpp:25
static _THREAD DF_FILE * fp
int ad_kill_flag
Definition: newdar.cpp:23
unsigned long int number_arr_links
Definition: fvar.hpp:2071
void arr_free_add(arr_link *tmp)
Description not yet available.
Definition: newdar.cpp:68
friend class arr_link
Definition: fvar.hpp:2072
Stores the adjoint gradient data that will be processed by gradcalc.
static _THREAD grad_stack * GRAD_STACK1
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
void df_print_identifier_string(void)
Description not yet available.
Definition: newdar.cpp:416
void check_derivative_values_break(const char *s, int i, int b)
Description not yet available.
Definition: newdar.cpp:348
void check_derivative_values(const char *s)
Description not yet available.
Definition: newdar.cpp:306