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

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

Fixing bug of automatic dection of buffer size

+) Split sending of longtitude and latitude event message to decrease its size into 2
+) Correct size of buffer on client and server side
+) Do some cleaning code

Test
+) On Curie
+) Test only on domain (without axis), all passed

  • 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: 32.0 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=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   // 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=1 ;
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    for(int i=0;i<=serverRank;i++)
629    {
630      jbegin_srv=jend_srv+1 ;
631      nj_srv=nj_glo.getValue()/nbServer ;
632      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
633      jend_srv=jbegin_srv+nj_srv-1 ;
634    }
635
636     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;
637     if (client->isServerLeader())
638     {
639       CMessage msg ;
640       msg<<this->getId() ;
641       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
642       event.push(client->getServerLeader(),1,msg) ;
643       client->sendEvent(event) ;
644     }
645     else client->sendEvent(event) ;
646  }
647
648  void CDomain::computeConnectedServer(void)
649  {
650    int i,j,i_ind,j_ind ;
651
652    ibegin_client=ibegin ; iend_client=iend ; ni_client=ni ;
653    jbegin_client=jbegin ; jend_client=jend ; nj_client=nj ;
654
655    CContext* context = CContext::getCurrent() ;
656    CContextClient* client=context->client ;
657    int nbServer=client->serverSize ;
658
659    // find how much client are connected to a server
660    int zoom_iend=zoom_ibegin+zoom_ni-1 ;
661    int zoom_jend=zoom_jbegin+zoom_nj-1 ;
662
663    int blockSize=nj_glo/nbServer ;
664    int ns=nj_glo%nbServer ;
665    int pos=ns*(blockSize+1) ;
666    int serverNum ;
667
668    mapConnectedServer.resize(ni,nj) ;
669    vector<int> nbData(nbServer,0) ;
670    vector<int> indServer(nbServer,-1) ;
671    vector<bool> IsConnected(nbServer,false) ;
672
673    for(j=0;j<nj;j++)
674      for(i=0;i<ni;i++)
675      {
676        i_ind=ibegin+i_index(i,j)-1 ;
677        j_ind=jbegin+j_index(i,j)-1 ;
678
679        if (j_ind<pos) serverNum=j_ind/(blockSize+1) ;
680        else serverNum=ns+(j_ind-pos)/blockSize ;
681        IsConnected[serverNum]=true ;
682
683        if (i_ind >= zoom_ibegin-1 && i_ind <= zoom_iend-1 && j_ind >= zoom_jbegin-1 && j_ind <= zoom_jend-1)
684        {
685          mapConnectedServer(i,j)=serverNum ;
686          nbData[serverNum]++ ;
687        }
688        else mapConnectedServer(i,j)=-1 ;
689      }
690
691
692    for(serverNum=0 ; serverNum<nbServer ; serverNum++)
693      if (IsConnected[serverNum])
694      {
695        ns=connectedServer.size() ;
696        indServer[serverNum]=ns;
697        connectedServer.push_back(serverNum) ;
698        nbDataSrv.push_back(nbData[serverNum]) ;
699      }
700
701     i_indSrv.resize(connectedServer.size()) ;
702     j_indSrv.resize(connectedServer.size()) ;
703
704     for(j=0;j<nj;j++)
705      for(i=0;i<ni;i++)
706      {
707        if (mapConnectedServer(i,j)>=0)
708        {
709          ns=indServer[mapConnectedServer(i,j)] ;
710          mapConnectedServer(i,j)= ns ;
711          i_indSrv[ns].push_back(i+ibegin-1) ;
712          j_indSrv[ns].push_back(j+jbegin-1) ;
713        }
714      }
715
716    int nbConnectedServer=connectedServer.size() ;
717
718    int* recvCount=new int[client->clientSize] ;
719    int* displ=new int[client->clientSize] ;
720    int* sendBuff=new int[nbConnectedServer] ;
721    valarray<int> nbClient(0,client->serverSize) ;
722
723    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
724
725    // get connected server for everybody
726    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
727
728    displ[0]=0 ;
729    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
730    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
731    int* recvBuff=new int[recvSize] ;
732
733
734    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
735    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
736
737    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
738
739    delete [] recvCount ;
740    delete [] displ ;
741    delete [] sendBuff ;
742    delete [] recvBuff ;
743  }
744
745
746  void CDomain::sendLonLat(void)
747  {
748    int ns,n,i,j,ind,nv ;
749    CContext* context = CContext::getCurrent() ;
750    CContextClient* client=context->client ;
751    // send lon lat for each connected server
752
753    CEventClient eventLon(getType(),EVENT_ID_LON) ;
754    CEventClient eventLat(getType(),EVENT_ID_LAT) ;
755
756    list<shared_ptr<CMessage> > list_msgLon ;
757    list<shared_ptr<CMessage> > list_msgLat ;
758    list< CArray<int,1>* > list_indi,list_indj ;
759    list< CArray<double,1>* >list_lon,list_lat ;
760    list< CArray<double,2>* >list_boundslon,list_boundslat ;
761
762    for(int ns=0;ns<connectedServer.size();ns++)
763    {
764      int nbData = nbDataSrv[ns] ;
765      CArray<int,1> indi(nbData) ;
766      CArray<int,1> indj(nbData) ;
767      CArray<double,1> lon(nbData) ;
768      CArray<double,1> lat(nbData) ;
769      CArray<double,2> boundslon(nvertex,nbData) ;
770      CArray<double,2> boundslat(nvertex,nbData) ;
771
772      for(n=0;n<nbData;n++)
773      {
774        i=i_indSrv[ns][n] ;
775        j=j_indSrv[ns][n] ;
776        ind=(i-(zoom_ibegin_client-1))+(j-(zoom_jbegin_client-1))*zoom_ni_client ;
777
778        lon(n)=lonvalue(ind) ;
779        lat(n)=latvalue(ind) ;
780        if (hasBounds)
781        {
782          for(nv=0;nv<nvertex;nv++)
783          {
784            boundslon(nv,n)=bounds_lon(nv,ind);
785            boundslat(nv,n)=bounds_lat(nv,ind);
786          }
787        }
788        indi(n)=ibegin+i_index(i-ibegin+1,j-jbegin+1)-1  ;
789        indj(n)=jbegin+j_index(i-ibegin+1,j-jbegin+1)-1  ;
790      }
791
792      list_indi.push_back(new CArray<int,1>(indi.copy())) ;
793      list_indj.push_back(new CArray<int,1>(indj.copy())) ;
794      list_lon.push_back(new CArray<double,1>(lon.copy())) ;
795      list_lat.push_back(new CArray<double,1>(lat.copy())) ;
796      if (hasBounds) list_boundslon.push_back(new CArray<double,2>(boundslon.copy())) ;
797      if (hasBounds) list_boundslat.push_back(new CArray<double,2>(boundslat.copy())) ;
798
799      list_msgLon.push_back(shared_ptr<CMessage>(new CMessage)) ;
800      list_msgLat.push_back(shared_ptr<CMessage>(new CMessage)) ;
801
802      *list_msgLon.back()<<this->getId()<<(int)type ; // enum ne fonctionne pour les message => ToFix
803      *list_msgLat.back()<<this->getId()<<(int)type ;
804      *list_msgLon.back()<<isCurvilinear ;
805      *list_msgLat.back()<<isCurvilinear ;
806      *list_msgLon.back()<<*list_indi.back()<<*list_indj.back()<<*list_lon.back() ;
807      *list_msgLat.back()<<*list_indi.back()<<*list_indj.back()<<*list_lat.back() ;
808
809      if (hasBounds)
810      {
811        *list_msgLon.back()<<*list_boundslon.back();
812        *list_msgLat.back()<<*list_boundslat.back();
813      }
814      eventLon.push(connectedServer[ns],nbSenders[ns],*list_msgLon.back()) ;
815      eventLat.push(connectedServer[ns],nbSenders[ns],*list_msgLat.back()) ;
816    }
817
818    client->sendEvent(eventLon) ;
819    client->sendEvent(eventLat) ;
820
821
822    for(list<CArray<int,1>* >::iterator it=list_indi.begin();it!=list_indi.end();it++) delete *it;
823    for(list<CArray<int,1>* >::iterator it=list_indj.begin();it!=list_indj.end();it++) delete *it;
824    for(list<CArray<double,1>* >::iterator it=list_lon.begin();it!=list_lon.end();it++)   delete *it;
825    for(list<CArray<double,1>* >::iterator it=list_lat.begin();it!=list_lat.end();it++)   delete *it;
826    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslon.begin();it!=list_boundslon.end();it++)   delete *it;
827    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslat.begin();it!=list_boundslat.end();it++)   delete *it;
828
829  }
830
831
832  bool CDomain::dispatchEvent(CEventServer& event)
833   {
834
835      if (SuperClass::dispatchEvent(event)) return true ;
836      else
837      {
838        switch(event.type)
839        {
840           case EVENT_ID_SERVER_ATTRIBUT :
841             recvServerAttribut(event) ;
842             return true ;
843             break ;
844           case EVENT_ID_LON :
845             recvLon(event) ;
846             return true ;
847             break ;
848           case EVENT_ID_LAT :
849             recvLat(event) ;
850             return true ;
851             break ;
852           default :
853             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
854                    <<"Unknown Event") ;
855           return false ;
856         }
857      }
858   }
859
860  void CDomain::recvServerAttribut(CEventServer& event)
861  {
862    CBufferIn* buffer=event.subEvents.begin()->buffer;
863    string domainId ;
864    *buffer>>domainId ;
865    get(domainId)->recvServerAttribut(*buffer) ;
866  }
867
868  void CDomain::recvServerAttribut(CBufferIn& buffer)
869  {
870    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
871    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
872
873     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
874
875
876    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
877    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
878    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
879
880    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
881    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
882    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
883
884    if (zoom_ni_srv<=0 || zoom_nj_srv<=0)
885    {
886      zoom_ibegin_srv=1 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
887      zoom_jbegin_srv=1 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
888    }
889    lonvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
890    lonvalue_srv = 0. ;
891    latvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
892    latvalue_srv = 0. ;
893    if (hasBounds)
894    {
895      bounds_lon_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
896      bounds_lon_srv = 0. ;
897      bounds_lat_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
898      bounds_lat_srv = 0. ;
899    }
900  }
901
902  void CDomain::recvLon(CEventServer& event)
903  {
904    list<CEventServer::SSubEvent>::iterator it ;
905    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
906    {
907      CBufferIn* buffer=it->buffer;
908      string domainId ;
909      *buffer>>domainId ;
910      get(domainId)->recvLon(*buffer) ;
911    }
912  }
913
914  void CDomain::recvLon(CBufferIn& buffer)
915  {
916    CArray<int,1> indi ;
917    CArray<int,1> indj ;
918    CArray<double,1> lon ;
919    CArray<double,2> boundslon ;
920
921    int type_int ;
922    buffer>>type_int>>isCurvilinear>>indi>>indj>>lon ;
923    if (hasBounds) buffer>>boundslon ;
924    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
925
926    int i,j,ind_srv ;
927    for(int ind=0;ind<indi.numElements();ind++)
928    {
929      i=indi(ind) ; j=indj(ind) ;
930      ind_srv=(i-(zoom_ibegin_srv-1))+(j-(zoom_jbegin_srv-1))*zoom_ni_srv ;
931      lonvalue_srv(ind_srv)=lon(ind) ;
932      if (hasBounds)
933      {
934        for(int nv=0;nv<nvertex;nv++)
935        {
936          bounds_lon_srv(nv,ind_srv)=boundslon(nv,ind) ;
937        }
938      }
939    }
940  }
941
942  void CDomain::recvLat(CEventServer& event)
943  {
944    list<CEventServer::SSubEvent>::iterator it ;
945    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
946    {
947      CBufferIn* buffer=it->buffer;
948      string domainId ;
949      *buffer>>domainId ;
950      get(domainId)->recvLat(*buffer) ;
951    }
952  }
953
954  void CDomain::recvLat(CBufferIn& buffer)
955  {
956    CArray<int,1> indi ;
957    CArray<int,1> indj ;
958    CArray<double,1> lat ;
959    CArray<double,2> boundslat ;
960
961    int type_int ;
962    buffer>>type_int>>isCurvilinear>>indi>>indj>>lat ;
963    if (hasBounds) buffer>>boundslat ;
964    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
965
966    int i,j,ind_srv ;
967    for(int ind=0;ind<indi.numElements();ind++)
968    {
969      i=indi(ind) ; j=indj(ind) ;
970      ind_srv=(i-(zoom_ibegin_srv-1))+(j-(zoom_jbegin_srv-1))*zoom_ni_srv ;
971      latvalue_srv(ind_srv)=lat(ind) ;
972      if (hasBounds)
973      {
974        for(int nv=0;nv<nvertex;nv++)
975        {
976          bounds_lat_srv(nv,ind_srv)=boundslat(nv,ind) ;
977        }
978      }
979    }
980  }
981//  void CDomain::recvLonLat(CEventServer& event)
982//  {
983//    list<CEventServer::SSubEvent>::iterator it ;
984//    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
985//    {
986//      CBufferIn* buffer=it->buffer;
987//      string domainId ;
988//      *buffer>>domainId ;
989//      get(domainId)->recvLonLat(*buffer) ;
990//    }
991//  }
992//
993//  void CDomain::recvLonLat(CBufferIn& buffer)
994//  {
995//    CArray<int,1> indi ;
996//    CArray<int,1> indj ;
997//    CArray<double,1> lon ;
998//    CArray<double,1> lat ;
999//    CArray<double,2> boundslon ;
1000//    CArray<double,2> boundslat ;
1001//
1002//    int type_int ;
1003//    buffer>>type_int>>isCurvilinear>>indi>>indj>>lon>>lat ;
1004//    if (hasBounds) buffer>>boundslon>>boundslat ;
1005//    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
1006//
1007//    int i,j,ind_srv ;
1008//    for(int ind=0;ind<indi.numElements();ind++)
1009//    {
1010//      i=indi(ind) ; j=indj(ind) ;
1011//      ind_srv=(i-(zoom_ibegin_srv-1))+(j-(zoom_jbegin_srv-1))*zoom_ni_srv ;
1012//      lonvalue_srv(ind_srv)=lon(ind) ;
1013//      latvalue_srv(ind_srv)=lat(ind) ;
1014//      if (hasBounds)
1015//      {
1016//        for(int nv=0;nv<nvertex;nv++)
1017//        {
1018//          bounds_lon_srv(nv,ind_srv)=boundslon(nv,ind) ;
1019//          bounds_lat_srv(nv,ind_srv)=boundslat(nv,ind) ;
1020//        }
1021//      }
1022//    }
1023//  }
1024   //----------------------------------------------------------------
1025
1026
1027
1028   ///---------------------------------------------------------------
1029
1030} // namespace xios
Note: See TracBrowser for help on using the repository browser.