source: vendor/nemo/current/NEMOGCM/EXTERNAL/XIOS/src/array_impl.cpp @ 44

Last change on this file since 44 was 44, checked in by cholod, 12 years ago

Load NEMO_TMP into vendor/nemo/current.

File size: 7.1 KB
Line 
1#include "array.hpp"
2#include "array_mac.hpp"
3#include "buffer_in.hpp"
4#include "buffer_out.hpp"
5
6namespace xios
7{
8   
9   /// ////////////////////// Définitions ////////////////////// ///
10/*
11   template <typename ValueType, StdSize NumDims, typename Allocator>
12      template <typename ExtentList>
13         CArray<ValueType, NumDims, Allocator>::CArray(const ExtentList & sizes)
14            : boost::multi_array<ValueType, NumDims, Allocator>
15                  (sizes, boost::fortran_storage_order())
16   { }
17*/
18   template <typename ValueType, StdSize NumDims, typename Allocator>
19       CArray<ValueType, NumDims, Allocator>::CArray()
20            : boost::multi_array<ValueType, NumDims, Allocator>
21                  (getExtentNull<NumDims>(), boost::fortran_storage_order())
22   { /* Ne rien faire de plus */ }
23/*   
24   template <typename ValueType, StdSize NumDims, typename Allocator>
25      template <typename ExtentList>
26         CArray<ValueType, NumDims, Allocator>::CArray
27            (const ExtentList & sizes, const boost::general_storage_order<NumDims> & store)
28               : boost::multi_array<ValueType, NumDims, Allocator> (sizes, store)
29   {  }
30*/
31   template <typename ValueType, StdSize NumDims, typename Allocator>
32      CArray<ValueType, NumDims, Allocator>::~CArray(void)
33   { /* Ne rien faire de plus */ }
34
35   //----------------------------------------------------------------
36
37   template <typename ValueType, StdSize NumDims, typename Allocator>
38      StdOStream & operator << (StdOStream & os,
39                                const CArray<ValueType, NumDims, Allocator> & array)
40   {
41      os << "CArray (" ;
42      for (StdSize i = 1; i < array.num_dimensions(); i++)
43         os << ", " << array.shape()[i];
44      os << ") = " ; 
45      for (StdSize i = 0; i < array.num_elements(); i++) os << (array.data()[i])<<"  ";
46      return (os);
47   }
48
49   //----------------------------------------------------------------
50
51   template <typename ValueType, StdSize NumDims, typename Allocator>
52      StdOStream & operator << 
53         (StdOStream & os, const boost::shared_ptr<CArray<ValueType, NumDims, Allocator> > & array)
54   { 
55      os << *array; 
56      return (os); 
57   }
58   
59   //----------------------------------------------------------------
60   
61   template <typename ValueType> void FromBinary
62         (StdIStream & is, ARRAY(ValueType, 1) & array)
63   {
64      ARRAY_ASSIGN(array, ValueType, 1, [1]);
65      array->fromBinary(is);
66   }
67   
68   template <typename ValueType> void FromBinary
69         (StdIStream & is, ARRAY(ValueType, 2) & array)
70   {
71      ARRAY_ASSIGN(array, ValueType, 2, [1][1]);
72      array->fromBinary(is);
73   }
74
75   template <typename ValueType> void FromBinary
76         (StdIStream & is, ARRAY(ValueType, 3) & array)
77   {
78      ARRAY_ASSIGN(array, ValueType, 3, [1][1][1]);
79      array->fromBinary(is);
80   }
81   //----------------------------------------------------------------
82
83   template <typename ValueType, StdSize NumDims, typename Allocator>
84      void CArray<ValueType, NumDims, Allocator>::toBinary  (StdOStream & os) const
85   {
86      typedef boost::multi_array_types::size_type LSize;
87      LSize nelem = this->num_elements();
88      LSize ndim  = this->num_dimensions();
89      const LSize * shape = this->shape();
90      const ValueType * data = this->data();
91
92      os.write (reinterpret_cast<const char*>(&ndim) , sizeof(LSize));
93      for (LSize i = 0; i < ndim; i++ )
94         os.write (reinterpret_cast<const char*>(&(shape[i])), sizeof(LSize));
95      os.write (reinterpret_cast<const char*>(&nelem), sizeof(LSize));
96      os.write (reinterpret_cast<const char*>(data), nelem * sizeof(ValueType));
97   }
98
99   //----------------------------------------------------------------
100
101   template <typename ValueType, StdSize NumDims, typename Allocator>
102      void CArray<ValueType, NumDims, Allocator>::fromBinary(StdIStream & is)
103   {
104      typedef boost::multi_array_types::size_type LSize;
105      LSize ndim = 0, nelem = 0, temp = 0;
106      std::vector<LSize> shape;
107      is.read (reinterpret_cast<char*>(&ndim) , sizeof(LSize));
108      for (LSize i = 0; i < ndim; i++ )
109      {
110         is.read (reinterpret_cast<char*>(&temp) , sizeof(LSize));
111         shape.push_back(temp);
112      }
113      this->resize(shape);
114      is.read (reinterpret_cast<char*>(&nelem), sizeof(LSize));
115      is.read (reinterpret_cast<char*>(this->data()), nelem * sizeof(ValueType));
116   }
117 
118    template <typename ValueType, StdSize NumDims, typename Allocator>
119    size_t CArray<ValueType, NumDims, Allocator>::getSize(void) const
120   {
121      typedef boost::multi_array_types::size_type LSize;
122      LSize nelem = this->num_elements();
123      LSize ndim  = this->num_dimensions();
124      const LSize * shape = this->shape();
125      const ValueType * data = this->data();
126      size_t ret ;
127      ret=sizeof(ndim) ;
128      for (LSize i = 0; i < ndim; i++ ) ret+=sizeof(shape[i]) ;
129      ret+=sizeof(nelem) ;
130      ret+=sizeof(ValueType)*nelem ;
131      return ret ;
132   }
133
134   template <typename ValueType, StdSize NumDims, typename Allocator>
135   bool CArray<ValueType, NumDims, Allocator>::toBuffer(CBufferOut& buffer) const
136   {
137      typedef boost::multi_array_types::size_type LSize;
138
139      LSize nelem = this->num_elements();
140      LSize ndim  = this->num_dimensions();
141      const LSize* shape = this->shape();
142      const ValueType* data = this->data();
143      bool ret ;
144     
145      ret=buffer.put(ndim) ;
146      for (LSize i = 0; i < ndim; i++ ) ret&=buffer.put(shape[i]) ;
147      ret&=buffer.put(nelem) ;
148      ret&=buffer.put(data,nelem) ;
149      return ret ;
150  }
151
152   template <typename ValueType, StdSize NumDims, typename Allocator>
153   bool CArray<ValueType, NumDims, Allocator>::fromBuffer(CBufferIn& buffer)
154   {
155      typedef boost::multi_array_types::size_type LSize;
156      LSize ndim = 0, nelem = 0, temp = 0;
157      std::vector<LSize> shape;
158      bool ret ;
159     
160      ret=buffer.get(ndim) ;
161      for (LSize i = 0; i < ndim; i++ )
162      {
163         ret&=buffer.get(temp) ;
164         shape.push_back(temp);
165      }
166      this->resize(shape);
167      ret&=buffer.get(nelem) ;
168      ret&=buffer.get(this->data(),nelem) ;
169      return ret ;
170   }
171   
172 
173 
174  template class CArray<double,1> ;
175  template class CArray<double,2> ;
176  template class CArray<double,3> ;
177  template class CArray<double,4> ;
178 
179  template class CArray<float,1> ;
180  template class CArray<float,2> ;
181  template class CArray<float,3> ;
182  template class CArray<float,4> ;
183 
184  template class CArray<bool,1> ;
185  template class CArray<bool,2> ;
186  template class CArray<bool,3> ;
187  template class CArray<bool,4> ;
188 
189  template class CArray<int,1> ;
190  template class CArray<int,2> ;
191  template class CArray<int,3> ;
192  template class CArray<int,4> ;
193 
194  template class CArray<long int,1> ;
195  template class CArray<long int,2> ;
196  template class CArray<long int,3> ;
197  template class CArray<long int,4> ;
198 
199  template void FromBinary<double>(StdIStream & is, ARRAY(double, 1) & array) ;
200  template void FromBinary<int>(StdIStream & is, ARRAY(int, 1) & array) ;
201  template void FromBinary<double>(StdIStream & is, ARRAY(double, 2) & array) ;
202  template void FromBinary<bool>(StdIStream & is, ARRAY(bool, 2) & array) ;
203 
204   ///---------------------------------------------------------------
205   
206} // namespace xios
Note: See TracBrowser for help on using the repository browser.