1 # Copyright (C) 2009-2015 EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from string import Template
23 from compat import Template,set
25 # cxx file of a MPI component.
26 # template parameters:
27 # component : component name
28 # servicesdef : extra "#include"
29 # servicesimpl : services implementations
31 #include "${component}.hxx"
35 #include <SALOME_NamingService.hxx>
36 #include <Utils_SALOME_Exception.hxx>
37 #include "Utils_CorbaException.hxx"
54 //! Constructor for component "${component}" instance
58 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
59 PortableServer::POA_ptr poa,
60 PortableServer::ObjectId * contId,
61 const char *instanceName,
62 const char *interfaceName,
64 : Engines_Component_i(orb, poa, contId, instanceName, interfaceName,
68 _id = _poa->activate_object(_thisObj);
71 ${component}_i::${component}_i(CORBA::ORB_ptr orb,
72 PortableServer::POA_ptr poa,
73 Engines::Container_ptr container,
74 const char *instanceName,
75 const char *interfaceName,
77 : Engines_Component_i(orb, poa, container, instanceName, interfaceName,
81 _id = _poa->activate_object(_thisObj);
84 //! Destructor for component "${component}" instance
85 ${component}_i::~${component}_i()
93 PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
94 PortableServer::POA_ptr poa,
95 PortableServer::ObjectId * contId,
96 const char *instanceName,
97 const char *interfaceName)
99 MESSAGE("PortableServer::ObjectId * ${component}Engine_factory()");
100 int is_mpi_container;
104 MPI_Initialized(&is_mpi_container);
105 if (!is_mpi_container)
109 MPI_Init(&argc, &argv);
112 MPI_Comm_rank( MPI_COMM_WORLD, &numproc );
113 regist = ( numproc == 0 );
114 ${component}_i * myEngine = new ${component}_i(orb, poa, contId, instanceName, interfaceName, regist);
115 return myEngine->getId() ;
119 cxxCompo=Template(cxxCompo)
121 # Header file of a MPI component
122 # template parameters:
123 # component : component name
124 # module : module name
125 # compodefs : additional "#include" and other specific declarations
126 # servicesdef : declaration of component services
128 #ifndef _${component}_HXX_
129 #define _${component}_HXX_
131 #include <SALOME_Component.hh>
132 #include "Superv_Component_i.hxx"
133 #include "${module}.hh"
134 #include "MPIObject_i.hxx"
140 class ${component}_i: public virtual POA_${module}_ORB::${component},
142 public virtual MPIObject_i,
143 public virtual Engines_Component_i
146 ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
147 PortableServer::ObjectId * contId,
148 const char *instanceName, const char *interfaceName,
150 ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
151 Engines::Container_ptr container,
152 const char *instanceName, const char *interfaceName,
154 virtual ~${component}_i();
160 PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
161 PortableServer::POA_ptr poa,
162 PortableServer::ObjectId * contId,
163 const char *instanceName,
164 const char *interfaceName);
169 hxxCompo=Template(hxxCompo)
171 # Declaration of the thread function to run a MPI service.
172 # template parameters:
173 # service : name of the service.
174 # input_vals : declarations of input ports of the service.
176 void * th_${service}(void * s);
178 int ip; // mpi process id
179 Engines::IORTab* tior;
181 } thread_${service}_struct;
184 hxxThreadService=Template(hxxThreadService)
187 # template parameters:
188 # module : module name
189 # component : component name
190 # service : service name
191 # out_vals : declaration of output ports ("type1 name1;\ntype2 name2;")
193 # in_vals : copy of input ports to thread structure
194 # parameters : list of parameters ("type1 name1, type2 name2")
197 void * th_${service}(void * s)
199 std::ostringstream msg;
200 exception_st *est = new exception_st;
201 est->exception = false;
203 thread_${service}_struct *st = (thread_${service}_struct *)s;
208 ${module}_ORB::${component}_var compo = ${module}_ORB::${component}::_narrow((*(st->tior))[st->ip]);
209 compo->${service_call};
211 catch(const SALOME::SALOME_Exception &ex)
213 est->exception = true;
214 est->msg = ex.details.text;
216 catch(const CORBA::Exception &ex)
218 est->exception = true;
219 msg << "CORBA::Exception: " << ex;
220 est->msg = msg.str();
227 void ${component}_i::${service}(${parameters})
229 beginService("${component}_i::${service}");
236 // Run the service in every MPI process
239 th = new pthread_t[_nbproc];
240 for(int ip=1;ip<_nbproc;ip++)
242 thread_${service}_struct *st = new thread_${service}_struct;
246 pthread_create(&(th[ip]),NULL,th_${service},(void*)st);
255 for(int ip=1;ip<_nbproc;ip++)
257 pthread_join(th[ip],&ret_th);
258 est = (exception_st*)ret_th;
261 std::ostringstream msg;
262 msg << "[" << ip << "] " << est->msg;
265 THROW_SALOME_CORBA_EXCEPTION(msg.str().c_str(),SALOME::INTERNAL_ERROR);
272 catch ( const SALOME_Exception & ex)
274 THROW_SALOME_CORBA_EXCEPTION(CORBA::string_dup(ex.what()), SALOME::INTERNAL_ERROR);
276 catch ( const SALOME::SALOME_Exception & ex)
280 catch ( const std::exception& ex)
282 THROW_SALOME_CORBA_EXCEPTION(CORBA::string_dup(ex.what()), SALOME::INTERNAL_ERROR);
286 THROW_SALOME_CORBA_EXCEPTION("unknown exception", SALOME::INTERNAL_ERROR);
288 endService("${component}_i::${service}");
292 cxxService=Template(cxxService)
295 interface ${component}:${inheritedinterface} Engines::MPIObject, Engines::EngineComponent
300 interface=Template(interface)