4#include <thrust/host_vector.h>
5#include <cusp/coo_matrix.h>
44template<
class value_type>
65 int num_blocks_per_line,
int num_different_blocks,
int n):
66 data(num_different_blocks*
n*
n),
67 cols_idx( num_block_rows*num_blocks_per_line),
91 cusp::coo_matrix<int, value_type, cusp::host_memory>
asCuspMatrix()
const;
124 void display( std::ostream& os = std::cout,
bool show_data =
false)
const;
126 thrust::host_vector<value_type>
data;
177template<
class value_type>
203 void add_value(
int row,
int col,
const thrust::host_vector<value_type>& element)
205 assert( (
int)element.size() ==
n*
n);
206 int index =
data.size()/
n/
n;
207 data.insert(
data.end(), element.begin(), element.end());
242 void display(std::ostream& os = std::cout,
bool show_data =
false)
const;
244 thrust::host_vector<value_type>
data;
257template<
class value_type>
261 for(
int s=0; s<left_size; s++)
262 for(
int i=0; i<num_rows; i++)
263 for(
int k=0; k<n; k++)
264 for(
int j=right_range[0]; j<right_range[1]; j++)
266 int I = ((s*num_rows + i)*n+k)*right_size+j;
268 y[I] = beta == 0 ? (value_type)0 :
y[I]*beta;
269 for(
int d=0; d<blocks_per_line; d++)
272 for(
int q=0; q<n; q++)
273 temp = DG_FMA( data[ (data_idx[i*blocks_per_line+d]*n + k)*n+q],
274 x[((s*num_cols + cols_idx[i*blocks_per_line+d])*n+q)*right_size+j],
276 y[I] = DG_FMA( alpha,temp,
y[I]);
280template<
class value_type>
283 cusp::array1d<value_type, cusp::host_memory> values;
284 cusp::array1d<int, cusp::host_memory> row_indices;
285 cusp::array1d<int, cusp::host_memory> column_indices;
286 for(
int s=0; s<left_size; s++)
287 for(
int i=0; i<num_rows; i++)
288 for(
int k=0; k<n; k++)
289 for(
int j=right_range[0]; j<right_range[1]; j++)
291 int I = ((s*num_rows + i)*n+k)*right_size+j;
292 for(
int d=0; d<blocks_per_line; d++)
293 for(
int q=0; q<n; q++)
295 row_indices.push_back(I);
296 column_indices.push_back(
297 ((s*num_cols + cols_idx[i*blocks_per_line+d])*n+q)*right_size+j);
298 values.push_back(data[ (data_idx[i*blocks_per_line+d]*n + k)*n+q]);
301 cusp::coo_matrix<int, value_type, cusp::host_memory> A(
302 total_num_rows(), total_num_cols(), values.size());
303 A.row_indices = row_indices;
304 A.column_indices = column_indices;
309template<
class value_type>
315 std::cerr <<
"Beta != 1 yields wrong results in CooSparseBlockMat!! Beta = "<<beta<<
"\n";
316 assert( beta == 1 &&
"Beta != 1 yields wrong results in CooSparseBlockMat!!");
320 for(
int s=0; s<left_size; s++)
321 for(
int k=0; k<n; k++)
322 for(
int j=0; j<right_size; j++)
323 for(
int i=0; i<num_entries; i++)
326 for(
int q=0; q<n; q++)
327 temp = DG_FMA( data[ (data_idx[i]*n + k)*n+q],
329 x[cols_idx[i]][(q*left_size +s )*right_size+j],
331 int I = ((s*num_rows + rows_idx[i])*n+k)*right_size+j;
332 y[I] = DG_FMA( alpha,temp, y[I]);
339 os <<
"Data array has "<<data.size()/n/n<<
" blocks of size "<<n<<
"x"<<n<<
"\n";
340 os <<
"num_rows "<<num_rows<<
"\n";
341 os <<
"num_cols "<<num_cols<<
"\n";
342 os <<
"blocks_per_line "<<blocks_per_line<<
"\n";
344 os <<
"left_size "<<left_size<<
"\n";
345 os <<
"right_size "<<right_size<<
"\n";
346 os <<
"right_range_0 "<<right_range[0]<<
"\n";
347 os <<
"right_range_1 "<<right_range[1]<<
"\n";
348 os <<
"Column indices: \n";
349 for(
int i=0; i<num_rows; i++)
351 for(
int d=0; d<blocks_per_line; d++)
352 os << cols_idx[i*blocks_per_line + d] <<
" ";
355 os <<
"\n Data indices: \n";
356 for(
int i=0; i<num_rows; i++)
358 for(
int d=0; d<blocks_per_line; d++)
359 os << data_idx[i*blocks_per_line + d] <<
" ";
365 for(
unsigned i=0; i<data.size()/n/n; i++)
366 for(
unsigned k=0; k<n*n; k++)
369 res.
d = data[i*n*n+k];
370 os <<
"idx "<<i<<
" "<<res.
d <<
"\t"<<res.
i<<
"\n";
376template<
class value_type>
379 os <<
"Data array has "<<data.size()/n/n<<
" blocks of size "<<n<<
"x"<<n<<
"\n";
380 os <<
"num_rows "<<num_rows<<
"\n";
381 os <<
"num_cols "<<num_cols<<
"\n";
382 os <<
"num_entries "<<num_entries<<
"\n";
384 os <<
"left_size "<<left_size<<
"\n";
385 os <<
"right_size "<<right_size<<
"\n";
386 os <<
"row\tcolumn\tdata:\n";
387 for(
int i=0; i<num_entries; i++)
388 os << rows_idx[i]<<
"\t"<<cols_idx[i] <<
"\t"<<data_idx[i]<<
"\n";
392 for(
unsigned i=0; i<data.size()/n/n; i++)
393 for(
unsigned k=0; k<n*n; k++)
396 res.
d = data[i*n*n+k];
397 os <<
"idx "<<i<<
" "<<res.
d <<
"\t"<<res.
i<<
"\n";
Error classes or the dg library.
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Coo Sparse Block Matrix format.
Definition: sparseblockmat.h:179
int num_rows
number of rows
Definition: sparseblockmat.h:248
int total_num_cols() const
total number of columns is num_cols*n*left_size*right_size
Definition: sparseblockmat.h:220
thrust::host_vector< int > data_idx
is of size num_entries and contains indices into the data array
Definition: sparseblockmat.h:247
thrust::host_vector< value_type > data
The data array is of size n*n*num_different_blocks and contains the blocks.
Definition: sparseblockmat.h:244
void display(std::ostream &os=std::cout, bool show_data=false) const
Display internal data to a stream.
thrust::host_vector< int > rows_idx
is of size num_entries and contains the row indices
Definition: sparseblockmat.h:246
int left_size
size of the left Kronecker delta
Definition: sparseblockmat.h:252
int n
each block has size n*n
Definition: sparseblockmat.h:251
void symv(SharedVectorTag, SerialTag, value_type alpha, const value_type **x, value_type beta, value_type *RESTRICT y) const
Apply the matrix to a vector.
void add_value(int row, int col, const thrust::host_vector< value_type > &element)
Convenience function to assemble the matrix.
Definition: sparseblockmat.h:203
int total_num_rows() const
total number of rows is num_rows*n*left_size*right_size
Definition: sparseblockmat.h:216
int num_cols
number of columns
Definition: sparseblockmat.h:249
CooSparseBlockMat(int num_block_rows, int num_block_cols, int n, int left_size, int right_size)
Allocate storage.
Definition: sparseblockmat.h:191
CooSparseBlockMat()=default
default constructor does nothing
thrust::host_vector< int > cols_idx
is of size num_entries and contains the column indices
Definition: sparseblockmat.h:245
int right_size
size of the right Kronecker delta (is e.g 1 for a x - derivative)
Definition: sparseblockmat.h:253
int num_entries
number of entries in the matrix
Definition: sparseblockmat.h:250
Ell Sparse Block Matrix format.
Definition: sparseblockmat.h:46
void display(std::ostream &os=std::cout, bool show_data=false) const
Display internal data to a stream.
int total_num_cols() const
total number of columns is num_cols*n*left_size*right_size
Definition: sparseblockmat.h:82
EllSparseBlockMat()=default
default constructor does nothing
thrust::host_vector< int > data_idx
has the same size as cols_idx and contains indices into the data array, i.e. the block number
Definition: sparseblockmat.h:128
void set_default_range()
Set right_range[0] = 0, right_range[1] = right_size
Definition: sparseblockmat.h:105
int blocks_per_line
number of blocks in each line
Definition: sparseblockmat.h:132
int num_rows
number of block rows, each row contains blocks
Definition: sparseblockmat.h:130
void set_right_size(int new_right_size)
Set right_size = new_right_size; set_default_range();
Definition: sparseblockmat.h:110
thrust::host_vector< value_type > data
The data array is of size n*n*num_different_blocks and contains the blocks. The first block is contai...
Definition: sparseblockmat.h:126
EllSparseBlockMat(int num_block_rows, int num_block_cols, int num_blocks_per_line, int num_different_blocks, int n)
Allocate storage.
Definition: sparseblockmat.h:64
int total_num_rows() const
total number of rows is num_rows*n*left_size*right_size
Definition: sparseblockmat.h:78
void set_left_size(int new_left_size)
Set left_size = new_left_size;
Definition: sparseblockmat.h:115
int left_size
size of the left Kronecker delta
Definition: sparseblockmat.h:134
cusp::coo_matrix< int, value_type, cusp::host_memory > asCuspMatrix() const
Convert to a cusp coordinate sparse matrix.
void symv(SharedVectorTag, SerialTag, value_type alpha, const value_type *RESTRICT x, value_type beta, value_type *RESTRICT y) const
Apply the matrix to a vector.
int num_cols
number of block columns
Definition: sparseblockmat.h:131
thrust::host_vector< int > cols_idx
is of size num_rows*num_blocks_per_line and contains the column indices % n into the vector
Definition: sparseblockmat.h:127
int n
each block has size n*n
Definition: sparseblockmat.h:133
thrust::host_vector< int > right_range
range (can be used to apply the matrix to only part of the right rows
Definition: sparseblockmat.h:129
int right_size
size of the right Kronecker delta (is e.g 1 for a x - derivative)
Definition: sparseblockmat.h:135
Indicate sequential execution.
Definition: execution_policy.h:26
Indicate a contiguous chunk of shared memory.
Definition: vector_categories.h:41
indicate our sparse block matrix format
Definition: matrix_categories.h:33
T value_type
Definition: sparseblockmat.h:416
T value_type
Definition: sparseblockmat.h:410
The vector traits.
Definition: tensor_traits.h:31