CUV  0.9.201304091348
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
io.hpp
Go to the documentation of this file.
1 // All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // * Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // * Neither the name of the University of Bonn
12 // nor the names of its contributors may be used to endorse or promote
13 // products derived from this software without specific prior written
14 // permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 //*LE*
27 
36 #ifndef __CUV_BASICS_IO_HPP__
37 #define __CUV_BASICS_IO_HPP__
38 #include <boost/serialization/base_object.hpp>
39 #include <boost/serialization/vector.hpp>
40 #include <boost/serialization/shared_ptr.hpp>
41 #include <boost/serialization/array.hpp>
42 #include <boost/serialization/split_free.hpp>
43 #include <cuv/basics/tensor.hpp>
44 
46 namespace boost
47 {
49  namespace serialization
50  {
83  template<class Archive, class V, class M>
84  void load(Archive& ar, cuv::memory<V,M>& m, const unsigned int version ){
85  typename cuv::memory<V,M>::size_type size;
86  ar >> size;
87  if(size){
88  V* tmp = new V[size];
89  ar >> make_array(tmp,size);
90  // copy to dev
92  V* tmpo;
93  a.alloc(&tmpo, sizeof(V)*size);
94  a.copy(tmpo,tmp,size, cuv::host_memory_space());
95  m.reset(tmpo, size);
96  delete[] tmp;
97  }
98  }
102  template<class Archive, class V, class M>
103  void save(Archive& ar, const cuv::memory<V,M>& m, const unsigned int version ){
104  // copy to host
105  typename cuv::memory<V,M>::size_type size = m.size();
106  ar << size;
107  if(m.size()){
108  V* tmp = new V[size];
110  a.copy(tmp,m.ptr(),size,M());
111  ar << make_array(tmp, size);
112  delete[] tmp;
113  }
114  }
122  template<class Archive, class V, class M>
123  void serialize(Archive& ar, cuv::memory<V,M>& m, const unsigned int version){
124  boost::serialization::split_free(ar, m, version);
125  }
126 
134  template<class Archive, class V>
135  void serialize(Archive& ar, cuv::pitched_memory<V,cuv::dev_memory_space>& m, const unsigned int version){
136  ar & boost::serialization::base_object<cuv::memory<V,cuv::dev_memory_space> >(m);
137  ar & m.m_rows
138  & m.m_cols
139  & m.m_pitch;
140  //boost::serialization::split_free(ar, m, version);
141  }
142 
143 
144  /****************************************************
145  * serialize linear memory
146  ****************************************************/
154  template<class Archive, class V, class MS>
155  void serialize(Archive& ar, cuv::linear_memory<V,MS>& m, const unsigned int version){
156  ar & boost::serialization::base_object<cuv::memory<V,MS> >(m);
157  //boost::serialization::split_free(ar, m, version);
158  }
159 
160 
161  /****************************************************
162  * serialize tensor
163  ****************************************************/
171  template<class Archive, class V, class MS,class ML>
172  void save(Archive& ar, const cuv::tensor<V,MS, ML>& t, const unsigned int version){
173  ar << t.info().host_shape
174  << t.info().host_stride;
175  ar << t.mem();
176  if(t.ndim()>0 && t.mem()){
177  long int i = (long int)t.ptr() - (long int)t.mem()->ptr();
178  ar << i;
179  }
180  }
188  template<class Archive, class V, class MS, class ML>
189  void load(Archive& ar, cuv::tensor<V,MS, ML>& t, const unsigned int version){
190  ar >> t.info().host_shape;
191  ar >> t.info().host_stride;
192  ar >> t.mem();
193  if(t.ndim()>0 && t.mem()){
194  long int i;
195  ar >> i;
196  t.set_ptr_offset(i);
197  }
198  }
206  template<class Archive, class V, class MS, class ML>
207  void serialize(Archive& ar, cuv::tensor<V,MS, ML>& t, const unsigned int version){
208  boost::serialization::split_free(ar, t, version);
209  }
210 
212  }
213 }
214 #endif /* __CUV_BASICS_IO_HPP__ */