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

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

Final testing transfomation algorithm: inverse axis (local commit)

+) Make some minor change to make sure one element (axis or domain) be able to have several similar transformation

Test
+) On Curie
+) test_new_feature: test passed with correct data written

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