Code Examples

Example 6.1 Calling pvm_spawn routine from main().

Example 6.2 Using multiple calls to pvm_spawn from main().

Example 6.3 Creating combinations of sets.

Example 6.4 Definition of colorCombinations() function.

Example 6.5 Using PVM tasks to produce numeric combinations.

Example 6.6 Using the MessageId tag to distinguish data types.

Example 6.7 Using PVM to implement MPMD model of computation.



Programs

program6-1.cc Uses pvm_send to send a number to another PVM task that
is executing.

program6-2.cc Receives a number from its parent process and sends the result to its
parent process.



Program Profiles

Program 6-1

Program 6-2

Publisher:
Addison Wesley
ISBN:
0-13-101376-9
Year:
2004
Page Numbers:
691
Chapters:
13





Example 6.1


//Calling pvm_spawn routine from main(). 

int main(int argc, char *argv[])
{
   int TaskId[10];
   int TaskId2[5];
   pvm_spawn("set_combination",NULL,0,"",10,TaskId); // 1rst Spawn
   pvm_spawn("set_combination",argv,0,"",5,TaskId2); // 2nd Spawn
   //...
}




Example 6.2


// Using multiple calls to pvm_spawn from main().

int main(int argc, char *argv[])
{
   int Task1;
   int Task2;
   int Task3;
   //...
   pvm_spawn("set_combination", NULL,1,"host1",1,&Task1);
   pvm_spawn("set_combination",argv,1,"host2",1,&,Task2);
   pvm_spawn("set_combination",argv++,1,"host 3",1,&,Task3);
   //...
}




Example 6.3

// Creating combinations of sets.

int main(int argc,char *argv[])
{
   int RetCode,TaskId[4];
   RetCode = pvm_spawn("pvm_generic_combination",NULL,0,"",4,TaskId);
   if(RetCode == 4){
      colorCombinations(TaskId[0],9);
      colorCombinations(TaskId[1],12);
      numericCombinations(TaskId[2],4);
      numericCombinations(TaskId[3],3);
      saveResult(TaskId[0]);
      saveResult(TaskId[1]);
      saveResult(TaskId[2]);
      saveResult(TaskId[3]);
      pvm_exit();
   }
   else{
        cerr << "Error Spawning ChildProcess" << endl;
        pvm_exit();
   }
   return(0);
}




Example 6.4

// Definition of colorCombinations() function.

void colorCombinations(int TaskId,int Choices)
{
   int MessageId =1;
   char *Buffer;
   int Size;
   int N;
   string Source("blue purple green red yellow orange silver gray ");
   Source.append("pink black white brown light_green aqua beige cyan ");
   Source.append("olive azure magenta plum orchid violet maroon lavender");
   Source.append("\n");
   Buffer = new char[(Source.size() + 100)];
   strcpy(Buffer,Source.c_str());
   N = pvm_initsend(PvmDataDefault);
   pvm_pkint(&Choices,1,1);
   pvm_send(TaskId,MessageId);
   N = pvm_initsend(PvmDataDefault);
   pvm_pkbyte(Buffer,strlen(Buffer),1);
   pvm_send(TaskId,MessageId);
   delete Buffer;
}




Example 6.5


// Using PVM tasks to produce numeric combinations.

void numericCombinations(int TaskId,int Choices)
{
   int MessageId = 2;
   int N;
   double ImportantNumbers[7] = {3.00e+8,6.67e-11,1.99e+30, 
                                 1.67e-27,6.023e+23,6.63e-34,
                                 3.14159265359};
   N = pvm_initsend(PvmDataDefault);
   pvm_pkint(&Choices,1,1);
   pvm_send(TaskId,MessageId);
   N = pvm_initsend(PvmDataDefault);
   pvm_pkdouble(ImportantNumbers,5,1);
   pvm_send(TaskId,MessageId);
}




Example 6.6


// Using the MessageId tag to distinguish data types.

pvm_bufinfo(N,&NumBytes,&MessageId,&Ptid);
if(MessageId == 1){
   vector Source;
   Buf = new char[NumBytes];
   pvm_upkbyte(Buf,NumBytes,1);
   strstream Buffer;
   Buffer << Buf << ends;
   while(Buffer.good())
   {
      Buffer >> Color;
      if(!Buffer.eof()){
         Source.push_back(Color);
      }
   }
   generateCombinations(Source,Ptid,Value);
   delete Buf;
}
if(MessageId == 2){
   vector Source;
   double *ImportantNumber;
   NumBytes = NumBytes / sizeof(double);
   ImportantNumber = new double[NumBytes];
   pvm_upkdouble(ImportantNumber,NumBytes,1);
   copy(ImportantNumber,ImportantNumber +(NumBytes + 1),
   inserter(Source,Source.begin()));
   generateCombinations(Source,Ptid,Value);
   delete ImportantNumber;

}




Example 6.7


// Using PVM to implement MPMD model of computation.

int main(int argc, char *argv[])
{
   int Task1[20];
   int Task2[50];
   int Task3[30];
   //...
   pvm_spawn("pvm_generic_combination", NULL,1,"host1",20,Task1);
   pvm_spawn("generate_plans",argv,0,"",50,Task2);
   pvm_spawn("agent_filters",argv++,1,"host 3",30,&Task3);
   //...
}







Program 6.1

 
#include "pvm3.h"
#include <iostream>
#include <string.h>

int main(int argc,char *argv[])
{
   int RetCode,MessageId;
   int PTid, Tid;
   char Message[100];
   float Result[1];
   PTid = pvm_mytid();
   RetCode = pvm_spawn("program6-2",NULL,0,"",1,&Tid);
   if(RetCode == 1){
      MessageId = 1;
      strcpy(Message,"22");
      pvm_initsend(PvmDataDefault);
      pvm_pkstr(Message);
      pvm_send(Tid,MessageId);
      pvm_recv(Tid,MessageId);
      pvm_upkfloat(Result,1,1);
      cout << Result[0] << endl;
      pvm_exit();
      return(0);
   }
   else{
          cerr << "Could not spawn task " << endl;
          pvm_exit();
          return(1);
   }
}








Program 6.2


#include "pvm3.h"
#include "stdlib.h"

int main(int argc, char *argv[])
{
   int MessageId, Ptid;
   char Message[100];
   float Num,Result;
   Ptid = pvm_parent();
   MessageId = 1;
   pvm_recv(Ptid,MessageId);
   pvm_upkstr(Message);
   Num = atof(Message);
   Result = Num / 7.0001;
   pvm_initsend(PvmDataDefault);
   pvm_pkfloat(&Result,1,1);
   pvm_send(Ptid,MessageId);
   pvm_exit();
   return(0);
}







Program Profile 6.1

Program Name:
program6-1.cc

Description:
Uses pvm_send to send a number to another PVM task that is executing
(Program 6.2) and pvm_recv to receive a number from that task.

Libraries Required:
libpvm3

Headers Required:
"pvm3.h" <iostream> <string.h>

Compile & Link Instructions:
c++ -o program6-1 -I $PVM_ROOT/include -L $PVM_ROOT/lib/$PVM_ARCH -l pvm3

Test Environment:
Solaris 8, PVM 3.4.3, SuSE Linux 7.1, gcc 2.95.2

Execution Instructions:
./program6-1

Notes:
pvmd must be running.






Program Profile 6.2

Program Name:
program6-2.cc

Description:
This program receives a number from its parent process and divides that number by 7. It sends the result to its parent process.

Libraries Required:
libpvm3

Headers Required:
<pvm3.h> <stdlib.h>

Compile & Link Instructions:
c++ -o program6-2 -I $PVM_ROOT/include program6-2.cc -L $PVM_ROOT/lib/PVM_ARCH -lpvm3

Test Environment:
SuSE Linux 7.1 gnu C++ 2.95.2 , Solaris 8 Workshop 6 , PVM 3.4.3

Execution Instructions:
This program is spawned by Program 6.1

Notes:
pvmd must be running.






Sample chapter, summaries, captions, table of contents, code example and listings are provided for your information. Copyright 2003 Addison Wesley. All rights reserved. No part of these materials may be duplicated or reproduced, in any form or by any means, without the written permission of the publisher.