CUSP
Loading...
Searching...
No Matches
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#include <cusp/detail/format.h>
25
26#include <cusp/array1d.h>
27#include <cusp/memory.h>
28
29#include <cusp/detail/array2d_format_utils.h>
30#include <cusp/detail/matrix_base.h>
31
32#include <thrust/functional.h>
33
34namespace 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
92template<typename ValueType, typename MemorySpace, typename Orientation = cusp::row_major>
93class array2d : public cusp::detail::matrix_base<int,ValueType,MemorySpace,cusp::array2d_format>
94{
95private:
96
97 typedef cusp::detail::matrix_base<int,ValueType,MemorySpace,cusp::array2d_format> Parent;
98
99public:
101 typedef Orientation orientation;
102
103 template<typename MemorySpace2>
104 struct rebind {
106 };
107
108 typedef cusp::array1d<ValueType, MemorySpace> values_array_type;
110
113
114 typedef cusp::detail::row_or_column_view<
115 typename values_array_type::iterator,::cuda::std::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,::cuda::std::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,::cuda::std::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,::cuda::std::is_same<Orientation,cusp::column_major>::value
134 > const_column_view_type;
135
136 typedef typename const_column_view_type::ArrayType const_column_view;
137
138 typedef typename cusp::detail::transpose_orientation<Orientation>::type transpose_orientation;
144 size_t pitch;
145
148 values_array_type values;
149
153 : Parent(), pitch(0), values(0) {}
154
160 array2d(size_t num_rows, size_t num_cols)
161 : Parent(num_rows, num_cols, num_rows * num_cols),
162 pitch(cusp::detail::minor_dimension(num_rows, num_cols, orientation())),
163 values(num_rows * num_cols) {}
164
171 array2d(size_t num_rows, size_t num_cols, const ValueType& value)
172 : Parent(num_rows, num_cols, num_rows * num_cols),
173 pitch(cusp::detail::minor_dimension(num_rows, num_cols, orientation())),
174 values(num_rows * num_cols, value) {}
175
183 array2d(const size_t num_rows, const size_t num_cols, const ValueType& value, const size_t pitch);
184
189 template <typename MatrixType>
190 array2d(const MatrixType& matrix);
191
197 typename values_array_type::reference operator()(const size_t i, const size_t j);
198
204 typename values_array_type::const_reference operator()(const size_t i, const size_t j) const;
205
214 void resize(const size_t num_rows, const size_t num_cols);
215
226 void resize(const size_t num_rows, const size_t num_cols, const size_t pitch);
227
231 void swap(array2d& matrix);
232
237 row_view row(const size_t i);
238
243 column_view column(const size_t i);
244
249 const_row_view row(const size_t i) const;
250
255 const_column_view column(const size_t i) const;
256
261 array2d& operator=(const array2d& matrix);
262
268 template <typename MatrixType>
269 array2d& operator=(const MatrixType& matrix);
270
275 transpose_const_view_type T(void) const;
276
277}; // class array2d
327template<typename ArrayView, class Orientation = cusp::row_major>
329 : public cusp::detail::matrix_base<int,
330 typename ArrayView::value_type,
331 typename ArrayView::memory_space,
332 cusp::array2d_format>
333{
334private:
335
336 typedef cusp::detail::matrix_base<int,
337 typename ArrayView::value_type,
338 typename ArrayView::memory_space,
339 cusp::array2d_format> Parent;
340
341public:
342
344 typedef Orientation orientation;
345
346 typedef ArrayView values_array_type;
347
349
351
352 typedef cusp::detail::row_or_column_view<
353 typename values_array_type::iterator,::cuda::std::is_same<Orientation,cusp::row_major>::value
354 > row_view_type;
355 typedef typename row_view_type::ArrayType row_view;
356
357 typedef cusp::detail::row_or_column_view<
358 typename values_array_type::iterator,::cuda::std::is_same<Orientation,cusp::column_major>::value
359 > column_view_type;
360 typedef typename column_view_type::ArrayType column_view;
361
362 typedef typename cusp::detail::transpose_orientation<Orientation>::type transpose_orientation;
363 typedef cusp::array2d_view<ArrayView, transpose_orientation> transpose_const_view_type;
368 size_t pitch;
369
372 values_array_type values;
373
377 : Parent(), pitch(0), values(0) {}
378
383 : Parent(a), pitch(a.pitch), values(a.values) {}
384
385 // TODO handle different Orientation (pitch = major)
386 //template <typename Array2, typename Orientation2>
387 //array2d_view(const array2d_view<Array2,Orientation2>& A)
388
394
403 template <typename Array2>
404 array2d_view(size_t num_rows, size_t num_cols, size_t pitch, const Array2& values)
405 : Parent(num_rows, num_cols, num_rows * num_cols), pitch(pitch), values(values) {}
406
412 typename values_array_type::reference operator()(const size_t i, const size_t j) const;
413
422 void resize(const size_t num_rows, const size_t num_cols);
423
434 void resize(const size_t num_rows, const size_t num_cols, const size_t pitch);
435
440 row_view row(size_t i);
441
446 column_view column(size_t i);
447
452 row_view row(size_t i) const;
453
458 column_view column(size_t i) const;
459
464 transpose_const_view_type T(void) const;
465}; // end array2d_view class
466
467
477template <typename Iterator, typename Orientation>
479make_array2d_view(size_t num_rows, size_t num_cols, size_t pitch, const cusp::array1d_view<Iterator>& values, Orientation)
480{
481 return array2d_view<typename cusp::array1d_view<Iterator>,Orientation>(num_rows, num_cols, pitch, values);
482}
483
492template <typename Array, typename Orientation>
493array2d_view<Array,Orientation>
498
508template<typename T, class MemorySpace, class Orientation>
509array2d_view<typename cusp::array1d_view<typename cusp::array1d<T,MemorySpace>::iterator>, Orientation>
514
524template<typename T, class MemorySpace, class Orientation>
525array2d_view<typename cusp::array1d_view<typename cusp::array1d<T,MemorySpace>::const_iterator>, Orientation>
527{
528 return cusp::make_array2d_view(v.num_rows, v.num_cols, v.pitch, cusp::make_array1d_view(v.values), Orientation());
529}
533} // end namespace cusp
534
535#include <cusp/detail/array2d.inl>
536
1D array of elements that may reside in "host" or "device" memory space
The array1d_view class is a 1D vector container that wraps data from various iterators in a array1d d...
Definition array1d.h:362
The array1d class is a 1D vector container that may contain elements stored in "host" or "device" mem...
Definition array1d.h:99
The array2d_view is a view of a array2d container.
Definition array2d.h:333
row_view row(size_t i)
values_array_type values
Definition array2d.h:372
void resize(const size_t num_rows, const size_t num_cols)
values_array_type::reference operator()(const size_t i, const size_t j) const
column_view column(size_t i) const
row_view row(size_t i) const
array2d_view(size_t num_rows, size_t num_cols, size_t pitch, const Array2 &values)
Definition array2d.h:404
void resize(const size_t num_rows, const size_t num_cols, const size_t pitch)
array2d_view(const array2d_view &a)
Definition array2d.h:382
transpose_const_view_type T(void) const
array2d_view(array2d< typename Parent::value_type, typename Parent::memory_space, orientation > &a)
Definition array2d.h:392
column_view column(size_t i)
The array2d class is a 2D vector container that may contain elements stored in "host" or "device" mem...
Definition array2d.h:94
values_array_type::const_reference operator()(const size_t i, const size_t j) const
size_t pitch
Definition array2d.h:144
values_array_type::reference operator()(const size_t i, const size_t j)
const_column_view column(const size_t i) const
array2d(const size_t num_rows, const size_t num_cols, const ValueType &value, const size_t pitch)
transpose_const_view_type T(void) const
column_view column(const size_t i)
array2d & operator=(const MatrixType &matrix)
array2d(const MatrixType &matrix)
void resize(const size_t num_rows, const size_t num_cols)
array2d(size_t num_rows, size_t num_cols, const ValueType &value)
Definition array2d.h:171
row_view row(const size_t i)
void resize(const size_t num_rows, const size_t num_cols, const size_t pitch)
const_row_view row(const size_t i) const
array2d & operator=(const array2d &matrix)
void swap(array2d &matrix)
values_array_type values
Definition array2d.h:148
array2d(void)
Definition array2d.h:152
array2d(size_t num_rows, size_t num_cols)
Definition array2d.h:160
array1d_view< Iterator > make_array1d_view(Iterator first, Iterator last)
Definition array1d.h:695
array2d_view< typename cusp::array1d_view< Iterator >, Orientation > make_array2d_view(size_t num_rows, size_t num_cols, size_t pitch, const cusp::array1d_view< Iterator > &values, Orientation)
Definition array2d.h:479