ADMB Documentation  11.1.2535
 All Classes Files Functions Variables Typedefs Friends Defines
cmpdif3.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: cmpdif3.cpp 2521 2014-10-29 18:10:22Z 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   gradient_structure::get_fp()->fread(str1,num_bytes);
00046   cout << "in report_gradstack_flag  " << str1 << endl;
00047 }
00048 
00049 #if defined(CHK_ID_STRING)
00050 
00054 static void report_gradstack_flag2(void)
00055 {
00056   verify_identifier_string("stack");
00057   // Back up the stream and read the number of bytes written in the
00058   // ``write function'' corresponding to this ``read function''
00059   int num_bytes;
00060   gradient_structure::get_fp()->fread(&num_bytes,sizeof(int));
00061   char str1[100];
00062   str1[0]='\0';
00063   gradient_structure::get_fp()->fread(str1,num_bytes);
00064   int i,j;
00065   gradient_structure::get_fp()->fread(&j,sizeof(int));
00066   gradient_structure::get_fp()->fread(&i,sizeof(int));
00067   cout << "in report_gradstack_flag  " << str1 << endl;
00068   if (i==3 && j==91)
00069     cout << "HERE" << endl;
00070 }
00071 #endif
00072 
00077 void set_gradstack_flag(char* str)
00078 {
00079 #if defined(CHK_ID_STRING)
00080   //int wsize=sizeof(char);
00081 #ifdef OPT_LIB
00082   int length=(int)strlen(str);
00083 #else
00084   size_t _length = strlen(str);
00085   assert(_length <= INT_MAX);
00086   int length=(int)_length;
00087 #endif
00088   gradient_structure::get_fp()->fwrite(str,length);
00089   gradient_structure::get_fp()->fwrite(&length,sizeof(int));
00090   gradient_structure::GRAD_STACK1->
00091     set_gradient_stack(report_gradstack_flag);
00092   save_identifier_string("stack");
00093 #endif
00094 }
00095 
00100 void set_gradstack_flag(char* _str,int i,int j)
00101 {
00102 #if defined(CHK_ID_STRING)
00103   adstring ads=_str;
00104   ads+=" ";
00105   ads+=str(i);
00106   ads+=" ";
00107   ads+=str(j);
00108   //int wsize=sizeof(char);
00109   char * str=(char*)(ads);
00110 #ifdef OPT_LIB
00111   int length=(int)strlen(str);
00112 #else
00113   size_t _length = strlen(str);
00114   assert(_length <= INT_MAX);
00115   int length=(int)_length;
00116 #endif
00117   gradient_structure::get_fp()->fwrite(&i,sizeof(int));
00118   gradient_structure::get_fp()->fwrite(&j,sizeof(int));
00119   gradient_structure::get_fp()->fwrite(str,length);
00120   gradient_structure::get_fp()->fwrite(&length,sizeof(int));
00121   gradient_structure::GRAD_STACK1->
00122     set_gradient_stack(report_gradstack_flag2);
00123   save_identifier_string("stack");
00124 #endif
00125 }
00126 
00136 void verify_identifier_string(const char* str1)
00137 {
00138 #if defined(CHK_ID_STRING)
00139   // Back up the stream and read the number of bytes written in the
00140   // ``write function'' corresponding to this ``read function''
00141   size_t num_bytes=strlen(str1);
00142   char str[10];
00143   str[num_bytes]='\0';
00144   gradient_structure::get_fp()->fread(str,num_bytes);
00145   //clogf << "in verify_id_string " << str1 << endl;
00146   if(strcmp(str1,str))
00147   {
00148     cerr << "Error reading stack identifer for " << str1 << endl;
00149     ad_exit(1);
00150   }
00151 #endif
00152 }
00153 
00158 adstring get_string_marker(void)
00159 {
00160   adstring str1;
00161 #if defined(CHK_ID_STRING)
00162   // Back up the stream and read the number of bytes written in the
00163   // ``write function'' corresponding to this ``read function''
00164   long int num_bytes=5;
00165   char str[10];
00166   str[num_bytes]='\0';
00167   gradient_structure::get_fp()->fread(str,num_bytes);
00168   //clogf << "in verify_id_string " << str1 << endl;
00169   str1=str;
00170 #endif
00171  return str1;
00172 }
00173 
00178 void ivector::save_ivector_position(void) const
00179 {
00180   // saves the size and address information for a ivector
00181   unsigned wsize=sizeof(ivector_position);
00182   ivector_position tmp(*this);
00183   gradient_structure::get_fp()->fwrite(&tmp,wsize);
00184 }
00185 
00190 void dvar_vector::save_dvar_vector_position(void) const
00191 {
00192   // saves the size and address information for a dvar_vector
00193   unsigned wsize=sizeof(dvar_vector_position);
00194   dvar_vector_position tmp(*this);
00195   gradient_structure::get_fp()->fwrite(&tmp,size_t(wsize));
00196 }
00197 
00202 void save_ad_pointer(void * p)
00203 {
00204   // saves the size and address information for a dvar_vector
00205   unsigned wsize=sizeof(void *);
00206   gradient_structure::get_fp()->fwrite(&p,size_t(wsize));
00207 }
00208 
00213 void * restore_ad_pointer(void)
00214 {
00215   void * p=0;
00216   // saves the size and address information for a dvar_vector
00217   unsigned wsize=sizeof(void *);
00218   gradient_structure::get_fp()->fread(&p,size_t(wsize));
00219   return p;
00220 }