MachineBroker.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.2 $
00022 // $Date: 2005/11/23 18:33:33 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/actor/machineBroker/MachineBroker.cpp,v $
00024                                                                         
00025 // Written: fmk
00026 // Revision: A
00027 
00028 #include <MachineBroker.h>
00029 #include <OPS_Globals.h>
00030 #include <ID.h>
00031 #include <Channel.h>
00032 
00033 #include <Actor.h>
00034 #include <FEM_ObjectBroker.h>
00035 
00036 #include <OPS_Globals.h>
00037 
00038 MachineBroker::MachineBroker(FEM_ObjectBroker *theBroker)
00039   :theObjectBroker(theBroker), actorChannels(0), numActorChannels(0), numActiveChannels(0), activeChannels(0)
00040 {
00041 
00042 }
00043 
00044 MachineBroker::~MachineBroker()
00045 {  
00046   if (actorChannels != 0) {
00047     delete [] actorChannels;
00048     delete activeChannels;
00049   }  
00050 }
00051 
00052 
00053 int
00054 MachineBroker::shutdown(void)
00055 {  
00056   // send the termination notice to all machineBrokers running actorProcesses
00057   for (int i=0; i<numActorChannels; i++) {
00058     ID idData(1);
00059     idData(0) = 0;
00060     Channel *theChannel = actorChannels[i];
00061     if (theChannel->sendID(0, 0, idData) < 0) {
00062       opserr << "MachineBroker::shutdown(void) - failed to send ID\n";
00063     }
00064 
00065     if (theChannel->recvID(0, 0, idData) < 0) {
00066       opserr << "MachineBroker::shutdown(void) - failed to recv ID\n";
00067     }
00068 
00069     this->freeProcess(theChannel);
00070   }
00071 
00072   if (actorChannels != 0) {
00073     delete [] actorChannels;
00074     delete activeChannels;
00075     actorChannels = 0;
00076     activeChannels = 0;
00077     numActorChannels = 0;
00078     numActiveChannels = 0;
00079   }
00080 
00081   return 0;
00082 }
00083 
00084 
00085 int
00086 MachineBroker::runActors(void)
00087 {
00088   Channel *theChannel = this->getMyChannel();
00089   ID idData(1);
00090   int done = 0;
00091 
00092   // loop until recv kill signal
00093   while (done == 0) {
00094 
00095     if (theChannel->recvID(0, 0, idData) < 0) {
00096       opserr << "MachineBroker::runActors(void) - failed to recv ID\n";
00097     }
00098 
00099     int actorType = idData(0);
00100     
00101     // switch on data type
00102     if (idData(0) == 0) {
00103       done = 1;
00104 
00105       if (theChannel->sendID(0, 0, idData) < 0) {
00106         opserr << "MachineBroker::run(void) - failed to send ID\n";
00107       }
00108 
00109       return 0;
00110 
00111     } else {
00112 
00113       // create an actor of approriate type
00114       Actor *theActor = theObjectBroker->getNewActor(actorType, theChannel);
00115       if (theActor == 0) {
00116         opserr << "MachineBroker::run(void) - invalid actor type\n";
00117         idData(0) = 1;
00118       } else
00119         idData(0) = 0;
00120 
00121       // send ID back indicating wheter actor was created 
00122       if (theChannel->sendID(0, 0, idData) < 0) {
00123         opserr << "MachineBroker::run(void) - failed to send ID\n";
00124       }
00125 
00126       // run the actor object
00127       if (theActor->run() != 0) {
00128         opserr << "MachineBroker::run(void) - actor failed while running\n";
00129       }  
00130 
00131       // destroying theActor
00132       delete theActor;
00133     }
00134     done = 0;
00135   }
00136 
00137   return 0;
00138 }
00139 
00140 
00141 Channel *
00142 MachineBroker::startActor(int actorType, int compDemand)
00143 {
00144   if (compDemand != 0) 
00145     opserr << "MachineBroker::startActor() - does not take computational demand variable into account\n";
00146 
00147   Channel *theChannel = 0;
00148 
00149   // check if have an available machine broker running runActors() and waiting to start an actor running
00150   if (numActiveChannels < numActorChannels) {
00151 
00152     for (int i=0; i<numActorChannels; i++) {
00153       if ((*activeChannels)(i) == 0) {
00154         theChannel = actorChannels[i];
00155         i=numActorChannels;
00156         numActiveChannels++;
00157         (*activeChannels)(i) = 1;
00158       }
00159     }
00160   }
00161   
00162   // if no available connection established .. establish a new one
00163   if (theChannel == 0) {
00164 
00165     theChannel = this->getRemoteProcess();
00166 
00167     if (theChannel == 0) {
00168       opserr << "MachineBroker::startActor() - no available channel available\n";
00169       return 0;
00170     }
00171 
00172     Channel **nextChannels = new Channel *[numActorChannels +1];
00173     ID *nextChannelID = new ID(numActorChannels+1);
00174     for (int i=0; i<numActorChannels; i++) {
00175       nextChannels[i] = actorChannels[i];
00176       (*nextChannelID)(i) = (*activeChannels)(i);
00177     }
00178 
00179     nextChannels[numActorChannels] = theChannel;
00180     (*nextChannelID)(numActorChannels) = 0;    
00181 
00182     // clean up old memory
00183     if (actorChannels != 0) {
00184       delete [] actorChannels;
00185       delete activeChannels;
00186     }
00187 
00188     // reset the arrays
00189     actorChannels = nextChannels;
00190     activeChannels = nextChannelID;    
00191     numActorChannels++;
00192     numActiveChannels++;    
00193   }
00194 
00195   // now that we have a channel to a machine broker waiting to run some actor processes, start the actor
00196   ID idData(1);
00197   idData(0) = actorType;
00198   if (theChannel->sendID(0, 0, idData) != 0) {
00199     opserr << "MachineBroker::startActor() - failed to send actorType\n";
00200     this->freeProcess(theChannel);
00201     return 0;    
00202   }
00203 
00204   if (theChannel->recvID(0, 0, idData) != 0) {
00205     opserr << "MachineBroker::startActor() - remote process failure\n";
00206     return 0;    
00207   }
00208   if (idData(0) != 0) {
00209     opserr << "MachineBroker::startActor() - could not start actorType: " << actorType << endln;
00210     this->freeProcess(theChannel);
00211     return 0;    
00212   }
00213 
00214   return theChannel;
00215   
00216 }
00217 
00218 int
00219 MachineBroker::finishedWithActor(Channel *theChannel)
00220 {
00221   // send the termination notice to all machineBrokers running actorProcesses
00222   for (int i=0; i<numActorChannels; i++) {
00223     if (theChannel == actorChannels[i]) {
00224       numActiveChannels--;
00225       (*activeChannels)(i) = 0;
00226       return 0;
00227     }
00228   }
00229   
00230   // if get here .. startActor() not called or subclass override
00231   return -1;
00232 }

Generated on Mon Oct 23 15:04:56 2006 for OpenSees by doxygen 1.5.0