ADMB Documentation  Fournier-pthread.1088
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
thread_stuff7.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_master(void)
00012 {
00013   verify_identifier_string("CV");
00014   adpthread_manager * ptr=(adpthread_manager*)(restore_pointer_value());
00015   ptr->adjoint_send_dvar5_array_to_master();
00016 }
00017 
00018 void adpthread_manager::adjoint_send_dvar5_array_to_master(void)
00019 {
00020   verify_identifier_string("HH");
00021   int sno=restore_int_value();
00022   verify_id_string_from_master("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_master(const dvar5_array &x,int sno)
00084 {
00085   send_id_string_to_master("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_master);
00152 }
00153 
00154 void adpthread_manager::send_dvar5_array(const dvar5_array &x,int sno)
00155 {
00156   send_id_string_to_master("HYD",sno);
00157 
00158   int h5min=x.indexmin();
00159   int h5max=x.indexmax();
00160   writebuffer(&h5min,sizeof(int),sno);
00161   writebuffer(&h5max,sizeof(int),sno);
00162   int l5;
00163 
00164   ivector hsmin(h5min,h5max);
00165   ivector hsmax(h5min,h5max);
00166   imatrix smin(h5min,h5max);
00167   imatrix smax(h5min,h5max);
00168 
00169   for (l5=h5min;l5<=h5max;l5++)
00170   {
00171     hsmin(l5)=x(l5).indexmin();
00172     hsmax(l5)=x(l5).indexmax();
00173     writebuffer(&(hsmin(l5)),sizeof(int),sno);
00174     writebuffer(&(hsmax(l5)),sizeof(int),sno);
00175     int l;
00176   
00177     smin(l5).allocate(hsmin(l5),hsmax(l5));
00178     smax(l5).allocate(hsmin(l5),hsmax(l5));
00179     for (l=hsmin(l5);l<=hsmax(l5);l++)
00180     {
00181       smin(l5,l)=x(l5,l).indexmin();
00182       smax(l5,l)=x(l5,l).indexmax();
00183       int i;
00184       writebuffer(&(smin(l5,l)),sizeof(int),sno);
00185       writebuffer(&(smax(l5,l)),sizeof(int),sno);
00186       for (i=smin(l5,l);i<=smax(l5,l);i++)
00187       {
00188         int rmin=x(l5,l,i).indexmin();
00189         int rmax=x(l5,l,i).indexmax();
00190         writebuffer(&rmin,sizeof(int),sno);
00191         writebuffer(&rmax,sizeof(int),sno);
00192         for (int j=rmin;j<=rmax;j++)
00193         {
00194           int cmin=x(l5,l,i,j).indexmin();
00195           int cmax=x(l5,l,i,j).indexmax();
00196           writebuffer(&cmin,sizeof(int),sno);
00197           writebuffer(&cmax,sizeof(int),sno);
00198           int sz=cmax-cmin+1;
00199           writebuffer(&(value(x(l5,l,i,j)(cmin))),sz*sizeof(double),sno);
00200         }
00201       }
00202     }
00203   }
00204   save_identifier_string("VC");
00205   // !!! should we optimize this ?
00206   for (int l5=h5min;l5<=h5max;l5++)
00207   {
00208     for (int l=hsmin(l5);l<=hsmax(l5);l++)
00209     {
00210       for (int i=smin(l5,l);i<=smax(l5,l);i++)
00211       {
00212         x(l5,l,i).save_dvar_matrix_position();
00213       }
00214     }
00215   }
00216   save_identifier_string("OP");
00217   save_int_value(sno);
00218   save_identifier_string("HH");
00219   save_pointer_value(this);
00220   save_identifier_string("CV");
00221   gradient_structure::GRAD_STACK1->
00222     set_gradient_stack(::adjoint_send_dvar5_array_to_master);
00223 }
00224 
00225 void adpthread_manager::send_d5_array(const d5_array &x,int sno)
00226 {
00227   send_id_string_to_master("LOZ",sno);
00228 
00229   int h5min=x.indexmin();
00230   int h5max=x.indexmax();
00231   writebuffer(&h5min,sizeof(int),sno);
00232   writebuffer(&h5max,sizeof(int),sno);
00233   int l5;
00234 
00235   ivector hsmin(h5min,h5max);
00236   ivector hsmax(h5min,h5max);
00237   imatrix smin(h5min,h5max);
00238   imatrix smax(h5min,h5max);
00239 
00240   for (l5=h5min;l5<=h5max;l5++)
00241   {
00242     hsmin(l5)=x(l5).indexmin();
00243     hsmax(l5)=x(l5).indexmax();
00244     writebuffer(&(hsmin(l5)),sizeof(int),sno);
00245     writebuffer(&(hsmax(l5)),sizeof(int),sno);
00246     int l;
00247   
00248     smin(l5).allocate(hsmin(l5),hsmax(l5));
00249     smax(l5).allocate(hsmin(l5),hsmax(l5));
00250     for (l=hsmin(l5);l<=hsmax(l5);l++)
00251     {
00252       smin(l5,l)=x(l5,l).indexmin();
00253       smax(l5,l)=x(l5,l).indexmax();
00254       int i;
00255       writebuffer(&(smin(l5,l)),sizeof(int),sno);
00256       writebuffer(&(smax(l5,l)),sizeof(int),sno);
00257       for (i=smin(l5,l);i<=smax(l5,l);i++)
00258       {
00259         int rmin=x(l5,l,i).indexmin();
00260         int rmax=x(l5,l,i).indexmax();
00261         writebuffer(&rmin,sizeof(int),sno);
00262         writebuffer(&rmax,sizeof(int),sno);
00263         for (int j=rmin;j<=rmax;j++)
00264         {
00265           int cmin=x(l5,l,i,j).indexmin();
00266           int cmax=x(l5,l,i,j).indexmax();
00267           writebuffer(&cmin,sizeof(int),sno);
00268           writebuffer(&cmax,sizeof(int),sno);
00269           int sz=cmax-cmin+1;
00270           writebuffer(&(x(l5,l,i,j)(cmin)),sz*sizeof(double),sno);
00271         }
00272       }
00273     }
00274   }
00275 }
00276 
00277 void adjoint_get_dvar5_array_from_slave(void)
00278 {
00279   verify_identifier_string("I5");
00280   adpthread_manager * ptr=(adpthread_manager*)(restore_pointer_value());
00281   ptr->adjoint_get_dvar5_array_from_slave();
00282 }
00283 
00284 void adpthread_manager::adjoint_get_dvar5_array_from_slave(void)
00285 {
00286   verify_identifier_string("LG");
00287   int sno=restore_int_value();
00288   verify_identifier_string("W8");
00289   int h5min=restore_int_value();
00290   int h5max=restore_int_value();
00291   verify_identifier_string("GF");
00292   int l;
00293   d5_array dv(h5min,h5max);
00294   ivector hsmin(h5min,h5max);
00295   ivector hsmax(h5min,h5max);
00296   imatrix smin(h5min,h5max);
00297   imatrix smax(h5min,h5max);
00298   for (int l5=h5max;l5>=h5min;l5--)
00299   {
00300     hsmin(l5)=restore_int_value();
00301     hsmax(l5)=restore_int_value();
00302     smin(l5).allocate(hsmin(l5),hsmax(l5));
00303     smax(l5).allocate(hsmin(l5),hsmax(l5));
00304     dv(l5).allocate(hsmin(l5),hsmax(l5));
00305     for (l=hsmax(l5);l>=hsmin(l5);l--)
00306     {
00307       smin(l5,l)=restore_int_value();
00308       smax(l5,l)=restore_int_value();
00309       dv(l5,l).allocate(smin(l5,l),smax(l5,l));
00310       for (int i=smax(l5,l);i>=smin(l5,l);i--)
00311       {
00312         dvar_matrix_position dvpos=restore_dvar_matrix_position();
00313         dv(l5,l,i)=restore_dvar_matrix_derivatives(dvpos);
00314       }
00315     }
00316   }
00317   verify_identifier_string("Y5");
00318   send_id_string_to_slave("FDG",sno);
00319   writebuffer(&h5min,sizeof(int),sno);
00320   writebuffer(&h5max,sizeof(int),sno);
00321   for (int l5=h5min;l5<=h5max;l5++)
00322   {
00323     writebuffer(&(hsmin(l5)),sizeof(int),sno);
00324     writebuffer(&(hsmax(l5)),sizeof(int),sno);
00325     for (l=hsmin(l5);l<=hsmax(l5);l++)
00326     {
00327       writebuffer(&(smin(l5,l)),sizeof(int),sno);
00328       writebuffer(&(smax(l5,l)),sizeof(int),sno);
00329       int i;
00330       for (i=smin(l5,l);i<=smax(l5,l);i++)
00331       {
00332         int rmin=dv(l5,l,i).indexmin();
00333         int rmax=dv(l5,l,i).indexmax();
00334         writebuffer(&rmin,sizeof(int),sno);
00335         writebuffer(&rmax,sizeof(int),sno);
00336         for (int j=rmin;j<=rmax;j++)
00337         {
00338           int cmin=dv(l5,l,i,j).indexmin();
00339           int cmax=dv(l5,l,i,j).indexmax();
00340           writebuffer(&cmin,sizeof(int),sno);
00341           writebuffer(&cmax,sizeof(int),sno);
00342           int sz=cmax-cmin+1;
00343           writebuffer(&(dv(l5,l,i,j)(cmin)),sz*sizeof(double),sno);
00344         }
00345       }
00346     }
00347   }
00348 }
00349 dvar5_array adpthread_manager::get_dvar5_array_from_slave(int sno)
00350 {
00351   verify_id_string_from_slave("HYD",sno);
00352  // &***********************************************************
00353  // &***********************************************************
00354 
00355   int h5min;
00356   int h5max;
00357   readbuffer(&h5min,sizeof(int),sno);
00358   readbuffer(&h5max,sizeof(int),sno);
00359   int l5;
00360   dvar5_array M(h5min,h5max);
00361 
00362   ivector hsmin(h5min,h5max);
00363   ivector hsmax(h5min,h5max);
00364   imatrix smin(h5min,h5max);
00365   imatrix smax(h5min,h5max);
00366 
00367   for (l5=h5min;l5<=h5max;l5++)
00368   {
00369     readbuffer(&(hsmin(l5)),sizeof(int),sno);
00370     readbuffer(&(hsmax(l5)),sizeof(int),sno);
00371     M(l5).allocate(hsmin(l5),hsmax(l5));
00372     int l;
00373   
00374     smin(l5).allocate(hsmin(l5),hsmax(l5));
00375     smax(l5).allocate(hsmin(l5),hsmax(l5));
00376     for (l=hsmin(l5);l<=hsmax(l5);l++)
00377     {
00378       int i;
00379       readbuffer(&(smin(l5,l)),sizeof(int),sno);
00380       readbuffer(&(smax(l5,l)),sizeof(int),sno);
00381       M(l5,l).allocate(smin(l5,l),smax(l5,l));
00382       for (i=smin(l5,l);i<=smax(l5,l);i++)
00383       {
00384         int rmin,rmax;
00385         readbuffer(&rmin,sizeof(int),sno);
00386         readbuffer(&rmax,sizeof(int),sno);
00387         M(l5,l,i).allocate(rmin,rmax);
00388         for (int j=rmin;j<=rmax;j++)
00389         {
00390           int cmin,cmax;
00391           readbuffer(&cmin,sizeof(int),sno);
00392           readbuffer(&cmax,sizeof(int),sno);
00393           M(l5,l,i,j).allocate(cmin,cmax);
00394           int sz=cmax-cmin+1;
00395           readbuffer(&(value(M(l5,l,i,j)(cmin))),sz*sizeof(double),sno);
00396         }
00397       }
00398     }
00399   }
00400 
00401   save_identifier_string("Y5");
00402   for (l5=h5min;l5<=h5max;l5++)
00403   {
00404     for (int l=hsmin(l5);l<=hsmax(l5);l++)
00405     {
00406       for (int i=smin(l5,l);i<=smax(l5,l);i++)
00407       {
00408         M(l5,l,i).save_dvar_matrix_position();
00409       }
00410       save_int_value(smax(l5,l));
00411       save_int_value(smin(l5,l));
00412     }
00413     save_int_value(hsmax(l5));
00414     save_int_value(hsmin(l5));
00415   }
00416   save_identifier_string("GF");
00417   save_int_value(h5max);
00418   save_int_value(h5min);
00419   save_identifier_string("W8");
00420   save_int_value(sno);
00421   save_identifier_string("LG");
00422   save_pointer_value(this);
00423   save_identifier_string("I5");
00424   gradient_structure::GRAD_STACK1->
00425             set_gradient_stack(::adjoint_get_dvar5_array_from_slave);
00426   return M;
00427 }
00428 
00429 dvar5_array adpthread_manager::get_dvar5_array(int sno)
00430 {
00431   verify_id_string_from_slave("HYD",sno);
00432  // &***********************************************************
00433  // &***********************************************************
00434 
00435   int h5min;
00436   int h5max;
00437   readbuffer(&h5min,sizeof(int),sno);
00438   readbuffer(&h5max,sizeof(int),sno);
00439   int l5;
00440   dvar5_array M(h5min,h5max);
00441 
00442   ivector hsmin(h5min,h5max);
00443   ivector hsmax(h5min,h5max);
00444   imatrix smin(h5min,h5max);
00445   imatrix smax(h5min,h5max);
00446 
00447   for (l5=h5min;l5<=h5max;l5++)
00448   {
00449     readbuffer(&(hsmin(l5)),sizeof(int),sno);
00450     readbuffer(&(hsmax(l5)),sizeof(int),sno);
00451     M(l5).allocate(hsmin(l5),hsmax(l5));
00452     int l;
00453   
00454     smin(l5).allocate(hsmin(l5),hsmax(l5));
00455     smax(l5).allocate(hsmin(l5),hsmax(l5));
00456     for (l=hsmin(l5);l<=hsmax(l5);l++)
00457     {
00458       int i;
00459       readbuffer(&(smin(l5,l)),sizeof(int),sno);
00460       readbuffer(&(smax(l5,l)),sizeof(int),sno);
00461       M(l5,l).allocate(smin(l5,l),smax(l5,l));
00462       for (i=smin(l5,l);i<=smax(l5,l);i++)
00463       {
00464         int rmin,rmax;
00465         readbuffer(&rmin,sizeof(int),sno);
00466         readbuffer(&rmax,sizeof(int),sno);
00467         M(l5,l,i).allocate(rmin,rmax);
00468         for (int j=rmin;j<=rmax;j++)
00469         {
00470           int cmin,cmax;
00471           readbuffer(&cmin,sizeof(int),sno);
00472           readbuffer(&cmax,sizeof(int),sno);
00473           M(l5,l,i,j).allocate(cmin,cmax);
00474           int sz=cmax-cmin+1;
00475           readbuffer(&(value(M(l5,l,i,j)(cmin))),sz*sizeof(double),sno);
00476         }
00477       }
00478     }
00479   }
00480 
00481   save_identifier_string("Y5");
00482   for (l5=h5min;l5<=h5max;l5++)
00483   {
00484     for (int l=hsmin(l5);l<=hsmax(l5);l++)
00485     {
00486       for (int i=smin(l5,l);i<=smax(l5,l);i++)
00487       {
00488         M(l5,l,i).save_dvar_matrix_position();
00489       }
00490       save_int_value(smax(l5,l));
00491       save_int_value(smin(l5,l));
00492     }
00493     save_int_value(hsmax(l5));
00494     save_int_value(hsmin(l5));
00495   }
00496   save_identifier_string("GF");
00497   save_int_value(h5max);
00498   save_int_value(h5min);
00499   save_identifier_string("W8");
00500   save_int_value(sno);
00501   save_identifier_string("LG");
00502   save_pointer_value(this);
00503   save_identifier_string("I5");
00504   gradient_structure::GRAD_STACK1->
00505             set_gradient_stack(::adjoint_get_dvar5_array_from_slave);
00506   return M;
00507 }
00508 
00509 d5_array adpthread_manager::get_d5_array(int sno)
00510 {
00511   verify_id_string_from_slave("LOZ",sno);
00512 
00513   int h5min;
00514   int h5max;
00515   readbuffer(&h5min,sizeof(int),sno);
00516   readbuffer(&h5max,sizeof(int),sno);
00517   int l5;
00518   d5_array M(h5min,h5max);
00519 
00520   ivector hsmin(h5min,h5max);
00521   ivector hsmax(h5min,h5max);
00522   imatrix smin(h5min,h5max);
00523   imatrix smax(h5min,h5max);
00524 
00525   for (l5=h5min;l5<=h5max;l5++)
00526   {
00527     readbuffer(&(hsmin(l5)),sizeof(int),sno);
00528     readbuffer(&(hsmax(l5)),sizeof(int),sno);
00529     M(l5).allocate(hsmin(l5),hsmax(l5));
00530     int l;
00531   
00532     smin(l5).allocate(hsmin(l5),hsmax(l5));
00533     smax(l5).allocate(hsmin(l5),hsmax(l5));
00534     for (l=hsmin(l5);l<=hsmax(l5);l++)
00535     {
00536       int i;
00537       readbuffer(&(smin(l5,l)),sizeof(int),sno);
00538       readbuffer(&(smax(l5,l)),sizeof(int),sno);
00539       M(l5,l).allocate(smin(l5,l),smax(l5,l));
00540       for (i=smin(l5,l);i<=smax(l5,l);i++)
00541       {
00542         int rmin,rmax;
00543         readbuffer(&rmin,sizeof(int),sno);
00544         readbuffer(&rmax,sizeof(int),sno);
00545         M(l5,l,i).allocate(rmin,rmax);
00546         for (int j=rmin;j<=rmax;j++)
00547         {
00548           int cmin,cmax;
00549           readbuffer(&cmin,sizeof(int),sno);
00550           readbuffer(&cmax,sizeof(int),sno);
00551           M(l5,l,i,j).allocate(cmin,cmax);
00552           int sz=cmax-cmin+1;
00553           readbuffer(&(M(l5,l,i,j)(cmin)),sz*sizeof(double),sno);
00554         }
00555       }
00556     }
00557   }
00558   return M;
00559 }
00560