ADMB Documentation  11.1.1674
 All Classes Files Functions Variables Typedefs Friends Defines
slgrdclc.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: slgrdclc.cpp 1112 2013-07-12 21:41:41Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 #include <sys/stat.h>
00014 #include <fcntl.h>
00015 #include <string.h>
00016 
00017 #ifdef __TURBOC__
00018   #pragma hdrstop
00019   #include <iostream.h>
00020 #endif
00021 
00022 #ifdef __ZTC__
00023   #include <iostream.hpp>
00024 #endif
00025 
00026 #if defined (__WAT32__)
00027 #  include <io.h>
00028 #endif
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 
00033 #ifdef __MSVC32__
00034   #define lseek _lseek
00035   #define  read _read
00036   #define write _write
00037 #endif
00038 
00039 #ifdef __SUN__
00040   #include <iostream.h>
00041   #include <fcntl.h>
00042   #include <sys/stat.h>
00043   #include <sys/types.h>
00044   #ifndef __MSVC32__
00045     #include <unistd.h>
00046   #endif
00047 #endif
00048 
00049 #if defined(__GNU__) || defined(UNIXKLUDGE)
00050   //#include <iostream.h>
00051   #include <fcntl.h>
00052   #include <sys/stat.h>
00053   #include <sys/types.h>
00054   #include <unistd.h>
00055 #endif
00056 
00057 #if defined(__NDPX__ )
00058   extern "C" {
00059     int lseek(int, int, int);
00060     int read(int, char*, int);
00061   };
00062 #endif
00063 
00064 #include <math.h>
00065 
00066 #if (defined(__ZTC__) && !defined(DOS386))
00067   void _far * _cdecl _farptr_norm(void _far *);
00068   void _far * _cdecl _farptr_fromlong(unsigned long);
00069   long _cdecl _farptr_tolong(void _far *);
00070 #endif
00071 
00072 //void KLUDGEX(void * p);
00073 
00078 void slave_gradcalc(void)
00079 {
00080   if (gradient_structure::no_derivatives)
00081   {
00082     reset_gradient_stack();
00083   }
00084   else
00085   {
00086     gradient_structure::TOTAL_BYTES = 0;
00087     gradient_structure::PREVIOUS_TOTAL_BYTES=0;
00088     unsigned int i;
00089     my_off_t lpos;
00090 
00091      gradient_structure::GRAD_STACK1->_GRADFILE_PTR =
00092                 gradient_structure::GRAD_STACK1->gradfile_handle();
00093 
00094     int& _GRADFILE_PTR=gradient_structure::GRAD_STACK1->_GRADFILE_PTR;
00095 
00096     lpos = lseek(_GRADFILE_PTR,0L,SEEK_CUR);
00097 
00098     if(gradient_structure::GRAD_STACK1->ptr
00099          <= gradient_structure::GRAD_STACK1->ptr_first)
00100     {
00101      /*
00102       #ifdef SAFE_ARRAYS
00103         cerr << "warning -- calling gradcalc when no calculations generating"
00104      << endl << "derivative information have occurred" << endl;
00105       #endif
00106      */
00107       return;
00108     }    // current is one past the end so -- it
00109 
00110     if (gradient_structure::save_var_flag)
00111     {
00112       gradient_structure::save_arrays();
00113       gradient_structure::save_variables();
00114     }
00115 
00116     gradient_structure::GRAD_STACK1->ptr--;
00117 
00118     for (i=0; i<gradient_structure::GRAD_LIST->nlinks; i++)
00119     {
00120       * (double*) (gradient_structure::GRAD_LIST->dlink_addresses[i]) = 0;
00121     }
00122 
00123     #if defined (__BORLANDC__) && !defined(DOS386)
00124       double_and_int huge * tmp;
00125     #else
00126       double_and_int * tmp;
00127     #endif
00128 
00129     #if defined (__BORLANDC__) && !defined(DOS386)
00130        tmp = (double_and_int huge *) gradient_structure::ARRAY_MEMBLOCK_BASE;
00131     #else
00132        tmp = (double_and_int *) gradient_structure::ARRAY_MEMBLOCK_BASE;
00133     #endif
00134 
00135     unsigned long int max_last_offset = gradient_structure::ARR_LIST1->get_max_last_offset();
00136 
00137     unsigned int size = sizeof(double_and_int );
00138 
00139     double * zptr;
00140 
00141     for (i=0 ; i< (max_last_offset/size) ; i++ )
00142     {
00143       tmp->x = 0;
00144       #if defined (__ZTC__)
00145         #if defined(DOS386)
00146         tmp++;
00147          #else
00148           tmp = (double_and_int  *) _farptr_norm( (void*) (++tmp)  );
00149          #endif
00150        #endif
00151        #if defined (__BORLANDC__)
00152           tmp++;
00153        #endif
00154        #if (!defined (__ZTC__) && !defined (__BORLANDC__))
00155          tmp++;
00156        #endif
00157     }
00158 
00159     //* gradient_structure::GRAD_STACK1->ptr->dep_addr  = 1;
00160     zptr = gradient_structure::GRAD_STACK1->ptr->dep_addr;
00161 
00162     //double z;
00163     int break_flag=1;
00164 
00165     do
00166     {
00167       gradient_structure::GRAD_STACK1->ptr++;
00168       #ifdef FAST_ASSEMBLER
00169         gradloop();
00170       #else
00171         grad_stack_entry * grad_ptr_first=
00172           gradient_structure::GRAD_STACK1->ptr_first;
00173         while (gradient_structure::GRAD_STACK1->ptr-- >
00174         grad_ptr_first)
00175         {
00176           //KLUDGEX(gradient_structure::GRAD_STACK1->ptr);
00177           (*(gradient_structure::GRAD_STACK1->ptr->func))();
00178         }
00179 
00180       #endif
00181 
00182       // back up the file one buffer size and read forward
00183       //KLUDGEX(gradient_structure::GRAD_STACK1->ptr);
00184       lpos = lseek(gradient_structure::GRAD_STACK1->_GRADFILE_PTR,
00185           -((long int)(sizeof(grad_stack_entry)*gradient_structure::
00186             GRAD_STACK1->length)),SEEK_CUR);
00187       //KLUDGEX(gradient_structure::GRAD_STACK1->ptr);
00188 
00189       break_flag=gradient_structure::GRAD_STACK1->read_grad_stack_buffer(lpos);
00190 
00191     }  while (break_flag); // do
00192 
00193     {
00194      #ifdef GRAD_DIAG
00195       my_off_t ttmp =
00196      #endif
00197         lseek(gradient_structure::GRAD_STACK1->_GRADFILE_PTR, 0,SEEK_CUR);
00198      #ifdef GRAD_DIAG
00199         cout << "Offset in file at end of gradcalc is " << ttmp
00200                 << " bytes from the beginning\n";
00201      #endif
00202     }
00203 
00204 
00205     gradient_structure::GRAD_STACK1->ptr = gradient_structure::GRAD_STACK1->ptr_first;
00206 
00207     if (gradient_structure::save_var_flag)
00208     {
00209       gradient_structure::restore_arrays();
00210       gradient_structure::restore_variables();
00211     }
00212   }
00213 }