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

Last change on this file since 552 was 551, checked in by mhnguyen, 9 years ago

Redesigning grid structure

+) Add an intermediate class to calculate distribution on client and servers
+) Change all index of attributes to zero (0), instead of one(1)

Test
+) On Curie
+) Test new features passes but some data are still shifted

  • 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: 35.8 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(), isClientChecked(false)
24   { /* Ne rien faire de plus */ }
25
26   CDomain::CDomain(const StdString & id)
27      : CObjectTemplate<CDomain>(id), CDomainAttributes()
28      , isChecked(false), relFiles(), isClientChecked(false)
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=0 ;
112         iend=0 ;
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() < 0 || iend.getValue() > (ni_glo.getValue()-1))
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() < 0 || jend.getValue() > (nj_glo.getValue()-1))
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 < 0  || zoom_jbegin < 0 || zoom_iend > (ni_glo-1) || zoom_jend > (nj_glo-1))
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 = 0;
501         zoom_jbegin = 0;
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   // Divide function checkAttributes into 2 seperate ones
537   // This function only checks all attributes of current domain
538   void CDomain::checkAttributesOnClient()
539   {
540     if (this->isClientChecked) return;
541     CContext* context=CContext::getCurrent();
542
543      this->checkDomain();
544      this->checkZoom();
545      this->checkBounds();
546
547      if (context->hasClient)
548      { // CÃŽté client uniquement
549         this->checkMask();
550         this->checkDomainData();
551         this->checkCompression();
552         this->completeLonLatClient();
553         this->computeConnectedServer() ;
554      }
555      else
556      { // CÃŽté serveur uniquement
557//         if (!this->isEmpty())
558// ne sert plus //   this->completeLonLatServer();
559      }
560
561      this->isClientChecked = true;
562   }
563
564   // Send all checked attributes to server
565   void CDomain::sendCheckedAttributes()
566   {
567     if (!this->isClientChecked) checkAttributesOnClient();
568     CContext* context=CContext::getCurrent() ;
569
570     if (this->isChecked) return;
571     if (context->hasClient)
572     {
573       sendServerAttribut() ;
574       sendLonLat() ;
575     }
576
577     this->isChecked = true;
578   }
579
580   void CDomain::checkAttributes(void)
581   {
582      if (this->isChecked) return;
583      CContext* context=CContext::getCurrent() ;
584
585      this->checkDomain();
586      this->checkZoom();
587      this->checkBounds();
588
589      if (context->hasClient)
590      { // CÃŽté client uniquement
591         this->checkMask();
592         this->checkDomainData();
593         this->checkCompression();
594         this->completeLonLatClient();
595      }
596      else
597      { // CÃŽté serveur uniquement
598//         if (!this->isEmpty())
599// ne sert plus //   this->completeLonLatServer();
600      }
601
602      if (context->hasClient)
603      {
604        computeConnectedServer() ;
605        sendServerAttribut() ;
606        sendLonLat() ;
607      }
608
609      this->isChecked = true;
610   }
611
612  void CDomain::sendServerAttribut(void)
613  {
614    int ni_srv=ni_glo.getValue() ;
615    int ibegin_srv=0 ;
616    int iend_srv=ni_glo.getValue() ;
617
618    int nj_srv ;
619    int jbegin_srv ;
620    int jend_srv ;
621
622    CContext* context=CContext::getCurrent() ;
623    CContextClient* client=context->client ;
624    int nbServer=client->serverSize ;
625    int serverRank=client->getServerLeader() ;
626
627//    jend_srv=0 ;
628    jend_srv= -1 ;
629    for(int i=0;i<=serverRank;i++)
630    {
631      jbegin_srv=jend_srv+1 ;
632      nj_srv=nj_glo.getValue()/nbServer ;
633      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
634      jend_srv=jbegin_srv+nj_srv-1 ;
635    }
636
637     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;
638     if (client->isServerLeader())
639     {
640       CMessage msg ;
641       msg<<this->getId() ;
642       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
643       event.push(client->getServerLeader(),1,msg) ;
644       client->sendEvent(event) ;
645     }
646     else client->sendEvent(event) ;
647  }
648
649  void CDomain::computeConnectedServer(void)
650  {
651    int i,j,i_ind,j_ind ;
652
653    ibegin_client=ibegin ; iend_client=iend ; ni_client=ni ;
654    jbegin_client=jbegin ; jend_client=jend ; nj_client=nj ;
655
656    CContext* context = CContext::getCurrent() ;
657    CContextClient* client=context->client ;
658    int nbServer=client->serverSize ;
659
660    // find how much client are connected to a server
661    int zoom_iend=zoom_ibegin+zoom_ni-1 ;
662    int zoom_jend=zoom_jbegin+zoom_nj-1 ;
663
664    int blockSize=nj_glo/nbServer ;
665    int ns=nj_glo%nbServer ;
666    int pos=ns*(blockSize+1) ;
667//    int pos=ns*(blockSize) ;
668    int serverNum ;
669
670    mapConnectedServer.resize(ni,nj) ;
671    vector<int> nbData(nbServer,0) ;
672    vector<int> indServer(nbServer,-1) ;
673    vector<bool> IsConnected(nbServer,false) ;
674
675    for(j=0;j<nj;j++)
676      for(i=0;i<ni;i++)
677      {
678        i_ind=ibegin+i_index(i,j) ;
679        j_ind=jbegin+j_index(i,j) ;
680
681        if (j_ind<pos) serverNum=j_ind/(blockSize+1) ;
682        else serverNum=ns+(j_ind-pos)/blockSize ;
683        IsConnected[serverNum]=true ;
684
685        if (i_ind >= zoom_ibegin && i_ind <= zoom_iend && j_ind >= zoom_jbegin && j_ind <= zoom_jend)
686        {
687          mapConnectedServer(i,j)=serverNum ;
688          nbData[serverNum]++ ;
689        }
690        else mapConnectedServer(i,j)=-1 ;
691      }
692
693
694    for(serverNum=0 ; serverNum<nbServer ; serverNum++)
695      if (IsConnected[serverNum])
696      {
697        ns=connectedServer.size() ;
698        indServer[serverNum]=ns;
699        connectedServer.push_back(serverNum) ;
700        nbDataSrv.push_back(nbData[serverNum]) ;
701      }
702
703     i_indSrv.resize(connectedServer.size()) ;
704     j_indSrv.resize(connectedServer.size()) ;
705
706     for(j=0;j<nj;j++)
707      for(i=0;i<ni;i++)
708      {
709        if (mapConnectedServer(i,j)>=0)
710        {
711          ns=indServer[mapConnectedServer(i,j)] ;
712          mapConnectedServer(i,j)= ns ;
713          i_indSrv[ns].push_back(i+ibegin) ;
714          j_indSrv[ns].push_back(j+jbegin) ;
715        }
716      }
717
718    int nbConnectedServer=connectedServer.size() ;
719
720    int* recvCount=new int[client->clientSize] ;
721    int* displ=new int[client->clientSize] ;
722    int* sendBuff=new int[nbConnectedServer] ;
723    valarray<int> nbClient(0,client->serverSize) ;
724
725    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
726
727    // get connected server for everybody
728    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
729
730    displ[0]=0 ;
731    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
732    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
733    int* recvBuff=new int[recvSize] ;
734
735
736    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
737    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
738
739    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
740
741    delete [] recvCount ;
742    delete [] displ ;
743    delete [] sendBuff ;
744    delete [] recvBuff ;
745  }
746
747
748//  void CDomain::sendLonLat(void)
749//  {
750//    int ns,n,i,j,ind,nv ;
751//    CContext* context = CContext::getCurrent() ;
752//    CContextClient* client=context->client ;
753//    // send lon lat for each connected server
754//
755//    CEventClient eventLon(getType(),EVENT_ID_LON) ;
756//    CEventClient eventLat(getType(),EVENT_ID_LAT) ;
757//
758//    list<shared_ptr<CMessage> > list_msgLon ;
759//    list<shared_ptr<CMessage> > list_msgLat ;
760//    list< CArray<int,1>* > list_indi,list_indj ;
761//    list< CArray<double,1>* >list_lon,list_lat ;
762//    list< CArray<double,2>* >list_boundslon,list_boundslat ;
763//
764//    for(int ns=0;ns<connectedServer.size();ns++)
765//    {
766//      int nbData = nbDataSrv[ns] ;
767//      CArray<int,1> indi(nbData) ;
768//      CArray<int,1> indj(nbData) ;
769//      CArray<double,1> lon(nbData) ;
770//      CArray<double,1> lat(nbData) ;
771//      CArray<double,2> boundslon(nvertex,nbData) ;
772//      CArray<double,2> boundslat(nvertex,nbData) ;
773//
774//      for(n=0;n<nbData;n++)
775//      {
776//        i=i_indSrv[ns][n] ;
777//        j=j_indSrv[ns][n] ;
778//        ind=(i-(zoom_ibegin_client-1))+(j-(zoom_jbegin_client-1))*zoom_ni_client ;
779//
780//        lon(n)=lonvalue(ind) ;
781//        lat(n)=latvalue(ind) ;
782//        if (hasBounds)
783//        {
784//          for(nv=0;nv<nvertex;nv++)
785//          {
786//            boundslon(nv,n)=bounds_lon(nv,ind);
787//            boundslat(nv,n)=bounds_lat(nv,ind);
788//          }
789//        }
790//        indi(n)=ibegin+i_index(i-ibegin+1,j-jbegin+1)-1  ;
791//        indj(n)=jbegin+j_index(i-ibegin+1,j-jbegin+1)-1  ;
792//      }
793//
794//      list_indi.push_back(new CArray<int,1>(indi.copy())) ;
795//      list_indj.push_back(new CArray<int,1>(indj.copy())) ;
796//      list_lon.push_back(new CArray<double,1>(lon.copy())) ;
797//      list_lat.push_back(new CArray<double,1>(lat.copy())) ;
798//      if (hasBounds) list_boundslon.push_back(new CArray<double,2>(boundslon.copy())) ;
799//      if (hasBounds) list_boundslat.push_back(new CArray<double,2>(boundslat.copy())) ;
800//
801//      list_msgLon.push_back(shared_ptr<CMessage>(new CMessage)) ;
802//      list_msgLat.push_back(shared_ptr<CMessage>(new CMessage)) ;
803//
804//      *list_msgLon.back()<<this->getId()<<(int)type ; // enum ne fonctionne pour les message => ToFix
805//      *list_msgLat.back()<<this->getId()<<(int)type ;
806//      *list_msgLon.back()<<isCurvilinear ;
807//      *list_msgLat.back()<<isCurvilinear ;
808//      *list_msgLon.back()<<*list_indi.back()<<*list_indj.back()<<*list_lon.back() ;
809//      *list_msgLat.back()<<*list_indi.back()<<*list_indj.back()<<*list_lat.back() ;
810//
811//      if (hasBounds)
812//      {
813//        *list_msgLon.back()<<*list_boundslon.back();
814//        *list_msgLat.back()<<*list_boundslat.back();
815//      }
816//      eventLon.push(connectedServer[ns],nbSenders[ns],*list_msgLon.back()) ;
817//      eventLat.push(connectedServer[ns],nbSenders[ns],*list_msgLat.back()) ;
818//    }
819//
820//    client->sendEvent(eventLon) ;
821//    client->sendEvent(eventLat) ;
822//
823//
824//    for(list<CArray<int,1>* >::iterator it=list_indi.begin();it!=list_indi.end();it++) delete *it;
825//    for(list<CArray<int,1>* >::iterator it=list_indj.begin();it!=list_indj.end();it++) delete *it;
826//    for(list<CArray<double,1>* >::iterator it=list_lon.begin();it!=list_lon.end();it++)   delete *it;
827//    for(list<CArray<double,1>* >::iterator it=list_lat.begin();it!=list_lat.end();it++)   delete *it;
828//    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslon.begin();it!=list_boundslon.end();it++)   delete *it;
829//    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslat.begin();it!=list_boundslat.end();it++)   delete *it;
830//
831//  }
832
833  void CDomain::sendLonLat(void)
834  {
835    int ns,n,i,j,ind,nv ;
836    CContext* context = CContext::getCurrent() ;
837    CContextClient* client=context->client ;
838    // send lon lat for each connected server
839
840    CEventClient eventLon(getType(),EVENT_ID_LON) ;
841    CEventClient eventLat(getType(),EVENT_ID_LAT) ;
842
843    list<shared_ptr<CMessage> > list_msgLon ;
844    list<shared_ptr<CMessage> > list_msgLat ;
845    list< CArray<int,1>* > list_indi,list_indj ;
846    list< CArray<double,1>* >list_lon,list_lat ;
847    list< CArray<double,2>* >list_boundslon,list_boundslat ;
848
849    for(int ns=0;ns<connectedServer.size();ns++)
850    {
851      int nbData = nbDataSrv[ns] ;
852      CArray<int,1> indi(nbData) ;
853      CArray<int,1> indj(nbData) ;
854      CArray<double,1> lon(nbData) ;
855      CArray<double,1> lat(nbData) ;
856      CArray<double,2> boundslon(nvertex,nbData) ;
857      CArray<double,2> boundslat(nvertex,nbData) ;
858
859      for(n=0;n<nbData;n++)
860      {
861        i=i_indSrv[ns][n] ;
862        j=j_indSrv[ns][n] ;
863//        ind=(i-(zoom_ibegin_client))+(j-(zoom_jbegin_client-1))*zoom_ni_client ;
864        ind=(i-(zoom_ibegin_client))+(j-(zoom_jbegin_client))*zoom_ni_client ;
865
866        lon(n)=lonvalue(ind) ;
867        lat(n)=latvalue(ind) ;
868        if (hasBounds)
869        {
870          for(nv=0;nv<nvertex;nv++)
871          {
872            boundslon(nv,n)=bounds_lon(nv,ind);
873            boundslat(nv,n)=bounds_lat(nv,ind);
874          }
875        }
876        indi(n)=ibegin+i_index(i-ibegin,j-jbegin)  ;
877        indj(n)=jbegin+j_index(i-ibegin,j-jbegin)  ;
878//        indi(n)=ibegin+i_index(i-ibegin+1,j-jbegin+1)-1  ;
879//        indj(n)=jbegin+j_index(i-ibegin+1,j-jbegin+1)-1  ;
880      }
881
882      list_indi.push_back(new CArray<int,1>(indi.copy())) ;
883      list_indj.push_back(new CArray<int,1>(indj.copy())) ;
884      list_lon.push_back(new CArray<double,1>(lon.copy())) ;
885      list_lat.push_back(new CArray<double,1>(lat.copy())) ;
886      if (hasBounds) list_boundslon.push_back(new CArray<double,2>(boundslon.copy())) ;
887      if (hasBounds) list_boundslat.push_back(new CArray<double,2>(boundslat.copy())) ;
888
889      list_msgLon.push_back(shared_ptr<CMessage>(new CMessage)) ;
890      list_msgLat.push_back(shared_ptr<CMessage>(new CMessage)) ;
891
892      *list_msgLon.back()<<this->getId()<<(int)type ; // enum ne fonctionne pour les message => ToFix
893      *list_msgLat.back()<<this->getId()<<(int)type ;
894      *list_msgLon.back()<<isCurvilinear ;
895      *list_msgLat.back()<<isCurvilinear ;
896      *list_msgLon.back()<<*list_indi.back()<<*list_indj.back()<<*list_lon.back() ;
897      *list_msgLat.back()<<*list_indi.back()<<*list_indj.back()<<*list_lat.back() ;
898
899      if (hasBounds)
900      {
901        *list_msgLon.back()<<*list_boundslon.back();
902        *list_msgLat.back()<<*list_boundslat.back();
903      }
904      eventLon.push(connectedServer[ns],nbSenders[ns],*list_msgLon.back()) ;
905      eventLat.push(connectedServer[ns],nbSenders[ns],*list_msgLat.back()) ;
906    }
907
908    client->sendEvent(eventLon) ;
909    client->sendEvent(eventLat) ;
910
911
912    for(list<CArray<int,1>* >::iterator it=list_indi.begin();it!=list_indi.end();it++) delete *it;
913    for(list<CArray<int,1>* >::iterator it=list_indj.begin();it!=list_indj.end();it++) delete *it;
914    for(list<CArray<double,1>* >::iterator it=list_lon.begin();it!=list_lon.end();it++)   delete *it;
915    for(list<CArray<double,1>* >::iterator it=list_lat.begin();it!=list_lat.end();it++)   delete *it;
916    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslon.begin();it!=list_boundslon.end();it++)   delete *it;
917    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslat.begin();it!=list_boundslat.end();it++)   delete *it;
918
919  }
920
921  bool CDomain::dispatchEvent(CEventServer& event)
922   {
923
924      if (SuperClass::dispatchEvent(event)) return true ;
925      else
926      {
927        switch(event.type)
928        {
929           case EVENT_ID_SERVER_ATTRIBUT :
930             recvServerAttribut(event) ;
931             return true ;
932             break ;
933           case EVENT_ID_LON :
934             recvLon(event) ;
935             return true ;
936             break ;
937           case EVENT_ID_LAT :
938             recvLat(event) ;
939             return true ;
940             break ;
941           default :
942             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
943                    <<"Unknown Event") ;
944           return false ;
945         }
946      }
947   }
948
949  void CDomain::recvServerAttribut(CEventServer& event)
950  {
951    CBufferIn* buffer=event.subEvents.begin()->buffer;
952    string domainId ;
953    *buffer>>domainId ;
954    get(domainId)->recvServerAttribut(*buffer) ;
955  }
956
957  void CDomain::recvServerAttribut(CBufferIn& buffer)
958  {
959    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
960    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
961
962     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
963
964
965    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
966    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
967    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
968
969    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
970    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
971    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
972
973    if (zoom_ni_srv<=0 || zoom_nj_srv<=0)
974    {
975      zoom_ibegin_srv=0 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
976      zoom_jbegin_srv=0 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
977    }
978    lonvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
979    lonvalue_srv = 0. ;
980    latvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
981    latvalue_srv = 0. ;
982    if (hasBounds)
983    {
984      bounds_lon_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
985      bounds_lon_srv = 0. ;
986      bounds_lat_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
987      bounds_lat_srv = 0. ;
988    }
989  }
990
991  void CDomain::recvLon(CEventServer& event)
992  {
993    list<CEventServer::SSubEvent>::iterator it ;
994    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
995    {
996      CBufferIn* buffer=it->buffer;
997      string domainId ;
998      *buffer>>domainId ;
999      get(domainId)->recvLon(*buffer) ;
1000    }
1001  }
1002
1003  void CDomain::recvLon(CBufferIn& buffer)
1004  {
1005    CArray<int,1> indi ;
1006    CArray<int,1> indj ;
1007    CArray<double,1> lon ;
1008    CArray<double,2> boundslon ;
1009
1010    int type_int ;
1011    buffer>>type_int>>isCurvilinear>>indi>>indj>>lon ;
1012    if (hasBounds) buffer>>boundslon ;
1013    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
1014
1015    int i,j,ind_srv ;
1016    for(int ind=0;ind<indi.numElements();ind++)
1017    {
1018      i=indi(ind) ; j=indj(ind) ;
1019      ind_srv=(i-(zoom_ibegin_srv))+(j-(zoom_jbegin_srv))*zoom_ni_srv ;
1020      lonvalue_srv(ind_srv)=lon(ind) ;
1021      if (hasBounds)
1022      {
1023        for(int nv=0;nv<nvertex;nv++)
1024        {
1025          bounds_lon_srv(nv,ind_srv)=boundslon(nv,ind) ;
1026        }
1027      }
1028    }
1029  }
1030
1031  void CDomain::recvLat(CEventServer& event)
1032  {
1033    list<CEventServer::SSubEvent>::iterator it ;
1034    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
1035    {
1036      CBufferIn* buffer=it->buffer;
1037      string domainId ;
1038      *buffer>>domainId ;
1039      get(domainId)->recvLat(*buffer) ;
1040    }
1041  }
1042
1043  void CDomain::recvLat(CBufferIn& buffer)
1044  {
1045    CArray<int,1> indi ;
1046    CArray<int,1> indj ;
1047    CArray<double,1> lat ;
1048    CArray<double,2> boundslat ;
1049
1050    int type_int ;
1051    buffer>>type_int>>isCurvilinear>>indi>>indj>>lat ;
1052    if (hasBounds) buffer>>boundslat ;
1053    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
1054
1055    int i,j,ind_srv ;
1056    for(int ind=0;ind<indi.numElements();ind++)
1057    {
1058      i=indi(ind) ; j=indj(ind) ;
1059      ind_srv=(i-(zoom_ibegin_srv))+(j-(zoom_jbegin_srv))*zoom_ni_srv ;
1060      latvalue_srv(ind_srv)=lat(ind) ;
1061      if (hasBounds)
1062      {
1063        for(int nv=0;nv<nvertex;nv++)
1064        {
1065          bounds_lat_srv(nv,ind_srv)=boundslat(nv,ind) ;
1066        }
1067      }
1068    }
1069  }
1070//  void CDomain::recvLonLat(CEventServer& event)
1071//  {
1072//    list<CEventServer::SSubEvent>::iterator it ;
1073//    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
1074//    {
1075//      CBufferIn* buffer=it->buffer;
1076//      string domainId ;
1077//      *buffer>>domainId ;
1078//      get(domainId)->recvLonLat(*buffer) ;
1079//    }
1080//  }
1081//
1082//  void CDomain::recvLonLat(CBufferIn& buffer)
1083//  {
1084//    CArray<int,1> indi ;
1085//    CArray<int,1> indj ;
1086//    CArray<double,1> lon ;
1087//    CArray<double,1> lat ;
1088//    CArray<double,2> boundslon ;
1089//    CArray<double,2> boundslat ;
1090//
1091//    int type_int ;
1092//    buffer>>type_int>>isCurvilinear>>indi>>indj>>lon>>lat ;
1093//    if (hasBounds) buffer>>boundslon>>boundslat ;
1094//    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
1095//
1096//    int i,j,ind_srv ;
1097//    for(int ind=0;ind<indi.numElements();ind++)
1098//    {
1099//      i=indi(ind) ; j=indj(ind) ;
1100//      ind_srv=(i-(zoom_ibegin_srv-1))+(j-(zoom_jbegin_srv-1))*zoom_ni_srv ;
1101//      lonvalue_srv(ind_srv)=lon(ind) ;
1102//      latvalue_srv(ind_srv)=lat(ind) ;
1103//      if (hasBounds)
1104//      {
1105//        for(int nv=0;nv<nvertex;nv++)
1106//        {
1107//          bounds_lon_srv(nv,ind_srv)=boundslon(nv,ind) ;
1108//          bounds_lat_srv(nv,ind_srv)=boundslat(nv,ind) ;
1109//        }
1110//      }
1111//    }
1112//  }
1113   //----------------------------------------------------------------
1114
1115   DEFINE_REF_FUNC(Domain,domain)
1116
1117   ///---------------------------------------------------------------
1118
1119} // namespace xios
Note: See TracBrowser for help on using the repository browser.