CUV  0.9.201304091348
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
cuda_array.hpp
Go to the documentation of this file.
1 //*LB*
2 // Copyright (c) 2010, University of Bonn, Institute for Computer Science VI
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the University of Bonn
14 // nor the names of its contributors may be used to endorse or promote
15 // products derived from this software without specific prior written
16 // permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //*LE*
29 
38 #ifndef __CUDA_ARRAY_HPP__
39 #define __CUDA_ARRAY_HPP__
40 
41 #include <cuv/basics/tensor.hpp>
42 #include <cuv/basics/matrix.hpp>
43 
44 class cudaArray; // forward declaration of cudaArray so we do not need to include cuda headers here
45 
46 
47 namespace cuv
48 {
52  template<class __value_type, class __memory_space_type, class __index_type = unsigned int >
53  class cuda_array
54  : public matrix<__value_type, __index_type>{
55  public:
56  typedef __memory_space_type memory_space_type;
58  typedef typename base_type::value_type value_type;
59  typedef typename base_type::index_type index_type;
61  using base_type::m_width;
62  using base_type::m_height;
64 
65  private:
66  cudaArray* m_ptr;
67  unsigned int m_dim;
68 
69  public:
78  cuda_array(const index_type& height, const index_type& width, const index_type& depth=1, const unsigned int dim=1)
79  :base_type(height, width)
80  ,m_depth(depth)
81  ,m_ptr(NULL)
82  ,m_dim(dim)
83  {
84  alloc();
85  }
86 
95  template<class S>
96  cuda_array(const tensor<value_type,S,row_major>& src, const unsigned int dim=1)
97  :base_type(0, 0)
98  , m_ptr(NULL)
99  , m_dim(dim)
100  {
101  if(src.ndim()==2){
102  m_height = src.shape()[0];
103  m_width = src.shape()[1];
104  m_depth = 1;
105  }else if(src.ndim()==3){
106  m_depth = src.shape()[0];
107  m_height = src.shape()[1];
108  m_width = src.shape()[2];
109  }
110  alloc();
111  assign(src);
112  }
114  dealloc();
115  }
116  inline index_type w()const{return m_width;}
117  inline index_type h()const{return m_height;}
118  inline index_type d()const{return m_depth;}
119  inline index_type n()const{return m_width*m_height*m_depth;}
120  inline index_type dim()const{return m_dim; }
121  inline cudaArray* ptr() {return m_ptr;}
122  inline const cudaArray* ptr() const{return m_ptr;}
123  void alloc();
124  void dealloc();
125 
141 
145  __value_type operator()(const __index_type& i, const __index_type& j)const;
146  };
147 }
148 
149 #endif /* __CUDA_ARRAY_HPP__ */