ADMB Documentation  11.1.1632
 All Classes Files Functions Variables Typedefs Friends Defines
paralle2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: paralle2.cpp 1136 2013-08-05 19:40:39Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 #include <admodel.h>
00008 
00009 void function_minimizer::quasi_newton_block_pvm_master(int nvar,int _crit,
00010   independent_variables& x,const dvector& _g,const double& _f)
00011 {
00012   double & f= (double&)_f;
00013   dvector& g=(dvector&) _g;
00014   // *********************************************************
00015   // block for quasi-newton minimization
00016   tracing_message(traceflag,"C2");
00017   //int itnold=0;
00018   fmm fmc(nvar);
00019   int on1;
00020   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-nox"))>-1)
00021   {
00022     fmc.noprintx=1;
00023   }
00024   fmc.maxfn= maxfn;
00025   int nopt=0;
00026   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-dd",nopt))>-1)
00027   {
00028     if (!nopt)
00029     {
00030       cerr << "Usage -iprint option needs integer  -- ignored" << endl;
00031       fmc.iprint=iprint;
00032     }
00033     else
00034     {
00035       int jj=atoi(ad_comm::argv[on1+1]);
00036       fmc.dcheck_flag=jj;
00037     }
00038   }
00039   nopt=0;
00040   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-iprint",nopt))>-1)
00041   {
00042     if (!nopt)
00043     {
00044       cerr << "Usage -iprint option needs integer  -- ignored" << endl;
00045       fmc.iprint=iprint;
00046     }
00047     else
00048     {
00049       int jj=atoi(ad_comm::argv[on1+1]);
00050       fmc.iprint=jj;
00051     }
00052   }
00053   else
00054   {
00055     fmc.iprint= iprint;
00056   }
00057   fmc.crit = crit;
00058   fmc.imax = imax;
00059   fmc.dfn= dfn;
00060   fmc.scroll_flag= scroll_flag;
00061   fmc.min_improve=min_improve;
00062   tracing_message(traceflag,"D2");
00063   gradient_structure::set_YES_DERIVATIVES();
00064   // set convergence criterion for this phase
00065   if (_crit)
00066   {
00067     fmc.crit = _crit;
00068   }
00069   if (!(!convergence_criteria))
00070   {
00071     int ind=min(convergence_criteria.indexmax(),
00072       initial_params::current_phase);
00073     fmc.crit=convergence_criteria(ind);
00074   }
00075   if (!(!maximum_function_evaluations))
00076   {
00077     int ind=min(maximum_function_evaluations.indexmax(),
00078       initial_params::current_phase);
00079     fmc.maxfn= (int) maximum_function_evaluations(ind);
00080   }
00081   tracing_message(traceflag,"E2");
00082   dvariable xf=initial_params::reset(dvar_vector(x));
00083   reset_gradient_stack();
00084   gradcalc(0,g);
00085   while (fmc.ireturn>=0)
00086   {
00087     tracing_message(traceflag,"F2");
00088     fmc.fmin(f,x,g);
00089     tracing_message(traceflag,"G2");
00090     //cout << fmc.ireturn << endl;
00091     send_int_to_slaves(fmc.ireturn);
00092     if (fmc.ireturn>0)
00093     {
00094       int arrive=check_pvm_message(1,1);
00095       if (arrive)
00096         cout << arrive << endl;
00097       pvm_master_function_evaluation(f,x,g,nvar);
00098       arrive=check_pvm_message(1,1);
00099       if (arrive)
00100         cout << arrive << endl;
00101       /*
00102       dvariable vf=0.0;
00103       dvar_vector vx=dvar_vector(x);
00104       vf=initial_params::reset(vx);
00105       send_x_to_slaves(vx);
00106       *objective_function_value::pobjfun=0.0;
00107       userfunction();
00108       vf+=*objective_function_value::pobjfun;
00109       dvar_vector fslave=get_f_from_slaves();
00110       vf+=sum(fslave);
00111       f=value(vf);
00112       gradcalc(nvar,g);
00113       */
00114     }
00115   }
00116   gradient_structure::set_NO_DERIVATIVES();
00117   ffbest=fmc.fbest;
00118   iexit=fmc.iexit;
00119   ifn=fmc.ifn;
00120   ihflag=fmc.ihflag;
00121   ihang=fmc.ihang;
00122   maxfn_flag=fmc.maxfn_flag;
00123   quit_flag=fmc.quit_flag;
00124   objective_function_value::gmax=fabs(fmc.gmax);
00125 } // end block for quasi newton minimization
00126 
00127 void function_minimizer::pvm_master_function_evaluation_no_derivatives
00128   (double& f,independent_variables& x,int nvar)
00129 {
00130   gradient_structure::set_NO_DERIVATIVES();
00131   dvariable vf=0.0;
00132   dvar_vector vx=dvar_vector(x);
00133   vf=initial_params::reset(vx);
00134   send_x_to_slaves(vx);
00135   *objective_function_value::pobjfun=0.0;
00136   userfunction();
00137   vf+=*objective_function_value::pobjfun;
00138   dvar_matrix fslave=get_f_from_slaves();
00139   vf+=sum(fslave);
00140   f=value(vf);
00141 }
00142 
00143 void function_minimizer::pvm_master_function_evaluation(double& f,
00144   independent_variables& x,const dvector & _g,int nvar)
00145 {
00146   dvector& g=(dvector&) _g;
00147   dvariable vf=0.0;
00148   dvar_vector vx=dvar_vector(x);
00149   vf=initial_params::reset(vx);
00150   send_x_to_slaves(vx);
00151   *objective_function_value::pobjfun=0.0;
00152   userfunction();
00153   vf+=*objective_function_value::pobjfun;
00154   imatrix im=get_int_from_slaves();
00155  /*
00156   if (im(1,1)!=67)
00157   {
00158     cout << "Error in im" << endl;
00159     ad_exit(1);
00160   }
00161  */
00162   dvar_matrix fslave=get_f_from_slaves();
00163   vf+=sum(fslave);
00164   f=value(vf);
00165   gradcalc(nvar,g);
00166 }
00167 
00168 #if defined(USE_ADPVM)
00169 void function_minimizer::function_evaluation_block_pvm_slave(void)
00170 {
00171   gradient_structure::set_YES_DERIVATIVES();
00172   reset_gradient_stack();
00173   //gradcalc(nvar,g);
00174   do
00175   {
00176     int ireturn=get_int_from_master();
00177     if (ireturn<0) break;
00178     pvm_slave_function_evaluation();
00179   }
00180   while(1);
00181   gradient_structure::set_NO_DERIVATIVES();
00182 }
00183 
00184 void function_minimizer::pvm_slave_function_evaluation(void)
00185 {
00186   dvariable vf=0.0;
00187   dvar_vector vx=get_x_from_master();
00188   vf=initial_params::reset(vx);
00189   *objective_function_value::pobjfun=0.0;
00190   userfunction();
00191   vf+=*objective_function_value::pobjfun;
00192   send_int_to_master(67);
00193   send_f_to_master(vf);
00194   slave_gradcalc();
00195 }
00196 
00197 void function_minimizer::pvm_slave_function_evaluation_no_derivatives(void)
00198 {
00199   gradient_structure::set_NO_DERIVATIVES();
00200   dvariable vf=0.0;
00201   dvar_vector vx=get_x_from_master();
00202   vf=initial_params::reset(vx);
00203   *objective_function_value::pobjfun=0.0;
00204   userfunction();
00205   vf+=*objective_function_value::pobjfun;
00206   send_f_to_master(vf);
00207 }
00208 
00209 void function_minimizer::pvm_slave_function_evaluation_noder(void)
00210 {
00211   dvariable vf=0.0;
00212   dvar_vector vx=get_x_from_master();
00213   vf=initial_params::reset(vx);
00214   *objective_function_value::pobjfun=0.0;
00215   userfunction();
00216   vf+=*objective_function_value::pobjfun;
00217   send_int_to_master(67);
00218   send_f_to_master(vf);
00219 }
00220 #endif //defined(USE_ADPVM)