Salome HOME
add check on mpi implementation
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelComponent_i.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME_ParallelComponent : implementation of container and engine for Parallel Kernel
24 //  File   : SALOME_ParallelComponent_i.cxx
25 //  Author : AndrĂ© RIBES, EDF
26 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
27 //
28 #include "SALOME_ParallelComponent_i.hxx"
29 #include "SALOME_ParallelContainer_i.hxx"
30
31 #include "OpUtil.hxx"
32 #include <stdio.h>
33 #ifndef WIN32
34 #include <dlfcn.h>
35 #endif
36 #include <cstdlib>
37 #include "utilities.h"
38 #include "Basics_Utils.hxx"
39
40 #ifndef WIN32
41 #include <sys/time.h>
42 #include <sys/resource.h>
43 #include <unistd.h>
44 #else
45 #include <sys/timeb.h>
46 int SIGUSR11 = 1000;
47 #endif
48
49 #include <paco_dummy.h>
50 #include <paco_omni.h>
51
52
53 extern bool _Sleeping ;
54 static Engines_Parallel_Component_i * theEngines_Component ;
55
56 bool Engines_Parallel_Component_i::_isMultiStudy = true;
57 bool Engines_Parallel_Component_i::_isMultiInstance = false;
58
59 //=============================================================================
60 /*! 
61  *  Standard Constructor for generic Component, used in derived class
62  *  Connection to Registry and Notification
63  *  \param orb Object Request broker given by Container
64  *  \parap poa Portable Object Adapter from Container (normally root_poa)
65  *  \param contId container CORBA id inside the server
66  *  \param instanceName unique instance name for this object (see Container_i)
67  *  \param interfaceName component class name
68  *  \param notif use of notification
69  */
70 //=============================================================================
71
72 Engines_Parallel_Component_i::Engines_Parallel_Component_i(CORBA::ORB_ptr orb, char * ior, int rank,
73                                          PortableServer::POA_ptr poa, 
74                                          PortableServer::ObjectId * contId, 
75                                          const char *instanceName,
76                                          const char *interfaceName,
77                                          bool notif,
78                                          bool regist) :
79   InterfaceParallel_impl(orb,ior,rank), 
80   Engines::EngineComponent_serv(orb,ior,rank),
81   Engines::EngineComponent_base_serv(orb,ior,rank),
82   Engines::Parallel_Component_serv(orb,ior,rank),
83   Engines::Parallel_Component_base_serv(orb,ior,rank),
84   _instanceName(instanceName),
85   _interfaceName(interfaceName),
86   _id(NULL),
87   _myConnexionToRegistry(0),
88   _ThreadId(0) ,
89   _ThreadCpuUsed(0) ,
90   _Executed(false) ,
91   _graphName("") ,
92   _nodeName(""),
93   _studyId(-1),
94   _destroyed(false),
95   _CanceledThread(false)
96 {
97   MESSAGE("Parallel Component constructor with instanceName "<< _instanceName);
98   //SCRUTE(pd_refCount);
99   _orb = CORBA::ORB::_duplicate(orb);
100   _poa = PortableServer::POA::_duplicate(poa);
101   _contId = contId ;
102   CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
103
104   if (regist)
105   {
106     CORBA::String_var the_ior = _orb->object_to_string(o);
107     _myConnexionToRegistry = new RegistryConnexion(0, 0, the_ior,"theSession",
108                                                    _instanceName.c_str());
109   }
110   _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
111
112   deploy_mutex = new pthread_mutex_t();
113   pthread_mutex_init(deploy_mutex, NULL);
114   _proxy = NULL;
115  //SCRUTE(pd_refCount);
116 }
117
118 //=============================================================================
119 /*! 
120  *  Destructor: call Container for decrement of instances count.
121  *  When instances count falls to 0, the container tries to remove the
122  *  component library (dlclose)
123  */
124 //=============================================================================
125
126 Engines_Parallel_Component_i::~Engines_Parallel_Component_i()
127 {
128   MESSAGE("Parallel Component destructor");
129   Engines_Parallel_Container_i::decInstanceCnt(_interfaceName);
130   if(_myConnexionToRegistry)delete _myConnexionToRegistry;
131   if(_notifSupplier)delete _notifSupplier;
132   if (_id)
133     delete(_id);
134
135   pthread_mutex_destroy(deploy_mutex);
136   delete deploy_mutex;
137   if (_proxy)
138     delete _proxy;
139 }
140
141 //=============================================================================
142 /*! 
143  *  CORBA method: return name of the instance, unique in this Container
144  */
145 //=============================================================================
146
147 char* Engines_Parallel_Component_i::instanceName()
148 {
149    return CORBA::string_dup(_instanceName.c_str()) ;
150 }
151
152 //=============================================================================
153 /*! 
154  *  CORBA method: return name of the component class
155  */
156 //=============================================================================
157
158 char* Engines_Parallel_Component_i::interfaceName()
159 {
160   return CORBA::string_dup(_interfaceName.c_str()) ;
161 }
162
163 //=============================================================================
164 /*! 
165  *  CORBA method: Get study Id
166  *  \return -1: not initialised (Internal Error)
167  *           0: multistudy component instance
168  *          >0: study id associated to this instance
169  */
170 //=============================================================================
171
172 CORBA::Long Engines_Parallel_Component_i::getStudyId()
173 {
174   return _studyId;
175 }
176
177 //=============================================================================
178 /*! 
179  *  CORBA method: Test if instance is alive and responds
180  */
181 //=============================================================================
182
183 void Engines_Parallel_Component_i::ping()
184 {
185 #ifndef WIN32
186   MESSAGE("Engines_Parallel_Component_i::ping() pid "<< getpid() << " threadid "
187           << pthread_self());
188 #else
189   MESSAGE("Engines_Parallel_Component_i::ping() pid "<< _getpid()<< " threadid "
190           << pthread_self().p );
191 #endif
192 }
193
194 //=============================================================================
195 /*! 
196  *  CORBA method: Deactivate this instance. CORBA object is deactivated (do not
197  *  respond any more to CORBA calls), the connection to Regsitry is removed
198  *  (Registry informed of deactivation), internal server reference counter on
199  *  the derived servant class is decremented, to allow destruction of the class
200  *  (delete) by POA, when there are no more references.
201  *  -- TO BE USED BY CONTAINER ONLY (Container housekeeping) --
202  */
203 //=============================================================================
204
205 void Engines_Parallel_Component_i::destroy()
206 {
207   MESSAGE("Engines_Parallel_Component_i::destroy()");
208   MESSAGE("Object Instance will be deleted when Shutdown of the container will be called");
209   if (!_destroyed)
210   {
211     _remove_ref();
212     _destroyed = true;
213   }
214 }
215
216 //=============================================================================
217 /*! 
218  *  CORBA method: return CORBA reference of the Container
219  *
220  */
221 //=============================================================================
222
223 Engines::Container_ptr Engines_Parallel_Component_i::GetContainerRef()
224 {
225   MESSAGE("Engines_Parallel_Component_i::GetContainerRef");
226   CORBA::Object_var o = _poa->id_to_reference(*_contId) ;
227   return Engines::Container::_narrow(o);
228 }
229
230 //=============================================================================
231 /*! 
232  *  CORBA method: 
233  *  Gives a sequence of (key=string,value=any) to the component. 
234  *  Base class component stores the sequence in a map.
235  *  The map is cleared before.
236  *  This map is for use by derived classes. 
237  *  \param dico sequence of (key=string,value=any)
238  */
239 //=============================================================================
240
241 void Engines_Parallel_Component_i::setProperties(const Engines::FieldsDict& dico)
242 {
243   _fieldsDict.clear();
244   for (CORBA::ULong i=0; i<dico.length(); i++)
245     {
246       std::string cle(dico[i].key);
247       _fieldsDict[cle] = dico[i].value;
248     }
249 }
250
251 //=============================================================================
252 /*! 
253  *  CORBA method: 
254  *  returns a previously stored map (key=string,value=any) as a sequence.
255  *  (see setProperties)
256  */
257 //=============================================================================
258
259 Engines::FieldsDict* Engines_Parallel_Component_i::getProperties()
260 {
261   Engines::FieldsDict_var copie = new Engines::FieldsDict;
262   copie->length(_fieldsDict.size());
263   std::map<std::string,CORBA::Any>::iterator it;
264   CORBA::ULong i = 0;
265   for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++, i++)
266     {
267       std::string cle((*it).first);
268       copie[i].key = CORBA::string_dup(cle.c_str());
269       copie[i].value = _fieldsDict[cle];
270     }
271   return copie._retn();
272 }
273
274 //=============================================================================
275 /*! 
276  *  CORBA method: used by Supervision to give names to this instance
277  */
278 //=============================================================================
279
280 void Engines_Parallel_Component_i::Names( const char * graphName ,
281                                  const char * nodeName )
282 {
283   _graphName = graphName;
284   _nodeName = nodeName;
285   MESSAGE("Engines_Parallel_Component_i::Names( '" << _graphName << "' , '" 
286                                                    << _nodeName << "' )");
287 }
288
289 //=============================================================================
290 /*! 
291  *  CORBA method: used in Supervision
292  */
293 //=============================================================================
294
295 bool Engines_Parallel_Component_i::Kill_impl() 
296 {
297 //  MESSAGE("Engines_Parallel_Component_i::Kill_i() pthread_t "<< pthread_self()
298 //          << " pid " << getpid() << " instanceName "
299 //          << _instanceName.c_str() << " interface " << _interfaceName.c_str()
300 //          << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
301 //          << dec << " _ThreadId " << _ThreadId << " this " << hex << this
302 //          << dec ) ;
303
304   bool RetVal = false ;
305 #ifndef WIN32
306   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
307     {
308       RetVal = Killer( _ThreadId , SIGUSR2 ) ;
309       _ThreadId = (pthread_t ) -1 ;
310     }
311
312 #else
313   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
314     {
315       RetVal = Killer( *_ThreadId , 0 ) ;
316       _ThreadId = (pthread_t* ) 0 ;
317     }
318
319 #endif
320   return RetVal ;
321 }
322
323 //=============================================================================
324 /*! 
325  *  CORBA method: used in Supervision
326  */
327 //=============================================================================
328
329 bool Engines_Parallel_Component_i::Stop_impl()
330 {
331 #ifndef WIN32
332   MESSAGE("Engines_Parallel_Component_i::Stop_i() pthread_t "<< pthread_self()
333           << " pid " << getpid() << " instanceName "
334           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
335           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
336           << dec << " _ThreadId " << _ThreadId );
337 #else
338   MESSAGE("Engines_Parallel_Component_i::Stop_i() pthread_t "<< pthread_self().p
339           << " pid " << _getpid() << " instanceName "
340           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
341           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
342           << dec << " _ThreadId " << _ThreadId );
343 #endif
344   
345
346   bool RetVal = false ;
347 #ifndef WIN32
348   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
349     {
350       RetVal = Killer( _ThreadId , 0 ) ;
351       _ThreadId = (pthread_t ) -1 ;
352     }
353 #else
354   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
355     {
356       RetVal = Killer( *_ThreadId , 0 ) ;
357       _ThreadId = (pthread_t* ) 0 ;
358     }
359 #endif
360   return RetVal ;
361 }
362
363 //=============================================================================
364 /*! 
365  *  CORBA method: used in Supervision
366  */
367 //=============================================================================
368
369 bool Engines_Parallel_Component_i::Suspend_impl()
370 {
371 #ifndef WIN32
372   MESSAGE("Engines_Parallel_Component_i::Suspend_i() pthread_t "<< pthread_self()
373           << " pid " << getpid() << " instanceName "
374           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
375           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
376           << dec << " _ThreadId " << _ThreadId );
377 #else
378   MESSAGE("Engines_Parallel_Component_i::Suspend_i() pthread_t "<< pthread_self().p
379           << " pid " << _getpid() << " instanceName "
380           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
381           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
382           << dec << " _ThreadId " << _ThreadId );
383 #endif
384
385   bool RetVal = false ;
386 #ifndef WIN32
387   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
388 #else
389   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
390 #endif
391     {
392       if ( _Sleeping )
393         {
394           return false ;
395         }
396     else 
397       {
398 #ifndef WIN32
399         RetVal = Killer( _ThreadId ,SIGINT ) ;
400 #else
401         RetVal = Killer( *_ThreadId ,SIGINT ) ;
402 #endif
403         //if ( RetVal ) _Sleeping = true;
404
405       }
406     }
407   return RetVal ;
408 }
409
410 //=============================================================================
411 /*! 
412  *  CORBA method: used in Supervision
413  */
414 //=============================================================================
415
416 bool Engines_Parallel_Component_i::Resume_impl()
417 {
418 #ifndef WIN32
419   MESSAGE("Engines_Parallel_Component_i::Resume_i() pthread_t "<< pthread_self()
420           << " pid " << getpid() << " instanceName "
421           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
422           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
423           << dec << " _ThreadId " << _ThreadId );
424 #else
425   MESSAGE("Engines_Parallel_Component_i::Resume_i() pthread_t "<< pthread_self().p
426           << " pid " << _getpid() << " instanceName "
427           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
428           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
429           << dec << " _ThreadId " << _ThreadId );
430 #endif
431   bool RetVal = false ;
432 #ifndef WIN32
433   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
434 #else
435   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
436 #endif
437     {
438     if ( _Sleeping ) 
439       {
440         _Sleeping = false ;
441         RetVal = true ;
442       }
443     else
444       {
445         RetVal = false ;
446       }
447     }
448   return RetVal ;
449 }
450
451 //=============================================================================
452 /*! 
453  *  CORBA method: 
454  */
455 //=============================================================================
456
457 CORBA::Long Engines_Parallel_Component_i::CpuUsed_impl()
458 {
459   long cpu = 0 ;
460   if ( _ThreadId || _Executed )
461     {
462     if ( _ThreadId > 0 )
463       {
464 #ifndef WIN32
465       if ( pthread_self() != _ThreadId )
466 #else
467       if ( pthread_self().p != _ThreadId->p )
468 #endif
469         {
470         if ( _Sleeping )
471           {
472           }
473         else
474           {
475             // Get Cpu in the appropriate thread with that object !...
476             theEngines_Component = this ;
477 #ifndef WIN32
478             Killer( _ThreadId ,SIGUSR1 ) ;
479 #else
480             Killer( *_ThreadId ,SIGUSR11 ) ;
481 #endif
482           }
483         cpu = _ThreadCpuUsed ;
484         }
485       else
486         {
487           _ThreadCpuUsed = CpuUsed() ;
488           cpu = _ThreadCpuUsed ;
489           // std::cout << pthread_self() << " Engines_Parallel_Component_i::CpuUsed_impl "
490           //      << _serviceName << " " << cpu << std::endl ;
491       }
492     }
493     else 
494       {
495         cpu = _ThreadCpuUsed ;
496         // std::cout << pthread_self() << " Engines_Parallel_Component_i::CpuUsed_impl "
497         //      << _serviceName << " " << cpu<< std::endl ;
498       }
499     }
500   else
501     {
502       // std::cout<< pthread_self()<<"Engines_Parallel_Component_i::CpuUsed_impl _ThreadId "
503       //     <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<std::endl;
504     }
505   return cpu ;
506 }
507
508
509 //=============================================================================
510 /*! 
511  *  C++ method: return Container Servant
512  */
513 //=============================================================================
514
515 Engines_Parallel_Container_i *Engines_Parallel_Component_i::GetContainerPtr()
516 {
517   return dynamic_cast<Engines_Parallel_Container_i*>(_poa->id_to_servant(*_contId)) ;
518 }
519
520 //=============================================================================
521 /*! 
522  *  C++ method: set study Id
523  *  \param studyId         0 if instance is not associated to a study, 
524  *                         >0 otherwise (== study id)
525  *  \return true if the set of study Id is OK
526  *  must be set once by Container, at instance creation,
527  *  and cannot be changed after.
528  */
529 //=============================================================================
530
531 CORBA::Boolean Engines_Parallel_Component_i::setStudyId(CORBA::Long studyId)
532 {
533   ASSERT( studyId >= 0);
534   CORBA::Boolean ret = false;
535   if (_studyId < 0) // --- not yet initialized 
536     {
537       _studyId = studyId;
538       ret = true;
539     }
540   else
541     if ( _studyId == studyId) ret = true;
542   return ret;
543 }
544
545 //=============================================================================
546 /*! 
547  *  C++ method: return CORBA instance id, the id is set in derived class
548  *  constructor, when instance is activated.
549  */
550 //=============================================================================
551
552 PortableServer::ObjectId * Engines_Parallel_Component_i::getId()
553 {
554 //  MESSAGE("PortableServer::ObjectId * Engines_Parallel_Component_i::getId()");
555   return _id ;
556 }
557
558 //=============================================================================
559 /*! 
560  *  C++ method: used by derived classes for supervision
561  */
562 //=============================================================================
563
564 void Engines_Parallel_Component_i::beginService(const char *serviceName)
565 {
566 #ifndef WIN32
567   MESSAGE(pthread_self() << "Send BeginService notification for " <<serviceName
568           << endl << "Parallel Component instance : " << _instanceName << endl << endl);
569 #else
570   MESSAGE(pthread_self().p << "Send BeginService notification for " <<serviceName
571           << endl << "Parallel Component instance : " << _instanceName << endl << endl);
572 #endif
573 #ifndef WIN32
574   _ThreadId = pthread_self() ;
575 #else
576   _ThreadId = new pthread_t;
577   _ThreadId->p = pthread_self().p ;
578   _ThreadId->x = pthread_self().x ;
579 #endif
580   _StartUsed = 0 ;
581   _StartUsed = CpuUsed_impl() ;
582   _ThreadCpuUsed = 0 ;
583   _Executed = true ;
584   _serviceName = serviceName ;
585   if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
586     {
587       perror("pthread_setcanceltype ") ;
588       exit(0) ;
589     }
590   if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
591     {
592       perror("pthread_setcancelstate ") ;
593       exit(0) ;
594     }
595 //  MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
596 //          << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
597 //          << " _graphName " << _graphName << " _nodeName " << _nodeName );
598
599   // --- for supervisor : all strings given with setProperties
600   //     are set in environment
601   bool overwrite = true;
602   std::map<std::string,CORBA::Any>::iterator it;
603   for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
604     {
605       std::string cle((*it).first);
606       if ((*it).second.type()->kind() == CORBA::tk_string)
607         {
608           const char* value;
609           (*it).second >>= value;
610           // ---todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC.
611 #if defined __GNUC__
612           //int ret = setenv(cle.c_str(), value, overwrite);
613           setenv(cle.c_str(), value, overwrite);
614 #else
615           //CCRT porting : setenv not defined in stdlib.h
616           std::string s(cle);
617           s+='=';
618           s+=value;
619           // char* cast because 1st arg of linux putenv function
620           // is not a const char* !
621           //int ret=putenv((char *)s.c_str());
622           putenv((char *)s.c_str());
623           //End of CCRT porting
624 #endif
625           MESSAGE("--- setenv: "<<cle<<" = "<< value);
626         }
627     }
628 }
629
630 //=============================================================================
631 /*! 
632  *  C++ method: used by derived classes for supervision
633  */
634 //=============================================================================
635
636 void Engines_Parallel_Component_i::endService(const char *serviceName)
637 {
638   if ( !_CanceledThread )
639     _ThreadCpuUsed = CpuUsed_impl() ;
640
641 #ifndef WIN32
642   MESSAGE(pthread_self() << " Send EndService notification for " << serviceName
643           << endl << " Parallel Component instance : " << _instanceName << " StartUsed "
644           << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
645 #else
646   MESSAGE(pthread_self().p << " Send EndService notification for " << serviceName
647           << endl << " Parallel Component instance : " << _instanceName << " StartUsed "
648     << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
649 #endif
650   _ThreadId = 0 ;
651 }
652
653 //=============================================================================
654 /*! 
655  *  C++ method: -- CHECK IF USED --
656  */
657 //=============================================================================
658
659 char* Engines_Parallel_Component_i::graphName()
660 {
661   return CORBA::string_dup( _graphName.c_str() ) ;
662 }
663
664 //=============================================================================
665 /*! 
666  *  C++ method: -- CHECK IF USED --
667  */
668 //=============================================================================
669
670 char* Engines_Parallel_Component_i::nodeName()
671 {
672   return CORBA::string_dup( _nodeName.c_str() ) ;
673 }
674
675 //=============================================================================
676 /*! 
677  *  C++ method: used in Supervision (see kill_impl)
678  */
679 //=============================================================================
680
681 bool Engines_Parallel_Component_i::Killer( pthread_t ThreadId , int signum )
682 {
683 #ifndef WIN32
684   if ( ThreadId )
685 #else
686   if ( ThreadId.p )
687 #endif
688     {
689       if ( signum == 0 )
690         {
691           if ( pthread_cancel( ThreadId ) )
692             {
693               perror("Killer pthread_cancel error") ;
694               return false ;
695             }
696           else
697             {
698 #ifndef WIN32
699               MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
700                       << " pthread_canceled") ;
701 #else
702         MESSAGE(pthread_self().p << "Killer : ThreadId " << ThreadId.p
703                       << " pthread_canceled") ;
704 #endif
705             }
706         }
707       else
708         {
709           if ( pthread_kill( ThreadId , signum ) == -1 )
710             {
711               perror("Killer pthread_kill error") ;
712               return false ;
713             }
714           else 
715             {
716 #ifndef WIN32
717         MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
718                       << " pthread_killed(" << signum << ")") ;
719 #else
720         MESSAGE(pthread_self().p << "Killer : ThreadId " << ThreadId.p
721                       << " pthread_killed(" << signum << ")") ;
722 #endif
723             }
724         }
725     }
726   return true ;
727 }
728
729 //=============================================================================
730 /*! 
731  *  C++ method:
732  */ 
733 //=============================================================================
734
735 void SetCpuUsed()
736 {
737   if (theEngines_Component)
738     theEngines_Component->SetCurCpu();
739 }
740
741 //=============================================================================
742 /*! 
743  *  C++ method:
744  */
745 //=============================================================================
746
747 void Engines_Parallel_Component_i::SetCurCpu()
748 {
749   _ThreadCpuUsed =  CpuUsed() ;
750   //  MESSAGE(pthread_self() << 
751   //  " Engines_Parallel_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
752 }
753
754 //=============================================================================
755 /*! 
756  *  C++ method:
757  */
758 //=============================================================================
759
760 long Engines_Parallel_Component_i::CpuUsed()
761 {
762   long cpu = 0 ;
763 #ifndef WIN32
764   struct rusage usage ;
765   if ( _ThreadId || _Executed )
766     {
767       if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
768         {
769           perror("Engines_Parallel_Component_i::CpuUsed") ;
770           return 0 ;
771         }
772       cpu = usage.ru_utime.tv_sec - _StartUsed ;
773       // std::cout << pthread_self() << " Engines_Parallel_Component_i::CpuUsed " << " "
774       //      << _serviceName   << usage.ru_utime.tv_sec << " - " << _StartUsed
775       //      << " = " << cpu << std::endl ;
776     }
777   else
778     {
779       // std::cout << pthread_self() << "Engines_Parallel_Component_i::CpuUsed _ThreadId "
780       //      << _ThreadId << " " << _serviceName<< " _StartUsed " 
781       //      << _StartUsed << std::endl ;
782     }
783 #else
784         // NOT implementet yet
785 #endif
786
787
788   return cpu ;
789 }
790
791 void CallCancelThread()
792 {
793   if ( theEngines_Component )
794     theEngines_Component->CancelThread() ;
795 }
796
797 //=============================================================================
798 /*!
799  *  C++ method:
800  */
801 //=============================================================================
802
803 void Engines_Parallel_Component_i::CancelThread()
804 {
805   _CanceledThread = true;
806 }
807
808 //=============================================================================
809 /*! 
810  *  C++ method: Send message to event channel
811  */
812 //=============================================================================
813
814 void Engines_Parallel_Component_i::sendMessage(const char *event_type,
815                                       const char *message)
816 {
817     _notifSupplier->Send(graphName(), nodeName(), event_type, message);
818 }
819
820 //=============================================================================
821 /*! 
822  *  C++ method: return standard library name built on component name
823  */
824 //=============================================================================
825
826 std::string Engines_Parallel_Component_i::GetDynLibraryName(const char *componentName)
827 {
828   std::string ret="lib";
829   ret+=componentName;
830   ret+="Engine.so";
831   return ret;
832 }
833
834 //=============================================================================
835 /*! 
836  *  C++ method: DumpPython default implementation
837  */
838 //=============================================================================
839
840 Engines::TMPFile* Engines_Parallel_Component_i::DumpPython(CORBA::Object_ptr theStudy, 
841                                                            CORBA::Boolean isPublished, 
842                                                            CORBA::Boolean isMultiFile,
843                                                            CORBA::Boolean& isValidScript)
844 {
845   const char* aScript = isMultiFile ? "def RebuildData(theStudy): pass" : "";
846   char* aBuffer = new char[strlen(aScript)+1];
847   strcpy(aBuffer, aScript);
848   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
849   int aBufferSize = strlen(aBuffer)+1;
850   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1); 
851   isValidScript = true;
852   return aStreamFile._retn(); 
853 }
854
855
856 Engines::Salome_file_ptr 
857 Engines_Parallel_Component_i::setInputFileToService(const char* service_name, 
858                                                     const char* Salome_file_name) 
859 {
860   // Try to find the service, if it doesn't exist, we add it.
861   _Service_file_map_it = _Input_Service_file_map.find(service_name);
862   if (_Service_file_map_it ==  _Input_Service_file_map.end()) {
863     _t_Salome_file_map * _map = new _t_Salome_file_map();
864     _Input_Service_file_map[service_name] = _map;
865     _t_Proxy_Salome_file_map * _proxy_map = new _t_Proxy_Salome_file_map();
866     _Proxy_Input_Service_file_map[service_name] = _proxy_map;
867     _t_IOR_Proxy_Salome_file_map * _IOR_proxy_map = new _t_IOR_Proxy_Salome_file_map();
868     _IOR_Proxy_Input_Service_file_map[service_name] = _IOR_proxy_map;
869   }
870   _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
871   _t_Proxy_Salome_file_map * _proxy_map = _Proxy_Input_Service_file_map[service_name];
872   _t_IOR_Proxy_Salome_file_map * _IOR_proxy_map = _IOR_Proxy_Input_Service_file_map[service_name];
873   
874   pthread_mutex_lock(deploy_mutex);
875   std::string proxy_ior;
876
877   // Try to find the Salome_file ...
878   _Salome_file_map_it = _map->find(Salome_file_name);
879   if (_Salome_file_map_it ==  _map->end()) {
880
881     // We create a new PaCO++ object.
882     // He has the same configuration than
883     // his component
884
885     // Firstly, we have to create the proxy object
886     // of the Salome_file and transmit his
887     // reference to the other nodes.
888     Engines::Parallel_Salome_file_proxy_impl * proxy = NULL;
889     if (getMyRank() == 0) {
890       proxy = new Engines::Parallel_Salome_file_proxy_impl(CORBA::ORB::_duplicate(_orb),
891                                                            new paco_omni_fabrique());
892       proxy->copyGlobalContext(this); 
893       PaCO::PacoTopology_t serveur_topo;
894       serveur_topo.total = getTotalNode();
895       proxy->setTopology(serveur_topo);
896
897       // We register the CORBA objet into the POA
898       CORBA::Object_ptr proxy_ref = proxy->_this();
899
900       // We send the reference to all the nodes...
901       Engines::Parallel_Component_var component_proxy = 
902         Engines::Parallel_Component::_narrow(InterfaceParallel_impl::_proxy);
903       component_proxy->send_parallel_proxy_object(proxy_ref);
904
905       // Adding proxy into the map
906       (*_proxy_map)[Salome_file_name] = proxy;
907     }
908     else {
909       this->wait_parallel_object_proxy();
910     }
911
912     proxy_ior = this->get_parallel_proxy_object();
913     (*_IOR_proxy_map)[Salome_file_name] = proxy_ior;
914
915     // We register each node of the parallel Salome_file object
916     // into the proxy.
917     for (int i = 0; i < getTotalNode(); i++) {
918       if (i ==  getMyRank()) {
919         Parallel_Salome_file_i * servant = 
920           new Parallel_Salome_file_i(CORBA::ORB::_duplicate(_orb), 
921                                      proxy_ior.c_str(),
922                                      i);
923         servant->copyGlobalContext(this); 
924         
925         // We register the CORBA objet into the POA
926         servant->POA_PaCO::InterfaceParallel::_this();
927
928         // Register the servant
929         servant->deploy();
930
931         // Adding servant to the map
932         (*_map)[Salome_file_name] = servant;
933       }
934
935       _my_com->paco_barrier();
936       // start parallel object
937       if (getMyRank() == 0) {
938         proxy->start();
939         _my_com->paco_barrier();
940       }
941       else
942         _my_com->paco_barrier();
943     }
944     // Parallel_Salome_file is created and deployed
945     delete _proxy;
946     _proxy = NULL;
947   }
948
949   pthread_mutex_unlock(deploy_mutex);
950   proxy_ior = (*_IOR_proxy_map)[Salome_file_name];
951   CORBA::Object_ptr proxy_ref = _orb->string_to_object(proxy_ior.c_str());
952   return Engines::Salome_file::_narrow(proxy_ref);
953 }
954
955 Engines::Salome_file_ptr 
956 Engines_Parallel_Component_i::setOutputFileToService(const char* service_name, 
957                                                      const char* Salome_file_name) 
958 {
959   // Try to find the service, if it doesn't exist, we add it.
960   _Service_file_map_it = _Output_Service_file_map.find(service_name);
961   if (_Service_file_map_it ==  _Output_Service_file_map.end()) {
962     _t_Salome_file_map * _map = new _t_Salome_file_map();
963     _Output_Service_file_map[service_name] = _map;
964     _t_Proxy_Salome_file_map * _proxy_map = new _t_Proxy_Salome_file_map();
965     _Proxy_Output_Service_file_map[service_name] = _proxy_map;
966     _t_IOR_Proxy_Salome_file_map * _IOR_proxy_map = new _t_IOR_Proxy_Salome_file_map();
967     _IOR_Proxy_Output_Service_file_map[service_name] = _IOR_proxy_map;
968   }
969   _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
970   _t_Proxy_Salome_file_map * _proxy_map = _Proxy_Output_Service_file_map[service_name];
971   _t_IOR_Proxy_Salome_file_map * _IOR_proxy_map = _IOR_Proxy_Output_Service_file_map[service_name];
972   
973   pthread_mutex_lock(deploy_mutex);
974   std::string proxy_ior;
975
976   // Try to find the Salome_file ...
977   _Salome_file_map_it = _map->find(Salome_file_name);
978   Engines::Parallel_Salome_file_proxy_impl * proxy;
979   if (_Salome_file_map_it ==  _map->end()) {
980
981     // We create a new PaCO++ object.
982     // He has the same configuration than
983     // his component
984
985     // Firstly, we have to create the proxy object
986     // of the Salome_file and transmit his
987     // reference to the other nodes.
988     if (getMyRank() == 0) {
989         proxy = new Engines::Parallel_Salome_file_proxy_impl(CORBA::ORB::_duplicate(_orb),
990                                                              new paco_omni_fabrique());
991       proxy->copyGlobalContext(this); 
992       PaCO::PacoTopology_t serveur_topo;
993       serveur_topo.total = getTotalNode();
994       proxy->setTopology(serveur_topo);
995
996       // We register the CORBA objet into the POA
997       CORBA::Object_ptr proxy_ref = proxy->_this();
998
999       // We send the reference to all the nodes...
1000       Engines::Parallel_Component_var component_proxy = 
1001         Engines::Parallel_Component::_narrow(InterfaceParallel_impl::_proxy);
1002       component_proxy->send_parallel_proxy_object(proxy_ref);
1003
1004       // Adding proxy into the map
1005       (*_proxy_map)[Salome_file_name] = proxy;
1006     }
1007     else {
1008       this->wait_parallel_object_proxy();
1009     }
1010
1011     proxy_ior = this->get_parallel_proxy_object();
1012     (*_IOR_proxy_map)[Salome_file_name] = proxy_ior;
1013
1014     // We register each node of the parallel Salome_file object
1015     // into the proxy.
1016     for (int i = 0; i < getTotalNode(); i++) {
1017       if (i ==  getMyRank()) {
1018         Parallel_Salome_file_i * servant = 
1019           new Parallel_Salome_file_i(CORBA::ORB::_duplicate(_orb), 
1020                                      proxy_ior.c_str(),
1021                                      i);
1022         servant->copyGlobalContext(this); 
1023         
1024         // We register the CORBA objet into the POA
1025         servant->POA_PaCO::InterfaceParallel::_this();
1026
1027         // Register the servant
1028         servant->deploy();
1029
1030         // Adding servant to the map
1031         (*_map)[Salome_file_name] = servant;
1032       }
1033
1034       _my_com->paco_barrier();
1035       // start parallel object
1036       if (getMyRank() == 0) {
1037         proxy->start();
1038         _my_com->paco_barrier();
1039       }
1040       else
1041         _my_com->paco_barrier();
1042     }
1043
1044     // Parallel_Salome_file is created and deployed
1045     delete _proxy;
1046     _proxy = NULL;
1047   }
1048   pthread_mutex_unlock(deploy_mutex);
1049   proxy_ior = (*_IOR_proxy_map)[Salome_file_name];
1050   CORBA::Object_ptr proxy_ref = _orb->string_to_object(proxy_ior.c_str());
1051   return Engines::Salome_file::_narrow(proxy_ref);
1052 }
1053
1054 Engines::Salome_file_ptr 
1055 Engines_Parallel_Component_i::getInputFileToService(const char* service_name, 
1056                                                     const char* Salome_file_name) 
1057 {
1058   // Try to find the service, if it doesn't exist, we throw an exception.
1059   _Proxy_Service_file_map_it = _Proxy_Input_Service_file_map.find(service_name);
1060   if (_Proxy_Service_file_map_it ==  _Proxy_Input_Service_file_map.end()) {
1061     SALOME::ExceptionStruct es;
1062     es.type = SALOME::INTERNAL_ERROR;
1063     es.text = "service doesn't have salome files";
1064     throw SALOME::SALOME_Exception(es);
1065   }
1066   _t_Proxy_Salome_file_map * _map = _Proxy_Input_Service_file_map[service_name];
1067
1068   // Try to find the Salome_file ...
1069   _Proxy_Salome_file_map_it = _map->find(Salome_file_name);
1070   if (_Proxy_Salome_file_map_it ==  _map->end()) {
1071     SALOME::ExceptionStruct es;
1072     es.type = SALOME::INTERNAL_ERROR;
1073     es.text = "service doesn't have this Salome_file";
1074     throw SALOME::SALOME_Exception(es);
1075   }
1076
1077   // Client get the proxy object
1078   Engines::Parallel_Salome_file_proxy_impl * Sfile = (*_map)[Salome_file_name];
1079   return Sfile->_this();
1080 }
1081
1082 Engines::Salome_file_ptr 
1083 Engines_Parallel_Component_i::getOutputFileToService(const char* service_name, 
1084                                                      const char* Salome_file_name) 
1085 {
1086   // Try to find the service, if it doesn't exist, we throw an exception.
1087   _Proxy_Service_file_map_it = _Proxy_Output_Service_file_map.find(service_name);
1088   if (_Proxy_Service_file_map_it ==  _Proxy_Output_Service_file_map.end()) {
1089     SALOME::ExceptionStruct es;
1090     es.type = SALOME::INTERNAL_ERROR;
1091     es.text = "service doesn't have salome files";
1092     throw SALOME::SALOME_Exception(es);
1093   }
1094   _t_Proxy_Salome_file_map * _map = _Proxy_Output_Service_file_map[service_name];
1095
1096   // Try to find the Salome_file ...
1097   _Proxy_Salome_file_map_it = _map->find(Salome_file_name);
1098   if (_Proxy_Salome_file_map_it ==  _map->end()) {
1099     SALOME::ExceptionStruct es;
1100     es.type = SALOME::INTERNAL_ERROR;
1101     es.text = "service doesn't have this Salome_file";
1102     throw SALOME::SALOME_Exception(es);
1103   }
1104
1105   // Client get the proxy object
1106   Engines::Parallel_Salome_file_proxy_impl * Sfile = (*_map)[Salome_file_name];
1107   return Sfile->_this();
1108 }
1109
1110
1111 void 
1112 Engines_Parallel_Component_i::checkInputFilesToService(const char* service_name) 
1113 {
1114   // Try to find the service, if it doesn't exist, nothing to do.
1115   _Proxy_Service_file_map_it = _Proxy_Input_Service_file_map.find(service_name);
1116   if (_Proxy_Service_file_map_it !=  _Proxy_Input_Service_file_map.end()) {
1117     _t_Proxy_Salome_file_map * _proxy_map = _Proxy_Input_Service_file_map[service_name];
1118     _t_Proxy_Salome_file_map::iterator begin = _proxy_map->begin();
1119     _t_Proxy_Salome_file_map::iterator end = _proxy_map->end();
1120
1121     for(;begin!=end;begin++) {
1122       Engines::Parallel_Salome_file_proxy_impl * file = begin->second;
1123       std::string file_port_name = begin->first;
1124       configureSalome_file(service_name, file_port_name, file);
1125       file->recvFiles();
1126     }
1127   }
1128 }
1129
1130 void 
1131 Engines_Parallel_Component_i::checkOutputFilesToService(const char* service_name) 
1132 {
1133   // Try to find the service, if it doesn't exist, nothing to do.
1134   _Proxy_Service_file_map_it = _Proxy_Output_Service_file_map.find(service_name);
1135   if (_Proxy_Service_file_map_it !=  _Proxy_Output_Service_file_map.end()) {
1136     _t_Proxy_Salome_file_map * _map = _Proxy_Output_Service_file_map[service_name];
1137     _t_Proxy_Salome_file_map::iterator begin = _map->begin();
1138     _t_Proxy_Salome_file_map::iterator end = _map->end();
1139
1140     for(;begin!=end;begin++) {
1141       Engines::Parallel_Salome_file_proxy_impl * file = begin->second;
1142       std::string file_port_name = begin->first;
1143       configureSalome_file(service_name, file_port_name, file);
1144       file->recvFiles();
1145     }
1146   }
1147
1148 }
1149
1150 //=============================================================================
1151 /*! 
1152  *  C++ method: Used by the Parallel Component to deploy a Parallel Salome_file
1153  */ 
1154 //=============================================================================
1155 void 
1156 Engines_Parallel_Component_i::send_parallel_proxy_object(CORBA::Object_ptr proxy_ref) {
1157   _proxy = _orb->object_to_string(proxy_ref);
1158 }
1159
1160 //=============================================================================
1161 /*! 
1162  *  C++ method: Used by the Parallel Component to deploy a Parallel Salome_file
1163  */ 
1164 //=============================================================================
1165 void 
1166 Engines_Parallel_Component_i::wait_parallel_object_proxy() {
1167   char * proxy = NULL;
1168   proxy =  get_parallel_proxy_object();
1169   while(proxy == NULL)
1170   {
1171     sleep(1);
1172     proxy = get_parallel_proxy_object();
1173   }
1174 }
1175
1176 //=============================================================================
1177 /*! 
1178  *  C++ method: Used by the Parallel Component to deploy a Parallel Salome_file
1179  */ 
1180 //=============================================================================
1181 char * 
1182 Engines_Parallel_Component_i::get_parallel_proxy_object() {
1183   return _proxy;
1184 }
1185
1186
1187 //=============================================================================
1188 /*! 
1189  *  C++ method: used to configure the Salome_file into the runtime.
1190  *  \param service_name name of the service that use this Salome_file
1191  *  \param file_port_name name of the Salome_file
1192  *  \param file Parallel Salome_file C++ object
1193  */
1194 //=============================================================================
1195 void
1196 Engines_Parallel_Component_i::configureSalome_file(std::string service_name,
1197                                                    std::string file_port_name,
1198                                                    Engines::Parallel_Salome_file_proxy_impl * file) 
1199 {
1200   // By default this method does nothing
1201 }