source: XIOS/trunk/src/node/domain.cpp @ 501

Last change on this file since 501 was 501, checked in by ymipsl, 10 years ago

Add licence copyright to all file ond directory src using the command :
svn propset -R copyright -F header_licence src

XIOS is now officialy under CeCILL licence

YM

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 28.5 KB
Line 
1#include "domain.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6
7#include "xmlioserver_spl.hpp"
8#include "event_client.hpp"
9#include "event_server.hpp"
10#include "buffer_in.hpp"
11#include "message.hpp"
12#include "type.hpp"
13#include "context.hpp"
14#include "context_client.hpp"
15#include "array_new.hpp"
16
17namespace xios {
18   
19   /// ////////////////////// Définitions ////////////////////// ///
20
21   CDomain::CDomain(void)
22      : CObjectTemplate<CDomain>(), CDomainAttributes()
23      , isChecked(false),  relFiles()
24   { /* Ne rien faire de plus */ }
25
26   CDomain::CDomain(const StdString & id)
27      : CObjectTemplate<CDomain>(id), CDomainAttributes()
28      , isChecked(false), relFiles()
29         { /* Ne rien faire de plus */ }
30
31   CDomain::~CDomain(void)
32   { 
33   }
34
35   ///---------------------------------------------------------------
36
37   const std::set<StdString> & CDomain::getRelFiles(void) const
38   {
39      return (this->relFiles); 
40   }
41
42   //----------------------------------------------------------------
43   
44   bool CDomain::hasZoom(void) const
45   {
46      return ((this->zoom_ni.getValue() != this->ni_glo.getValue()) && 
47              (this->zoom_nj.getValue() != this->nj_glo.getValue()));
48   }
49   
50   //----------------------------------------------------------------
51   
52   bool CDomain::isEmpty(void) const
53   {
54      return ((this->zoom_ni_srv == 0) || 
55              (this->zoom_nj_srv == 0));
56   }
57
58   //----------------------------------------------------------------
59
60   bool CDomain::IsWritten(const StdString & filename) const
61   {
62      return (this->relFiles.find(filename) != this->relFiles.end());
63   }
64
65   //----------------------------------------------------------------
66
67   void CDomain::addRelFile(const StdString & filename)
68   {
69      this->relFiles.insert(filename);
70   }
71
72
73   StdString CDomain::GetName(void)   { return (StdString("domain")); }
74   StdString CDomain::GetDefName(void){ return (CDomain::GetName()); }
75   ENodeType CDomain::GetType(void)   { return (eDomain); }
76
77   //----------------------------------------------------------------
78
79   void CDomain::checkDomain(void)
80   {
81      if (!type.isEmpty() && type==type_attr::unstructured)
82      {
83         if (ni_glo.isEmpty() || ni_glo <= 0 )
84         {
85            ERROR("CDomain::checkAttributes(void)",
86               << "[ Id = " << this->getId() << " ] "
87               << "The global domain is badly defined,"
88               << " check the \'ni_glo\'  value !") 
89         }
90         nj_glo=ni_glo ;
91         ni_glo=1 ;
92         if (!ni.isEmpty()) nj=ni ;
93         if (!ibegin.isEmpty()) jbegin=ibegin ;
94         if (!iend.isEmpty()) jend=iend ;
95         if (!i_index.isEmpty())
96         {
97          j_index.resize(1,nj) ;
98          for(int i=0;i<ni;i++) j_index(0,i)=i_index(i,0) ;
99          i_index.resize(1,nj) ;
100          for(int j=0;j<nj;j++) i_index(0,j)=0 ;
101         }
102
103         if (!mask.isEmpty())
104         {
105          CArray<int,2> mask_tmp(nj,1) ;
106          mask_tmp = mask ;
107          mask.resize(1,nj) ;
108          for(int j=0;j<nj;j++) mask(0,j)=mask_tmp(j,0) ;
109         }
110         ni=1 ;
111         ibegin=1 ;
112         iend=1 ;
113         
114      }
115      else if ((ni_glo.isEmpty() || ni_glo.getValue() <= 0 ) ||
116          (nj_glo.isEmpty() || nj_glo.getValue() <= 0 ))
117      {
118         ERROR("CDomain::checkAttributes(void)",
119               << "[ Id = " << this->getId() << " ] "
120               << "The global domain is badly defined,"
121               << " check the \'ni_glo\' et \'nj_glo\' values !") 
122      }
123      checkLocalIDomain() ;
124      checkLocalJDomain() ;
125     
126     if (i_index.isEmpty())
127     {
128       i_index.resize(ni,nj) ;
129       for(int j=0;j<nj;j++)
130         for(int i=0;i<ni;i++) i_index(i,j)=i ;   
131     }
132     
133     if (j_index.isEmpty())
134     {
135        j_index.resize(ni,nj) ;
136        for(int j=0;j<nj;j++)
137         for(int i=0;i<ni;i++) j_index(i,j)=j ;   
138     }
139 
140   }
141
142   //----------------------------------------------------------------
143
144   void CDomain::checkLocalIDomain(void)
145   {
146      if (!ni.isEmpty() && !ibegin.isEmpty() && iend.isEmpty())
147         iend.setValue(ibegin.getValue() + ni.getValue() - 1) ;
148
149      else if (!ni.isEmpty() && !iend.isEmpty()   && ibegin.isEmpty())
150         ibegin.setValue( - ni.getValue() + iend.getValue() + 1) ;
151
152      else if (!ibegin.isEmpty() && !iend.isEmpty() && ni.isEmpty())
153         ni.setValue(iend.getValue() - ibegin.getValue() + 1) ;
154
155      else if (!ibegin.isEmpty() && !iend.isEmpty() && !ni.isEmpty() )
156      {
157         if (iend.getValue() != ibegin.getValue() + ni.getValue() - 1)
158           ERROR("CDomain::checkAttributes(void)",
159                 << "The domain si wrong defined,"
160                 << " iend is different of (ibegin + ni - 1) !") ;
161      }
162      else
163      {
164         ERROR("CDomain::checkAttributes(void)",
165               << "The domain is wrong defined,"
166               << " 2 value from \'iend\', \'ibegin\', \'ni\'  must be defined !") ;
167      }
168
169
170      if (ni.getValue() < 0 || ibegin.getValue() > iend.getValue() ||
171          ibegin.getValue() < 1 || iend.getValue() > ni_glo.getValue())
172          {
173
174         ERROR("CDomain::checkAttributes(void)",
175               << "[ Id = " << this->getId() << " ] "
176               << "Local domain is wrong defined,"
177               << " Check the value : ni, ni_glo, ibegin, iend") ;
178        }
179   }
180
181   //----------------------------------------------------------------
182
183   void CDomain::checkLocalJDomain(void)
184   {
185      if (!nj.isEmpty() && !jbegin.isEmpty() && jend.isEmpty())
186         jend.setValue(jbegin.getValue() + nj.getValue() - 1) ;
187
188      else if (!nj.isEmpty() && !jend.isEmpty() && jbegin.isEmpty())
189         jbegin.setValue( - nj.getValue() + jend.getValue() + 1) ;
190
191      else if (!jbegin.isEmpty() && !jend.isEmpty() && nj.isEmpty())
192         nj.setValue(jend.getValue() - jbegin.getValue() + 1) ;
193
194      else if (!jbegin.isEmpty() && !jend.isEmpty() && !nj.isEmpty() )
195      {
196          if  (jend.getValue() != jbegin.getValue() + nj.getValue() - 1)
197             ERROR("CDomain::checkAttributes(void)",
198                 << "The domain is wrong defined,"
199                 << " iend is different of (jbegin + nj - 1) !") ;
200      }
201      else
202      {
203         ERROR("CDomain::checkAttributes(void)",
204               << "The domain is wrong defined,"
205               << " 2 values from  jend, jbegin, nj  must be defined !") ;
206      }
207
208      if (nj.getValue() < 0 || jbegin.getValue() > jend.getValue() ||
209          jbegin.getValue() < 1 || jend.getValue() > nj_glo.getValue())
210         ERROR("CDomain::checkAttributes(void)",
211               << "Domain is wrong defined,"
212               << " Check the values : nj, nj_glo, jbegin, jend") ;
213               
214     ibegin_client=ibegin ; iend_client=iend ; ni_client=ni ;
215     jbegin_client=jbegin ; jend_client=jend ; nj_client=nj ;
216   }
217
218   //----------------------------------------------------------------
219
220   void CDomain::checkMask(void)
221   {
222      using namespace std;
223     
224      int ibegin_mask = 0,
225          jbegin_mask = 0,
226          iend_mask = iend.getValue() - ibegin.getValue(),
227          jend_mask = jend.getValue() - jbegin.getValue();
228     
229      if (!zoom_ibegin.isEmpty())
230      {
231         int zoom_iend = zoom_ibegin.getValue() + zoom_ni.getValue() - 1;
232         int zoom_jend = zoom_jbegin.getValue() + zoom_nj.getValue() - 1;
233         
234         ibegin_mask = max (ibegin.getValue(), zoom_ibegin.getValue());
235         jbegin_mask = max (jbegin.getValue(), zoom_jbegin.getValue());
236         iend_mask   = min (iend.getValue(), zoom_iend);
237         jend_mask   = min (jend.getValue(), zoom_jend);
238                 
239         ibegin_mask -= ibegin.getValue();
240         jbegin_mask -= jbegin.getValue();
241         iend_mask   -= ibegin.getValue();
242         jend_mask   -= jbegin.getValue();
243      }
244     
245
246      if (!mask.isEmpty())
247      {
248         if ((mask.extent(0) != ni) ||
249             (mask.extent(1) != nj))
250            ERROR("CDomain::checkAttributes(void)",
251                  <<"the mask has not the same size than the local domain"<<endl
252                   <<"Local size is "<<ni<<"x"<<nj<<endl
253                  <<"Mask size is "<<mask.extent(0)<<"x"<<mask.extent(1));                 
254         for (int i = 0; i < ni; i++)
255         {
256            for (int j = 0; j < nj; j++)
257            {
258               if (i < ibegin_mask && i > iend_mask &&
259                   j < jbegin_mask && j > jend_mask )
260                     mask(i,j) = false;
261            }
262         }
263      }
264      else // (!mask.hasValue())
265      { // Si aucun masque n'est défini,
266        // on en crée un nouveau qui valide l'intégralité du domaine.
267         mask.resize(ni,nj) ;
268         for (int i = 0; i < ni.getValue(); i++)
269         {
270            for (int j = 0; j < nj.getValue(); j++)
271            {
272               if (i >= ibegin_mask && i <= iend_mask &&
273                   j >= jbegin_mask && j <= jend_mask )
274                     mask(i,j) = true;
275               else  mask(i,j) = false;
276            }
277         }
278      }
279   }
280
281
282   //----------------------------------------------------------------
283
284   void CDomain::checkDomainData(void)
285   {     
286      if (!data_dim.isEmpty() &&
287         !(data_dim.getValue() == 1 || data_dim.getValue() == 2))
288      {
289         ERROR("CDomain::checkAttributes(void)",
290               << "Data dimension incompatible (must be 1 or 2) !") ;
291      }
292      else if (data_dim.isEmpty())
293      {
294         ERROR("CDomain::checkAttributes(void)",
295               << "Data dimension undefined !") ;
296      }
297
298      if (data_ibegin.isEmpty())
299         data_ibegin.setValue(0) ;
300      if (data_jbegin.isEmpty() && (data_dim.getValue() == 2))
301         data_jbegin.setValue(0) ;
302
303      if (!data_ni.isEmpty() && (data_ni.getValue() <= 0))
304      {
305         ERROR("CDomain::checkAttributes(void)",
306               << "Data dimension is negative (data_ni).") ;
307      }
308      else if (data_ni.isEmpty())
309      {
310         data_ni.setValue((data_dim.getValue() == 1)
311                           ? (ni.getValue() * nj.getValue())
312                           : ni.getValue());
313      }
314
315      if (data_dim.getValue() == 2)
316      {
317         if (!data_nj.isEmpty() && (data_nj.getValue() <= 0) )
318         {
319            ERROR("CDomain::checkAttributes(void)",
320                  << "Data dimension is negative (data_nj).") ;
321         }
322         else if (data_nj.isEmpty())
323            data_nj.setValue(nj.getValue()) ;
324      }
325
326   }
327
328   //----------------------------------------------------------------
329
330   void CDomain::checkCompression(void)
331   {
332      if (!data_i_index.isEmpty())
333      {
334         int ssize = data_i_index.numElements();
335         if (!data_n_index.isEmpty() &&
336            (data_n_index.getValue() != ssize))
337         {
338            ERROR("CDomain::checkAttributes(void)",
339                  <<"Dimension data_i_index incompatible with data_n_index.") ;
340         }
341         else if (data_n_index.isEmpty())
342            data_n_index.setValue(ssize) ;
343
344         if (data_dim.getValue() == 2)
345         {
346            if (!data_j_index.isEmpty() &&
347               (data_j_index.numElements() != data_i_index.numElements()))
348            {
349               ERROR("CDomain::checkAttributes(void)",
350                     <<"Dimension data_j_index incompatible with data_i_index.") ;
351            }
352            else if (data_j_index.isEmpty())
353            {
354               ERROR("CDomain::checkAttributes(void)",
355                     <<"data_j_index must be defined !") ;
356            }
357         }
358      }
359      else
360      {
361         if (!data_n_index.isEmpty() ||
362            ((data_dim.getValue() == 2) && (!data_j_index.isEmpty())))
363            ERROR("CDomain::checkAttributes(void)", << "data_i_index undefined") ;
364      }
365
366      if (data_n_index.isEmpty())
367      { // -> bloc re-vérifié OK
368         if (data_dim.getValue() == 1)
369         {
370            const int dni = data_ni.getValue();
371            data_i_index.resize(dni) ;
372            data_n_index.setValue(dni);
373            for (int i = 0; i < dni; i++) data_i_index(i) = i+1 ;
374         }
375         else   // (data_dim == 2)
376         {
377            const int dni = data_ni.getValue() * data_nj.getValue();
378            data_i_index.resize(dni) ;
379            data_j_index.resize(dni) ;
380           
381            data_n_index.setValue(dni);
382           
383            for(int count = 0, j = 0; j  < data_nj.getValue(); j++)
384            {
385               for(int i = 0; i < data_ni.getValue(); i++, count++)
386               {
387                  data_i_index(count) = i+1 ;
388                  data_j_index(count) = j+1 ;
389               }
390            }
391         }
392      }
393   }
394
395   //----------------------------------------------------------------
396   
397   void CDomain::completeLonLatClient(void)
398   {
399      int i,j,k ;
400      CArray<double,1> lonvalue_temp(ni*nj) ;
401      CArray<double,1> latvalue_temp(ni*nj) ;
402      CArray<double,2> bounds_lon_temp(nvertex,ni*nj) ;
403      CArray<double,2> bounds_lat_temp(nvertex,ni*nj) ;
404     
405      if (type.isEmpty())
406      {
407        if ( lonvalue.numElements() == ni*nj && latvalue.numElements() == ni*nj ) 
408        {
409          type.setValue(type_attr::curvilinear) ;
410          isCurvilinear=true ;
411        }
412        else if ( lonvalue.numElements() == ni && latvalue.numElements() == nj ) 
413        {
414          type.setValue(type_attr::regular) ;
415          isCurvilinear=false ;
416        }
417        else ERROR("void CDomain::completeLonLatClient(void)",<<"the grid is nor curvilinear, nor cartesian, because the size of longitude and latitude array is not coherent with the domain size"<<endl
418                                                              <<"lonvalue size = " << lonvalue.numElements() << "different of ni or ni*nj"<<endl
419                                                              <<"latvalue size = " << latvalue.numElements() << "different of nj or ni*nj" ) ;
420      }
421      if (type==type_attr::curvilinear || type==type_attr::unstructured)
422      {
423        lonvalue_temp=lonvalue ;
424        latvalue_temp=latvalue ;
425        if (hasBounds) bounds_lon_temp=bounds_lon ;
426        if (hasBounds) bounds_lat_temp=bounds_lat ;
427      }
428      else
429      {
430        for(j=0;j<nj;j++)
431          for(i=0;i<ni;i++) 
432          {
433            k=j*ni+i ;
434            lonvalue_temp(k)=lonvalue(i) ;
435            latvalue_temp(k)=latvalue(j) ;
436            if (hasBounds)
437            {
438              for(int n=0;n<nvertex;n++) 
439              {
440                bounds_lon_temp(n,k)=bounds_lon(n,i) ;
441                bounds_lat_temp(n,k)=bounds_lat(n,j) ;
442              }
443            }
444          }
445      }
446         
447      StdSize dm = zoom_ni_client * zoom_nj_client;
448
449      lonvalue.resize(dm);
450      latvalue.resize(dm);
451         
452      for (int i = 0; i < zoom_ni_client; i++)
453      {
454        for (int j = 0; j < zoom_nj_client; j++)
455        {
456          lonvalue(i + j * zoom_ni_client) = lonvalue_temp( (i + zoom_ibegin_client-ibegin) + (j + zoom_jbegin_client -jbegin)*ni ); 
457          latvalue(i + j * zoom_ni_client) = latvalue_temp( (i + zoom_ibegin_client - ibegin)+(j + zoom_jbegin_client - jbegin)*ni );
458          if (hasBounds)
459          {
460            for(int n=0;n<nvertex;n++) 
461            {
462              bounds_lon(n,i + j * zoom_ni_client) = bounds_lon_temp( n, (i + zoom_ibegin_client - ibegin) + (j + zoom_jbegin_client -jbegin)*ni ); 
463              bounds_lat(n,i + j * zoom_ni_client) = bounds_lat_temp( n, (i + zoom_ibegin_client - ibegin)+(j + zoom_jbegin_client -jbegin)*ni );
464            }
465          }
466        }
467      }
468    }
469 
470
471   //----------------------------------------------------------------
472
473   void CDomain::checkZoom(void)
474   {
475      // Résolution et vérification des données globales de zoom.
476      if (!this->zoom_ni.isEmpty() || !this->zoom_nj.isEmpty() ||
477          !this->zoom_ibegin.isEmpty() || !this->zoom_jbegin.isEmpty())
478      {
479         if (this->zoom_ni.isEmpty()     || this->zoom_nj.isEmpty() ||
480             this->zoom_ibegin.isEmpty() || this->zoom_jbegin.isEmpty())
481         {
482            ERROR("CDomain::checkZoom(void)",
483                  <<"if one of zoom attributes is defined then all zoom attributes must be defined") ;
484         }
485         else
486         {
487            int zoom_iend = zoom_ibegin + zoom_ni - 1;
488            int zoom_jend = zoom_jbegin + zoom_nj - 1;
489               
490            if (zoom_ibegin < 1  || zoom_jbegin < 1 || zoom_iend > ni_glo || zoom_jend > nj_glo)
491               ERROR("CDomain::checkZoom(void)",
492                     << "Zoom is wrong defined,"
493                     << " Check the values : zoom_ni, zoom_nj, zoom_ibegin, zoom_jbegin") ;
494         }
495      }
496      else
497      {
498         zoom_ni = ni_glo; 
499         zoom_nj = nj_glo;
500         zoom_ibegin = 1;
501         zoom_jbegin = 1;
502      }
503     
504      // compute client zoom indices
505
506      int zoom_iend=zoom_ibegin+zoom_ni-1 ;
507      zoom_ibegin_client = ibegin_client > zoom_ibegin ? ibegin_client : zoom_ibegin ;
508      zoom_iend_client = iend_client < zoom_iend ? iend_client : zoom_iend ;
509      zoom_ni_client=zoom_iend_client-zoom_ibegin_client+1 ;
510      if (zoom_ni_client<0) zoom_ni_client=0 ;
511
512   
513      int zoom_jend=zoom_jbegin+zoom_nj-1 ;
514      zoom_jbegin_client = jbegin_client > zoom_jbegin ? jbegin_client : zoom_jbegin ;
515      zoom_jend_client = jend_client < zoom_jend ? jend_client : zoom_jend ;
516      zoom_nj_client=zoom_jend_client-zoom_jbegin_client+1 ;
517      if (zoom_nj_client<0) zoom_nj_client=0 ;
518
519   }
520   
521   void CDomain::checkBounds(void)
522   {
523     if (!nvertex.isEmpty() && !bounds_lon.isEmpty() && !bounds_lat.isEmpty())
524     {
525       hasBounds=true ;
526       
527     }
528     else 
529     {
530       hasBounds=false;
531       nvertex=0 ;
532     }
533   }
534           
535   //----------------------------------------------------------------
536
537   void CDomain::checkAttributes(void)
538   {
539      if (this->isChecked) return;
540      CContext* context=CContext::getCurrent() ;
541
542      this->checkDomain();
543      this->checkZoom();
544      this->checkBounds();
545     
546      if (context->hasClient)
547      { // CÃŽté client uniquement
548         this->checkMask();
549         this->checkDomainData();
550         this->checkCompression();
551         this->completeLonLatClient();
552      }
553      else
554      { // CÃŽté serveur uniquement
555//         if (!this->isEmpty())
556// ne sert plus //   this->completeLonLatServer();
557      }
558   
559      if (context->hasClient)
560      {
561        computeConnectedServer() ;
562        sendServerAttribut() ;
563        sendLonLat() ;
564      }
565     
566      this->isChecked = true;
567   }
568   
569  void CDomain::sendServerAttribut(void)
570  {
571    int ni_srv=ni_glo.getValue() ;
572    int ibegin_srv=1 ;
573    int iend_srv=ni_glo.getValue() ;
574     
575    int nj_srv ;
576    int jbegin_srv ;
577    int jend_srv ;
578   
579    CContext* context=CContext::getCurrent() ;
580    CContextClient* client=context->client ;
581    int nbServer=client->serverSize ;
582    int serverRank=client->getServerLeader() ;
583   
584    jend_srv=0 ;
585    for(int i=0;i<=serverRank;i++)
586    {
587      jbegin_srv=jend_srv+1 ;
588      nj_srv=nj_glo.getValue()/nbServer ;
589      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
590      jend_srv=jbegin_srv+nj_srv-1 ;
591    }
592   
593     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;   
594     if (client->isServerLeader())
595     {
596       CMessage msg ;
597       msg<<this->getId() ;
598       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
599       event.push(client->getServerLeader(),1,msg) ;
600       client->sendEvent(event) ;
601     }
602     else client->sendEvent(event) ;
603  }
604
605  void CDomain::computeConnectedServer(void)
606  {
607    int i,j,i_ind,j_ind ;
608   
609    ibegin_client=ibegin ; iend_client=iend ; ni_client=ni ;
610    jbegin_client=jbegin ; jend_client=jend ; nj_client=nj ;
611     
612    CContext* context = CContext::getCurrent() ;
613    CContextClient* client=context->client ;
614    int nbServer=client->serverSize ;
615
616    // find how much client are connected to a server
617    int zoom_iend=zoom_ibegin+zoom_ni-1 ;
618    int zoom_jend=zoom_jbegin+zoom_nj-1 ;
619   
620    int blockSize=nj_glo/nbServer ;
621    int ns=nj_glo%nbServer ;
622    int pos=ns*(blockSize+1) ;
623    int serverNum ;
624   
625    mapConnectedServer.resize(ni,nj) ;
626    vector<int> nbData(nbServer,0) ;
627    vector<int> indServer(nbServer,-1) ;
628    vector<bool> IsConnected(nbServer,false) ;
629   
630    for(j=0;j<nj;j++)
631      for(i=0;i<ni;i++)
632      {
633        i_ind=ibegin+i_index(i,j)-1 ;
634        j_ind=jbegin+j_index(i,j)-1 ;
635       
636        if (j_ind<pos) serverNum=j_ind/(blockSize+1) ;
637        else serverNum=ns+(j_ind-pos)/blockSize ;
638        IsConnected[serverNum]=true ;
639               
640        if (i_ind >= zoom_ibegin-1 && i_ind <= zoom_iend-1 && j_ind >= zoom_jbegin-1 && j_ind <= zoom_jend-1)
641        {
642          mapConnectedServer(i,j)=serverNum ;
643          nbData[serverNum]++ ;
644        }
645        else mapConnectedServer(i,j)=-1 ;
646      }
647
648
649    for(serverNum=0 ; serverNum<nbServer ; serverNum++) 
650      if (IsConnected[serverNum])
651      {
652        ns=connectedServer.size() ;
653        indServer[serverNum]=ns;
654        connectedServer.push_back(serverNum) ;
655        nbDataSrv.push_back(nbData[serverNum]) ;
656      }
657     
658     i_indSrv.resize(connectedServer.size()) ;
659     j_indSrv.resize(connectedServer.size()) ;
660
661     for(j=0;j<nj;j++)
662      for(i=0;i<ni;i++) 
663      {
664        if (mapConnectedServer(i,j)>=0)
665        {
666          ns=indServer[mapConnectedServer(i,j)] ;
667          mapConnectedServer(i,j)= ns ;
668          i_indSrv[ns].push_back(i+ibegin-1) ;
669          j_indSrv[ns].push_back(j+jbegin-1) ;
670        }
671      }
672         
673    int nbConnectedServer=connectedServer.size() ;
674
675    int* recvCount=new int[client->clientSize] ;
676    int* displ=new int[client->clientSize] ;
677    int* sendBuff=new int[nbConnectedServer] ;
678    valarray<int> nbClient(0,client->serverSize) ;
679   
680    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
681   
682    // get connected server for everybody
683    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
684   
685    displ[0]=0 ;
686    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
687    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
688    int* recvBuff=new int[recvSize] ;
689 
690   
691    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
692    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
693   
694    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
695   
696    delete [] recvCount ;
697    delete [] displ ;
698    delete [] sendBuff ;
699    delete [] recvBuff ;
700  }
701
702
703  void CDomain::sendLonLat(void)
704  {
705    int ns,n,i,j,ind,nv ;
706    CContext* context = CContext::getCurrent() ;
707    CContextClient* client=context->client ;
708    // send lon lat for each connected server
709    CEventClient event(getType(),EVENT_ID_LON_LAT) ;
710   
711    list<shared_ptr<CMessage> > list_msg ;   
712    list< CArray<int,1>* > list_indi,list_indj ;
713    list< CArray<double,1>* >list_lon,list_lat ;
714    list< CArray<double,2>* >list_boundslon,list_boundslat ;
715
716    for(int ns=0;ns<connectedServer.size();ns++)
717    {
718      int nbData = nbDataSrv[ns] ;
719      CArray<int,1> indi(nbData) ;
720      CArray<int,1> indj(nbData) ;
721      CArray<double,1> lon(nbData) ;
722      CArray<double,1> lat(nbData) ;
723      CArray<double,2> boundslon(nvertex,nbData) ;
724      CArray<double,2> boundslat(nvertex,nbData) ;
725     
726      for(n=0;n<nbData;n++) 
727      {
728        i=i_indSrv[ns][n] ;
729        j=j_indSrv[ns][n] ;
730        ind=(i-(zoom_ibegin_client-1))+(j-(zoom_jbegin_client-1))*zoom_ni_client ;
731       
732        lon(n)=lonvalue(ind) ;
733        lat(n)=latvalue(ind) ;
734        if (hasBounds)
735        {
736          for(nv=0;nv<nvertex;nv++)
737          {
738            boundslon(nv,n)=bounds_lon(nv,ind);
739            boundslat(nv,n)=bounds_lat(nv,ind);
740          }
741        }
742        indi(n)=ibegin+i_index(i-ibegin+1,j-jbegin+1)-1  ;
743        indj(n)=jbegin+j_index(i-ibegin+1,j-jbegin+1)-1  ;
744      }
745   
746      list_indi.push_back(new CArray<int,1>(indi.copy())) ;
747      list_indj.push_back(new CArray<int,1>(indj.copy())) ;
748      list_lon.push_back(new CArray<double,1>(lon.copy())) ;
749      list_lat.push_back(new CArray<double,1>(lat.copy())) ;
750      if (hasBounds) list_boundslon.push_back(new CArray<double,2>(boundslon.copy())) ;
751      if (hasBounds) list_boundslat.push_back(new CArray<double,2>(boundslat.copy())) ;
752
753      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
754
755      *list_msg.back()<<this->getId()<<(int)type ; // enum ne fonctionne pour les message => ToFix
756      *list_msg.back()<<isCurvilinear ;
757      *list_msg.back()<<*list_indi.back()<<*list_indj.back()<<*list_lon.back()<<*list_lat.back() ;
758      if (hasBounds) *list_msg.back()<<*list_boundslon.back()<<*list_boundslat.back();
759      event.push(connectedServer[ns],nbSenders[ns],*list_msg.back()) ;
760    }
761
762    client->sendEvent(event) ;
763   
764   
765    for(list<CArray<int,1>* >::iterator it=list_indi.begin();it!=list_indi.end();it++) delete *it;
766    for(list<CArray<int,1>* >::iterator it=list_indj.begin();it!=list_indj.end();it++) delete *it;
767    for(list<CArray<double,1>* >::iterator it=list_lon.begin();it!=list_lon.end();it++)   delete *it;
768    for(list<CArray<double,1>* >::iterator it=list_lat.begin();it!=list_lat.end();it++)   delete *it;
769    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslon.begin();it!=list_boundslon.end();it++)   delete *it;
770    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslat.begin();it!=list_boundslat.end();it++)   delete *it;   
771   
772  }
773 
774 
775  bool CDomain::dispatchEvent(CEventServer& event)
776   {
777     
778      if (SuperClass::dispatchEvent(event)) return true ;
779      else
780      {
781        switch(event.type)
782        {
783           case EVENT_ID_SERVER_ATTRIBUT :
784             recvServerAttribut(event) ;
785             return true ;
786             break ;
787           case EVENT_ID_LON_LAT :
788             recvLonLat(event) ;
789             return true ;
790             break ;
791           default :
792             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
793                    <<"Unknown Event") ;
794           return false ;
795         }
796      }
797   }
798   
799  void CDomain::recvServerAttribut(CEventServer& event)
800  {
801    CBufferIn* buffer=event.subEvents.begin()->buffer;
802    string domainId ;
803    *buffer>>domainId ;
804    get(domainId)->recvServerAttribut(*buffer) ;
805  }
806 
807  void CDomain::recvServerAttribut(CBufferIn& buffer)
808  {
809    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
810    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
811
812     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
813     
814   
815    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
816    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
817    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
818     
819    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
820    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
821    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
822
823    if (zoom_ni_srv<=0 || zoom_nj_srv<=0) 
824    {
825      zoom_ibegin_srv=1 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
826      zoom_jbegin_srv=1 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
827    }
828    lonvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
829    lonvalue_srv = 0. ;
830    latvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
831    latvalue_srv = 0. ;
832    if (hasBounds) 
833    {
834      bounds_lon_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
835      bounds_lon_srv = 0. ;
836      bounds_lat_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
837      bounds_lat_srv = 0. ;
838    }
839  }
840   
841  void CDomain::recvLonLat(CEventServer& event)
842  {
843    list<CEventServer::SSubEvent>::iterator it ;
844    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
845    {
846      CBufferIn* buffer=it->buffer;
847      string domainId ;
848      *buffer>>domainId ;
849      get(domainId)->recvLonLat(*buffer) ;
850    }
851  }
852 
853  void CDomain::recvLonLat(CBufferIn& buffer)
854  {
855    CArray<int,1> indi ;
856    CArray<int,1> indj ;
857    CArray<double,1> lon ;
858    CArray<double,1> lat ;
859    CArray<double,2> boundslon ;
860    CArray<double,2> boundslat ;
861   
862    int type_int ;
863    buffer>>type_int>>isCurvilinear>>indi>>indj>>lon>>lat ;
864    if (hasBounds) buffer>>boundslon>>boundslat ;
865    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
866
867    int i,j,ind_srv ;
868    for(int ind=0;ind<indi.numElements();ind++)
869    {
870      i=indi(ind) ; j=indj(ind) ;
871      ind_srv=(i-(zoom_ibegin_srv-1))+(j-(zoom_jbegin_srv-1))*zoom_ni_srv ;
872      lonvalue_srv(ind_srv)=lon(ind) ;
873      latvalue_srv(ind_srv)=lat(ind) ;
874      if (hasBounds) 
875      {
876        for(int nv=0;nv<nvertex;nv++) 
877        {
878          bounds_lon_srv(nv,ind_srv)=boundslon(nv,ind) ;
879          bounds_lat_srv(nv,ind_srv)=boundslat(nv,ind) ;
880        }
881      }
882    }
883  }
884   //----------------------------------------------------------------
885   
886   
887   
888   ///---------------------------------------------------------------
889
890} // namespace xios
Note: See TracBrowser for help on using the repository browser.