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 );
117 ${component}_i * myEngine = new ${component}_i(orb, poa, contId, instanceName, interfaceName, regist);
118 return myEngine->getId() ;
122 cxxCompo=Template(cxxCompo)
124 # Header file of a MPI component
125 # template parameters:
126 # component : component name
127 # module : module name
128 # compodefs : additional "#include" and other specific declarations
129 # servicesdef : declaration of component services
131 #ifndef _${component}_HXX_
132 #define _${component}_HXX_
134 #include <SALOME_Component.hh>
135 #include "Superv_Component_i.hxx"
136 #include "${module}.hh"
137 #include "MPIObject_i.hxx"
143 class ${component}_i: public virtual POA_${module}_ORB::${component},
145 public virtual MPIObject_i,
146 public virtual Engines_Component_i
149 ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
150 PortableServer::ObjectId * contId,
151 const char *instanceName, const char *interfaceName,
153 ${component}_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa,
154 Engines::Container_ptr container,
155 const char *instanceName, const char *interfaceName,
157 virtual ~${component}_i();
163 PortableServer::ObjectId * ${component}Engine_factory( CORBA::ORB_ptr orb,
164 PortableServer::POA_ptr poa,
165 PortableServer::ObjectId * contId,
166 const char *instanceName,
167 const char *interfaceName);
172 hxxCompo=Template(hxxCompo)
174 # Declaration of the thread function to run a MPI service.
175 # template parameters:
176 # service : name of the service.
177 # input_vals : declarations of input ports of the service.
179 void * th_${service}(void * s);
181 int ip; // mpi process id
182 Engines::IORTab* tior;
184 } thread_${service}_struct;
187 hxxThreadService=Template(hxxThreadService)
190 # template parameters:
191 # module : module name
192 # component : component name
193 # service : service name
194 # out_vals : declaration of output ports ("type1 name1;\ntype2 name2;")
196 # in_vals : copy of input ports to thread structure
197 # parameters : list of parameters ("type1 name1, type2 name2")
200 void * th_${service}(void * s)
202 std::ostringstream msg;
203 exception_st *est = new exception_st;
204 est->exception = false;
206 thread_${service}_struct *st = (thread_${service}_struct *)s;
211 ${module}_ORB::${component}_var compo = ${module}_ORB::${component}::_narrow((*(st->tior))[st->ip]);
212 compo->${service_call};
214 catch(const SALOME::SALOME_Exception &ex)
216 est->exception = true;
217 est->msg = ex.details.text;
219 catch(const CORBA::Exception &ex)
221 est->exception = true;
222 msg << "CORBA::Exception: " << ex;
223 est->msg = msg.str();
230 void ${component}_i::${service}(${parameters})
232 beginService("${component}_i::${service}");
239 // Run the service in every MPI process
242 th = new pthread_t[_nbproc];
243 for(int ip=1;ip<_nbproc;ip++)
245 thread_${service}_struct *st = new thread_${service}_struct;
249 pthread_create(&(th[ip]),NULL,th_${service},(void*)st);
258 for(int ip=1;ip<_nbproc;ip++)
260 pthread_join(th[ip],&ret_th);
261 est = (exception_st*)ret_th;
264 std::ostringstream msg;
265 msg << "[" << ip << "] " << est->msg;
268 THROW_SALOME_CORBA_EXCEPTION(msg.str().c_str(),SALOME::INTERNAL_ERROR);
275 catch ( const SALOME_Exception & ex)
277 THROW_SALOME_CORBA_EXCEPTION(CORBA::string_dup(ex.what()), SALOME::INTERNAL_ERROR);
278 /* SALOME::ExceptionStruct es;
279 es.text=CORBA::string_dup(ex.what());
280 es.type=SALOME::INTERNAL_ERROR;
281 throw SALOME::SALOME_Exception(es);*/
283 catch ( const SALOME::SALOME_Exception & ex)
287 catch ( const std::exception& ex)
289 THROW_SALOME_CORBA_EXCEPTION(CORBA::string_dup(ex.what()), SALOME::INTERNAL_ERROR);
290 /* //std::cerr << typeid(ex).name() << std::endl;
291 SALOME::ExceptionStruct es;
292 es.text=CORBA::string_dup(ex.what());
293 es.type=SALOME::INTERNAL_ERROR;
294 throw SALOME::SALOME_Exception(es);*/
298 THROW_SALOME_CORBA_EXCEPTION("unknown exception", SALOME::INTERNAL_ERROR);
299 /* std::cerr << "unknown exception" << std::endl;
300 SALOME::ExceptionStruct es;
301 es.text=CORBA::string_dup(" unknown exception");
302 es.type=SALOME::INTERNAL_ERROR;
303 throw SALOME::SALOME_Exception(es);*/
305 endService("${component}_i::${service}");
309 cxxService=Template(cxxService)
312 interface ${component}:${inheritedinterface} Engines::MPIObject, Engines::EngineComponent
317 interface=Template(interface)