Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
array2d.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2014 NVIDIA Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
21 #pragma once
22 
23 #include <cusp/detail/config.h>
24 
25 #include <cusp/memory.h>
26 #include <cusp/detail/format.h>
27 #include <cusp/array1d.h>
28 
29 #include <cusp/detail/array2d_format_utils.h>
30 #include <cusp/detail/matrix_base.h>
31 
32 #include <thrust/functional.h>
33 
34 namespace cusp
35 {
36 
37 // TODO document mapping of (i,j) onto values[pitch * i + j] or values[pitch * j + i]
38 // TODO document that array2d operations will try to respect .pitch of destination argument
39 
92 template<typename ValueType, typename MemorySpace, typename Orientation = cusp::row_major>
93 class array2d : public cusp::detail::matrix_base<int,ValueType,MemorySpace,cusp::array2d_format>
94 {
95 private:
96 
97  typedef typename cusp::detail::matrix_base<int,ValueType,MemorySpace,cusp::array2d_format> Parent;
98 
99 public:
101  typedef Orientation orientation;
102 
103  template<typename MemorySpace2>
104  struct rebind {
106  };
107 
108  typedef typename cusp::array1d<ValueType, MemorySpace> values_array_type;
109  typedef typename cusp::array2d<ValueType, MemorySpace, Orientation> container;
110 
113 
114  typedef cusp::detail::row_or_column_view<
115  typename values_array_type::iterator,thrust::detail::is_same<Orientation,cusp::row_major>::value>
116  row_view_type;
117 
118  typedef typename row_view_type::ArrayType row_view;
119 
120  typedef cusp::detail::row_or_column_view
121  <typename values_array_type::iterator,thrust::detail::is_same<Orientation,cusp::column_major>::value>
122  column_view_type;
123 
124  typedef typename column_view_type::ArrayType column_view;
125 
126  typedef cusp::detail::row_or_column_view
127  <typename values_array_type::const_iterator,thrust::detail::is_same<Orientation,cusp::row_major>::value>
128  const_row_view_type;
129 
130  typedef typename const_row_view_type::ArrayType const_row_view;
131 
132  typedef cusp::detail::row_or_column_view
133  <typename values_array_type::const_iterator,thrust::detail::is_same<Orientation,cusp::column_major>::value>
134  const_column_view_type;
135 
136  typedef typename const_column_view_type::ArrayType const_column_view;
141  size_t pitch;
142 
145  values_array_type values;
146 
149  array2d(void)
150  : Parent(), pitch(0), values(0) {}
151 
157  array2d(size_t num_rows, size_t num_cols)
158  : Parent(num_rows, num_cols, num_rows * num_cols),
159  pitch(cusp::detail::minor_dimension(num_rows, num_cols, orientation())),
160  values(num_rows * num_cols) {}
161 
168  array2d(size_t num_rows, size_t num_cols, const ValueType& value)
169  : Parent(num_rows, num_cols, num_rows * num_cols),
170  pitch(cusp::detail::minor_dimension(num_rows, num_cols, orientation())),
171  values(num_rows * num_cols, value) {}
172 
180  array2d(const size_t num_rows, const size_t num_cols, const ValueType& value, const size_t pitch);
181 
186  template <typename MatrixType>
187  array2d(const MatrixType& matrix);
188 
194  typename values_array_type::reference operator()(const size_t i, const size_t j);
195 
201  typename values_array_type::const_reference operator()(const size_t i, const size_t j) const;
202 
211  void resize(const size_t num_rows, const size_t num_cols);
212 
223  void resize(const size_t num_rows, const size_t num_cols, const size_t pitch);
224 
228  void swap(array2d& matrix);
229 
234  row_view row(const size_t i);
235 
240  column_view column(const size_t i);
241 
246  const_row_view row(const size_t i) const;
247 
252  const_column_view column(const size_t i) const;
253 
258  array2d& operator=(const array2d& matrix);
259 
265  template <typename MatrixType>
266  array2d& operator=(const MatrixType& matrix);
267 
268 }; // class array2d
318 template<typename ArrayView, class Orientation = cusp::row_major>
319 class array2d_view
320  : public cusp::detail::matrix_base<int,
321  typename ArrayView::value_type,
322  typename ArrayView::memory_space,
323  cusp::array2d_format>
324 {
325 private:
326 
327  typedef cusp::detail::matrix_base<int,
328  typename ArrayView::value_type,
329  typename ArrayView::memory_space,
330  cusp::array2d_format> Parent;
331 
332 public:
333 
335  typedef Orientation orientation;
336 
337  typedef ArrayView values_array_type;
338 
340 
341  typedef typename cusp::array2d_view<ArrayView, Orientation> view;
342 
343  typedef cusp::detail::row_or_column_view<
344  typename values_array_type::iterator,thrust::detail::is_same<Orientation,cusp::row_major>::value> row_view_type;
345  typedef typename row_view_type::ArrayType row_view;
346 
347  typedef cusp::detail::row_or_column_view<
348  typename values_array_type::iterator,thrust::detail::is_same<Orientation,cusp::column_major>::value> column_view_type;
349  typedef typename column_view_type::ArrayType column_view;
354  size_t pitch;
355 
358  values_array_type values;
359 
362  array2d_view(void)
363  : Parent(), pitch(0), values(0) {}
364 
368  array2d_view(const array2d_view& a)
369  : Parent(a), pitch(a.pitch), values(a.values) {}
370 
371  // TODO handle different Orientation (pitch = major)
372  //template <typename Array2, typename Orientation2>
373  //array2d_view(const array2d_view<Array2,Orientation2>& A)
374 
379  : Parent(a), pitch(a.pitch), values(a.values) {}
380 
389  template <typename Array2>
390  array2d_view(size_t num_rows, size_t num_cols, size_t pitch, const Array2& values)
391  : Parent(num_rows, num_cols, num_rows * num_cols), pitch(pitch), values(values) {}
392 
398  typename values_array_type::reference operator()(const size_t i, const size_t j) const;
399 
408  void resize(const size_t num_rows, const size_t num_cols);
409 
420  void resize(const size_t num_rows, const size_t num_cols, const size_t pitch);
421 
426  row_view row(size_t i);
427 
432  column_view column(size_t i);
433 
438  row_view row(size_t i) const;
439 
444  column_view column(size_t i) const;
445 }; // end array2d_view class
446 
447 
457 template <typename Iterator, typename Orientation>
459 make_array2d_view(size_t num_rows, size_t num_cols, size_t pitch, const cusp::array1d_view<Iterator>& values, Orientation)
460 {
461  return array2d_view<typename cusp::array1d_view<Iterator>,Orientation>(num_rows, num_cols, pitch, values);
462 }
463 
472 template <typename Array, typename Orientation>
475 {
477 }
478 
488 template<typename T, class MemorySpace, class Orientation>
491 {
492  return cusp::make_array2d_view(v.num_rows, v.num_cols, v.pitch, cusp::make_array1d_view(v.values), Orientation());
493 }
494 
504 template<typename T, class MemorySpace, class Orientation>
507 {
508  return cusp::make_array2d_view(v.num_rows, v.num_cols, v.pitch, cusp::make_array1d_view(v.values), Orientation());
509 }
513 } // end namespace cusp
514 
515 #include <cusp/detail/array2d.inl>
516 
void swap(array2d &matrix)
values_array_type::reference operator()(const size_t i, const size_t j)
The array1d_view class is a 1D vector container that wraps data from various iterators in a array1d d...
Definition: array1d.h:43
row_view row(const size_t i)
size_t pitch
Definition: array2d.h:141
void resize(const size_t num_rows, const size_t num_cols)
array2d(size_t num_rows, size_t num_cols)
Definition: array2d.h:157
array2d(size_t num_rows, size_t num_cols, const ValueType &value)
Definition: array2d.h:168
values_array_type values
Definition: array2d.h:357
The array2d_view is a view of a array2d container.
Definition: array2d.h:318
array2d(void)
Definition: array2d.h:149
array1d_view< Iterator > make_array1d_view(Iterator first, Iterator last)
Definition: array1d.h:693
column_view column(const size_t i)
array2d_view(void)
Definition: array2d.h:361
values_array_type::reference operator()(const size_t i, const size_t j) const
array2d_view< typename cusp::array1d_view< typename cusp::array1d< T, MemorySpace >::const_iterator >, Orientation > make_array2d_view(const cusp::array2d< T, MemorySpace, Orientation > &v)
Definition: array2d.h:505
values_array_type values
Definition: array2d.h:145
The array2d class is a 2D vector container that may contain elements stored in "host" or "device" mem...
Definition: array2d.h:93
void resize(const size_t num_rows, const size_t num_cols)
row_view row(size_t i)
array2d & operator=(const array2d &matrix)
1D array of elements that may reside in "host" or "device" memory space
column_view column(size_t i)