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

Contents of /branches/diaplayground/cusplibrary/cusp/coo_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: 12314 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 coo_matrix.h
18 * \brief Coordinate matrix format
19 */
20
21 #pragma once
22
23 #include <cusp/detail/config.h>
24
25 #include <cusp/format.h>
26 #include <cusp/array1d.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 coo_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 coo_matrix : Coordinate 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 must be sorted by row 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 coo_matrix on the host with 6 nonzeros and then copies the
54 * matrix to the device.
55 *
56 * \code
57 * #include <cusp/coo_matrix.h>
58 * ...
59 *
60 * // allocate storage for (4,3) matrix with 6 nonzeros
61 * cusp::coo_matrix<int,float,cusp::host_memory> A(4,3,6);
62 *
63 * // initialize matrix entries on host
64 * A.row_indices[0] = 0; A.column_indices[0] = 0; A.values[0] = 10;
65 * A.row_indices[1] = 0; A.column_indices[1] = 2; A.values[1] = 20;
66 * A.row_indices[2] = 2; A.column_indices[2] = 2; A.values[2] = 30;
67 * A.row_indices[3] = 3; A.column_indices[3] = 0; A.values[3] = 40;
68 * A.row_indices[4] = 3; A.column_indices[4] = 1; A.values[4] = 50;
69 * A.row_indices[5] = 3; A.column_indices[5] = 2; A.values[5] = 60;
70 *
71 * // A now represents the following matrix
72 * // [10 0 20]
73 * // [ 0 0 0]
74 * // [ 0 0 30]
75 * // [40 50 60]
76 *
77 * // copy to the device
78 * cusp::coo_matrix<int,float,cusp::device_memory> B = A;
79 * \endcode
80 *
81 */
82 template <typename IndexType, typename ValueType, class MemorySpace>
83 class coo_matrix : public detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format>
84 {
85 typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format> Parent;
86 public:
87 /*! rebind matrix to a different MemorySpace
88 */
89 template<typename MemorySpace2>
90 struct rebind { typedef cusp::coo_matrix<IndexType, ValueType, MemorySpace2> type; };
91
92 /*! type of \c row_indices array
93 */
94 typedef typename cusp::array1d<IndexType, MemorySpace> row_indices_array_type;
95
96 /*! type of \c column_indices array
97 */
98 typedef typename cusp::array1d<IndexType, MemorySpace> column_indices_array_type;
99
100 /*! type of \c values array
101 */
102 typedef typename cusp::array1d<ValueType, MemorySpace> values_array_type;
103
104 /*! equivalent container type
105 */
106 typedef typename cusp::coo_matrix<IndexType, ValueType, MemorySpace> container;
107
108 /*! equivalent view type
109 */
110 typedef typename cusp::coo_matrix_view<typename row_indices_array_type::view,
111 typename column_indices_array_type::view,
112 typename values_array_type::view,
113 IndexType, ValueType, MemorySpace> view;
114
115 /*! equivalent const_view type
116 */
117 typedef typename cusp::coo_matrix_view<typename row_indices_array_type::const_view,
118 typename column_indices_array_type::const_view,
119 typename values_array_type::const_view,
120 IndexType, ValueType, MemorySpace> const_view;
121
122 /*! Storage for the row indices of the COO data structure.
123 */
124 row_indices_array_type row_indices;
125
126 /*! Storage for the column indices of the COO data structure.
127 */
128 column_indices_array_type column_indices;
129
130 /*! Storage for the nonzero entries of the COO data structure.
131 */
132 values_array_type values;
133
134 /*! Construct an empty \p coo_matrix.
135 */
136 coo_matrix() {}
137
138 /*! Construct a \p coo_matrix with a specific shape and number of nonzero entries.
139 *
140 * \param num_rows Number of rows.
141 * \param num_cols Number of columns.
142 * \param num_entries Number of nonzero matrix entries.
143 */
144 coo_matrix(size_t num_rows, size_t num_cols, size_t num_entries)
145 : Parent(num_rows, num_cols, num_entries),
146 row_indices(num_entries), column_indices(num_entries), values(num_entries) {}
147
148 /*! Construct a \p coo_matrix from another matrix.
149 *
150 * \param matrix Another sparse or dense matrix.
151 */
152 template <typename MatrixType>
153 coo_matrix(const MatrixType& matrix);
154
155 /*! Resize matrix dimensions and underlying storage
156 */
157 void resize(size_t num_rows, size_t num_cols, size_t num_entries)
158 {
159 Parent::resize(num_rows, num_cols, num_entries);
160 row_indices.resize(num_entries);
161 column_indices.resize(num_entries);
162 values.resize(num_entries);
163 }
164
165 /*! Swap the contents of two \p coo_matrix objects.
166 *
167 * \param matrix Another \p coo_matrix with the same IndexType and ValueType.
168 */
169 void swap(coo_matrix& matrix)
170 {
171 Parent::swap(matrix);
172 row_indices.swap(matrix.row_indices);
173 column_indices.swap(matrix.column_indices);
174 values.swap(matrix.values);
175 }
176
177 /*! Assignment from another matrix.
178 *
179 * \param matrix Another sparse or dense matrix.
180 */
181 template <typename MatrixType>
182 coo_matrix& operator=(const MatrixType& matrix);
183
184 /*! Sort matrix elements by row index
185 */
186 void sort_by_row(void);
187
188 /*! Sort matrix elements by row and column index
189 */
190 void sort_by_row_and_column(void);
191
192 /*! Determine whether matrix elements are sorted by row index
193 *
194 * \return \c false, if the row indices are unsorted; \c true, otherwise.
195 */
196 bool is_sorted_by_row(void);
197
198 /*! Determine whether matrix elements are sorted by row and column index
199 *
200 * \return \c false, if the row and column indices are unsorted; \c true, otherwise.
201 */
202 bool is_sorted_by_row_and_column(void);
203 }; // class coo_matrix
204 /*! \}
205 */
206
207 /*! \addtogroup sparse_matrix_views Sparse Matrix Views
208 * \ingroup sparse_matrices
209 * \{
210 */
211
212 /*! \p coo_matrix_view : Coordinate matrix view
213 *
214 * \tparam Array1 Type of \c row_indices array view
215 * \tparam Array2 Type of \c column_indices array view
216 * \tparam Array3 Type of \c values array view
217 * \tparam IndexType Type used for matrix indices (e.g. \c int).
218 * \tparam ValueType Type used for matrix values (e.g. \c float).
219 * \tparam MemorySpace A memory space (e.g. \c cusp::host_memory or cusp::device_memory)
220 *
221 */
222 template <typename Array1,
223 typename Array2,
224 typename Array3,
225 typename IndexType = typename Array1::value_type,
226 typename ValueType = typename Array3::value_type,
227 typename MemorySpace = typename cusp::minimum_space<typename Array1::memory_space, typename Array2::memory_space, typename Array3::memory_space>::type >
228 class coo_matrix_view : public cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format>
229 {
230 typedef cusp::detail::matrix_base<IndexType,ValueType,MemorySpace,cusp::coo_format> Parent;
231 public:
232 typedef Array1 row_indices_array_type;
233 typedef Array2 column_indices_array_type;
234 typedef Array3 values_array_type;
235
236 /*! equivalent container type
237 */
238 typedef typename cusp::coo_matrix<IndexType, ValueType, MemorySpace> container;
239
240 /*! equivalent view type
241 */
242 typedef typename cusp::coo_matrix_view<Array1, Array2, Array3, IndexType, ValueType, MemorySpace> view;
243
244 /*! View of the row indices of the COO data structure. Also called the "row pointer" array.
245 */
246 row_indices_array_type row_indices;
247
248 /*! View of the column indices of the COO data structure.
249 */
250 column_indices_array_type column_indices;
251
252 /*! View for the nonzero entries of the COO data structure.
253 */
254 values_array_type values;
255
256 // construct empty view
257 coo_matrix_view(void)
258 : Parent() {}
259
260 // construct from existing COO matrix or view
261 template <typename Matrix>
262 coo_matrix_view(Matrix& A)
263 : Parent(A),
264 row_indices(A.row_indices),
265 column_indices(A.column_indices),
266 values(A.values) {}
267
268 // TODO check sizes here
269 coo_matrix_view(size_t num_rows,
270 size_t num_cols,
271 size_t num_entries,
272 Array1 row_indices,
273 Array2 column_indices,
274 Array3 values)
275 : Parent(num_rows, num_cols, num_entries),
276 row_indices(row_indices),
277 column_indices(column_indices),
278 values(values) {}
279
280 /*! Resize matrix dimensions and underlying storage
281 */
282 void resize(size_t num_rows, size_t num_cols, size_t num_entries)
283 {
284 Parent::resize(num_rows, num_cols, num_entries);
285 row_indices.resize(num_entries);
286 column_indices.resize(num_entries);
287 values.resize(num_entries);
288 }
289
290 /*! Sort matrix elements by row index
291 */
292 void sort_by_row(void);
293
294 /*! Sort matrix elements by row and column index
295 */
296 void sort_by_row_and_column(void);
297
298 /*! Determine whether matrix elements are sorted by row index
299 *
300 * \return \c false, if the row indices are unsorted; \c true, otherwise.
301 */
302 bool is_sorted_by_row(void);
303
304 /*! Determine whether matrix elements are sorted by row and column index
305 *
306 * \return \c false, if the row and column indices are unsorted; \c true, otherwise.
307 */
308 bool is_sorted_by_row_and_column(void);
309 };
310
311 /* Convenience functions */
312
313 template <typename Array1,
314 typename Array2,
315 typename Array3>
316 coo_matrix_view<Array1,Array2,Array3>
317 make_coo_matrix_view(size_t num_rows,
318 size_t num_cols,
319 size_t num_entries,
320 Array1 row_indices,
321 Array2 column_indices,
322 Array3 values)
323 {
324 return coo_matrix_view<Array1,Array2,Array3>
325 (num_rows, num_cols, num_entries,
326 row_indices, column_indices, values);
327 }
328
329 template <typename Array1,
330 typename Array2,
331 typename Array3,
332 typename IndexType,
333 typename ValueType,
334 typename MemorySpace>
335 coo_matrix_view<Array1,Array2,Array3,IndexType,ValueType,MemorySpace>
336 make_coo_matrix_view(const coo_matrix_view<Array1,Array2,Array3,IndexType,ValueType,MemorySpace>& m)
337 {
338 return coo_matrix_view<Array1,Array2,Array3,IndexType,ValueType,MemorySpace>(m);
339 }
340
341 template <typename IndexType, typename ValueType, class MemorySpace>
342 typename coo_matrix<IndexType,ValueType,MemorySpace>::view
343 make_coo_matrix_view(coo_matrix<IndexType,ValueType,MemorySpace>& m)
344 {
345 return make_coo_matrix_view
346 (m.num_rows, m.num_cols, m.num_entries,
347 make_array1d_view(m.row_indices),
348 make_array1d_view(m.column_indices),
349 make_array1d_view(m.values));
350 }
351
352 template <typename IndexType, typename ValueType, class MemorySpace>
353 typename coo_matrix<IndexType,ValueType,MemorySpace>::const_view
354 make_coo_matrix_view(const coo_matrix<IndexType,ValueType,MemorySpace>& m)
355 {
356 return make_coo_matrix_view
357 (m.num_rows, m.num_cols, m.num_entries,
358 make_array1d_view(m.row_indices),
359 make_array1d_view(m.column_indices),
360 make_array1d_view(m.values));
361 }
362 /*! \}
363 */
364
365
366 } // end namespace cusp
367
368 #include <cusp/detail/coo_matrix.inl>
369

  ViewVC Help
Powered by ViewVC 1.1.26