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

Last change on this file since 476 was 467, checked in by ymipsl, 10 years ago

Add functionnalities for reduced gaussian grid support.

YM

File size: 28.6 KB
Line 
1#include "domain.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6
7#include "xmlioserver_spl.hpp"
8#include "event_client.hpp"
9#include "event_server.hpp"
10#include "buffer_in.hpp"
11#include "message.hpp"
12#include "type.hpp"
13#include "context.hpp"
14#include "context_client.hpp"
15#include "array_new.hpp"
16
17namespace xios {
18   
19   /// ////////////////////// Définitions ////////////////////// ///
20
21   CDomain::CDomain(void)
22      : CObjectTemplate<CDomain>(), CDomainAttributes()
23      , isChecked(false),  relFiles()
24   { /* Ne rien faire de plus */ }
25
26   CDomain::CDomain(const StdString & id)
27      : CObjectTemplate<CDomain>(id), CDomainAttributes()
28      , isChecked(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         ni=1 ;
111         ibegin=1 ;
112         iend=1 ;
113         
114      }
115      else if ((ni_glo.isEmpty() || ni_glo.getValue() <= 0 ) ||
116          (nj_glo.isEmpty() || nj_glo.getValue() <= 0 ))
117      {
118         ERROR("CDomain::checkAttributes(void)",
119               << "[ Id = " << this->getId() << " ] "
120               << "The global domain is badly defined,"
121               << " check the \'ni_glo\' et \'nj_glo\' values !") 
122      }
123      checkLocalIDomain() ;
124      checkLocalJDomain() ;
125     
126     if (i_index.isEmpty())
127     {
128       i_index.resize(ni,nj) ;
129       for(int j=0;j<nj;j++)
130         for(int i=0;i<ni;i++) i_index(i,j)=i ;   
131     }
132     
133     if (j_index.isEmpty())
134     {
135        j_index.resize(ni,nj) ;
136        for(int j=0;j<nj;j++)
137         for(int i=0;i<ni;i++) j_index(i,j)=j ;   
138     }
139 
140   }
141
142   //----------------------------------------------------------------
143
144   void CDomain::checkLocalIDomain(void)
145   {
146      if (!ni.isEmpty() && !ibegin.isEmpty() && iend.isEmpty())
147         iend.setValue(ibegin.getValue() + ni.getValue() - 1) ;
148
149      else if (!ni.isEmpty() && !iend.isEmpty()   && ibegin.isEmpty())
150         ibegin.setValue( - ni.getValue() + iend.getValue() + 1) ;
151
152      else if (!ibegin.isEmpty() && !iend.isEmpty() && ni.isEmpty())
153         ni.setValue(iend.getValue() - ibegin.getValue() + 1) ;
154
155      else if (!ibegin.isEmpty() && !iend.isEmpty() && !ni.isEmpty() )
156      {
157         if (iend.getValue() != ibegin.getValue() + ni.getValue() - 1)
158           ERROR("CDomain::checkAttributes(void)",
159                 << "The domain si wrong defined,"
160                 << " iend is different of (ibegin + ni - 1) !") ;
161      }
162      else
163      {
164         ERROR("CDomain::checkAttributes(void)",
165               << "The domain is wrong defined,"
166               << " 2 value from \'iend\', \'ibegin\', \'ni\'  must be defined !") ;
167      }
168
169
170      if (ni.getValue() < 0 || ibegin.getValue() > iend.getValue() ||
171          ibegin.getValue() < 1 || iend.getValue() > ni_glo.getValue())
172          {
173                  cout<<ni_glo<<"  "<<ni<<"  "<<ibegin<<"  "<<iend<<endl;
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() < 1 || jend.getValue() > nj_glo.getValue())
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 < 1  || zoom_jbegin < 1 || zoom_iend > ni_glo || zoom_jend > nj_glo)
492               ERROR("CDomain::checkZoom(void)",
493                     << "Zoom is wrong defined,"
494                     << " Check the values : zoom_ni, zoom_nj, zoom_ibegin, zoom_ibegin") ;
495         }
496      }
497      else
498      {
499         zoom_ni = ni_glo; 
500         zoom_nj = nj_glo;
501         zoom_ibegin = 1;
502         zoom_jbegin = 1;
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
538   void CDomain::checkAttributes(void)
539   {
540      if (this->isChecked) return;
541      CContext* context=CContext::getCurrent() ;
542
543      this->checkDomain();
544      this->checkZoom();
545      this->checkBounds();
546     
547      if (context->hasClient)
548      { // CÃŽté client uniquement
549         this->checkMask();
550         this->checkDomainData();
551         this->checkCompression();
552         this->completeLonLatClient();
553      }
554      else
555      { // CÃŽté serveur uniquement
556//         if (!this->isEmpty())
557// ne sert plus //   this->completeLonLatServer();
558      }
559   
560      if (context->hasClient)
561      {
562        computeConnectedServer() ;
563        sendServerAttribut() ;
564        sendLonLat() ;
565      }
566     
567      this->isChecked = true;
568   }
569   
570  void CDomain::sendServerAttribut(void)
571  {
572    int ni_srv=ni_glo.getValue() ;
573    int ibegin_srv=1 ;
574    int iend_srv=ni_glo.getValue() ;
575     
576    int nj_srv ;
577    int jbegin_srv ;
578    int jend_srv ;
579   
580    CContext* context=CContext::getCurrent() ;
581    CContextClient* client=context->client ;
582    int nbServer=client->serverSize ;
583    int serverRank=client->getServerLeader() ;
584   
585    jend_srv=0 ;
586    for(int i=0;i<=serverRank;i++)
587    {
588      jbegin_srv=jend_srv+1 ;
589      nj_srv=nj_glo.getValue()/nbServer ;
590      if (i<nj_glo.getValue()%nbServer) nj_srv++ ;
591      jend_srv=jbegin_srv+nj_srv-1 ;
592    }
593   
594     CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;   
595     if (client->isServerLeader())
596     {
597       CMessage msg ;
598       msg<<this->getId() ;
599       msg<<ni_srv<<ibegin_srv<<iend_srv<<nj_srv<<jbegin_srv<<jend_srv;
600       event.push(client->getServerLeader(),1,msg) ;
601       client->sendEvent(event) ;
602     }
603     else client->sendEvent(event) ;
604  }
605
606  void CDomain::computeConnectedServer(void)
607  {
608    int i,j,i_ind,j_ind ;
609   
610    ibegin_client=ibegin ; iend_client=iend ; ni_client=ni ;
611    jbegin_client=jbegin ; jend_client=jend ; nj_client=nj ;
612     
613    CContext* context = CContext::getCurrent() ;
614    CContextClient* client=context->client ;
615    int nbServer=client->serverSize ;
616
617    // find how much client are connected to a server
618    int zoom_iend=zoom_ibegin+zoom_ni-1 ;
619    int zoom_jend=zoom_jbegin+zoom_nj-1 ;
620   
621    int blockSize=nj_glo/nbServer ;
622    int ns=nj_glo%nbServer ;
623    int pos=ns*(blockSize+1) ;
624    int serverNum ;
625   
626    mapConnectedServer.resize(ni,nj) ;
627    vector<int> nbData(nbServer,0) ;
628    vector<int> indServer(nbServer,-1) ;
629    vector<bool> IsConnected(nbServer,false) ;
630   
631    for(j=0;j<nj;j++)
632      for(i=0;i<ni;i++)
633      {
634        i_ind=ibegin+i_index(i,j)-1 ;
635        j_ind=jbegin+j_index(i,j)-1 ;
636       
637        if (j_ind<pos) serverNum=j_ind/(blockSize+1) ;
638        else serverNum=ns+(j_ind-pos)/blockSize ;
639        IsConnected[serverNum]=true ;
640               
641        if (i_ind >= zoom_ibegin-1 && i_ind <= zoom_iend-1 && j_ind >= zoom_jbegin-1 && j_ind <= zoom_jend-1)
642        {
643          mapConnectedServer(i,j)=serverNum ;
644          nbData[serverNum]++ ;
645        }
646        else mapConnectedServer(i,j)=-1 ;
647      }
648
649
650    for(serverNum=0 ; serverNum<nbServer ; serverNum++) 
651      if (IsConnected[serverNum])
652      {
653        ns=connectedServer.size() ;
654        indServer[serverNum]=ns;
655        connectedServer.push_back(serverNum) ;
656        nbDataSrv.push_back(nbData[serverNum]) ;
657      }
658     
659     i_indSrv.resize(connectedServer.size()) ;
660     j_indSrv.resize(connectedServer.size()) ;
661
662     for(j=0;j<nj;j++)
663      for(i=0;i<ni;i++) 
664      {
665        if (mapConnectedServer(i,j)>=0)
666        {
667          ns=indServer[mapConnectedServer(i,j)] ;
668          mapConnectedServer(i,j)= ns ;
669          i_indSrv[ns].push_back(i+ibegin-1) ;
670          j_indSrv[ns].push_back(j+jbegin-1) ;
671        }
672      }
673         
674    int nbConnectedServer=connectedServer.size() ;
675
676    int* recvCount=new int[client->clientSize] ;
677    int* displ=new int[client->clientSize] ;
678    int* sendBuff=new int[nbConnectedServer] ;
679    valarray<int> nbClient(0,client->serverSize) ;
680   
681    for(int n=0;n<nbConnectedServer;n++) sendBuff[n]=connectedServer[n] ;
682   
683    // get connected server for everybody
684    MPI_Allgather(&nbConnectedServer,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm) ;
685   
686    displ[0]=0 ;
687    for(int n=1;n<client->clientSize;n++) displ[n]=displ[n-1]+recvCount[n-1] ;
688    int recvSize=displ[client->clientSize-1]+recvCount[client->clientSize-1] ;
689    int* recvBuff=new int[recvSize] ;
690 
691   
692    MPI_Allgatherv(sendBuff,nbConnectedServer,MPI_INT,recvBuff,recvCount,displ,MPI_INT,client->intraComm) ;
693    for(int n=0;n<recvSize;n++) nbClient[recvBuff[n]]++ ;
694   
695    for(int n=0;n<nbConnectedServer;n++) nbSenders.push_back(nbClient[connectedServer[n]]) ;
696   
697    delete [] recvCount ;
698    delete [] displ ;
699    delete [] sendBuff ;
700    delete [] recvBuff ;
701  }
702
703
704  void CDomain::sendLonLat(void)
705  {
706    int ns,n,i,j,ind,nv ;
707    CContext* context = CContext::getCurrent() ;
708    CContextClient* client=context->client ;
709    // send lon lat for each connected server
710    CEventClient event(getType(),EVENT_ID_LON_LAT) ;
711   
712    list<shared_ptr<CMessage> > list_msg ;   
713    list< CArray<int,1>* > list_indi,list_indj ;
714    list< CArray<double,1>* >list_lon,list_lat ;
715    list< CArray<double,2>* >list_boundslon,list_boundslat ;
716
717    for(int ns=0;ns<connectedServer.size();ns++)
718    {
719      int nbData = nbDataSrv[ns] ;
720      CArray<int,1> indi(nbData) ;
721      CArray<int,1> indj(nbData) ;
722      CArray<double,1> lon(nbData) ;
723      CArray<double,1> lat(nbData) ;
724      CArray<double,2> boundslon(nvertex,nbData) ;
725      CArray<double,2> boundslat(nvertex,nbData) ;
726     
727      for(n=0;n<nbData;n++) 
728      {
729        i=i_indSrv[ns][n] ;
730        j=j_indSrv[ns][n] ;
731        ind=(i-(zoom_ibegin_client-1))+(j-(zoom_jbegin_client-1))*zoom_ni_client ;
732       
733        lon(n)=lonvalue(ind) ;
734        lat(n)=latvalue(ind) ;
735        if (hasBounds)
736        {
737          for(nv=0;nv<nvertex;nv++)
738          {
739            boundslon(nv,n)=bounds_lon(nv,ind);
740            boundslat(nv,n)=bounds_lat(nv,ind);
741          }
742        }
743        indi(n)=ibegin+i_index(i-ibegin+1,j-jbegin+1)-1  ;
744        indj(n)=jbegin+j_index(i-ibegin+1,j-jbegin+1)-1  ;
745      }
746   
747      list_indi.push_back(new CArray<int,1>(indi.copy())) ;
748      list_indj.push_back(new CArray<int,1>(indj.copy())) ;
749      list_lon.push_back(new CArray<double,1>(lon.copy())) ;
750      list_lat.push_back(new CArray<double,1>(lat.copy())) ;
751      if (hasBounds) list_boundslon.push_back(new CArray<double,2>(boundslon.copy())) ;
752      if (hasBounds) list_boundslat.push_back(new CArray<double,2>(boundslat.copy())) ;
753
754      list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ;
755
756      *list_msg.back()<<this->getId()<<(int)type ; // enum ne fonctionne pour les message => ToFix
757      *list_msg.back()<<isCurvilinear ;
758      *list_msg.back()<<*list_indi.back()<<*list_indj.back()<<*list_lon.back()<<*list_lat.back() ;
759      if (hasBounds) *list_msg.back()<<*list_boundslon.back()<<*list_boundslat.back();
760      event.push(connectedServer[ns],nbSenders[ns],*list_msg.back()) ;
761    }
762
763    client->sendEvent(event) ;
764   
765   
766    for(list<CArray<int,1>* >::iterator it=list_indi.begin();it!=list_indi.end();it++) delete *it;
767    for(list<CArray<int,1>* >::iterator it=list_indj.begin();it!=list_indj.end();it++) delete *it;
768    for(list<CArray<double,1>* >::iterator it=list_lon.begin();it!=list_lon.end();it++)   delete *it;
769    for(list<CArray<double,1>* >::iterator it=list_lat.begin();it!=list_lat.end();it++)   delete *it;
770    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslon.begin();it!=list_boundslon.end();it++)   delete *it;
771    if (hasBounds) for(list<CArray<double,2>* >::iterator it=list_boundslat.begin();it!=list_boundslat.end();it++)   delete *it;   
772   
773  }
774 
775 
776  bool CDomain::dispatchEvent(CEventServer& event)
777   {
778     
779      if (SuperClass::dispatchEvent(event)) return true ;
780      else
781      {
782        switch(event.type)
783        {
784           case EVENT_ID_SERVER_ATTRIBUT :
785             recvServerAttribut(event) ;
786             return true ;
787             break ;
788           case EVENT_ID_LON_LAT :
789             recvLonLat(event) ;
790             return true ;
791             break ;
792           default :
793             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
794                    <<"Unknown Event") ;
795           return false ;
796         }
797      }
798   }
799   
800  void CDomain::recvServerAttribut(CEventServer& event)
801  {
802    CBufferIn* buffer=event.subEvents.begin()->buffer;
803    string domainId ;
804    *buffer>>domainId ;
805    get(domainId)->recvServerAttribut(*buffer) ;
806  }
807 
808  void CDomain::recvServerAttribut(CBufferIn& buffer)
809  {
810    int zoom_iend=zoom_ibegin.getValue()+zoom_ni.getValue()-1 ;
811    int zoom_jend=zoom_jbegin.getValue()+zoom_nj.getValue()-1 ;
812
813     buffer>>ni_srv>>ibegin_srv>>iend_srv>>nj_srv>>jbegin_srv>>jend_srv;
814     
815   
816    zoom_ibegin_srv = zoom_ibegin.getValue() > ibegin_srv ? zoom_ibegin.getValue() : ibegin_srv ;
817    zoom_iend_srv = zoom_iend < iend_srv ? zoom_iend : iend_srv ;
818    zoom_ni_srv=zoom_iend_srv-zoom_ibegin_srv+1 ;
819     
820    zoom_jbegin_srv = zoom_jbegin.getValue() > jbegin_srv ? zoom_jbegin.getValue() : jbegin_srv ;
821    zoom_jend_srv = zoom_jend < jend_srv ? zoom_jend : jend_srv ;
822    zoom_nj_srv=zoom_jend_srv-zoom_jbegin_srv+1 ;
823
824    if (zoom_ni_srv<=0 || zoom_nj_srv<=0) 
825    {
826      zoom_ibegin_srv=1 ; zoom_iend_srv=0 ; zoom_ni_srv=0 ;
827      zoom_jbegin_srv=1 ; zoom_jend_srv=0 ; zoom_nj_srv=0 ;
828    }
829    lonvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
830    lonvalue_srv = 0. ;
831    latvalue_srv.resize(zoom_ni_srv*zoom_nj_srv) ;
832    latvalue_srv = 0. ;
833    if (hasBounds) 
834    {
835      bounds_lon_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
836      bounds_lon_srv = 0. ;
837      bounds_lat_srv.resize(nvertex,zoom_ni_srv*zoom_nj_srv) ;
838      bounds_lat_srv = 0. ;
839    }
840  }
841   
842  void CDomain::recvLonLat(CEventServer& event)
843  {
844    list<CEventServer::SSubEvent>::iterator it ;
845    for (it=event.subEvents.begin();it!=event.subEvents.end();++it)
846    {
847      CBufferIn* buffer=it->buffer;
848      string domainId ;
849      *buffer>>domainId ;
850      get(domainId)->recvLonLat(*buffer) ;
851    }
852  }
853 
854  void CDomain::recvLonLat(CBufferIn& buffer)
855  {
856    CArray<int,1> indi ;
857    CArray<int,1> indj ;
858    CArray<double,1> lon ;
859    CArray<double,1> lat ;
860    CArray<double,2> boundslon ;
861    CArray<double,2> boundslat ;
862   
863    int type_int ;
864    buffer>>type_int>>isCurvilinear>>indi>>indj>>lon>>lat ;
865    if (hasBounds) buffer>>boundslon>>boundslat ;
866    type.setValue((type_attr::t_enum)type_int) ; // probleme des type enum avec les buffers : ToFix
867
868    int i,j,ind_srv ;
869    for(int ind=0;ind<indi.numElements();ind++)
870    {
871      i=indi(ind) ; j=indj(ind) ;
872      ind_srv=(i-(zoom_ibegin_srv-1))+(j-(zoom_jbegin_srv-1))*zoom_ni_srv ;
873      lonvalue_srv(ind_srv)=lon(ind) ;
874      latvalue_srv(ind_srv)=lat(ind) ;
875      if (hasBounds) 
876      {
877        for(int nv=0;nv<nvertex;nv++) 
878        {
879          bounds_lon_srv(nv,ind_srv)=boundslon(nv,ind) ;
880          bounds_lat_srv(nv,ind_srv)=boundslat(nv,ind) ;
881        }
882      }
883    }
884  }
885   //----------------------------------------------------------------
886   
887   
888   
889   ///---------------------------------------------------------------
890
891} // namespace xios
Note: See TracBrowser for help on using the repository browser.