ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gradstrc.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  */
11 #ifdef USE_THREAD
12  #include <mutex>
13 #endif
14 #ifndef _MSC_VER
15  #include <unistd.h>
16 #endif
17 #include "fvar.hpp"
18 
19 //#define THREAD_SAFE
20 
21 #if defined(THREAD_SAFE)
22  pthread_mutex_t mutex_return_arrays = PTHREAD_MUTEX_INITIALIZER;
23 #endif
24 
25 void null_ptr_err_message(void);
26 
27 #include <string.h>
28 
29 #if defined(__TURBOC__)
30  #pragma hdrstop
31  #include <iostream.h>
32  #include <alloc.h>
33 #endif
34 
35 #include <stdlib.h>
36 
37 #ifdef __ZTC__
38  #include <iostream.hpp>
39  int _cdecl farfree(void _far *ptr);
40  void _far * _cdecl farmalloc(unsigned long size);
41 #endif
42 
43 #ifdef DEBUG
44  #include <cassert>
45 #endif
46 
47 #ifndef __SUNPRO_CC
48 typedef int (* fptr) (const char * format, ...) ;
49 #endif
50 
51 //void adwait(double);
52 extern "C"{
54 
55  void spdll_exit(int ierr)
56  {
57  ad_printf(" Exception -- error code %d\n",ierr);
58  ad_printf(" Pause");
59  //adwait(4.0);
60 #if defined(USE_EXCEPTIONS)
61  throw spdll_exception(ierr);
62 #endif
63  }
64 }
65 
66 // *************************************************************
67 // *************************************************************
68 int ctlc_flag = 0;
72 #if (defined(NO_DERIVS))
74 #endif
78 //int gradient_structure::RETURN_INDEX = 0;
79 //dvariable * gradient_structure::FRETURN = NULL;
80 #ifdef __BORLANDC__
82 long int gradient_structure::CMPDIF_BUFFER_SIZE=140000000L;
83 #else
86 #endif
87 
89 
90 unsigned int gradient_structure::MAX_NVAR_OFFSET = 5000;
91 unsigned long gradient_structure::ARRAY_MEMBLOCK_SIZE = 0L; //js
92 //arr_list * gradient_structure::ARR_FREE_LIST1 = NULL;
93 unsigned int gradient_structure::MAX_DLINKS = 5000;
94 
95 // note: ARRAY_MEMBLOCK stuff is set by tpl2cpp for historical reasons
96 // those settings could be moved into this file in the future
97 // - Ian Taylor 5/3/2012
98 
99 //unsigned long int gradient_structure::ARRAY_MEMBLOCK_BASE = 0L;
100 #ifdef DIAG
101 long int _farptr_tolong(void *) ;
102 #endif
103 void memory_allocate_error(const char * s, void * ptr);
104 
106 void gradient_structure::create(const unsigned int size)
107 {
108  if (size > 0)
109  {
110  gradients_size = size;
113  for (unsigned int id = 1; id < gradients_size; ++id)
114  {
115  gradients[id] = new gradient_structure(10000L, id);
116  }
118  }
119 }
121 {
122  _instance = instance;
123  return _instance;
124 }
127 {
128 #ifdef DEBUG
129  assert(id < gradients_size);
130 #endif
131  _instance = gradients[id];
132  return _instance;
133 }
136 {
137  if (gradients_size > 0)
138  {
139  gradients[0] = nullptr;
140 
141  for (unsigned int id = 1; id < gradients_size; ++id)
142  {
143  if (gradients[id] != nullptr)
144  {
145  delete gradients[id];
146  gradients[id] = nullptr;
147  }
148  }
149 
150  delete [] gradients;
151  gradients = nullptr;
152 
153  gradients_size = 0;
154  }
155  _instance = this;
156 }
157 
163 {
164  size_t tmp = TOTAL_BYTES - PREVIOUS_TOTAL_BYTES;
165  PREVIOUS_TOTAL_BYTES = TOTAL_BYTES;
166  return tmp;
167 }
168 
173 char lastchar(char* s)
174 {
175  size_t k = strlen(s);
176  return s[k - 1];
177 }
178 
184 {
185  return TOTAL_BYTES;
186 }
187 
188  void fill_ad_random_part(void);
189  extern char ad_random_part[6];
190 
193 {
194 #if defined(USE_VECTOR_SHAPE_POOL)
195  void cleanup_xpools();
196  cleanup_xpools();
197 #endif
199  if (gs)
200  {
201  delete gs;
202  gs = nullptr;
203  }
204 }
205 
211 {
212  int on,nopt = 0;
213  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mdl",nopt))>-1)
214  {
215  if (nopt ==1)
216  {
217  const int i = atoi(ad_comm::argv[on+1]);
218  if (i > 0)
219  {
220  gradient_structure::MAX_DLINKS = (unsigned int)i;
221  }
222  }
223  else
224  {
225  cerr << "Wrong number of options to -mdl -- must be 1"
226  " you have " << nopt << endl;
227  ad_exit(1);
228  }
229  }
230  unsigned int numlinks=gradient_structure::MAX_DLINKS;
231  //cout << sizeof(dlink) << endl;
232 
233 #ifdef DEBUG
234  //cerr << "sizeof(char) is not equal 1) --"
235  // " need to modify allocate_dvariable_space in gradstrc.cpp" << endl;
236  assert(sizeof(char) == 1);
237 
238  //cerr << "sizeof(dlink) is greater than 2*sizeof(double) --"
239  // " need to modify allocate_dvariable_space in gradstrc.cpp" << endl;
240  assert(sizeof(dlink) == 2 * sizeof(double));
241 #endif
242  const size_t size = 2 * sizeof(double) * (numlinks + 1);
243  char* tmp1 = (char*)malloc(size * sizeof(char));
244  if (!tmp1)
245  {
246  cerr << "Error[" << __FILE__ << ":" << __LINE__
247  << "]: unable to allocate memory.\n";
248  ad_exit(1);
249  }
250  else
251  {
252  dlink * dl=(dlink*)tmp1;
253  tmp1+=2*sizeof(double);
254  dl->prev=NULL;
255  dlink * prev=dl;
257  int& nlinks=(int&)GRAD_LIST->nlinks;
258  GRAD_LIST->dlink_addresses[nlinks++]=dl;
259  for (unsigned int i=1;i<=numlinks;i++)
260  {
261  dl=(dlink*)tmp1;
262  dl->prev=prev;
263  prev=dl;
264  tmp1+=2*sizeof(double);
265 
266  GRAD_LIST->dlink_addresses[nlinks++]=dl;
267  // keep track of the links so you can zero them out
268  }
269  GRAD_LIST->last=dl;
270  }
271 }
272 
273 #ifdef USE_THREAD
274 std::mutex gsm;
275 #endif
276 
280 gradient_structure::gradient_structure(const long int _size, const unsigned int id):
281  NVAR(0),
282  x(id),
283  hessian_ptr(NULL),
284  max_last_offset(0)
285 {
286 #ifdef DEBUG
287  assert(_size > 0);
288 #endif
290 
291  gradients = nullptr;
292  gradients_size = 0;
293 
294  TOTAL_BYTES = 0;
296 
297  save_var_file_flag = 0;
298 
299 #ifdef USE_THREAD
300  gsm.lock();
301 #endif
302  ++instances;
303 #ifdef USE_THREAD
304  gsm.unlock();
305 #endif
306 
307  //Should be a multiple of sizeof(double_and_int)
308  const long int remainder = _size % sizeof(double_and_int);
310  static_cast<unsigned long int>(_size - remainder);
311 
312  {
313  int on,nopt = 0;
314  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-ndv",nopt))>-1)
315  {
316  if (!nopt)
317  {
318  cerr << "Usage -ndv option needs integer -- ignored" << endl;
319  }
320  else
321  {
322  int jj=atoi(ad_comm::argv[on+1]);
323  if (jj<=0)
324  {
325  cerr << "Usage -ndv option needs positive integer -- ignored"
326  << endl;
327  }
328  else
329  {
331  }
332  }
333  }
335  memory_allocate_error("DEPVARS_INFO", (void *) DEPVARS_INFO);
336  }
337 
338  if (x > 0)
340  else
342 
343  memory_allocate_error("_fp", (void*)_fp);
344 
346 
347 #ifdef DIAG
348  cerr <<" In gradient_structure::gradient_structure()\n";
349  cerr <<" ARRAY_MEMBLOCK_SIZE = " << ARRAY_MEMBLOCK_SIZE << "\n";
350 #endif
351 
352  {
353  GRAD_LIST = new dlist;
354  memory_allocate_error("GRAD_LIST", (void *) GRAD_LIST);
355  }
356  {
357  ARR_LIST1 = new arr_list;
358  memory_allocate_error("ARR_LIST1", (void *) ARR_LIST1);
359  }
360 
361  void* temp_ptr = NULL;
362 #ifdef __ZTC__
363  if ((temp_ptr = farmalloc(ARRAY_MEMBLOCK_SIZE)) == 0)
364 #else
365  if ((temp_ptr = (void*)malloc(ARRAY_MEMBLOCK_SIZE)) == 0)
366 #endif
367  {
368  cerr << "insufficient memory to allocate space for ARRAY_MEMBLOCKa\n";
369  ad_exit(1);
370  }
371 
372  ARR_LIST1->ARRAY_MEMBLOCK_BASE = temp_ptr;
373 
374  const size_t adjustment = (8 -((size_t)ARR_LIST1->ARRAY_MEMBLOCK_BASE.ptr) % 8) % 8;
375  ARR_LIST1->ARRAY_MEMBLOCK_BASE.adjust(adjustment);
376 
377  if (x > 0)
379  else
380  _GRAD_STACK1 = new grad_stack();
381  memory_allocate_error("_GRAD_STACK1", _GRAD_STACK1);
384 
385 #ifdef DIAG
386  cout << "_GRAD_STACK1= "<< _farptr_tolong(_GRAD_STACK1)<<"\n";
387 #endif
388 
389  {
391  memory_allocate_error("INDVAR_LIST",INDVAR_LIST);
392  // ****************************************************************
393  // ****************************************************************
394  int nopt=0;
395  int on=0;
396 
397  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mno",nopt))>-1)
398  {
399  if (nopt ==1)
400  {
401  const int i = atoi(ad_comm::argv[on+1]);
402  MAX_NVAR_OFFSET = static_cast<unsigned int>(i);
403  }
404  else
405  {
406  cerr << "Wrong number of options to -mno -- must be 1"
407  " you have " << nopt << endl;
408  ad_exit(1);
409  }
410  }
411 
412  // ****************************************************************
413  // ****************************************************************
414 
415  INDVAR_LIST->address = new double * [ (size_t) MAX_NVAR_OFFSET];
416  memory_allocate_error("INDVAR_LIST->address",INDVAR_LIST->address);
417  }
418 
419  _instance = this;
420 
421  //allocate_dvariable_space();
422  if (true)
423  {
425  memory_allocate_error("RETURN_ARRAYS",RETURN_ARRAYS);
426 
427  //allocate_dvariable_space();
428  for (unsigned int i = 0; i < NUM_RETURN_ARRAYS; ++i)
429  {
431  memory_allocate_error("RETURN_ARRAYS[i]", RETURN_ARRAYS[i]);
432  }
433  RETURN_ARRAYS_PTR = 0;
437  //RETURN_INDEX = 0;
438 
440  memory_allocate_error("RETURN_INDICES_CONTAINER",RETURN_PTR_CONTAINER);
441 
442  for (unsigned int i = 0; i < NUM_RETURN_ARRAYS; ++i)
443  {
444  RETURN_PTR_CONTAINER[i] = 0;
445  }
446  }
447  else
448  {
449  RETURN_ARRAYS = nullptr;
450  RETURN_ARRAYS_PTR = 0;
451  MIN_RETURN = nullptr;
452  MAX_RETURN = nullptr;
453  RETURN_PTR = nullptr;
454  RETURN_PTR_CONTAINER = nullptr;
455  }
456 }
458 {
459  /*
460  if (++_instance->RETURN_PTR > _instance->MAX_RETURN)
461  _instance->RETURN_PTR = _instance->MIN_RETURN;
462 
463  return _instance->RETURN_PTR;
464  */
466 }
467 
475 {
477 }
479 {
480 #if defined(THREAD_SAFE)
481  pthread_mutex_lock(&mutex_return_arrays);
482 #endif
486  {
487  cerr << " Overflow in RETURN_ARRAYS stack -- Increase NUM_RETURN_ARRAYS\n";
488  cerr << " There may be a RETURN_ARRAYS_INCREMENT()\n";
489  cerr << " which is not matched by a RETURN_ARRAYS_DECREMENT()\n";
490  ad_exit(24);
491  }
496 #if defined(THREAD_SAFE)
497  pthread_mutex_unlock(&mutex_return_arrays);
498 #endif
499 }
500 
508 {
510 }
512 {
513 #if defined(THREAD_SAFE)
514  pthread_mutex_lock(&mutex_return_arrays);
515 #endif
516  if (RETURN_ARRAYS_PTR == 0)
517  {
518  cerr << " Error -- RETURN_ARRAYS_PTR is 0 \n";
519  cerr << " There must be a RETURN_ARRAYS_DECREMENT()\n";
520  cerr << " which is not matched by a RETURN_ARRAYS_INCREMENT()\n";
521  ad_exit(24);
522  }
524 
529 
530 #if defined(THREAD_SAFE)
531  pthread_mutex_unlock(&mutex_return_arrays);
532 #endif
533 }
538 {
539  _instance = this;
540 
541  NVAR = 0;
542  if (RETURN_ARRAYS != NULL)
543  {
544  for (unsigned int i = 0; i < NUM_RETURN_ARRAYS; ++i)
545  {
546  delete [] RETURN_ARRAYS[i];
547  RETURN_ARRAYS[i] = NULL;
548  }
549  delete [] RETURN_ARRAYS;
550  RETURN_ARRAYS = NULL;
551  delete [] RETURN_PTR_CONTAINER;
552  RETURN_PTR_CONTAINER = NULL;
553  }
554  if (INDVAR_LIST != NULL)
555  {
556  delete [] INDVAR_LIST->address;
557  delete INDVAR_LIST;
558  INDVAR_LIST = NULL;
559  }
560  if (_GRAD_STACK1 != NULL)
561  {
562  delete _GRAD_STACK1;
563  _GRAD_STACK1 = NULL;
564  }
566 
567  if (ARR_LIST1 != NULL)
568  {
569  delete ARR_LIST1;
570  ARR_LIST1 = NULL;
571  }
572  if (GRAD_LIST != NULL)
573  {
574  delete GRAD_LIST;
575  GRAD_LIST = NULL;
576  }
577 
578 #ifdef USE_THREAD
579  gsm.lock();
580 #endif
581  --instances;
582 #ifdef USE_THREAD
583  gsm.unlock();
584 #endif
585 
586  if (DEPVARS_INFO==NULL)
587  {
589  ad_exit(1);
590  }
591 
592  delete DEPVARS_INFO;
593  DEPVARS_INFO=NULL;
594 
595  if (_fp == NULL)
596  {
597  cerr << "Trying to close stream referenced by a NULL pointer\n"
598  " in ~gradient_structure\n";
599  ad_exit(1);
600  }
601 
602  delete _fp;
603  _fp = NULL;
604 
605  gradient_structure::fp = nullptr;
606 
607  _instance = nullptr;
608 
610 }
611 
616 {
617  cerr << "Trying to delete a NULL pointer in ~gradient_structure" << endl;
618 }
619 
624 void memory_allocate_error(const char * s, void * ptr)
625 {
626  if (ptr == NULL)
627  {
628  cerr << " Error trying to allocate " << s << "\n";
629  ad_exit(21);
630  }
631 }
632 
633  #if defined(NO_DERIVS)
634 
642  {
643  no_derivatives=1;
644  }
645 
651  {
652  no_derivatives=0;
653  }
654  #endif
655 
661  {
662  save_var_flag=1;
663  }
664 
670  {
671  save_var_flag=0;
672  }
673 
679  {
680  if (i<1)
681  {
682  cerr << " Error in "
683  "gradient_structure::set_NUM_DEPENDENT_VARIABLES(int i)"
684  << endl << " value of i must be >= 1" << endl;
685  i=1;
686  }
688  }
indvar_offset_list * INDVAR_LIST
static int NUM_DEPENDENT_VARIABLES
grad_stack_entry * true_ptr_first
Definition: fvar.hpp:923
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
void cleanup_xpools()
Definition: dfpool.cpp:22
void RETURN_ARRAYS_DECREMENT(void)
Decrements gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:507
double ** address
Definition: fvar.hpp:1288
virtual ~gradient_structure()
Destructor.
Definition: gradstrc.cpp:537
dependent_variables_information * DEPVARS_INFO
void create(const unsigned int size)
Allocate array of gradient_structure instances with size elements.
Definition: gradstrc.cpp:106
#define x
Description not yet available.
Definition: fvar.hpp:7937
static char ** argv
Definition: fvar.hpp:8866
static unsigned long ARRAY_MEMBLOCK_SIZE
void memory_allocate_error(const char *s, void *ptr)
Description not yet available.
Definition: gradstrc.cpp:624
static long int USE_FOR_HESSIAN
exitptr ad_exit
Definition: gradstrc.cpp:53
friend void cleanup_temporary_files()
Close gradient and variable files and free gradient structure memory.
Definition: gradstrc.cpp:192
char lastchar(char *)
Description not yet available.
Definition: gradstrc.cpp:173
static unsigned int NUM_RETURN_ARRAYS
dlink * last
Definition: fvar.hpp:836
static unsigned int MAX_NVAR_OFFSET
int atoi(adstring &s)
Returns a integer converted from input s.
Definition: atoi.cpp:20
void allocate_dvariable_space()
Description not yet available.
Definition: gradstrc.cpp:210
Holds the data for the prevariable class.
Definition: fvar.hpp:191
humungous_pointer ARRAY_MEMBLOCK_BASE
Definition: fvar.hpp:2074
static unsigned int RETURN_ARRAYS_SIZE
size_t totalbytes(void)
Description not yet available.
Definition: gradstrc.cpp:183
prnstream & endl(prnstream &)
void null_ptr_err_message(void)
Outputs null error message.
Definition: gradstrc.cpp:615
static dvariable * next_RETURN_PTR()
Definition: gradstrc.cpp:457
void RETURN_ARRAYS_INCREMENT()
Definition: gradstrc.cpp:478
static int argc
Definition: fvar.hpp:8863
unsigned int nlinks
Definition: fvar.hpp:837
size_t NUM_GRADSTACK_BYTES_WRITTEN()
Description not yet available.
Definition: gradstrc.cpp:162
void clean()
Delete gradient_structure instances.
Definition: gradstrc.cpp:135
dlink ** dlink_addresses
Definition: fvar.hpp:838
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
static gradient_structure * reset(gradient_structure *)
Definition: gradstrc.cpp:120
static _THREAD gradient_structure * _instance
dvariable ** RETURN_PTR_CONTAINER
void adjust(const size_t)
Description not yet available.
Definition: humung.cpp:50
char ad_random_part[6]
Definition: gradstak.cpp:85
Description not yet available.
Definition: fvar.hpp:1284
gradient_structure * set(const unsigned int id)
Set _instance of gradient_structure from instances with id.
Definition: gradstrc.cpp:126
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
static _THREAD DF_FILE * fp
static void set_YES_SAVE_VARIABLES_VALUES()
Description not yet available.
Definition: gradstrc.cpp:660
static void set_NUM_DEPENDENT_VARIABLES(int i)
Description not yet available.
Definition: gradstrc.cpp:678
dvariable ** RETURN_ARRAYS
static int Hybrid_bounded_flag
Link list.
Definition: fvar.hpp:834
static void set_NO_SAVE_VARIABLES_VALUES()
Description not yet available.
Definition: gradstrc.cpp:669
unsigned int RETURN_ARRAYS_PTR
void fill_ad_random_part(void)
Definition: gradstak.cpp:87
int(* fptr)(const char *format,...)
Definition: gradstrc.cpp:48
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
void RETURN_ARRAYS_DECREMENT()
Definition: gradstrc.cpp:511
static unsigned int MAX_DLINKS
void(* exitptr)(int)
Definition: fvar.hpp:180
void RETURN_ARRAYS_INCREMENT(void)
Increments gradient_structure::RETURN_ARRAYS_PTR.
Definition: gradstrc.cpp:474
static size_t GRADSTACK_BUFFER_SIZE
static _THREAD grad_stack * GRAD_STACK1
Description not yet available.
Definition: fvar.hpp:766
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
void spdll_exit(int)
Definition: gradstrc.cpp:55
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487
gradient_structure ** gradients
void cleanup_temporary_files()
Close gradient and variable files and free gradient structure memory.
Definition: gradstrc.cpp:192
unsigned int gradients_size
int ctlc_flag
Definition: gradstrc.cpp:68
static size_t CMPDIF_BUFFER_SIZE