mpiParameterMain.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: 2006/09/01 00:49:09 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/tcl/mpiParameterMain.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: mpiParameterMain.cpp,v 1.2 2006/09/01 00:49:09 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 #include <Channel.h>
00116 #include <Message.h>
00117 
00118 extern PartitionedDomain theDomain;
00119 
00120 extern int OPS_PARALLEL_PROCESSING;
00121 extern int OPS_NUM_SUBDOMAINS;
00122 extern bool OPS_PARTITIONED;
00123 extern FEM_ObjectBroker *OPS_OBJECT_BROKER;
00124 extern MachineBroker    *OPS_MACHINE;
00125 extern bool OPS_USING_MAIN_DOMAIN;
00126 extern int OPS_MAIN_DOMAIN_PARTITION_ID;
00127 
00128 MachineBroker *theMachineBroker = 0;
00129 
00130 int
00131 main(int argc, char **argv)
00132 {
00133   FEM_ObjectBroker theBroker;
00134   MPI_MachineBroker theMachine(&theBroker, argc, argv);
00135   theMachineBroker = &theMachine;
00136 
00137   int rank = theMachine.getPID();
00138   int np = theMachine.getNP();
00139 
00140   //
00141   // if rank 0 we send all args
00142   //
00143 
00144   int numArg = 0;
00145   int sizeArg = 0;
00146   char **args = 0;
00147   char *dataArgs = 0;
00148 
00149   if (rank == 0) {
00150 
00151     for (int i=0; i<argc; i++)
00152       if (argv[i] == NULL) {
00153         i = argc+1;
00154       } else {
00155         numArg ++;
00156         sizeArg += strlen(argv[i])+1;
00157       }
00158 
00159     static ID data(2);
00160     data(0) = numArg;
00161     data(1) = sizeArg;
00162 
00163     dataArgs = new char[sizeArg];
00164     int loc = 0;
00165     for (int i=0; i<numArg; i++) {
00166       int lengthArg = strlen(argv[i]);
00167       strncpy(&dataArgs[loc], argv[i],lengthArg+1);
00168       loc+=lengthArg+1;
00169     }
00170 
00171     Message msgChar(dataArgs, sizeArg);
00172 
00173     for (int j=0; j<np-1; j++) {
00174       Channel *otherChannel = theMachine.getRemoteProcess();
00175       otherChannel->sendID(0,0,data);
00176       otherChannel->sendMsg(0,0,msgChar);
00177     }
00178       
00179   } else {
00180 
00181     static ID data(2);    
00182 
00183     Channel *myChannel = theMachine.getMyChannel();
00184     myChannel->recvID(0,0,data);
00185     numArg = data(0);
00186     sizeArg = data(1);
00187     dataArgs = new char[sizeArg];
00188     Message msgChar(dataArgs, sizeArg);
00189     
00190     myChannel->recvMsg(0,0,msgChar);
00191   }
00192 
00193   args = new char *[numArg];
00194   args[0] = dataArgs;
00195   int argCount = 1;
00196   for (int j=1; j<sizeArg-1; j++)
00197     if (dataArgs[j] == '\0') {
00198       args[argCount] = &dataArgs[j+1];
00199       argCount++;
00200     }
00201 
00202 #ifndef TCL_LOCAL_APPINIT
00203 #define TCL_LOCAL_APPINIT Tcl_AppInit    
00204 #endif
00205 
00206 #ifdef TCL_LOCAL_MAIN_HOOK
00207   extern int TCL_LOCAL_MAIN_HOOK _ANSI_ARGS_((int *argc, char ***argv));
00208 #endif
00209     
00210 #ifdef TCL_XT_TEST
00211   XtToolkitInitialize();
00212 #endif
00213     
00214 #ifdef TCL_LOCAL_MAIN_HOOK
00215   TCL_LOCAL_MAIN_HOOK(&argc, &argv);
00216 #endif
00217 
00218   g3TclMain(numArg, args, TCL_LOCAL_APPINIT, rank, np);
00219 
00220   // some clean up to shut the remotes down if still running
00221   //  theDomain.clearAll();
00222   
00223   // shutdown the remote machines
00224   //  theMachine.shutdown();
00225 
00226   //
00227   // mpi clean up
00228   //
00229   
00230   fprintf(stderr, "Process Terminating %d\n", rank);
00231   
00232   return 0;
00233 }
00234 
00235 /*
00236  *----------------------------------------------------------------------
00237  *
00238  * Tcl_AppInit --
00239  *
00240  *      This procedure performs application-specific initialization.
00241  *      Most applications, especially those that incorporate additional
00242  *      packages, will have their own version of this procedure.
00243  *
00244  * Results:
00245  *      Returns a standard Tcl completion code, and leaves an error
00246  *      message in the interp's result if an error occurs.
00247  *
00248  * Side effects:
00249  *      Depends on the startup script.
00250  *
00251  *----------------------------------------------------------------------
00252  */
00253 
00254 
00255 int Tcl_AppInit(Tcl_Interp *interp)
00256 {
00257     if (Tcl_Init(interp) == TCL_ERROR) {
00258         return TCL_ERROR;
00259     }
00260 
00261 #ifdef TCL_TEST
00262 #ifdef TCL_XT_TEST
00263      if (Tclxttest_Init(interp) == TCL_ERROR) {
00264          return TCL_ERROR;
00265      }
00266 #endif
00267     if (Tcltest_Init(interp) == TCL_ERROR) {
00268         return TCL_ERROR;
00269     }
00270     Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init,
00271             (Tcl_PackageInitProc *) NULL);
00272     if (TclObjTest_Init(interp) == TCL_ERROR) {
00273         return TCL_ERROR;
00274     }
00275 #ifdef TCL_THREADS
00276     if (TclThread_Init(interp) == TCL_ERROR) {
00277         return TCL_ERROR;
00278     }
00279 #endif
00280     if (Procbodytest_Init(interp) == TCL_ERROR) {
00281         return TCL_ERROR;
00282     }
00283     Tcl_StaticPackage(interp, "procbodytest", Procbodytest_Init,
00284             Procbodytest_SafeInit);
00285 #endif /* TCL_TEST */
00286 
00287     /*
00288      * Call the init procedures for included packages.  Each call should
00289      * look like this:
00290      *
00291      * if (Mod_Init(interp) == TCL_ERROR) {
00292      *     return TCL_ERROR;
00293      * }
00294      *
00295      * where "Mod" is the name of the module.
00296      */
00297 
00298     /*
00299      * Call Tcl_CreateCommand for application-specific commands, if
00300      * they weren't already created by the init procedures called above.
00301      */
00302 
00303     if (g3AppInit(interp) < 0)
00304         return TCL_ERROR;
00305 
00306     /*
00307      * Specify a user-specific startup file to invoke if the application
00308      * is run interactively.  Typically the startup file is "~/.apprc"
00309      * where "app" is the name of the application.  If this line is deleted
00310      * then no user-specific startup file will be run under any conditions.
00311      */
00312 
00313     Tcl_SetVar(interp, "tcl_rcFileName", "~/.tclshrc", TCL_GLOBAL_ONLY);
00314     return TCL_OK;
00315 }
00316 
00317 
00318 int OpenSeesExit(ClientData clientData, Tcl_Interp *interp, int argc, TCL_Char **argv)
00319 {
00320   theDomain.clearAll();
00321 
00322   //
00323   // mpi clean up
00324   //
00325 
00326   if (theMachineBroker != 0) {
00327     theMachineBroker->shutdown();
00328     fprintf(stderr, "Process Terminating\n");
00329   }
00330 
00331   Tcl_Exit(0);
00332 
00333   return 0;
00334 }
00335 

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