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