1 // Copyright (C) 2016-2022 CEA/DEN, 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
20 #include "VisualizationComponent.hxx"
24 #include <SALOME_NamingService.hxx>
25 #include <Utils_SALOME_Exception.hxx>
26 #include "Utils_CorbaException.hxx"
39 #include "MEDCouplingFieldDoubleClient.hxx"
40 #include "ParaMEDCouplingFieldDoubleServant.hxx"
41 #include "omniORB4/poa.h"
48 //! Constructor for component "VisualizationComponent" instance
52 VisualizationComponent_i::VisualizationComponent_i(CORBA::ORB_ptr orb,
53 PortableServer::POA_ptr poa,
54 PortableServer::ObjectId * contId,
55 const char *instanceName,
56 const char *interfaceName,
58 : Engines_Component_i(orb, poa, contId, instanceName, interfaceName,
62 _id = _poa->activate_object(_thisObj);
65 VisualizationComponent_i::VisualizationComponent_i(CORBA::ORB_ptr orb,
66 PortableServer::POA_ptr poa,
67 Engines::Container_ptr container,
68 const char *instanceName,
69 const char *interfaceName,
71 : Engines_Component_i(orb, poa, container, instanceName, interfaceName,
75 _id = _poa->activate_object(_thisObj);
78 //! Destructor for component "VisualizationComponent" instance
79 VisualizationComponent_i::~VisualizationComponent_i()
84 void * th_Visualize(void * s)
86 std::ostringstream msg;
87 exception_st *est = new exception_st;
88 est->exception = false;
90 thread_Visualize_struct *st = (thread_Visualize_struct *)s;
95 PARAVIS_ORB::VisualizationComponent_var compo = PARAVIS_ORB::VisualizationComponent::_narrow((*(st->tior))[st->ip]);
96 compo->Visualize(st->field,st->path_python_file);
98 catch(const SALOME::SALOME_Exception &ex)
100 est->exception = true;
101 est->msg = ex.details.text;
103 catch(const CORBA::Exception &ex)
105 est->exception = true;
106 msg << "CORBA::Exception: " << ex;
107 est->msg = msg.str();
114 void VisualizationComponent_i::Visualize(SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_ptr field,const char* path_python_file)
116 beginService("VisualizationComponent_i::Visualize");
123 // Run the service in every MPI process
126 th = new pthread_t[_nbproc];
127 for(int ip=1;ip<_nbproc;ip++)
129 thread_Visualize_struct *st = new thread_Visualize_struct;
133 st->path_python_file = path_python_file;
134 pthread_create(&(th[ip]),NULL,th_Visualize,(void*)st);
140 const MEDCoupling::MEDCouplingFieldDouble * local_field(NULL);
141 int nb_fields = field->tior()->length();
142 if(nb_fields == _nbproc)
144 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_ptr local_corba_field =
145 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface::_narrow((*(field->tior()))[_numproc]);
148 PortableServer::ServantBase *ret;
150 ret=PortableServer::POA::_the_root_poa()->reference_to_servant(local_corba_field);
151 std::cerr << "Servant succeeded!" << std::endl;
152 MEDCoupling::ParaMEDCouplingFieldDoubleServant* servant_field =
153 dynamic_cast<MEDCoupling::ParaMEDCouplingFieldDoubleServant*>(ret);
154 if(servant_field != NULL)
156 std::cerr << "In-situ configuration!" << std::endl;
157 // same container, same mpi proc, use the pointer directly.
158 local_field = servant_field->getPointer();
163 // different container - need to make a copy of the field.
165 std::cerr << "Co-processing configuration!" << std::endl;
166 local_field = MEDCoupling::MEDCouplingFieldDoubleClient::New(local_corba_field);
169 else if(nb_fields < _nbproc)
171 if(_numproc < nb_fields)
173 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_ptr local_corba_field =
174 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface::_narrow((*(field->tior()))[_numproc]);
175 local_field = MEDCoupling::MEDCouplingFieldDoubleClient::New(local_corba_field);
178 else //nb_fields > _nbproc
180 int q = nb_fields / _nbproc; // int division
181 int r = nb_fields - q * _nbproc;
186 // get one more field to process
187 start = _numproc * (q + 1);
192 start = r * (q + 1) + (_numproc - r) * q;
196 std::cerr << "Proc n° " << _numproc << ". Merge fields from " << start << " to " << end << std::endl;
198 std::vector<const MEDCoupling::MEDCouplingFieldDouble *> fieldsToProcess;
199 for(int i = start; i < end; i++)
201 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface_ptr local_corba_field =
202 SALOME_MED::ParaMEDCouplingFieldDoubleCorbaInterface::_narrow((*(field->tior()))[i]);
203 fieldsToProcess.push_back(MEDCoupling::MEDCouplingFieldDoubleClient::New(local_corba_field));
206 local_field = MEDCoupling::MEDCouplingFieldDouble::MergeFields(fieldsToProcess);
210 v.run(const_cast<MEDCoupling::MEDCouplingFieldDouble*>(local_field), path_python_file);
215 for(int ip=1;ip<_nbproc;ip++)
217 pthread_join(th[ip],&ret_th);
218 est = (exception_st*)ret_th;
221 std::ostringstream msg;
222 msg << "[" << ip << "] " << est->msg;
225 THROW_SALOME_CORBA_EXCEPTION(msg.str().c_str(),SALOME::INTERNAL_ERROR);
232 catch ( const SALOME_Exception & ex)
234 THROW_SALOME_CORBA_EXCEPTION(CORBA::string_dup(ex.what()), SALOME::INTERNAL_ERROR);
236 catch ( const SALOME::SALOME_Exception & ex)
240 catch ( const std::exception& ex)
242 THROW_SALOME_CORBA_EXCEPTION(CORBA::string_dup(ex.what()), SALOME::INTERNAL_ERROR);
246 THROW_SALOME_CORBA_EXCEPTION("unknown exception", SALOME::INTERNAL_ERROR);
248 endService("VisualizationComponent_i::Visualize");
255 PortableServer::ObjectId * VisualizationComponentEngine_factory( CORBA::ORB_ptr orb,
256 PortableServer::POA_ptr poa,
257 PortableServer::ObjectId * contId,
258 const char *instanceName,
259 const char *interfaceName)
261 MESSAGE("PortableServer::ObjectId * VisualizationComponentEngine_factory()");
262 int is_mpi_container;
266 MPI_Initialized(&is_mpi_container);
267 if (!is_mpi_container)
271 MPI_Init(&argc, &argv);
274 MPI_Comm_rank( MPI_COMM_WORLD, &numproc );
275 regist = ( numproc == 0 );
276 VisualizationComponent_i * myEngine = new VisualizationComponent_i(orb, poa, contId, instanceName, interfaceName, regist);
277 return myEngine->getId() ;