ADMB Documentation  11.1.1632
 All Classes Files Functions Variables Typedefs Friends Defines
pvmvar1.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: pvmvar1.cpp 1108 2013-07-11 23:46:23Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 #include <admodel.h>
00008 
00009 #if defined(USE_ADPVM)
00010 
00011  int pvm_params::num_pvm_params=0;
00012  const int pvm_params::maxnum_pvm_params=50;
00013  pvm_params * pvm_params::varsptr[pvm_params::maxnum_pvm_params+1];
00014 
00015   void pvm_int::assign(const ivector& u)
00016   {
00017     if(ad_comm::pvm_manager)
00018     {
00019       int nsp=ad_comm::pvm_manager->num_slave_processes;
00020       if (u.indexmin() !=0 || u.indexmax() != nsp)
00021       {
00022         cerr << "Error in pvm_int::assign  validindex bounds must be 0 "
00023              << ad_comm::pvm_manager->num_slave_processes << endl;
00024         ad_exit(1);
00025       }
00026       if (allocated(v))
00027         v.deallocate();
00028       v.allocate(0,nsp);
00029       v=u;
00030       d=u(0);
00031     }
00032   }
00033   pvm_int::operator int()
00034   {
00035     return d;
00036   }
00037   pvm_number::operator double() { return d;}
00038 
00039   void pvm_number::assign(const dvector& u)
00040   {
00041     if(ad_comm::pvm_manager)
00042     {
00043       int nsp=ad_comm::pvm_manager->num_slave_processes;
00044       if (u.indexmin() !=0 || u.indexmax() != nsp)
00045       {
00046         cerr << "Error in pvm_number::assign  valid index bounds must be 0 "
00047              << ad_comm::pvm_manager->num_slave_processes << endl;
00048         ad_exit(1);
00049       }
00050       if (allocated(v))
00051         v.deallocate();
00052       v.allocate(0,nsp);
00053       v=u;
00054       d=u(0);
00055     }
00056   }
00057 
00058   void pvm_params::send_all_to_slaves(void)
00059   {
00060     for (int i=0;i<num_pvm_params;i++)
00061     {
00062       varsptr[i]->send_to_slaves();
00063     }
00064   }
00065 
00066   void pvm_params::get_all_from_master(void)
00067   {
00068     for (int i=0;i<num_pvm_params;i++)
00069     {
00070       varsptr[i]->get_from_master();
00071     }
00072   }
00073 
00074   void pvm_params::allocate(const char * s)
00075   {
00076     add_to_list();
00077   }
00078 
00079   void pvm_params::allocate(void)
00080   {
00081     add_to_list();
00082   }
00083 
00084   void pvm_params::add_to_list(void)
00085   {
00086     if (num_pvm_params>=pvm_params::maxnum_pvm_params)
00087     {
00088       cerr << " This version of ADMB only supports "
00089            <<  pvm_params::num_pvm_params << " pvm parameter"
00090         " objects" << endl;
00091       ad_exit(1);
00092     }
00093     varsptr[num_pvm_params++]= this; // this is the list of
00094                                          // fundamental objects
00095   }
00096 
00097 
00098 
00099 
00100 void pvm_int::send_to_slaves(void)
00101 {
00102   // *********  begin constant send block  *************
00103   int ii=1;
00104   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00105   {
00106     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00107              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00108     {
00109       int bufid = adpvm_master_cinitsend( PvmDataRaw );
00110       adpvm_pack(v(ii++));
00111       adpvm_master_csend(ad_comm::pvm_manager->id(i,j));
00112     }
00113   }
00114   // *********  end constant send block  *************
00115 }
00116 
00117 
00118 void pvm_number::send_to_slaves(void)
00119 {
00120   // *********  begin constant send block  *************
00121   int ii=1;
00122   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00123   {
00124     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00125              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00126     {
00127       int bufid = adpvm_master_cinitsend( PvmDataRaw );
00128       adpvm_pack(v(ii++));
00129       adpvm_master_csend(ad_comm::pvm_manager->id(i,j));
00130     }
00131   }
00132   // *********  end constant send block  *************
00133 }
00134 
00135 
00136 
00137 void pvm_int::get_from_master(void)
00138 {
00139   int x;
00140   int ptid=pvm_parent();
00141   // ***************  begin constant receive block *********************
00142   adpvm_slave_crecv(ptid);
00143   adpvm_unpack(x);
00144   adpvm_slave_end_creceive();
00145   // ***************  end receive block ***********************************
00146   d=x;
00147 }
00148 
00149 void pvm_number::get_from_master(void)
00150 {
00151   double x;
00152   int ptid=pvm_parent();
00153   // ***************  begin constant receive block *********************
00154   adpvm_slave_crecv(ptid);
00155   adpvm_unpack(x);
00156   adpvm_slave_end_creceive();
00157   // ***************  end receive block ***********************************
00158   d=x;
00159 }
00160 #endif //  #if defined(USE_ADPVM)