ADMB Documentation  11.1x.2711
 All Classes Files Functions Variables Typedefs Friends Defines
cmpdif3.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: cmpdif3.cpp 2634 2014-11-12 23:01:48Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <fvar.hpp>
00012 
00013 #ifdef __TURBOC__
00014   #pragma hdrstop
00015   #include <iostream.h>
00016 #endif
00017 
00018 #ifdef __ZTC__
00019   #include <iostream.hpp>
00020 #endif
00021 
00022 #include <string.h>
00023 
00024 #ifndef OPT_LIB
00025   #include <cassert>
00026   #include <climits>
00027   #define CHK_ID_STRING
00028 #endif
00029 
00030 extern ofstream clogf;
00031 
00036 void report_gradstack_flag(void)
00037 {
00038   verify_identifier_string("stack");
00039   // Back up the stream and read the number of bytes written in the
00040   // ``write function'' corresponding to this ``read function''
00041   int num_bytes;
00042   gradient_structure::get_fp()->fread(&num_bytes,sizeof(int));
00043   char str1[100];
00044   str1[0]='\0';
00045 #ifndef OPT_LIB
00046   assert(num_bytes > 0);
00047 #endif
00048   gradient_structure::get_fp()->fread(str1,(size_t)num_bytes);
00049   cout << "in report_gradstack_flag  " << str1 << endl;
00050 }
00051 
00052 #if defined(CHK_ID_STRING)
00053 
00057 static void report_gradstack_flag2(void)
00058 {
00059   verify_identifier_string("stack");
00060   // Back up the stream and read the number of bytes written in the
00061   // ``write function'' corresponding to this ``read function''
00062   int num_bytes;
00063   gradient_structure::get_fp()->fread(&num_bytes,sizeof(int));
00064   char str1[100];
00065   str1[0]='\0';
00066 #ifndef OPT_LIB
00067   assert(num_bytes >= 0);
00068 #endif
00069   gradient_structure::get_fp()->fread(str1, (size_t)num_bytes);
00070   int i,j;
00071   gradient_structure::get_fp()->fread(&j,sizeof(int));
00072   gradient_structure::get_fp()->fread(&i,sizeof(int));
00073   cout << "in report_gradstack_flag  " << str1 << endl;
00074 }
00075 #endif
00076 
00081 void set_gradstack_flag(char* str)
00082 {
00083 #if defined(CHK_ID_STRING)
00084   //int wsize=sizeof(char);
00085 #ifdef OPT_LIB
00086   int length=(int)strlen(str);
00087 #else
00088   size_t _length = strlen(str);
00089   assert(_length <= INT_MAX);
00090   int length=(int)_length;
00091 #endif
00092 #ifndef OPT_LIB
00093   assert(length >= 0);
00094 #endif
00095   gradient_structure::get_fp()->fwrite(str, (size_t)length);
00096   gradient_structure::get_fp()->fwrite(&length,sizeof(int));
00097   gradient_structure::GRAD_STACK1->
00098     set_gradient_stack(report_gradstack_flag);
00099   save_identifier_string("stack");
00100 #endif
00101 }
00102 
00107 void set_gradstack_flag(char* _str,int i,int j)
00108 {
00109 #if defined(CHK_ID_STRING)
00110   adstring ads=_str;
00111   ads+=" ";
00112   ads+=str(i);
00113   ads+=" ";
00114   ads+=str(j);
00115   //int wsize=sizeof(char);
00116   char * str=(char*)(ads);
00117 #ifdef OPT_LIB
00118   int length=(int)strlen(str);
00119 #else
00120   size_t _length = strlen(str);
00121   assert(_length <= INT_MAX);
00122   int length=(int)_length;
00123 #endif
00124   gradient_structure::get_fp()->fwrite(&i,sizeof(int));
00125   gradient_structure::get_fp()->fwrite(&j,sizeof(int));
00126 #ifndef OPT_LIB
00127   assert(length >= 0);
00128 #endif
00129   gradient_structure::get_fp()->fwrite(str, (size_t)length);
00130   gradient_structure::get_fp()->fwrite(&length,sizeof(int));
00131   gradient_structure::GRAD_STACK1->
00132     set_gradient_stack(report_gradstack_flag2);
00133   save_identifier_string("stack");
00134 #endif
00135 }
00136 
00146 void verify_identifier_string(const char* str1)
00147 {
00148 #if defined(CHK_ID_STRING)
00149   // Back up the stream and read the number of bytes written in the
00150   // ``write function'' corresponding to this ``read function''
00151   size_t num_bytes=strlen(str1);
00152   char str[10];
00153   str[num_bytes]='\0';
00154   gradient_structure::get_fp()->fread(str,num_bytes);
00155   //clogf << "in verify_id_string " << str1 << endl;
00156   if(strcmp(str1,str))
00157   {
00158     cerr << "Error reading stack identifer for " << str1 << endl;
00159     ad_exit(1);
00160   }
00161 #endif
00162 }
00163 
00168 adstring get_string_marker(void)
00169 {
00170   adstring str1;
00171 #if defined(CHK_ID_STRING)
00172   // Back up the stream and read the number of bytes written in the
00173   // ``write function'' corresponding to this ``read function''
00174   long int num_bytes=5;
00175   char str[10];
00176   str[num_bytes]='\0';
00177 #ifndef OPT_LIB
00178   assert(num_bytes > 0);
00179 #endif
00180   gradient_structure::get_fp()->fread(str,(size_t)num_bytes);
00181   //clogf << "in verify_id_string " << str1 << endl;
00182   str1=str;
00183 #endif
00184  return str1;
00185 }
00186 
00191 void ivector::save_ivector_position(void) const
00192 {
00193   // saves the size and address information for a ivector
00194   unsigned wsize=sizeof(ivector_position);
00195   ivector_position tmp(*this);
00196   gradient_structure::get_fp()->fwrite(&tmp,wsize);
00197 }
00198 
00203 void dvar_vector::save_dvar_vector_position(void) const
00204 {
00205   // saves the size and address information for a dvar_vector
00206   unsigned wsize=sizeof(dvar_vector_position);
00207   dvar_vector_position tmp(*this);
00208   gradient_structure::get_fp()->fwrite(&tmp,size_t(wsize));
00209 }
00210 
00215 void save_ad_pointer(void * p)
00216 {
00217   // saves the size and address information for a dvar_vector
00218   unsigned wsize=sizeof(void *);
00219   gradient_structure::get_fp()->fwrite(&p,size_t(wsize));
00220 }
00221 
00226 void * restore_ad_pointer(void)
00227 {
00228   void * p=0;
00229   // saves the size and address information for a dvar_vector
00230   unsigned wsize=sizeof(void *);
00231   gradient_structure::get_fp()->fread(&p,size_t(wsize));
00232   return p;
00233 }