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

Last change on this file since 611 was 611, checked in by rlacroix, 9 years ago

Improve CF compliance: Add a new domain attribute "area".

Fixes ticket #68.

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