Sandia Home Sandia Home
Main Page | Publications | Downloads | Configuration | Running the Code | Solver Parameters | FAQ | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

cex_worker.cpp

Go to the documentation of this file.
00001 // $Id: cex_worker.cpp,v 1.1 2006/06/30 19:14:31 tgkolda Exp $ 
00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/example-custom-executor/cex_worker.cpp,v $ 
00003 
00019 #include<iostream>
00020 #include "mpi.h"                // <-- Provides MPI
00021 #include "msgtags.hpp"          // <-- For MPI tags
00022 
00024 bool constraint(int n, double* x)
00025 {
00026   double tmp = 0;
00027 
00028   for (int i = 0; i < n; i ++)
00029     tmp += x[i] * x[i];
00030 
00031   return (tmp <= 1.0);
00032 } 
00033 
00035 double feval(int n, double* x)
00036 {
00037   double f = 0;
00038   
00039   for (int i = 0; i < n; i ++)
00040     f += (i + 1) * x[i] * x[i];
00041 
00042   return(f);
00043 } 
00045 void worker()
00046 {
00047   // **** Receive initial message with problem size ***
00048   int n;                        // Problem size
00049   MPI_Status status;
00050 
00051   // NOTE: This matches a send by the master.
00052   MPI_Recv(&n, 1, MPI_INT, 0, SIZE, MPI_COMM_WORLD, &status);
00053   
00054   // *** Continuously receive and process incoming messages ***
00055 
00056   // Create other variables
00057   double x[n];                  // Vector to be evaluated
00058   int msgtag;                   // Message tag
00059   int tag;                      // Vector tag
00060   int code;                     // Solution code (0 = constraint violation, 1 = success)
00061   double f;                     // Functin value
00062   
00063   while (1)
00064   {
00065     // Blocking probe for the next message
00066     MPI_Probe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
00067     
00068     // Extract the message tag
00069     msgtag = status.MPI_TAG;
00070     
00071     // Check for termination tag and, if so, quit
00072     if (msgtag == QUIT)
00073       break;
00074     
00075     // Receive message in two parts: tag and x 
00076     // NOTE: This must match the corresponding calls to MPI_Send 
00077     //       in CustomExecutor::spawn
00078     MPI_Recv(&tag, 1, MPI_INT, 0, XTAG,  MPI_COMM_WORLD, &status);
00079     MPI_Recv(x, n, MPI_DOUBLE, 0, XVEC, MPI_COMM_WORLD, &status);
00080     
00081     // Evaluate the function at x if the constraint is not violated
00082     if (constraint(n,x))
00083     {
00084       code = 0;
00085     }
00086     else
00087     {
00088       code = 1;
00089       f = feval(n,x);
00090     }
00091     
00092     // Return the answer in three parts: tag, code, f. These are
00093     // interpreted by the recv function in the executor.  
00094     // NOTE: This must match the correpsonding calls to MPI_Recv 
00095     //       in CustomExecutor::recv
00096     MPI_Send(&tag,  1, MPI_INT, 0, XTAG, MPI_COMM_WORLD);
00097     MPI_Send(&code, 1, MPI_INT, 0, CODE, MPI_COMM_WORLD);
00098     MPI_Send(&f, 1, MPI_DOUBLE, 0, FVAL, MPI_COMM_WORLD);
00099   }
00100 
00101 }
00102 

 

© Sandia Corporation | Site Contact | Privacy and Security

Generated on Fri Feb 16 10:33:35 2007 for APPSPACK 5.0.1 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2002