org/objectweb/proactive/mpi/MPISpmdProxy.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.mpi;
00032 
00033 import org.apache.log4j.Logger;
00034 
00035 import org.objectweb.proactive.ActiveObjectCreationException;
00036 import org.objectweb.proactive.ProActive;
00037 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
00038 import org.objectweb.proactive.core.node.NodeException;
00039 import org.objectweb.proactive.core.util.log.Loggers;
00040 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00041 
00042 import java.io.IOException;
00043 
00044 import java.util.ArrayList;
00045 import java.util.Hashtable;
00046 
00047 
00048 public class MPISpmdProxy implements MPISpmd, java.io.Serializable {
00049     protected final static Logger MPI_PROXY_LOGGER = ProActiveLogger.getLogger(Loggers.MPI);
00050 
00052     private String status = MPIConstants.MPI_DEFAULT_STATUS;
00053 
00055     private String name;
00056 
00058     private MPISpmdImpl target;
00059 
00063     public MPISpmdProxy(VirtualNode vn) throws RuntimeException {
00064         try {
00065             target = (MPISpmdImpl) ProActive.newActive(MPISpmdImpl.class.getName(),
00066                     new Object[] { vn });
00067         } catch (ActiveObjectCreationException e) {
00068             e.printStackTrace();
00069         } catch (NodeException e) {
00070             e.printStackTrace();
00071         }
00072         name = target.getName();
00073         MPI_PROXY_LOGGER.debug("[MPI Proxy] creating MPI SPMD active object: " +
00074             name);
00075         MPI_PROXY_LOGGER.debug("[MPI Proxy] status : " + status);
00076 
00077         try {
00078             ProActive.setImmediateService(target, "killMPI");
00079             ProActive.setImmediateService(target, "getStatus");
00080             ProActive.setImmediateService(target, "isFinished");
00081             ProActive.setImmediateService(target, "getSpmdClassesParams");
00082             ProActive.setImmediateService(target, "getSpmdClasses");
00083             ProActive.setImmediateService(target, "getClassesParams");
00084             ProActive.setImmediateService(target, "getClasses");
00085         } catch (IOException e) {
00086             e.printStackTrace();
00087         }
00088     }
00089 
00094     public MPIResult startMPI() throws IllegalMPIStateException {
00095         MPI_PROXY_LOGGER.debug(
00096             "[MPI Proxy] call start method on active object ");
00097         MPI_PROXY_LOGGER.debug("[MPI Proxy] status : " + status);
00098         // UNSTARTED/DEFAULT status
00099         if (status.equals(MPIConstants.MPI_UNSTARTED)) {
00100             setStatus(MPIConstants.MPI_RUNNING);
00101             return target.startMPI();
00102         } else {
00103             ProActive.terminateActiveObject(target, true);
00104             throw new IllegalMPIStateException(
00105                 "!!! ERROR: cannot start MPI process " + this.name +
00106                 " Caused by: MPI process has already been started once ");
00107         }
00108     }
00109 
00115     public MPIResult reStartMPI() throws IllegalMPIStateException {
00116         MPI_PROXY_LOGGER.debug(
00117             "[MPI Proxy] call reStart method on active object ");
00118         MPI_PROXY_LOGGER.debug("[MPI Proxy] status : " + status);
00119         // check if program is already finished and change status if yes
00120         checkTerminationStatus();
00121         // UNSTARTED
00122         if (!status.equals(MPIConstants.MPI_UNSTARTED)) {
00123             setStatus(MPIConstants.MPI_RUNNING);
00124             target.reinitProcess();
00125             return target.startMPI();
00126         } else {
00127             ProActive.terminateActiveObject(target, true);
00128             throw new IllegalMPIStateException(
00129                 "!!! ERROR: cannot restart MPI process " + this.name +
00130                 " Caused by: no mpi process has been started once before");
00131         }
00132     }
00133 
00139     public boolean killMPI() {
00140         MPI_PROXY_LOGGER.debug("[MPI Proxy] Kill MPI Process ");
00141         checkTerminationStatus();
00142         // RUNNING status
00143         if (status.equals(MPIConstants.MPI_RUNNING)) {
00144             setStatus(MPIConstants.MPI_KILLED);
00145             return target.killMPI();
00146         } // FINISHED/KILLED status
00147         else if (status.equals(MPIConstants.MPI_FINISHED) ||
00148                 status.equals(MPIConstants.MPI_KILLED)) {
00149             return false;
00150         }
00151         // UNSTARTED status
00152         else {
00153             ProActive.terminateActiveObject(target, true);
00154             throw new IllegalMPIStateException(
00155                 "!!! ERROR: cannot kill MPI process " + this.name +
00156                 " Caused by: no mpi process has been started once before!");
00157         }
00158     }
00159 
00163     public String getStatus() {
00164         checkTerminationStatus();
00165         return status;
00166     }
00167 
00172     public void setMPICommandArguments(String arguments) {
00173         target.setMPICommandArguments(arguments);
00174     }
00175 
00180     public String getName() {
00181         return this.name;
00182     }
00183 
00184     public String toString() {
00185         StringBuilder sb = new StringBuilder(target.toString());
00186         sb.append("\n Status: ");
00187         sb.append(getStatus());
00188         return sb.toString();
00189     }
00190 
00191     //
00192     // -- PRIVATE METHODS FOR SERIALIZATION -------------------------------------------------
00193     //
00194     //    private void writeObject(java.io.ObjectOutputStream out)
00195     //        throws IllegalMPIStateException {
00196     //        target.killMPI();
00197     //        ProActive.terminateActiveObject(target, true);
00198     //        throw new IllegalMPIStateException(
00199     //            "Copy's not allowed for MPI proxy object");
00200     //    }
00201     private void setStatus(String status) {
00202         this.status = status;
00203     }
00204 
00205     private void checkTerminationStatus() {
00206         if (target.isFinished() && (!status.equals(MPIConstants.MPI_FINISHED)) &&
00207                 (!status.equals(MPIConstants.MPI_KILLED))) {
00208             status = MPIConstants.MPI_FINISHED;
00209         }
00210     }
00211 
00212     public VirtualNode getVn() {
00213         return this.target.getVn();
00214     }
00215 
00216     //  ----+----+----+----+----+----+----+----+----+----+----+-------+----+----
00217     //  --+----+---- methods for the future wrapping with control ----+----+----
00218     //  ----+----+----+----+----+----+----+----+----+----+----+-------+----+----
00219     public void newActiveSpmd(String cl) {
00220         this.target.newActiveSpmd(cl);
00221     }
00222 
00223     public void newActiveSpmd(String cl, Object[] params) {
00224         this.target.newActiveSpmd(cl, params);
00225     }
00226 
00227     public void newActiveSpmd(String cl, Object[][] params) {
00228         this.target.newActiveSpmd(cl, params);
00229     }
00230 
00231     public void newActive(String cl, Object[] params, int rank) {
00232         this.target.newActive(cl, params, rank);
00233     }
00234 
00235     public ArrayList getClasses() {
00236         return this.target.getClasses();
00237     }
00238 
00239     public ArrayList getSpmdClasses() {
00240         return this.target.getSpmdClasses();
00241     }
00242 
00243     public Hashtable getClassesParams() {
00244         return this.target.getClassesParams();
00245     }
00246 
00247     public Hashtable getSpmdClassesParams() {
00248         return this.target.getSpmdClassesParams();
00249     }
00250 
00251     public String getRemoteLibraryPath() {
00252         return this.target.getRemoteLibraryPath();
00253     }
00254 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1