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

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

Seperating global index computation on client and server side

+) Create a class which do mapping in general manner, between client and server index global
+) Remove some redundant functions and variables
+) Add some comments to code

Test
+) On Curie. Only test_new_features.f90
+) Test passes and results are correct.
+) Need to change index from 1 to 0

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