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

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

Correct some bugs on discovering server index and do some code cleanings

+) Add some checking functions to make sure mpi_isend and mpi_irecv work correctly
+) Add comments to code
+) Remove some redundant code and comments

Test
+) On Curie
+) The new functions are tested in test_new_features.f90. Test_client and test_complete work like before
+) Test cases:

  • 3 dimension grid with: 1 domain, 1 axis
  • 3 dimension grid with: 3 axis
  • Attached and connected

+) All pass and results are correct

TODO:
+) Fix zoom bug with grid composed of only one axis

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