/[escript]/branches/diaplayground/cusplibrary/cusp/ell_matrix.h
ViewVC logotype

Contents of /branches/diaplayground/cusplibrary/cusp/ell_matrix.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4955 - (show annotations)
Tue May 20 04:33:15 2014 UTC (5 years, 1 month ago) by caltinay
File MIME type: text/plain
File size: 12641 byte(s)
added pristine copy of cusplibrary (apache license) to be used by ripley.

1 /*
2 * Copyright 2008-2009 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
17 /*! \file ell_matrix.h
18 * \brief ELLPACK/ITPACK matrix format.
19 */
20
21 #pragma once
22
23 #include <cusp/detail/config.h>
24
25 #include <cusp/format.h>
26 #include <cusp/memory.h>
27 #include <cusp/detail/matrix_base.h>
28 #include <cusp/detail/utils.h>
29
30 namespace cusp
31 {
32
33 // Forward definitions
34 struct column_major;
35 template<typename ValueType, class MemorySpace, class Orientation> class array2d;
36 template<typename Array, class Orientation> class array2d_view;
37 template <typename Array1, typename Array2, typename IndexType, typename ValueType, typename MemorySpace> class ell_matrix_view;
38
39 /*! \addtogroup sparse_matrices Sparse Matrices
40 */
41
42 /*! \addtogroup sparse_matrix_containers Sparse Matrix Containers
43 * \ingroup sparse_matrices
44 * \{
45 */
46
47 /*! \p ell_matrix : ELLPACK/ITPACK matrix container
48 *
49 * \tparam IndexType Type used for matrix indices (e.g. \c int).
50 * \tparam ValueType Type used for matrix values (e.g. \c float).
51 * \tparam MemorySpace A memory space (e.g. \c cusp::host_memory or cusp::device_memory)
52 *
53 * \note The matrix entries must be sorted by column index.
54 * \note The matrix entries within each row should be shifted to the left.
55 * \note The matrix should not contain duplicate entries.
56 *
57 * The following code snippet demonstrates how to create a 4-by-3
58 * \p ell_matrix on the host with 3 nonzeros per row (6 total nonzeros)
59 * and then copies the matrix to the device.
60 *
61 * \code
62 * #include <cusp/ell_matrix.h>
63 * ...
64 *
65 * // allocate storage for (4,3) matrix with 6 nonzeros and at most 3 nonzeros per row.
66 * cusp::ell_matrix<int,float,cusp::host_memory> A(4,3,6,3);
67 *
68 * // X is used to fill unused entries in the matrix
69 * const int X = cusp::ell_matrix<int,float,cusp::host_memory>::invalid_index;
70 *
71 * // initialize matrix entries on host
72 * A.column_indices(0,0) = 0; A.values(0,0) = 10;
73 * A.column_indices(0,1) = 2; A.values(0,1) = 20; // shifted to leftmost position
74 * A.column_indices(0,2) = X; A.values(0,2) = 0; // padding
75 *
76 * A.column_indices(1,0) = X; A.values(1,0) = 0; // padding
77 * A.column_indices(1,1) = X; A.values(1,1) = 0; // padding
78 * A.column_indices(1,2) = X; A.values(1,2) = 0; // padding
79 *
80 * A.column_indices(2,0) = 2; A.values(2,0) = 30; // shifted to leftmost position
81 * A.column_indices(2,1) = X; A.values(2,1) = 0; // padding
82 * A.column_indices(2,2) = X; A.values(2,2) = 0; // padding
83 *
84 * A.column_indices(3,0) = 0; A.values(3,0) = 40;
85 * A.column_indices(3,1) = 1; A.values(3,1) = 50;
86 * A.column_indices(3,2) = 2; A.values(3,2) = 60;
87 *
88 * // A now represents the following matrix
89 * // [10 0 20]
90 * // [ 0 0 0]
91 * // [ 0 0 30]
92 * // [40 50 60]
93 *
94 * // copy to the device
95 * cusp::ell_matrix<int,float,cusp::device_memory> B = A;
96 * \endcode
97 *
98 */
99 template <typename IndexType, typename ValueType, class MemorySpace>
100 class ell_matrix : public detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::ell_format>
101 {
102 typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::ell_format> Parent;
103 public:
104 /*! rebind matrix to a different MemorySpace
105 */
106 template<typename MemorySpace2>
107 struct rebind { typedef cusp::ell_matrix<IndexType, ValueType, MemorySpace2> type; };
108
109 /*! type of column indices array
110 */
111 typedef typename cusp::array2d<IndexType, MemorySpace, cusp::column_major> column_indices_array_type;
112
113 /*! type of values array
114 */
115 typedef typename cusp::array2d<ValueType, MemorySpace, cusp::column_major> values_array_type;
116
117 /*! equivalent container type
118 */
119 typedef typename cusp::ell_matrix<IndexType, ValueType, MemorySpace> container;
120
121 /*! equivalent view type
122 */
123 typedef typename cusp::ell_matrix_view<typename column_indices_array_type::view,
124 typename values_array_type::view,
125 IndexType, ValueType, MemorySpace> view;
126
127 /*! equivalent const_view type
128 */
129 typedef typename cusp::ell_matrix_view<typename column_indices_array_type::const_view,
130 typename values_array_type::const_view,
131 IndexType, ValueType, MemorySpace> const_view;
132
133
134 /*! Value used to pad the rows of the column_indices array.
135 */
136 const static IndexType invalid_index = static_cast<IndexType>(-1);
137
138 /*! Storage for the column indices of the ELL data structure.
139 */
140 column_indices_array_type column_indices;
141
142 /*! Storage for the nonzero entries of the ELL data structure.
143 */
144 values_array_type values;
145
146 /*! Construct an empty \p ell_matrix.
147 */
148 ell_matrix() {}
149
150 /*! Construct an \p ell_matrix with a specific shape, number of nonzero entries,
151 * and maximum number of nonzero entries per row.
152 *
153 * \param num_rows Number of rows.
154 * \param num_cols Number of columns.
155 * \param num_entries Number of nonzero matrix entries.
156 * \param num_entries_per_row Maximum number of nonzeros per row.
157 * \param alignment Amount of padding used to align the data structure (default 32).
158 */
159 ell_matrix(size_t num_rows, size_t num_cols, size_t num_entries,
160 size_t num_entries_per_row, size_t alignment = 32)
161 : Parent(num_rows, num_cols, num_entries)
162 {
163 // TODO use array2d constructor when it can accept pitch
164 column_indices.resize(num_rows, num_entries_per_row, detail::round_up(num_rows, alignment));
165 values.resize (num_rows, num_entries_per_row, detail::round_up(num_rows, alignment));
166 }
167
168 /*! Construct an \p ell_matrix from another matrix.
169 *
170 * \param matrix Another sparse or dense matrix.
171 */
172 template <typename MatrixType>
173 ell_matrix(const MatrixType& matrix);
174
175 /*! Resize matrix dimensions and underlying storage
176 */
177 void resize(size_t num_rows, size_t num_cols, size_t num_entries,
178 size_t num_entries_per_row)
179 {
180 Parent::resize(num_rows, num_cols, num_entries);
181 column_indices.resize(num_rows, num_entries_per_row);
182 values.resize(num_rows, num_entries_per_row);
183 }
184
185 /*! Resize matrix dimensions and underlying storage
186 */
187 void resize(size_t num_rows, size_t num_cols, size_t num_entries,
188 size_t num_entries_per_row, size_t alignment)
189 {
190 Parent::resize(num_rows, num_cols, num_entries);
191 column_indices.resize(num_rows, num_entries_per_row, detail::round_up(num_rows, alignment));
192 values.resize (num_rows, num_entries_per_row, detail::round_up(num_rows, alignment));
193 }
194
195 /*! Swap the contents of two \p ell_matrix objects.
196 *
197 * \param matrix Another \p ell_matrix with the same IndexType and ValueType.
198 */
199 void swap(ell_matrix& matrix)
200 {
201 Parent::swap(matrix);
202 column_indices.swap(matrix.column_indices);
203 values.swap(matrix.values);
204 }
205
206 /*! Assignment from another matrix.
207 *
208 * \param matrix Another sparse or dense matrix.
209 */
210 template <typename MatrixType>
211 ell_matrix& operator=(const MatrixType& matrix);
212 }; // class ell_matrix
213 /*! \}
214 */
215
216
217 /*! \addtogroup sparse_matrix_views Sparse Matrix Views
218 * \ingroup sparse_matrices
219 * \{
220 */
221
222 /*! \p ell_matrix_view : ELLPACK/ITPACK matrix view
223 *
224 * \tparam Array1 Type of \c column_indices array view
225 * \tparam Array2 Type of \c values array view
226 * \tparam IndexType Type used for matrix indices (e.g. \c int).
227 * \tparam ValueType Type used for matrix values (e.g. \c float).
228 * \tparam MemorySpace A memory space (e.g. \c cusp::host_memory or cusp::device_memory)
229 *
230 */
231 template <typename Array1,
232 typename Array2,
233 typename IndexType = typename Array1::value_type,
234 typename ValueType = typename Array2::value_type,
235 typename MemorySpace = typename cusp::minimum_space<typename Array1::memory_space, typename Array2::memory_space>::type >
236 class ell_matrix_view : public detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::ell_format>
237 {
238 typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::ell_format> Parent;
239 public:
240 /*! type of \c column_indices array
241 */
242 typedef Array1 column_indices_array_type;
243
244 /*! type of \c values array
245 */
246 typedef Array2 values_array_type;
247
248 /*! equivalent container type
249 */
250 typedef typename cusp::ell_matrix<IndexType, ValueType, MemorySpace> container;
251
252 /*! equivalent view type
253 */
254 typedef typename cusp::ell_matrix_view<Array1, Array2, IndexType, ValueType, MemorySpace> view;
255
256 /*! Value used to pad the rows of the column_indices array.
257 */
258 const static IndexType invalid_index = static_cast<IndexType>(-1);
259
260 /*! View to column indices of the ELL data structure.
261 */
262 column_indices_array_type column_indices;
263
264 /*! View to nonzero entries of the ELL data structure.
265 */
266 values_array_type values;
267
268 /*! Construct an empty \p ell_matrix_view.
269 */
270 ell_matrix_view() {}
271
272 template <typename OtherArray1, typename OtherArray2>
273 ell_matrix_view(size_t num_rows, size_t num_cols, size_t num_entries,
274 OtherArray1& column_indices, OtherArray2& values)
275 : Parent(num_rows, num_cols, num_entries), column_indices(column_indices), values(values) {}
276
277 template <typename OtherArray1, typename OtherArray2>
278 ell_matrix_view(size_t num_rows, size_t num_cols, size_t num_entries,
279 const OtherArray1& column_indices, const OtherArray2& values)
280 : Parent(num_rows, num_cols, num_entries), column_indices(column_indices), values(values) {}
281
282 template <typename Matrix>
283 ell_matrix_view(Matrix& A)
284 : Parent(A), column_indices(A.column_indices), values(A.values) {}
285
286 template <typename Matrix>
287 ell_matrix_view(const Matrix& A)
288 : Parent(A), column_indices(A.column_indices), values(A.values) {}
289
290 /*! Resize matrix dimensions and underlying storage
291 */
292 void resize(size_t num_rows, size_t num_cols, size_t num_entries,
293 size_t num_entries_per_row)
294 {
295 Parent::resize(num_rows, num_cols, num_entries);
296 column_indices.resize(num_rows, num_entries_per_row);
297 values.resize(num_rows, num_entries_per_row);
298 }
299
300 /*! Resize matrix dimensions and underlying storage
301 */
302 void resize(size_t num_rows, size_t num_cols, size_t num_entries,
303 size_t num_entries_per_row, size_t alignment)
304 {
305 Parent::resize(num_rows, num_cols, num_entries);
306 column_indices.resize(num_rows, num_entries_per_row, detail::round_up(num_rows, alignment));
307 values.resize (num_rows, num_entries_per_row, detail::round_up(num_rows, alignment));
308 }
309 }; // class ell_matrix_view
310
311
312 template <typename Array1,
313 typename Array2>
314 ell_matrix_view<Array1,Array2>
315 make_ell_matrix_view(size_t num_rows,
316 size_t num_cols,
317 size_t num_entries,
318 Array1 column_indices,
319 Array2 values);
320
321 template <typename Array1,
322 typename Array2,
323 typename IndexType,
324 typename ValueType,
325 typename MemorySpace>
326 ell_matrix_view<Array1,Array2,IndexType,ValueType,MemorySpace>
327 make_ell_matrix_view(const ell_matrix_view<Array1,Array2,IndexType,ValueType,MemorySpace>& m);
328
329 template <typename IndexType, typename ValueType, class MemorySpace>
330 typename ell_matrix<IndexType,ValueType,MemorySpace>::view
331 make_ell_matrix_view(ell_matrix<IndexType,ValueType,MemorySpace>& m);
332
333 template <typename IndexType, typename ValueType, class MemorySpace>
334 typename ell_matrix<IndexType,ValueType,MemorySpace>::const_view
335 make_ell_matrix_view(const ell_matrix<IndexType,ValueType,MemorySpace>& m);
336 /*! \}
337 */
338
339 } // end namespace cusp
340
341 #include <cusp/array2d.h>
342 #include <cusp/detail/ell_matrix.inl>
343

  ViewVC Help
Powered by ViewVC 1.1.26