mpiMain.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.4 $
00022 // $Date: 2006/09/26 21:29:35 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/tcl/mpiMain.cpp,v $
00024 
00025 /* 
00026  * tclAppInit.c --
00027  *
00028  *      Provides a default version of the main program and Tcl_AppInit
00029  *      procedure for Tcl applications (without Tk).
00030  *
00031  * Copyright (c) 1993 The Regents of the University of California.
00032  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
00033  * Copyright (c) 1998-1999 by Scriptics Corporation.
00034  *
00035  * See the file "license.terms" for information on usage and redistribution
00036  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00037  *
00038  * RCS: @(#) $Id: mpiMain.cpp,v 1.4 2006/09/26 21:29:35 fmk Exp $
00039  */
00040 
00041 extern "C" {
00042 #include <tcl.h>
00043 #include <tk.h>
00044 }
00045 
00046 
00047 // #include <mpi.h>
00048 #include "commands.h"
00049 
00050 /*
00051  * The following variable is a special hack that is needed in order for
00052  * Sun shared libraries to be used for Tcl.
00053  */
00054 
00055 #ifdef _KAI
00056 //extern "C" int matherr();
00057 #endif
00058 
00059 #ifdef _UNIX
00060 #include <math.h>
00061 
00062 //int *tclDummyMathPtr = (int *)matherr;
00063 #endif
00064 
00065 
00066 #ifdef TCL_TEST
00067 
00068 extern "C" {
00069 #include "tclInt.h"
00070 }
00071 
00072 
00073 
00074 extern int              Procbodytest_Init _ANSI_ARGS_((Tcl_Interp *interp));
00075 extern int              Procbodytest_SafeInit _ANSI_ARGS_((Tcl_Interp *interp));
00076 extern int              TclObjTest_Init _ANSI_ARGS_((Tcl_Interp *interp));
00077 extern int              Tcltest_Init _ANSI_ARGS_((Tcl_Interp *interp));
00078 #ifdef TCL_THREADS
00079 extern int              TclThread_Init _ANSI_ARGS_((Tcl_Interp *interp));
00080 #endif
00081 
00082 #endif /* TCL_TEST */
00083 
00084 #ifdef TCL_XT_TEST
00085 extern void             XtToolkitInitialize _ANSI_ARGS_((void));
00086 extern int              Tclxttest_Init _ANSI_ARGS_((Tcl_Interp *interp));
00087 #endif
00088 
00089 /*
00090  *----------------------------------------------------------------------
00091  *
00092  * main --
00093  *
00094  *      This is the main program for the application.
00095  *
00096  * Results:
00097  *      None: Tcl_Main never returns here, so this procedure never
00098  *      returns either.
00099  *
00100  * Side effects:
00101  *      Whatever the application does.
00102  *
00103  *----------------------------------------------------------------------
00104  */
00105 
00106 extern void g3TclMain(int argc, char **argv, Tcl_AppInitProc *appInitProc, int rank, int np);
00107 #include <stdio.h>
00108 #include <string.h>
00109 
00110 #include <PartitionedDomain.h>
00111 #include <MPI_MachineBroker.h>
00112 #include <ShadowSubdomain.h>
00113 #include <ActorSubdomain.h>
00114 #include <FEM_ObjectBroker.h>
00115 
00116 extern PartitionedDomain theDomain;
00117 
00118 extern int OPS_PARALLEL_PROCESSING;
00119 extern int OPS_NUM_SUBDOMAINS;
00120 extern bool OPS_PARTITIONED;
00121 extern FEM_ObjectBroker *OPS_OBJECT_BROKER;
00122 extern MachineBroker    *OPS_MACHINE;
00123 extern bool OPS_USING_MAIN_DOMAIN;
00124 extern int OPS_MAIN_DOMAIN_PARTITION_ID;
00125 
00126 MPI_MachineBroker *theMachineBroker = 0;
00127 
00128 int
00129 main(int argc, char **argv)
00130 {
00131   FEM_ObjectBroker theBroker;
00132   MPI_MachineBroker theMachine(&theBroker, argc, argv);
00133   theMachineBroker = &theMachine;
00134 
00135   int rank = theMachine.getPID();
00136   int np = theMachine.getNP();
00137 
00138   //
00139   // depending on rank we do something
00140   //
00141   if (rank != 0) {
00142 
00143     //
00144     // on slave processes we spin waiting to create & run actors
00145     //
00146     fprintf(stderr, "Slave Process Running\n");
00147     theMachine.runActors();
00148     fprintf(stderr, "Slave Process DONE %d\n", rank);
00149 
00150   } else {
00151 
00152     //
00153     // on process 0 we create some ShadowSubdomains & then start the OpenSees interpreter
00154     //
00155     fprintf(stderr, "Master Process Running OpenSees Interpreter\n");   
00156 
00157     //
00158     // set some global parameters
00159     //
00160     OPS_OBJECT_BROKER = &theBroker;
00161     OPS_MACHINE = &theMachine;
00162     OPS_PARALLEL_PROCESSING = np;
00163 
00164     if (np%2 == 0) {
00165       OPS_NUM_SUBDOMAINS = np;
00166       OPS_USING_MAIN_DOMAIN = true;
00167       OPS_MAIN_DOMAIN_PARTITION_ID = 1;
00168     } else
00169       OPS_NUM_SUBDOMAINS = np - 1;
00170 
00171     OPS_PARTITIONED    = false;
00172     
00173     //
00174     // the rest straightr out of regular tclMain to start our interpreter
00175     //
00176 
00177     /*
00178      * The following #if block allows you to change the AppInit
00179      * function by using a #define of TCL_LOCAL_APPINIT instead
00180      * of rewriting this entire file.  The #if checks for that
00181      * #define and uses Tcl_AppInit if it doesn't exist.
00182      */
00183 
00184 #ifndef TCL_LOCAL_APPINIT
00185 #define TCL_LOCAL_APPINIT Tcl_AppInit    
00186 #endif
00187     
00188     /* fmk - comment out the following block to get to compile 
00189        extern "C" int TCL_LOCAL_APPINIT _ANSI_ARGS_((Tcl_Interp *interp));
00190        fmk - end commented block */
00191 
00192     /*
00193      * The following #if block allows you to change how Tcl finds the startup
00194      * script, prime the library or encoding paths, fiddle with the argv,
00195      * etc., without needing to rewrite Tcl_Main()
00196      */
00197     
00198 #ifdef TCL_LOCAL_MAIN_HOOK
00199     extern int TCL_LOCAL_MAIN_HOOK _ANSI_ARGS_((int *argc, char ***argv));
00200 #endif
00201     
00202 #ifdef TCL_XT_TEST
00203     XtToolkitInitialize();
00204 #endif
00205     
00206 #ifdef TCL_LOCAL_MAIN_HOOK
00207     TCL_LOCAL_MAIN_HOOK(&argc, &argv);
00208 #endif
00209 
00210     g3TclMain(argc, argv, TCL_LOCAL_APPINIT, 1, 0);
00211 
00212     // some clean up to shut the remotes down if still running
00213     theDomain.clearAll();
00214     
00215     // shutdown the remote machines
00216     theMachine.shutdown();
00217   }
00218   
00219   //
00220   // mpi clean up
00221   //
00222   
00223 
00224   fprintf(stderr, "Process Terminating %d\n", rank);
00225   
00226   return 0;
00227 }
00228 
00229 /*
00230  *----------------------------------------------------------------------
00231  *
00232  * Tcl_AppInit --
00233  *
00234  *      This procedure performs application-specific initialization.
00235  *      Most applications, especially those that incorporate additional
00236  *      packages, will have their own version of this procedure.
00237  *
00238  * Results:
00239  *      Returns a standard Tcl completion code, and leaves an error
00240  *      message in the interp's result if an error occurs.
00241  *
00242  * Side effects:
00243  *      Depends on the startup script.
00244  *
00245  *----------------------------------------------------------------------
00246  */
00247 
00248 
00249 int Tcl_AppInit(Tcl_Interp *interp)
00250 {
00251     if (Tcl_Init(interp) == TCL_ERROR) {
00252         return TCL_ERROR;
00253     }
00254 
00255 #ifdef TCL_TEST
00256 #ifdef TCL_XT_TEST
00257      if (Tclxttest_Init(interp) == TCL_ERROR) {
00258          return TCL_ERROR;
00259      }
00260 #endif
00261     if (Tcltest_Init(interp) == TCL_ERROR) {
00262         return TCL_ERROR;
00263     }
00264     Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init,
00265             (Tcl_PackageInitProc *) NULL);
00266     if (TclObjTest_Init(interp) == TCL_ERROR) {
00267         return TCL_ERROR;
00268     }
00269 #ifdef TCL_THREADS
00270     if (TclThread_Init(interp) == TCL_ERROR) {
00271         return TCL_ERROR;
00272     }
00273 #endif
00274     if (Procbodytest_Init(interp) == TCL_ERROR) {
00275         return TCL_ERROR;
00276     }
00277     Tcl_StaticPackage(interp, "procbodytest", Procbodytest_Init,
00278             Procbodytest_SafeInit);
00279 #endif /* TCL_TEST */
00280 
00281     /*
00282      * Call the init procedures for included packages.  Each call should
00283      * look like this:
00284      *
00285      * if (Mod_Init(interp) == TCL_ERROR) {
00286      *     return TCL_ERROR;
00287      * }
00288      *
00289      * where "Mod" is the name of the module.
00290      */
00291 
00292     /*
00293      * Call Tcl_CreateCommand for application-specific commands, if
00294      * they weren't already created by the init procedures called above.
00295      */
00296 
00297     if (g3AppInit(interp) < 0)
00298         return TCL_ERROR;
00299 
00300     /*
00301      * Specify a user-specific startup file to invoke if the application
00302      * is run interactively.  Typically the startup file is "~/.apprc"
00303      * where "app" is the name of the application.  If this line is deleted
00304      * then no user-specific startup file will be run under any conditions.
00305      */
00306 
00307     Tcl_SetVar(interp, "tcl_rcFileName", "~/.tclshrc", TCL_GLOBAL_ONLY);
00308     return TCL_OK;
00309 }
00310 
00311 
00312 int OpenSeesExit(ClientData clientData, Tcl_Interp *interp, int argc, TCL_Char **argv)
00313 {
00314   theDomain.clearAll();
00315 
00316   //
00317   // mpi clean up
00318   //
00319 
00320   if (theMachineBroker != 0) {
00321     theMachineBroker->shutdown();
00322     fprintf(stderr, "Process Terminating\n");
00323   }
00324 
00325   Tcl_Exit(0);
00326 
00327   return 0;
00328 }
00329 

Generated on Mon Oct 23 15:05:30 2006 for OpenSees by doxygen 1.5.0