2 #include "VisualizationComponent.hxx"
6 #include <SALOME_NamingService.hxx>
7 #include <Utils_SALOME_Exception.hxx>
8 #include "Utils_CorbaException.hxx"
21 #include "MEDCouplingFieldDoubleClient.hxx"
22 #include "ParaMEDCouplingFieldDoubleServant.hxx"
23 #include "omniORB4/poa.h"
30 //! Constructor for component "VisualizationComponent" instance
34 VisualizationComponent_i::VisualizationComponent_i(CORBA::ORB_ptr orb,
35 PortableServer::POA_ptr poa,
36 PortableServer::ObjectId * contId,
37 const char *instanceName,
38 const char *interfaceName,
40 : Engines_Component_i(orb, poa, contId, instanceName, interfaceName,
44 _id = _poa->activate_object(_thisObj);
47 VisualizationComponent_i::VisualizationComponent_i(CORBA::ORB_ptr orb,
48 PortableServer::POA_ptr poa,
49 Engines::Container_ptr container,
50 const char *instanceName,
51 const char *interfaceName,
53 : Engines_Component_i(orb, poa, container, instanceName, interfaceName,
57 _id = _poa->activate_object(_thisObj);
60 //! Destructor for component "VisualizationComponent" instance
61 VisualizationComponent_i::~VisualizationComponent_i()
66 void * th_Visualize(void * s)
68 std::ostringstream msg;
69 exception_st *est = new exception_st;
70 est->exception = false;
72 thread_Visualize_struct *st = (thread_Visualize_struct *)s;
77 PARAVIS_ORB::VisualizationComponent_var compo = PARAVIS_ORB::VisualizationComponent::_narrow((*(st->tior))[st->ip]);
78 compo->Visualize(st->field,st->path_python_file);
80 catch(const SALOME::SALOME_Exception &ex)
82 est->exception = true;
83 est->msg = ex.details.text;
85 catch(const CORBA::Exception &ex)
87 est->exception = true;
88 msg << "CORBA::Exception: " << ex;
96 void VisualizationComponent_i::Visualize(SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_ptr field,const char* path_python_file)
98 beginService("VisualizationComponent_i::Visualize");
105 // Run the service in every MPI process
108 th = new pthread_t[_nbproc];
109 for(int ip=1;ip<_nbproc;ip++)
111 thread_Visualize_struct *st = new thread_Visualize_struct;
115 st->path_python_file = path_python_file;
116 pthread_create(&(th[ip]),NULL,th_Visualize,(void*)st);
122 const MEDCoupling::MEDCouplingFieldDouble * local_field(NULL);
123 int nb_fields = field->tior()->length();
124 if(nb_fields == _nbproc)
126 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_ptr local_corba_field =
127 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface::_narrow((*(field->tior()))[_numproc]);
130 PortableServer::ServantBase *ret;
132 ret=PortableServer::POA::_the_root_poa()->reference_to_servant(local_corba_field);
133 std::cerr << "Servant succeeded!" << std::endl;
134 MEDCoupling::ParaMEDCouplingFieldDoubleServant* servant_field =
135 dynamic_cast<MEDCoupling::ParaMEDCouplingFieldDoubleServant*>(ret);
136 if(servant_field != NULL)
138 std::cerr << "In-situ configuration!" << std::endl;
139 // same container, same mpi proc, use the pointer directly.
140 local_field = servant_field->getPointer();
145 // different container - need to make a copy of the field.
147 std::cerr << "Co-processing configuration!" << std::endl;
148 local_field = MEDCoupling::MEDCouplingFieldDoubleClient::New(local_corba_field);
151 else if(nb_fields < _nbproc)
153 if(_numproc < nb_fields)
155 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_ptr local_corba_field =
156 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface::_narrow((*(field->tior()))[_numproc]);
157 local_field = MEDCoupling::MEDCouplingFieldDoubleClient::New(local_corba_field);
160 else //nb_fields > _nbproc
162 int q = nb_fields / _nbproc; // int division
163 int r = nb_fields - q * _nbproc;
168 // get one more field to process
169 start = _numproc * (q + 1);
174 start = r * (q + 1) + (_numproc - r) * q;
178 std::cerr << "Proc n° " << _numproc << ". Merge fields from " << start << " to " << end << std::endl;
180 std::vector<const MEDCoupling::MEDCouplingFieldDouble *> fieldsToProcess;
181 for(int i = start; i < end; i++)
183 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_ptr local_corba_field =
184 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface::_narrow((*(field->tior()))[i]);
185 fieldsToProcess.push_back(MEDCoupling::MEDCouplingFieldDoubleClient::New(local_corba_field));
188 local_field = MEDCoupling::MEDCouplingFieldDouble::MergeFields(fieldsToProcess);
192 v.run(const_cast<MEDCoupling::MEDCouplingFieldDouble*>(local_field), path_python_file);
197 for(int ip=1;ip<_nbproc;ip++)
199 pthread_join(th[ip],&ret_th);
200 est = (exception_st*)ret_th;
203 std::ostringstream msg;
204 msg << "[" << ip << "] " << est->msg;
207 THROW_SALOME_CORBA_EXCEPTION(msg.str().c_str(),SALOME::INTERNAL_ERROR);
214 catch ( const SALOME_Exception & ex)
216 THROW_SALOME_CORBA_EXCEPTION(CORBA::string_dup(ex.what()), SALOME::INTERNAL_ERROR);
218 catch ( const SALOME::SALOME_Exception & ex)
222 catch ( const std::exception& ex)
224 THROW_SALOME_CORBA_EXCEPTION(CORBA::string_dup(ex.what()), SALOME::INTERNAL_ERROR);
228 THROW_SALOME_CORBA_EXCEPTION("unknown exception", SALOME::INTERNAL_ERROR);
230 endService("VisualizationComponent_i::Visualize");
237 PortableServer::ObjectId * VisualizationComponentEngine_factory( CORBA::ORB_ptr orb,
238 PortableServer::POA_ptr poa,
239 PortableServer::ObjectId * contId,
240 const char *instanceName,
241 const char *interfaceName)
243 MESSAGE("PortableServer::ObjectId * VisualizationComponentEngine_factory()");
244 int is_mpi_container;
248 MPI_Initialized(&is_mpi_container);
249 if (!is_mpi_container)
253 MPI_Init(&argc, &argv);
256 MPI_Comm_rank( MPI_COMM_WORLD, &numproc );
257 regist = ( numproc == 0 );
258 VisualizationComponent_i * myEngine = new VisualizationComponent_i(orb, poa, contId, instanceName, interfaceName, regist);
259 return myEngine->getId() ;