source: XIOS/branchs/xios-1.0/src/node/domain.cpp @ 678

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

Relax some checks on domain attributes to allow empty local domains.

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