ADMB Documentation  11.1.1632
 All Classes Files Functions Variables Typedefs Friends Defines
parallel.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: parallel.cpp 1343 2013-11-19 19:56:22Z 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(linux) && !defined(_WIN32) && !defined(__MINGW64__) && !defined(__CYGWIN__)
00010 #include <ctype.h>
00011     void strlwr(char * s)
00012     {
00013       if (s)
00014       {
00015         int n=strlen(s);
00016   for (int i=0;i<n;i++)
00017         {
00018           if (isupper(s[i]))
00019           {
00020       s[i]=tolower(s[i]);
00021           }
00022         }
00023       }
00024     }
00025 #endif
00026 typedef char * pchar;
00027   // dvar_vector get_x_from_master(void)
00028   // {
00029   //   dvar_vector x;
00030   //   int ptid=pvm_parent();
00031   //   // ***************  begin variable receive block *********************
00032   //   adpvm_slave_vrecv(ptid);
00033   //   adpvm_unpack(x);
00034   //   adpvm_slave_end_vreceive();
00035   //   // ***************  end receive block ***********************************
00036   //   return x;
00037   // }
00038   //
00039   //
00040   // void send_f_to_master(const dvariable& f)
00041   // {
00042   //   // ***************  begin send block ***********************************
00043   //   int ptid=pvm_parent();
00044   //   adpvm_slave_vinitsend(PvmDataDefault);/* allocate message buffer */
00045   //   adpvm_pack(f);
00046   //   adpvm_slave_vsend(ptid);/* send buffer to master */
00047   //   // ***************  end send block ***********************************
00048   // }
00049   //
00050 #if defined(USE_ADPVM)
00051 #include "adpvm2.h"
00052 
00053 adpvm_manager::adpvm_manager(int _mode)
00054 {
00055   cout << "calling load library" << endl;
00056   if (load_adpvm_library() < 0)
00057   {
00058     cerr << "error loading pvm library" << endl;
00059     exit(1);
00060   }
00061   pvm_setopt(PvmRoute, PvmRouteDirect);  /* channel for communication */
00062   /* get and display configuration of the parallel machine */
00063   int status=pvm_config( &nhost, &narch, &hostp );  /* get configuration */
00064   if (status<0)
00065   {
00066     cerr << "error trying to get configuration of pvm (virtual machine)" << endl;
00067     if (status == PvmSysErr)
00068       cerr << " PVM Daemon not responing -- maybe it is not started" << endl;
00069     ad_exit(1);
00070   }
00071   printf("I found the following hosts in your virtual machine\n");
00072   int i;
00073   for (i = 0; i < nhost; i++)
00074   {
00075     printf("    %s\n", hostp[i].hi_name);
00076   }
00077   //id.allocate(0,nhost);
00078   mode=_mode;
00079   if (mode == 1)  // master
00080   {
00081     slave_argv = new adpvm_slave_args(20,20);
00082     int ierr=pvm_catchout(stdout);
00083     if (ierr<0)
00084     {
00085       cerr << "Error in pvm_catchout" << endl;
00086     }
00087     strcpy(*slave_argv,"progname");
00088     int on1,nopt1;
00089     if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-exec",nopt1))>-1)
00090     {
00091       if (nopt1 !=1)
00092       {
00093         cerr << "Wrong number of options to -exec -- must be 1"
00094           " you have " << nopt1 << endl;
00095         ad_exit(1);
00096       }
00097       slave_names+= ad_comm::argv[on1+1];
00098     }
00099     else
00100     {
00101       slave_names+="test";
00102     }
00103   }
00104   timing_flag=0;
00105   int on1,nopt1;
00106   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-pvmtime",nopt1))>-1)
00107   {
00108     timing_flag=1;
00109   }
00110 }
00111 
00112 char * adpvm_slave_args::operator () (int i)
00113 {
00114   if (i>=num_args || i<0)
00115   {
00116     cerr << "index value of " << i
00117          << "is out of bounds in adpvm_slave_args"
00118          << " maximum value is " << num_args-1
00119          << " minimum value is 0 " << endl;
00120     ad_exit(1);
00121   }
00122   return argv[i];
00123 }
00124 typedef char * charptr;
00125 
00126 adpvm_slave_args::adpvm_slave_args(int _num_args,int _length_args)
00127 {
00128   //char ** argv;
00129   counter=1;
00130   num_args=_num_args;
00131   if (allocated(length_args))
00132     length_args.deallocate();
00133   length_args.allocate(0,num_args-1);
00134   length_args=_length_args;
00135   argv = new charptr[num_args+1];
00136   argv[0] = new char[20];
00137   for (int i = 1; i < num_args; i++)
00138   {
00139     argv[i] = NULL;
00140   }
00141   argv[num_args]=NULL;
00142 }
00143 
00144 adpvm_slave_args::~adpvm_slave_args()
00145 {
00146   if (argv)
00147   {
00148     for (int i = 0; i < num_args; i++)
00149     {
00150       if (argv[i])
00151       {
00152         delete [] argv[i];
00153         argv[i]=NULL;
00154       }
00155     }
00156     delete [] argv;
00157     argv = NULL;
00158     num_args=0;
00159   }
00160 }
00161 
00162 int adpvm_manager::start_slave_processes(const ad_comm& _mp)
00163 {
00164   ad_comm& mp=(ad_comm&) _mp;
00165   cout << (mp.pvm_manager)->nhost;
00166   int i,j,check;
00167   mp.get_slave_assignments();
00168   if (allocated(id)) id.deallocate();
00169   id.allocate(1,nhost,1,num_per_host);
00170   adstring slavedir;
00171   int on=0;
00172   int noptslave=0;
00173 
00174   int td32flag=option_match(ad_comm::argc,ad_comm::argv,"-td32");
00175 
00176   if (td32flag>0)
00177   {
00178     strcpy(*slave_argv,slave_names[1]);
00179   }
00180 
00181   if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-dbg"))>-1)
00182      strcpy(*slave_argv,"-dbg");
00183   if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-slavedir",noptslave))>-1)
00184   {
00185     if (noptslave !=1)
00186     {
00187       cerr << "Wrong number of options to -slavedir -- must be 1"
00188         " you have " << noptslave << endl;
00189       ad_exit(1);
00190     }
00191   }
00192   else
00193   {
00194     noptslave=0;
00195   }
00196   int on1,nopt1;
00197   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-mcmc",nopt1))>-1)
00198   {
00199     if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-mcmc2",nopt1))>-1)
00200     {
00201       cerr << "Error -mcmc2 option not implemented for parallel processing"
00202            << endl;
00203       ad_exit(1);
00204     }
00205     if (nopt1 !=1)
00206     {
00207       cerr << "Wrong number of options to -mcmc -- must be 1"
00208         " you have " << noptslave << endl;
00209       ad_exit(1);
00210     }
00211     strcpy((*slave_argv),"-mcmc");
00212     strcpy((*slave_argv),ad_comm::argv[on1+1]);
00213   }
00214 
00215   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-nohess"))>-1)
00216   {
00217     strcpy((*slave_argv),"-nohess");
00218   }
00219 
00220   if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-lprof"))>-1)
00221   {
00222     strcpy((*slave_argv),"-lprof");
00223   }
00224   int gdbflag=option_match(ad_comm::argc,ad_comm::argv,"-gdb");
00225   int slave_con=option_match(ad_comm::argc,ad_comm::argv,"-scon");
00226 
00227   int ii=0;
00228   for (i=0; i<nhost; i++) /* spawn processes on */
00229   {       /* all physical machines */
00230     for (j=slave_assignments(i+1).indexmin();j<=slave_assignments(i+1).indexmax();
00231       j++)
00232     {
00233       strcpy((*slave_argv),"-slavename");
00234       strcpy((*slave_argv),ad_comm::adprogram_name);
00235       strcpy((*slave_argv),"-slave");
00236       strcpy((*slave_argv),(char*)(str(slave_assignments(i+1,j))));
00237       if (noptslave ==1)
00238       {
00239         strcpy((*slave_argv),"-slavedir");
00240         strcpy((*slave_argv),ad_comm::argv[on+1]);
00241       }
00242 
00243       if (td32flag<0)
00244       {
00245         //check=pvm_spawn(slave_names[1],*slave_argv ,
00246         char ** args;
00247         args=new pchar[10];
00248         args[0]=new char[20];
00249         args[1]=new char[20];
00250         strcpy(args[0],"-slave");
00251         strcpy(args[1],(char*)str(ii+1));
00252         args[2]=0;
00253         check=pvm_spawn(ad_comm::adprogram_name,args ,
00254           PvmTaskHost /* | PvmTaskDebug */ ,
00255           hostp[i].hi_name, 1, &(id(i+1,j)));
00256         ii++;
00257       }
00258       else
00259       {
00260         char ** args;
00261         args=new pchar[10];
00262         if (slave_con>0)
00263         {
00264           args[0]=new char[50];
00265           args[1]=new char[20];
00266           args[2]=new char[20];
00267           strncpy(args[0],ad_comm::adprogram_name,49);
00268           strncpy(args[1],"-slave",19);
00269           strncpy(args[2],(char*)str(ii+1),19);
00270           args[3]=0;
00271         }
00272         else
00273         {
00274           args[0]=new char[20];
00275           args[1]=new char[50];
00276           args[2]=new char[20];
00277           args[3]=new char[50];
00278 
00279           strcpy(args[0],"td32");
00280           strncpy(args[1],ad_comm::adprogram_name,49);
00281           strcpy(args[2],"-slave");
00282           strcpy(args[3],(char*)str(ii+1));
00283           args[4]=0;
00284         }
00285         //check=pvm_spawn("nrunslave",*slave_argv ,
00286         check=pvm_spawn("nrunslave",args ,
00287           PvmTaskHost /* | PvmTaskDebug */ ,
00288           hostp[i].hi_name, 1, &(id(i+1,j)));
00289         ii++;
00290       }
00291 
00292       (*slave_argv)--;
00293       (*slave_argv)--;
00294       (*slave_argv)--;
00295       (*slave_argv)--;
00296       if (noptslave ==1)
00297       {
00298         (*slave_argv)--;
00299         (*slave_argv)--;
00300       }
00301       if (!check)
00302         printf("Couldn't start process on %s\n",hostp[i].hi_name);
00303       else
00304         printf("Started process on %s\n",hostp[i].hi_name);
00305     }
00306   }
00307   return check;
00308 }
00309 
00310 #if defined(USE_LAPLACE)
00311 
00312 
00313 int adpvm_manager::start_slave_processes_for_random_effects(const ad_comm& _mp)
00314 {
00315   ad_comm& mp=(ad_comm&) _mp;
00316   cout << (mp.pvm_manager)->nhost;
00317   int i,j,check;
00318   mp.get_slave_assignments();
00319   if (allocated(id)) id.deallocate();
00320   id.allocate(1,nhost,1,num_per_host);
00321   adstring slavedir;
00322   int on=0;
00323   int use_td32=1;
00324 
00325   if (use_td32)
00326   {
00327     char ** pvmargs=new pchar[ad_comm::argc+5];
00328     int i;
00329     for (i=0;i<ad_comm::argc+5;i++)
00330     {
00331       pvmargs[i]=0;
00332     }
00333     pvmargs[0]=new char[strlen(ad_comm::adprogram_name)+1];
00334     strcpy(pvmargs[0],ad_comm::adprogram_name);
00335 
00336     strlwr(pvmargs[0]);
00337     for (i=1;i<ad_comm::argc;i++)
00338     {
00339       pvmargs[i]=new char[strlen(ad_comm::argv[i])+1];
00340       strcpy(pvmargs[i],ad_comm::argv[i]);
00341     }
00342     pvmargs[ad_comm::argc]=new char[strlen("-slave")+1];
00343     strcpy(pvmargs[ad_comm::argc],"-slave");
00344 
00345     pvmargs[ad_comm::argc+1]=new char[10];
00346     pvmargs[ad_comm::argc+3]=0;
00347 
00348 
00349     for (i=0;i<nhost;i++)
00350     {
00351       for (j=1;j<=num_per_host(i+1);j++)
00352       {
00353         strncpy(pvmargs[ad_comm::argc+1],
00354           (char*)(str(slave_assignments(i+1,j))),9);
00355         //check=pvm_spawn(pvmargs[0],pvmargs,
00356         check=pvm_spawn("td32rem",pvmargs,
00357           PvmTaskHost /* | PvmTaskDebug */ ,
00358           hostp[i].hi_name, 1, &(id(i+1,j)));
00359         if (!check)
00360           printf("Couldn't start slave process %d on %s\n",j,hostp[i].hi_name);
00361         else
00362           printf("Started slave process %d on %s\n",j,hostp[i].hi_name);
00363       }
00364     }
00365   }
00366   else
00367   {
00368     for (i=0;i<nhost;i++)
00369     {
00370       for (j=1;j<=num_per_host(i+1);j++)
00371       {
00372         check=pvm_spawn(ad_comm::adprogram_name,0,
00373           PvmTaskHost /* | PvmTaskDebug */ ,
00374           hostp[i].hi_name, 1, &(id(i+1,j)));
00375         if (!check)
00376           printf("Couldn't start slave process %d on %s\n",j,hostp[i].hi_name);
00377         else
00378           printf("Started slave process %d on %s\n",j,hostp[i].hi_name);
00379       }
00380     }
00381   }
00382   return 0;
00383 }
00384 
00385 #endif // #if defined(USE_LAPLACE)
00386 adpvm_manager::~adpvm_manager(void)
00387 {
00388   int i;
00389   // clean up slaves -- this stops all the slave processes
00390   for (i=1; i<=nhost; i++)  /* spawn processes on */
00391   {       /* all physical machines */
00392     for (int j=slave_assignments(i).indexmin();
00393              j<=slave_assignments(i).indexmax();j++)
00394     {
00395       pvm_kill(id(i,j));
00396     }
00397   }
00398   //delete [] slave_argv;
00399   //slave_argv=NULL;
00400 }
00401 
00402 void send_cf_to_slaves(int nvar,int current_phase,const dvar_vector&  x)
00403 {
00404   // *********  begin constant send block  *************
00405   //for (int i=1;i<=ad_comm::pvm_manager->nhost;i++)
00406   //{
00407   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00408   {
00409     for (int j=ad_comm::pvm_manager->slave_assignments(i+1).indexmin();
00410              j<=ad_comm::pvm_manager->slave_assignments(i+1).indexmax();j++)
00411     {
00412       int bufid = adpvm_master_cinitsend( PvmDataRaw );
00413       pvm_pack(current_phase);
00414       pvm_pack(nvar);
00415       adpvm_master_csend(ad_comm::pvm_manager->id(i,j));
00416     }
00417   }
00418   // *********  end variable send block  *************
00419 }
00420 
00421 
00422 void send_int_to_slaves(int  x)
00423 {
00424   // *********  begin constant send block  *************
00425   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00426   {
00427     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00428              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00429     {
00430       int bufid = adpvm_master_cinitsend( PvmDataRaw );
00431       adpvm_pack(x);
00432       adpvm_master_csend(ad_comm::pvm_manager->id(i,j));
00433     }
00434   }
00435   // *********  end constant send block  *************
00436 }
00437 
00438 
00439 void send_x_to_slaves(const dvar_vector&  x)
00440 {
00441   // *********  begin variable send block  *************
00442   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00443   {
00444     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00445              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00446     {
00447       int bufid = adpvm_master_vinitsend( PvmDataRaw );
00448       adpvm_pack(x);
00449       adpvm_master_vsend(ad_comm::pvm_manager->id(i,j));
00450     }
00451   }
00452   // *********  end constant send block  *************
00453 }
00454 
00455 void send_dvector_to_slaves(const dvector&  x)
00456 {
00457   // *********  begin variable send block  *************
00458   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00459   {
00460     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00461              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00462     {
00463       int bufid = adpvm_master_cinitsend( PvmDataRaw );
00464       adpvm_pack(x);
00465       adpvm_master_csend(ad_comm::pvm_manager->id(i,j));
00466     }
00467   }
00468   // *********  end constant send block  *************
00469 }
00470 
00471 void send_dmatrix_to_slaves(const dmatrix&  x,ivector& jmin,ivector& jmax)
00472 {
00473   // *********  begin variable send block  *************
00474   int ii=1;
00475   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00476   {
00477     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00478              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00479     {
00480       int kmin=x.indexmin();
00481       int kmax=x.indexmax();
00482       int lmin=jmin(ii);
00483       int lmax=jmax(ii);
00484       ii++;
00485       dmatrix H(kmin,kmax,lmin,lmax);
00486       for (int k=kmin;k<=kmax;k++)
00487         for (int l=lmin;l<=lmax;l++)
00488           H(k,l)=x(k,l);
00489 
00490       int bufid = adpvm_master_cinitsend( PvmDataRaw );
00491       adpvm_pack(H);
00492       adpvm_master_csend(ad_comm::pvm_manager->id(i,j));
00493     }
00494   }
00495   // *********  end constant send block  *************
00496 }
00497 
00498 imatrix get_int_from_slaves(void)
00499 {
00500   imatrix fslave(1,ad_comm::pvm_manager->nhost,1,ad_comm::pvm_manager->num_per_host);
00501   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00502   {
00503     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00504              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00505     {
00506       // *********  begin constant receive block  *************
00507       adpvm_master_crecv(ad_comm::pvm_manager->id(i,j)); //get the values from slave
00508       adpvm_unpack(fslave(i,j));
00509       adpvm_master_end_creceive();  // have got all the values
00510     }
00511   }
00512   // *********  end variable receive block  *************
00513   return fslave;
00514 }
00515 
00516 dvar_matrix get_f_from_slaves(void)
00517 {
00518   dvar_matrix fslave(1,ad_comm::pvm_manager->nhost,1,ad_comm::pvm_manager->num_per_host);
00519   // *********  begin variable send block  *************
00520   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00521   {
00522     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00523              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00524     {
00525       // *********  begin variable receive block  *************
00526       adpvm_master_vrecv(ad_comm::pvm_manager->id(i,j)); //get the values from slave
00527       adpvm_unpack(fslave(i,j));
00528       adpvm_master_end_vreceive();  // have got all the values
00529     }
00530   }
00531   // *********  end variable receive block  *************
00532   return fslave;
00533 }
00534 
00535 d3_array get_dmatrix_from_slaves(void)
00536 {
00537   d3_array M(1,ad_comm::pvm_manager->num_slave_processes);
00538   // *********  begin variable send block  *************
00539   int ii=1;
00540   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00541   {
00542     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00543              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00544     {
00545       // *********  begin variable receive block  *************
00546       adpvm_master_crecv(ad_comm::pvm_manager->id(i,j)); //get the values from slave
00547       adpvm_unpack(M(ii++));
00548       adpvm_master_end_creceive();  // have got all the values
00549     }
00550   }
00551   // *********  end variable receive block  *************
00552   return M;
00553 }
00554 d3_array get_dmatrix_from_slaves(const imatrix& flags)
00555 {
00556   d3_array M(1,ad_comm::pvm_manager->num_slave_processes);
00557   // *********  begin variable send block  *************
00558   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00559   {
00560     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00561              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00562     {
00563       if (flags(i,j))
00564       {
00565         // *********  begin variable receive block  *************
00566         adpvm_master_crecv(ad_comm::pvm_manager->id(i,j)); //get the values from slave
00567         adpvm_unpack(M(i));
00568         adpvm_master_end_creceive();  // have got all the values
00569       }
00570     }
00571   }
00572   // *********  end variable receive block  *************
00573   return M;
00574 }
00575 
00576 
00577 dmatrix get_dvector_from_slaves(void)
00578 {
00579   dmatrix M(1,ad_comm::pvm_manager->num_slave_processes);
00580   // *********  begin variable send block  *************
00581   int ii=1;
00582   for (int i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00583   {
00584     for (int j=ad_comm::pvm_manager->slave_assignments(i).indexmin();
00585              j<=ad_comm::pvm_manager->slave_assignments(i).indexmax();j++)
00586     {
00587       // *********  begin variable receive block  *************
00588       adpvm_master_crecv(ad_comm::pvm_manager->id(i,j)); //get the values from slave
00589       adpvm_unpack(M(ii++));
00590       adpvm_master_end_creceive();  // have got all the values
00591     }
00592   }
00593   // *********  end variable receive block  *************
00594   return M;
00595 }
00596 
00597 int get_int_from_master(void)
00598 {
00599   int x;
00600   int ptid=pvm_parent();
00601   // ***************  begin constant receive block *********************
00602   adpvm_slave_crecv(ptid);
00603   adpvm_unpack(x);
00604   adpvm_slave_end_creceive();
00605   // ***************  end receive block ***********************************
00606   return x;
00607 }
00608 
00609 void send_int_to_master(int i)
00610 {
00611   // ***************  begin send block ***********************************
00612   int ptid=pvm_parent();
00613   //adpvm_slave_cinitsend(PvmDataDefault);/* allocate message buffer */
00614   adpvm_slave_cinitsend(PvmDataRaw);/* allocate message buffer */
00615   adpvm_pack(i);
00616   adpvm_slave_csend(ptid);/* send buffer to master */
00617   // ***************  end send block ***********************************
00618 }
00619 
00620 void send_dvector_to_master(const dvector& v)
00621 {
00622   // ***************  begin send block ***********************************
00623   int ptid=pvm_parent();
00624   //adpvm_slave_cinitsend(PvmDataDefault);/* allocate message buffer */
00625   adpvm_slave_cinitsend(PvmDataRaw);/* allocate message buffer */
00626   adpvm_pack(v);
00627   adpvm_slave_csend(ptid);/* send buffer to master */
00628   // ***************  end send block ***********************************
00629 }
00630 
00631 void send_dmatrix_to_master(const dmatrix& m)
00632 {
00633   // ***************  begin send block ***********************************
00634   int ptid=pvm_parent();
00635   //adpvm_slave_cinitsend(PvmDataDefault);/* allocate message buffer */
00636   adpvm_slave_cinitsend(PvmDataRaw);/* allocate message buffer */
00637   adpvm_pack(m);
00638   adpvm_slave_csend(ptid);/* send buffer to master */
00639   // ***************  end send block ***********************************
00640 }
00641 
00642 
00643 void adpvm_manager::send_variable_values_to_slaves(void)
00644 {
00645   int i,j;
00646   //for (i=ad_comm::pvm_manager->nhost;i>=1;i--)
00647   //{
00648   for (i=1; i<=ad_comm::pvm_manager-> nhost; i++)
00649   {
00650     for (j=1; j<=ad_comm::pvm_manager-> num_per_host(i); j++)
00651     {
00652       // *********  begin constant send block  *************
00653       int bufid = adpvm_master_cinitsend( PvmDataRaw );
00654       // pack all the variables
00655       for (int jj=1;jj<=initial_params::num_initial_params;jj++)
00656       {
00657         initial_params::varsptr[jj-1]->pvm_pack();
00658       }
00659       adpvm_master_csend(id(i,j));
00660       // *********  end constant send block  *************
00661     }
00662   }
00663 }
00664 
00665 void adpvm_manager::get_variable_values_from_master(void)
00666 {
00667   int i,j;
00668   // ***************  begin constant receive block *************************
00669   int ptid=pvm_parent();
00670   adpvm_slave_crecv(ptid);
00671   // unpack all the variables
00672   for (j=1;j<=initial_params::num_initial_params;j++)
00673   {
00674     initial_params::varsptr[j-1]->pvm_unpack();
00675   }
00676   adpvm_slave_end_creceive();
00677   // ***************  end receive block ***********************************
00678 }
00679 
00680 void admaster_slave_variable_interface(const ad_comm& mp)
00681 {
00682   if (ad_comm::pvm_manager)
00683   {
00684 
00685 
00686 #  if defined(USE_LAPLACE)
00687     if (function_minimizer::random_effects_flag)
00688     {
00689       if (ad_comm::pvm_manager->mode==1)
00690       {
00691         ad_comm::pvm_manager->start_slave_processes_for_random_effects(mp);
00692         ad_comm::pvm_manager->send_variable_values_to_slaves();
00693       }
00694       if (ad_comm::pvm_manager->mode==2)
00695         ad_comm::pvm_manager->get_variable_values_from_master();
00696     }
00697     else
00698     {
00699 #  endif
00700       if (ad_comm::pvm_manager->mode==1)
00701       {
00702         ad_comm::pvm_manager->start_slave_processes(mp);
00703         ad_comm::pvm_manager->send_variable_values_to_slaves();
00704       }
00705       if (ad_comm::pvm_manager->mode==2)
00706       {
00707         //ad_comm::pvm_manager->slave_number=get_int_from_master();
00708         ad_comm::pvm_manager->get_variable_values_from_master();
00709       }
00710 #  if defined(USE_LAPLACE)
00711     }
00712 #  endif
00713   }
00714 }
00715 void admaster_slave_variable_interface(void){;}
00716 
00717 #else  //#if defined(USE_ADPVM)
00718 
00719 void admaster_slave_variable_interface(void){;}
00720 void ad_comm::get_slave_assignments(void) { ;}
00721 
00722 void admaster_slave_variable_interface(const ad_comm& mp){;}
00723 
00724 adpvm_slave_args::operator char ** ()
00725 {
00726   return argv;
00727 }
00728 
00729 
00730 void strcpy(const adpvm_slave_args& _a,const char * s)
00731 {
00732   adpvm_slave_args& a=(adpvm_slave_args&) _a;
00733   if (((char**)(a))[a.counter])
00734   {
00735     delete [] (((char**)(a))[a.counter]);
00736   }
00737   int len=strlen(s);
00738   if (len>1000) len=1000;
00739   ((char**)(a))[a.counter] = new char[len+1];
00740   if (a.counter>=a.get_num_args()-1)
00741   {
00742     cerr << "need to increase the number of slave args" <<endl;
00743     ad_exit(1);
00744   }
00745   strncpy(((char**)(a))[a.counter++],s,len+1);
00746   strcpy(((char**)(a))[0],(char*)str(a.counter));
00747 }
00748 
00749 void adpvm_slave_args::operator -- (void)
00750 {
00751   if (counter>0)
00752   {
00753     if (argv[counter])
00754     {
00755       delete [] argv[counter];
00756       argv[counter]=NULL;
00757     }
00758     counter--;
00759     strcpy(argv[0],(char*)str(counter));
00760   }
00761 }
00762 
00763 int adpvm_slave_args::get_num_args(void){return num_args;}
00764 #endif //#if defined(USE_ADPVM)