/[escript]/trunk/cusplibrary/cusp/dia_matrix.h
ViewVC logotype

Annotation of /trunk/cusplibrary/cusp/dia_matrix.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5225 - (hide annotations)
Mon Oct 27 01:33:45 2014 UTC (5 years, 8 months ago) by caltinay
File MIME type: text/plain
File size: 11810 byte(s)
dummy commit to see if submin is happy now.

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

  ViewVC Help
Powered by ViewVC 1.1.26