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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26