/usr/include/viennacl/coordinate_matrix.hpp is in libviennacl-dev 1.5.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | #ifndef VIENNACL_COORDINATE_MATRIX_HPP_
#define VIENNACL_COORDINATE_MATRIX_HPP_
/* =========================================================================
Copyright (c) 2010-2014, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the PDF manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
/** @file viennacl/coordinate_matrix.hpp
@brief Implementation of the coordinate_matrix class
*/
#include <map>
#include <vector>
#include <list>
#include "viennacl/forwards.h"
#include "viennacl/vector.hpp"
#include "viennacl/linalg/sparse_matrix_operations.hpp"
namespace viennacl
{
//provide copy-operation:
/** @brief Copies a sparse matrix from the host to the OpenCL device (either GPU or multi-core CPU)
*
* For the requirements on the CPU_MATRIX type, see the documentation of the function copy(CPU_MATRIX, compressed_matrix<>)
*
* @param cpu_matrix A sparse matrix on the host.
* @param gpu_matrix A compressed_matrix from ViennaCL
*/
template <typename CPU_MATRIX, typename SCALARTYPE, unsigned int ALIGNMENT>
void copy(const CPU_MATRIX & cpu_matrix,
coordinate_matrix<SCALARTYPE, ALIGNMENT> & gpu_matrix )
{
assert( (gpu_matrix.size1() == 0 || viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
assert( (gpu_matrix.size2() == 0 || viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
vcl_size_t group_num = 64;
// Step 1: Determine nonzeros:
if ( cpu_matrix.size1() > 0 && cpu_matrix.size2() > 0 )
{
vcl_size_t num_entries = 0;
for (typename CPU_MATRIX::const_iterator1 row_it = cpu_matrix.begin1();
row_it != cpu_matrix.end1();
++row_it)
{
for (typename CPU_MATRIX::const_iterator2 col_it = row_it.begin();
col_it != row_it.end();
++col_it)
{
++num_entries;
}
}
// Step 2: Set up matrix data:
gpu_matrix.nonzeros_ = num_entries;
gpu_matrix.rows_ = cpu_matrix.size1();
gpu_matrix.cols_ = cpu_matrix.size2();
viennacl::backend::typesafe_host_array<unsigned int> group_boundaries(gpu_matrix.handle3(), group_num + 1);
viennacl::backend::typesafe_host_array<unsigned int> coord_buffer(gpu_matrix.handle12(), 2*gpu_matrix.internal_nnz());
std::vector<SCALARTYPE> elements(gpu_matrix.internal_nnz());
vcl_size_t data_index = 0;
vcl_size_t current_fraction = 0;
group_boundaries.set(0, 0);
for (typename CPU_MATRIX::const_iterator1 row_it = cpu_matrix.begin1();
row_it != cpu_matrix.end1();
++row_it)
{
for (typename CPU_MATRIX::const_iterator2 col_it = row_it.begin();
col_it != row_it.end();
++col_it)
{
coord_buffer.set(2*data_index, col_it.index1());
coord_buffer.set(2*data_index + 1, col_it.index2());
elements[data_index] = *col_it;
++data_index;
}
while (data_index > (current_fraction + 1) / static_cast<double>(group_num) * num_entries) //split data equally over 64 groups
group_boundaries.set(++current_fraction, data_index);
}
//write end of last group:
group_boundaries.set(group_num, data_index);
//group_boundaries[1] = data_index; //for one compute unit
//std::cout << "Group boundaries: " << std::endl;
//for (vcl_size_t i=0; i<group_boundaries.size(); ++i)
// std::cout << group_boundaries[i] << std::endl;
viennacl::backend::memory_create(gpu_matrix.group_boundaries_, group_boundaries.raw_size(), traits::context(gpu_matrix.group_boundaries_), group_boundaries.get());
viennacl::backend::memory_create(gpu_matrix.coord_buffer_, coord_buffer.raw_size(), traits::context(gpu_matrix.coord_buffer_), coord_buffer.get());
viennacl::backend::memory_create(gpu_matrix.elements_, sizeof(SCALARTYPE)*elements.size(), traits::context(gpu_matrix.elements_), &(elements[0]));
}
}
/** @brief Copies a sparse matrix in the std::vector< std::map < > > format to an OpenCL device.
*
* @param cpu_matrix A sparse square matrix on the host.
* @param gpu_matrix A coordinate_matrix from ViennaCL
*/
template <typename SCALARTYPE, unsigned int ALIGNMENT>
void copy(const std::vector< std::map<unsigned int, SCALARTYPE> > & cpu_matrix,
coordinate_matrix<SCALARTYPE, ALIGNMENT> & gpu_matrix )
{
copy(tools::const_sparse_matrix_adapter<SCALARTYPE>(cpu_matrix, cpu_matrix.size(), cpu_matrix.size()), gpu_matrix);
}
//gpu to cpu:
/** @brief Copies a sparse matrix from the OpenCL device (either GPU or multi-core CPU) to the host.
*
* There are two type requirements on the CPU_MATRIX type (fulfilled by e.g. boost::numeric::ublas):
* - resize(rows, cols) A resize function to bring the matrix into the correct size
* - operator(i,j) Write new entries via the parenthesis operator
*
* @param gpu_matrix A coordinate_matrix from ViennaCL
* @param cpu_matrix A sparse matrix on the host.
*/
template <typename CPU_MATRIX, typename SCALARTYPE, unsigned int ALIGNMENT>
void copy(const coordinate_matrix<SCALARTYPE, ALIGNMENT> & gpu_matrix,
CPU_MATRIX & cpu_matrix )
{
assert( (viennacl::traits::size1(cpu_matrix) == gpu_matrix.size1()) && bool("Size mismatch") );
assert( (viennacl::traits::size2(cpu_matrix) == gpu_matrix.size2()) && bool("Size mismatch") );
if ( gpu_matrix.size1() > 0 && gpu_matrix.size2() > 0 )
{
//get raw data from memory:
viennacl::backend::typesafe_host_array<unsigned int> coord_buffer(gpu_matrix.handle12(), 2*gpu_matrix.nnz());
std::vector<SCALARTYPE> elements(gpu_matrix.nnz());
//std::cout << "GPU nonzeros: " << gpu_matrix.nnz() << std::endl;
viennacl::backend::memory_read(gpu_matrix.handle12(), 0, coord_buffer.raw_size(), coord_buffer.get());
viennacl::backend::memory_read(gpu_matrix.handle(), 0, sizeof(SCALARTYPE) * elements.size(), &(elements[0]));
//fill the cpu_matrix:
for (vcl_size_t index = 0; index < gpu_matrix.nnz(); ++index)
cpu_matrix(coord_buffer[2*index], coord_buffer[2*index+1]) = elements[index];
}
}
/** @brief Copies a sparse matrix from an OpenCL device to the host. The host type is the std::vector< std::map < > > format .
*
* @param gpu_matrix A coordinate_matrix from ViennaCL
* @param cpu_matrix A sparse matrix on the host.
*/
template <typename SCALARTYPE, unsigned int ALIGNMENT>
void copy(const coordinate_matrix<SCALARTYPE, ALIGNMENT> & gpu_matrix,
std::vector< std::map<unsigned int, SCALARTYPE> > & cpu_matrix)
{
tools::sparse_matrix_adapter<SCALARTYPE> temp(cpu_matrix, gpu_matrix.size1(), gpu_matrix.size2());
copy(gpu_matrix, temp);
}
//////////////////////// coordinate_matrix //////////////////////////
/** @brief A sparse square matrix, where entries are stored as triplets (i,j, val), where i and j are the row and column indices and val denotes the entry.
*
* The present implementation of coordinate_matrix suffers from poor runtime efficiency. Users are adviced to use compressed_matrix in the meanwhile.
*
* @tparam SCALARTYPE The floating point type (either float or double, checked at compile time)
* @tparam ALIGNMENT The internal memory size for the arrays, given by (size()/ALIGNMENT + 1) * ALIGNMENT. ALIGNMENT must be a power of two.
*/
template<class SCALARTYPE, unsigned int ALIGNMENT /* see forwards.h */ >
class coordinate_matrix
{
public:
typedef viennacl::backend::mem_handle handle_type;
typedef scalar<typename viennacl::tools::CHECK_SCALAR_TEMPLATE_ARGUMENT<SCALARTYPE>::ResultType> value_type;
typedef vcl_size_t size_type;
/** @brief Default construction of a coordinate matrix. No memory is allocated */
coordinate_matrix() : rows_(0), cols_(0), nonzeros_(0), group_num_(64) {}
explicit coordinate_matrix(viennacl::context ctx) : rows_(0), cols_(0), nonzeros_(0), group_num_(64)
{
group_boundaries_.switch_active_handle_id(ctx.memory_type());
coord_buffer_.switch_active_handle_id(ctx.memory_type());
elements_.switch_active_handle_id(ctx.memory_type());
#ifdef VIENNACL_WITH_OPENCL
if (ctx.memory_type() == OPENCL_MEMORY)
{
group_boundaries_.opencl_handle().context(ctx.opencl_context());
coord_buffer_.opencl_handle().context(ctx.opencl_context());
elements_.opencl_handle().context(ctx.opencl_context());
}
#endif
}
/** @brief Construction of a coordinate matrix with the supplied number of rows and columns. If the number of nonzeros is positive, memory is allocated
*
* @param rows Number of rows
* @param cols Number of columns
* @param nonzeros Optional number of nonzeros for memory preallocation
* @param ctx Optional context in which the matrix is created (one out of multiple OpenCL contexts, CUDA, host)
*/
coordinate_matrix(vcl_size_t rows, vcl_size_t cols, vcl_size_t nonzeros = 0, viennacl::context ctx = viennacl::context()) :
rows_(rows), cols_(cols), nonzeros_(nonzeros)
{
if (nonzeros > 0)
{
viennacl::backend::memory_create(group_boundaries_, viennacl::backend::typesafe_host_array<unsigned int>().element_size() * (group_num_ + 1), ctx);
viennacl::backend::memory_create(coord_buffer_, viennacl::backend::typesafe_host_array<unsigned int>().element_size() * 2 * internal_nnz(), ctx);
viennacl::backend::memory_create(elements_, sizeof(SCALARTYPE) * internal_nnz(), ctx);
}
else
{
group_boundaries_.switch_active_handle_id(ctx.memory_type());
coord_buffer_.switch_active_handle_id(ctx.memory_type());
elements_.switch_active_handle_id(ctx.memory_type());
#ifdef VIENNACL_WITH_OPENCL
if (ctx.memory_type() == OPENCL_MEMORY)
{
group_boundaries_.opencl_handle().context(ctx.opencl_context());
coord_buffer_.opencl_handle().context(ctx.opencl_context());
elements_.opencl_handle().context(ctx.opencl_context());
}
#endif
}
}
/** @brief Construction of a coordinate matrix with the supplied number of rows and columns in the supplied context. Does not yet allocate memory.
*
* @param rows Number of rows
* @param cols Number of columns
* @param ctx Context in which to create the matrix
*/
explicit coordinate_matrix(vcl_size_t rows, vcl_size_t cols, viennacl::context ctx)
: rows_(rows), cols_(cols), nonzeros_(0)
{
group_boundaries_.switch_active_handle_id(ctx.memory_type());
coord_buffer_.switch_active_handle_id(ctx.memory_type());
elements_.switch_active_handle_id(ctx.memory_type());
#ifdef VIENNACL_WITH_OPENCL
if (ctx.memory_type() == OPENCL_MEMORY)
{
group_boundaries_.opencl_handle().context(ctx.opencl_context());
coord_buffer_.opencl_handle().context(ctx.opencl_context());
elements_.opencl_handle().context(ctx.opencl_context());
}
#endif
}
/** @brief Allocate memory for the supplied number of nonzeros in the matrix. Old values are preserved. */
void reserve(vcl_size_t new_nonzeros)
{
if (new_nonzeros > nonzeros_) //TODO: Do we need to initialize new memory with zero?
{
handle_type coord_buffer_old;
handle_type elements_old;
viennacl::backend::memory_shallow_copy(coord_buffer_, coord_buffer_old);
viennacl::backend::memory_shallow_copy(elements_, elements_old);
vcl_size_t internal_new_nnz = viennacl::tools::align_to_multiple<vcl_size_t>(new_nonzeros, ALIGNMENT);
viennacl::backend::typesafe_host_array<unsigned int> size_deducer(coord_buffer_);
viennacl::backend::memory_create(coord_buffer_, size_deducer.element_size() * 2 * internal_new_nnz, viennacl::traits::context(coord_buffer_));
viennacl::backend::memory_create(elements_, sizeof(SCALARTYPE) * internal_new_nnz, viennacl::traits::context(elements_));
viennacl::backend::memory_copy(coord_buffer_old, coord_buffer_, 0, 0, size_deducer.element_size() * 2 * nonzeros_);
viennacl::backend::memory_copy(elements_old, elements_, 0, 0, sizeof(SCALARTYPE) * nonzeros_);
nonzeros_ = new_nonzeros;
}
}
/** @brief Resize the matrix.
*
* @param new_size1 New number of rows
* @param new_size2 New number of columns
* @param preserve If true, the old values are preserved. At present, old values are always discarded.
*/
void resize(vcl_size_t new_size1, vcl_size_t new_size2, bool preserve = true)
{
assert (new_size1 > 0 && new_size2 > 0);
if (new_size1 < rows_ || new_size2 < cols_) //enlarge buffer
{
std::vector<std::map<unsigned int, SCALARTYPE> > stl_sparse_matrix;
if (rows_ > 0)
stl_sparse_matrix.resize(rows_);
if (preserve && rows_ > 0)
viennacl::copy(*this, stl_sparse_matrix);
stl_sparse_matrix.resize(new_size1);
//std::cout << "Cropping STL matrix of size " << stl_sparse_matrix.size() << std::endl;
if (new_size2 < cols_ && rows_ > 0)
{
for (vcl_size_t i=0; i<stl_sparse_matrix.size(); ++i)
{
std::list<unsigned int> to_delete;
for (typename std::map<unsigned int, SCALARTYPE>::iterator it = stl_sparse_matrix[i].begin();
it != stl_sparse_matrix[i].end();
++it)
{
if (it->first >= new_size2)
to_delete.push_back(it->first);
}
for (std::list<unsigned int>::iterator it = to_delete.begin(); it != to_delete.end(); ++it)
stl_sparse_matrix[i].erase(*it);
}
//std::cout << "Cropping done..." << std::endl;
}
rows_ = new_size1;
cols_ = new_size2;
viennacl::copy(stl_sparse_matrix, *this);
}
rows_ = new_size1;
cols_ = new_size2;
}
/** @brief Returns the number of rows */
vcl_size_t size1() const { return rows_; }
/** @brief Returns the number of columns */
vcl_size_t size2() const { return cols_; }
/** @brief Returns the number of nonzero entries */
vcl_size_t nnz() const { return nonzeros_; }
/** @brief Returns the number of internal nonzero entries */
vcl_size_t internal_nnz() const { return viennacl::tools::align_to_multiple<vcl_size_t>(nonzeros_, ALIGNMENT); }
/** @brief Returns the OpenCL handle to the (row, column) index array */
const handle_type & handle12() const { return coord_buffer_; }
/** @brief Returns the OpenCL handle to the matrix entry array */
const handle_type & handle() const { return elements_; }
/** @brief Returns the OpenCL handle to the group start index array */
const handle_type & handle3() const { return group_boundaries_; }
vcl_size_t groups() const { return group_num_; }
#if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
template <typename CPU_MATRIX>
friend void copy(const CPU_MATRIX & cpu_matrix, coordinate_matrix & gpu_matrix );
#else
template <typename CPU_MATRIX, typename SCALARTYPE2, unsigned int ALIGNMENT2>
friend void copy(const CPU_MATRIX & cpu_matrix, coordinate_matrix<SCALARTYPE2, ALIGNMENT2> & gpu_matrix );
#endif
private:
/** @brief Copy constructor is by now not available. */
coordinate_matrix(coordinate_matrix const &);
/** @brief Assignment is by now not available. */
coordinate_matrix & operator=(coordinate_matrix const &);
vcl_size_t rows_;
vcl_size_t cols_;
vcl_size_t nonzeros_;
vcl_size_t group_num_;
handle_type coord_buffer_;
handle_type elements_;
handle_type group_boundaries_;
};
//
// Specify available operations:
//
/** \cond */
namespace linalg
{
namespace detail
{
// x = A * y
template <typename T, unsigned int A>
struct op_executor<vector_base<T>, op_assign, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
{
static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
{
// check for the special case x = A * x
if (viennacl::traits::handle(lhs) == viennacl::traits::handle(rhs.rhs()))
{
viennacl::vector<T> temp(lhs);
viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
lhs = temp;
}
else
viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), lhs);
}
};
template <typename T, unsigned int A>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
{
static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
{
viennacl::vector<T> temp(lhs);
viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
lhs += temp;
}
};
template <typename T, unsigned int A>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> >
{
static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_base<T>, op_prod> const & rhs)
{
viennacl::vector<T> temp(lhs);
viennacl::linalg::prod_impl(rhs.lhs(), rhs.rhs(), temp);
lhs -= temp;
}
};
// x = A * vec_op
template <typename T, unsigned int A, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_assign, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
{
static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
{
viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
viennacl::linalg::prod_impl(rhs.lhs(), temp, lhs);
}
};
// x += A * vec_op
template <typename T, unsigned int A, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_inplace_add, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
{
static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
{
viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
viennacl::vector<T> temp_result(lhs);
viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
lhs += temp_result;
}
};
// x -= A * vec_op
template <typename T, unsigned int A, typename LHS, typename RHS, typename OP>
struct op_executor<vector_base<T>, op_inplace_sub, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> >
{
static void apply(vector_base<T> & lhs, vector_expression<const coordinate_matrix<T, A>, const vector_expression<const LHS, const RHS, OP>, op_prod> const & rhs)
{
viennacl::vector<T> temp(rhs.rhs(), viennacl::traits::context(rhs));
viennacl::vector<T> temp_result(lhs);
viennacl::linalg::prod_impl(rhs.lhs(), temp, temp_result);
lhs -= temp_result;
}
};
} // namespace detail
} // namespace linalg
/** \endcond */
}
#endif
|