Salome HOME
SMH: Merge with V2_2_3 version (to be compatible with MED from maintainance branch...
[modules/kernel.git] / src / MPILifeCycleCORBA / MPILifeCycleCORBA.py
1 #==============================================================================
2 #  File      : MPILifeCycleCORBA.py
3 #  Created   : ven may 30 08:42:01 CEST 2003
4 #  Author    : Bernard SECHER, CEA
5 #  Project   : SALOME
6 #  Copyright : CEA 2003
7 #  $Header$
8 #==============================================================================
9
10 from LifeCycleCORBA import *
11 from Utils_Identity import getShortHostName
12
13 class MPILifeCycleCORBA(LifeCycleCORBA):
14     #-------------------------------------------------------------------------
15
16     def __init__(self, orb):
17         MESSAGE( "MPILifeCycleCORBA::__init__" )
18         LifeCycleCORBA.__init__(self, orb)
19
20     #-------------------------------------------------------------------------
21
22     def FindOrStartMPIContainer(self, theComputer , theMPIContainerRoot, nbproc ):
23         theMPIContainer = theMPIContainerRoot + "_" + str(nbproc)
24         MESSAGE( "FindOrStartMPIContainer" + theComputer + theMPIContainer )
25         aMPIContainer = self.FindContainer( theComputer + "/" + theMPIContainer )
26         if aMPIContainer is None :
27             if (theMPIContainerRoot == "MPIFactoryServer") | (theMPIContainerRoot == "MPIFactoryServerPy") :
28                 if theComputer == getShortHostName() :
29                     rshstr = ""
30                 else :
31                     rshstr = "rsh -n " + theComputer + " "
32                 path = self.ComputerPath( theComputer )
33 ##                if path != "" :
34 ##                    rshstr = rshstr + path + "/../bin/salome/"
35 ##                else :
36 ##                    rshstr = rshstr + os.getenv( "KERNEL_ROOT_DIR" ) + "/bin/"
37 #                    rshstr = rshstr + os.getenv( "PWD" ) + "/"
38                 if theMPIContainerRoot == "MPIFactoryServer" :
39                     rshstr = rshstr + "mpirun -np " + str(nbproc) + " " + path + "SALOME_MPIContainer "
40                 else :
41                     rshstr = rshstr + path + "SALOME_MPIContainerPy.py '"
42                 rshstr = rshstr + theMPIContainer + " -"
43                 omniORBcfg = os.getenv( "OMNIORB_CONFIG" )
44 #                omniORBcfg = os.getenv( "HOME" ) + "/.omniORB.cfg"
45                 file = os.open( omniORBcfg , os.O_RDONLY )
46                 ORBInitRef = os.read(file,132)
47                 if ORBInitRef[len(ORBInitRef)-1] == '\n' :
48                     ORBInitRef,bsn = ORBInitRef.split('\n')
49                 os.close( file )
50                 rshstr = rshstr + ORBInitRef
51                 if theMPIContainerRoot == "MPIFactoryServerPy" :
52                     rshstr = rshstr + "'"
53                 rshstr = rshstr + " > /tmp/" + theMPIContainer + "_"
54                 rshstr = rshstr + theComputer
55                 rshstr = rshstr + ".log 2>&1 &"
56                 os.system( rshstr )
57                 MESSAGE( "FindOrStartMPIContainer" + rshstr + " done" )
58             else :
59                 if theMPIContainer.find('Py') == -1 :
60                     aMPIContainer = self.FindContainer( theComputer + "/" + "MPIFactoryServer_" + str(nbproc) )
61                 else :
62                     aMPIContainer = self.FindContainer( theComputer + "/" + "MPIFactoryServerPy_" + str(nbproc) )
63                 aMPIContainer = aMPIContainer.start_impl( theMPIContainer )
64
65             count = 21
66             while aMPIContainer is None :
67                 time.sleep(1)
68                 count = count - 1
69                 MESSAGE( str(count) + ". Waiting for " + theComputer + "/" + theMPIContainer )
70                 aMPIContainer = self.FindContainer( theComputer + "/" + theMPIContainer )
71                 if count == 0 :
72                     return aMPIContainer
73             
74         return  aMPIContainer       
75         #os.system("rsh -n dm2s0017 /export/home/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515")
76
77     #-------------------------------------------------------------------------
78
79     def FindOrLoadMPIComponent(self, MPIcontainerName, MPIcomponentName, nbproc):
80
81         theComputer,theMPIContainerRoot = self.ContainerName( MPIcontainerName )
82         theMPIContainer = theMPIContainerRoot + "_" + str(nbproc)
83         name = [CosNaming.NameComponent(theComputer,"dir"),
84                 CosNaming.NameComponent(theMPIContainer,"dir"),
85                 CosNaming.NameComponent(MPIcomponentName,"object")]
86         try:
87             obj = self._containerRootContext.resolve(name)
88         except CosNaming.NamingContext.NotFound, ex:
89             MESSAGE( "component " + MPIcomponentName + " not found, trying to load" )
90             MPIcontainer = self.FindContainer(theComputer + "/" + theMPIContainer)
91             if MPIcontainer is None:
92                 MESSAGE( "MPIcontainer " + theComputer + "/" + theMPIContainer + " not found in Naming Service, trying to start" )
93                 if (theMPIContainerRoot != "MPIFactoryServer") & (theMPIContainerRoot != "MPIFactoryServerPy") :
94                     if theMPIContainer.find('Py') == -1 :
95                         theMPIFactorycontainerRoot = "MPIFactoryServer"
96                         theMPIFactorycontainer = theMPIFactorycontainerRoot + "_" + str(nbproc)
97                     else :
98                         theMPIFactorycontainerRoot = "MPIFactoryServerPy"
99                         theMPIFactorycontainer = theMPIFactorycontainerRoot + "_" + str(nbproc)
100                     MPIFactorycontainer = self.FindContainer(theComputer + "/" + theMPIFactorycontainer)
101                     if MPIFactorycontainer is None:
102                         MESSAGE( "MPIcontainer " + theComputer + "/" + theMPIFactorycontainer + " not found in Naming Service, trying to start" )
103                         MPIFactorycontainer = self.FindOrStartMPIContainer(theComputer,theMPIFactorycontainerRoot,nbproc)
104                 else:
105                     MPIFactorycontainer = self.FindOrStartMPIContainer(theComputer,theMPIContainerRoot,nbproc)
106                 if MPIFactorycontainer != None :
107                     MPIcontainer = self.FindOrStartMPIContainer(theComputer,theMPIContainerRoot,nbproc)
108
109             if MPIcontainer != None:
110                 compoinfo = self._catalog.GetComponent(MPIcomponentName)
111                 if compoinfo is None:
112                     MESSAGE( "MPIcomponent " + MPIcomponentName + " not found in Module Catalog" )
113                 else:
114                     try:
115                         machineName = theComputer
116                         path = compoinfo.GetPathPrefix(machineName) + "/"
117                     except SALOME_ModuleCatalog.NotFound, ex:
118                         MESSAGE( "machine " + machineName + " not found in Module Catalog" )
119                         MESSAGE( "trying localhost" )
120                         try:
121                             path = compoinfo.GetPathPrefix("localhost") + "/"
122                         except SALOME_ModuleCatalog.NotFound, ex:
123                             path = ""
124                     implementation = path + "lib" + MPIcomponentName + "Engine.so"
125                     MESSAGE( "Trying to load " + implementation )
126                     try:
127                         MPIcomponent = MPIcontainer.load_impl(MPIcomponentName, implementation)
128                         MESSAGE( "component " + MPIcomponent._get_instanceName() + " launched !" )
129                         return MPIcomponent
130                     except:
131                         MESSAGE( "component " + MPIcomponentName + " NOT launched !" )
132
133         else:
134             try:
135                 MPIcomponent = obj._narrow(Engines.Component)
136                 if MPIcomponent is None:
137                     MESSAGE( MPIcomponentName + " is not a component !" )
138                 else:
139                     MESSAGE( "MPIcomponent " + MPIcomponent._get_instanceName() + " found !" )
140                 return MPIcomponent
141             except:
142                 MESSAGE( MPIcomponentName + " failure" )
143                 return None