ADMB Documentation  Fournier-pthread.1088
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
thread_stuff8.cpp
Go to the documentation of this file.
00001 #include <admodel.h>
00002 #include <cstddef>
00003 #include "adthread.h"
00004 #if !defined(OPT_LIB)
00005 #  if !defined(CHK_ID_STRING)
00006 #    define CHK_ID_STRING
00007 #  endif
00008 #endif
00009 
00010 
00011 void adjoint_send_dvar5_array_to_slave(void)
00012 {
00013   verify_identifier_string("CV");
00014   adpthread_manager * ptr=(adpthread_manager*)(restore_pointer_value());
00015   ptr->adjoint_send_dvar5_array_to_slave();
00016 }
00017 
00018 void adpthread_manager::adjoint_send_dvar5_array_to_slave(void)
00019 {
00020   verify_identifier_string("HH");
00021   int sno=restore_int_value();
00022   verify_id_string_from_slave("FDG",sno);
00023   int h5min;
00024   int h5max;
00025   readbuffer(&h5min,sizeof(int),sno);
00026   readbuffer(&h5max,sizeof(int),sno);
00027   d5_array M(h5min,h5max);
00028   ivector hsmin(h5min,h5max);
00029   ivector hsmax(h5min,h5max);
00030   imatrix smin(h5min,h5max);
00031   imatrix smax(h5min,h5max);
00032   i3_array rmin(h5min,h5max);
00033   i3_array rmax(h5min,h5max);
00034   for (int l5=h5min;l5<=h5max;l5++)
00035   {
00036     readbuffer(&(hsmin(l5)),sizeof(int),sno);
00037     readbuffer(&(hsmax(l5)),sizeof(int),sno);
00038     M(l5).allocate(hsmin(l5),hsmax(l5));
00039     int l;
00040     rmin(l5).allocate(hsmin(l5),hsmax(l5));
00041     rmax(l5).allocate(hsmin(l5),hsmax(l5));
00042     smin(l5).allocate(hsmin(l5),hsmax(l5));
00043     smax(l5).allocate(hsmin(l5),hsmax(l5));
00044     for (l=hsmin(l5);l<=hsmax(l5);l++)
00045     {
00046       readbuffer(&(smin(l5,l)),sizeof(int),sno);
00047       readbuffer(&(smax(l5,l)),sizeof(int),sno);
00048       M(l5,l).allocate(smin(l5,l),smax(l5,l));
00049       rmin(l5,l).allocate(smin(l5,l),smax(l5,l));
00050       rmax(l5,l).allocate(smin(l5,l),smax(l5,l));
00051       for (int i=smin(l5,l);i<=smax(l5,l);i++)
00052       {
00053         readbuffer(&(rmin(l5,l,i)),sizeof(int),sno);
00054         readbuffer(&(rmax(l5,l,i)),sizeof(int),sno);
00055         M(l5,l,i).allocate(rmin(l5,l,i),rmax(l5,l,i));
00056         for (int j=rmin(l5,l,i);j<=rmax(l5,l,i);j++)
00057         { 
00058           int cmin,cmax;
00059           readbuffer(&cmin,sizeof(int),sno);
00060           readbuffer(&cmax,sizeof(int),sno);
00061           M(l5,l,i,j).allocate(cmin,cmax);
00062           int sz=cmax-cmin+1;
00063           readbuffer(&(M(l5,l,i,j)(cmin)),sz*sizeof(double),sno);
00064         }
00065       }
00066     }
00067   }
00068   verify_identifier_string("OP");
00069   for (int l5=h5max;l5>=h5min;l5--)
00070   {
00071     for (int l=hsmax(l5);l>=hsmin(l5);l--)
00072     {
00073       for (int i=smax(l5,l);i>=smin(l5,l);i--)
00074       {
00075         dvar_matrix_position dmpos=restore_dvar_matrix_position();
00076         M(l5,l,i).save_dmatrix_derivatives(dmpos);
00077       }
00078     }
00079   }
00080   verify_identifier_string("VC");
00081 }
00082 
00083 void adpthread_manager::send_dvar5_array_to_slave(const dvar5_array &x,int sno)
00084 {
00085   send_id_string_to_slave("HYD",sno);
00086 
00087   int h5min=x.indexmin();
00088   int h5max=x.indexmax();
00089   writebuffer(&h5min,sizeof(int),sno);
00090   writebuffer(&h5max,sizeof(int),sno);
00091   int l5;
00092 
00093   ivector hsmin(h5min,h5max);
00094   ivector hsmax(h5min,h5max);
00095   imatrix smin(h5min,h5max);
00096   imatrix smax(h5min,h5max);
00097 
00098   for (l5=h5min;l5<=h5max;l5++)
00099   {
00100     hsmin(l5)=x(l5).indexmin();
00101     hsmax(l5)=x(l5).indexmax();
00102     writebuffer(&(hsmin(l5)),sizeof(int),sno);
00103     writebuffer(&(hsmax(l5)),sizeof(int),sno);
00104     int l;
00105   
00106     smin(l5).allocate(hsmin(l5),hsmax(l5));
00107     smax(l5).allocate(hsmin(l5),hsmax(l5));
00108     for (l=hsmin(l5);l<=hsmax(l5);l++)
00109     {
00110       smin(l5,l)=x(l5,l).indexmin();
00111       smax(l5,l)=x(l5,l).indexmax();
00112       int i;
00113       writebuffer(&(smin(l5,l)),sizeof(int),sno);
00114       writebuffer(&(smax(l5,l)),sizeof(int),sno);
00115       for (i=smin(l5,l);i<=smax(l5,l);i++)
00116       {
00117         int rmin=x(l5,l,i).indexmin();
00118         int rmax=x(l5,l,i).indexmax();
00119         writebuffer(&rmin,sizeof(int),sno);
00120         writebuffer(&rmax,sizeof(int),sno);
00121         for (int j=rmin;j<=rmax;j++)
00122         {
00123           int cmin=x(l5,l,i,j).indexmin();
00124           int cmax=x(l5,l,i,j).indexmax();
00125           writebuffer(&cmin,sizeof(int),sno);
00126           writebuffer(&cmax,sizeof(int),sno);
00127           int sz=cmax-cmin+1;
00128           writebuffer(&(value(x(l5,l,i,j)(cmin))),sz*sizeof(double),sno);
00129         }
00130       }
00131     }
00132   }
00133   save_identifier_string("VC");
00134   // !!! should we optimize this ?
00135   for (int l5=h5min;l5<=h5max;l5++)
00136   {
00137     for (int l=hsmin(l5);l<=hsmax(l5);l++)
00138     {
00139       for (int i=smin(l5,l);i<=smax(l5,l);i++)
00140       {
00141         x(l5,l,i).save_dvar_matrix_position();
00142       }
00143     }
00144   }
00145   save_identifier_string("OP");
00146   save_int_value(sno);
00147   save_identifier_string("HH");
00148   save_pointer_value(this);
00149   save_identifier_string("CV");
00150   gradient_structure::GRAD_STACK1->
00151     set_gradient_stack(::adjoint_send_dvar5_array_to_slave);
00152 }
00153 
00154 void adjoint_get_dvar5_array_from_master(void)
00155 {
00156   verify_identifier_string("I5");
00157   adpthread_manager * ptr=(adpthread_manager*)(restore_pointer_value());
00158   ptr->adjoint_get_dvar5_array_from_master();
00159 }
00160 
00161 void adpthread_manager::adjoint_get_dvar5_array_from_master(void)
00162 {
00163   verify_identifier_string("LG");
00164   int sno=restore_int_value();
00165   verify_identifier_string("W8");
00166   int h5min=restore_int_value();
00167   int h5max=restore_int_value();
00168   verify_identifier_string("GF");
00169   int l;
00170   d5_array dv(h5min,h5max);
00171   ivector hsmin(h5min,h5max);
00172   ivector hsmax(h5min,h5max);
00173   imatrix smin(h5min,h5max);
00174   imatrix smax(h5min,h5max);
00175   for (int l5=h5max;l5>=h5min;l5--)
00176   {
00177     hsmin(l5)=restore_int_value();
00178     hsmax(l5)=restore_int_value();
00179     smin(l5).allocate(hsmin(l5),hsmax(l5));
00180     smax(l5).allocate(hsmin(l5),hsmax(l5));
00181     dv(l5).allocate(hsmin(l5),hsmax(l5));
00182     for (l=hsmax(l5);l>=hsmin(l5);l--)
00183     {
00184       smin(l5,l)=restore_int_value();
00185       smax(l5,l)=restore_int_value();
00186       dv(l5,l).allocate(smin(l5,l),smax(l5,l));
00187       for (int i=smax(l5,l);i>=smin(l5,l);i--)
00188       {
00189         dvar_matrix_position dvpos=restore_dvar_matrix_position();
00190         dv(l5,l,i)=restore_dvar_matrix_derivatives(dvpos);
00191       }
00192     }
00193   }
00194   verify_identifier_string("X9");
00195   send_id_string_to_master("FDG",sno);
00196   writebuffer(&h5min,sizeof(int),sno);
00197   writebuffer(&h5max,sizeof(int),sno);
00198   for (int l5=h5min;l5<=h5max;l5++)
00199   {
00200     writebuffer(&(hsmin(l5)),sizeof(int),sno);
00201     writebuffer(&(hsmax(l5)),sizeof(int),sno);
00202     for (l=hsmin(l5);l<=hsmax(l5);l++)
00203     {
00204       writebuffer(&(smin(l5,l)),sizeof(int),sno);
00205       writebuffer(&(smax(l5,l)),sizeof(int),sno);
00206       int i;
00207       for (i=smin(l5,l);i<=smax(l5,l);i++)
00208       {
00209         int rmin=dv(l5,l,i).indexmin();
00210         int rmax=dv(l5,l,i).indexmax();
00211         writebuffer(&rmin,sizeof(int),sno);
00212         writebuffer(&rmax,sizeof(int),sno);
00213         for (int j=rmin;j<=rmax;j++)
00214         {
00215           int cmin=dv(l5,l,i,j).indexmin();
00216           int cmax=dv(l5,l,i,j).indexmax();
00217           writebuffer(&cmin,sizeof(int),sno);
00218           writebuffer(&cmax,sizeof(int),sno);
00219           int sz=cmax-cmin+1;
00220           writebuffer(&(dv(l5,l,i,j)(cmin)),sz*sizeof(double),sno);
00221         }
00222       }
00223     }
00224   }
00225 }
00226 dvar5_array adpthread_manager::get_dvar5_array_from_master(int sno)
00227 {
00228   verify_id_string_from_master("HYD",sno);
00229  // &***********************************************************
00230  // &***********************************************************
00231 
00232   int h5min;
00233   int h5max;
00234   readbuffer(&h5min,sizeof(int),sno);
00235   readbuffer(&h5max,sizeof(int),sno);
00236   int l5;
00237   dvar5_array M(h5min,h5max);
00238 
00239   ivector hsmin(h5min,h5max);
00240   ivector hsmax(h5min,h5max);
00241   imatrix smin(h5min,h5max);
00242   imatrix smax(h5min,h5max);
00243 
00244   for (l5=h5min;l5<=h5max;l5++)
00245   {
00246     readbuffer(&(hsmin(l5)),sizeof(int),sno);
00247     readbuffer(&(hsmax(l5)),sizeof(int),sno);
00248     M(l5).allocate(hsmin(l5),hsmax(l5));
00249     int l;
00250   
00251     smin(l5).allocate(hsmin(l5),hsmax(l5));
00252     smax(l5).allocate(hsmin(l5),hsmax(l5));
00253     for (l=hsmin(l5);l<=hsmax(l5);l++)
00254     {
00255       int i;
00256       readbuffer(&(smin(l5,l)),sizeof(int),sno);
00257       readbuffer(&(smax(l5,l)),sizeof(int),sno);
00258       M(l5,l).allocate(smin(l5,l),smax(l5,l));
00259       for (i=smin(l5,l);i<=smax(l5,l);i++)
00260       {
00261         int rmin,rmax;
00262         readbuffer(&rmin,sizeof(int),sno);
00263         readbuffer(&rmax,sizeof(int),sno);
00264         M(l5,l,i).allocate(rmin,rmax);
00265         for (int j=rmin;j<=rmax;j++)
00266         {
00267           int cmin,cmax;
00268           readbuffer(&cmin,sizeof(int),sno);
00269           readbuffer(&cmax,sizeof(int),sno);
00270           M(l5,l,i,j).allocate(cmin,cmax);
00271           int sz=cmax-cmin+1;
00272           readbuffer(&(value(M(l5,l,i,j)(cmin))),sz*sizeof(double),sno);
00273         }
00274       }
00275     }
00276   }
00277 
00278   save_identifier_string("X9");
00279   for (l5=h5min;l5<=h5max;l5++)
00280   {
00281     for (int l=hsmin(l5);l<=hsmax(l5);l++)
00282     {
00283       for (int i=smin(l5,l);i<=smax(l5,l);i++)
00284       {
00285         M(l5,l,i).save_dvar_matrix_position();
00286       }
00287       save_int_value(smax(l5,l));
00288       save_int_value(smin(l5,l));
00289     }
00290     save_int_value(hsmax(l5));
00291     save_int_value(hsmin(l5));
00292   }
00293   save_identifier_string("GF");
00294   save_int_value(h5max);
00295   save_int_value(h5min);
00296   save_identifier_string("W8");
00297   save_int_value(sno);
00298   save_identifier_string("LG");
00299   save_pointer_value(this);
00300   save_identifier_string("I5");
00301   gradient_structure::GRAD_STACK1->
00302             set_gradient_stack(::adjoint_get_dvar5_array_from_master);
00303   return M;
00304 }
00305