PARALUTION  1.0.0
PARALUTION
paralution_dealii.hpp
Go to the documentation of this file.
1 // Deal II path
2 #include <lac/sparse_matrix.h>
3 #include <lac/sparsity_pattern.h>
4 #include <lac/vector.h>
5 #include <lac/block_sparse_matrix.h>
6 
7 
8 // PARALUTION path
9 #include <paralution.hpp>
10 
13 template <typename ValueType>
14 void import_dealii_matrix(dealii::BlockSparseMatrix<ValueType> &deal_mat,
16 
17  // TODO
18  // change for a Base class
19 
20  // Matrix size
21  int n = deal_mat.n();
22  int m = deal_mat.m();
23  // int nnz = deal_mat.n_actually_nonzero_elements();
24  int nnz = deal_mat.n_nonzero_elements();
25 
26  // COO values
27  int *col = NULL;
28  int *row = NULL;
29  ValueType *val = NULL;
30 
31  paralution::allocate_host(nnz, &row);
32  paralution::allocate_host(nnz, &col);
33  paralution::allocate_host(nnz, &val);
34 
35  dealii::BlockSparseMatrix<double>::iterator it = deal_mat.begin(),
36  it_end = deal_mat.end();
37 
38  int i = 0;
39  for (; it!=it_end; ++it) {
40 
41  col[i] = it.operator*().column();
42  row[i] = it.operator*().row();
43  val[i] = it.operator*().value();
44 
45  ++i;
46  }
47 
48  assert(i == nnz);
49 
50  mat->SetDataPtrCOO(&row, &col, &val,
51  "Deal II Matrix",
52  nnz, n, m);
53 
54  mat->ConvertToCSR();
55 
56 } ;
57 
58 
61 template <typename ValueType>
62 void import_dealii_matrix(const dealii::SparsityPattern &sp,
63  const dealii::SparseMatrix<ValueType> &deal_mat,
65 
66  // Matrix size
67  int n = deal_mat.n();
68  int m = deal_mat.m();
69  // int nnz = deal_mat.n_actually_nonzero_elements();
70  int nnz = deal_mat.n_nonzero_elements();
71 
72  // COO values
73  int *col = NULL;
74  int *row = NULL;
75  ValueType *val = NULL;
76 
77  paralution::allocate_host(nnz, &row);
78  paralution::allocate_host(nnz, &col);
79  paralution::allocate_host(nnz, &val);
80 
81  dealii::SparsityPatternIterators::Iterator it = sp.begin();
82  dealii::SparsityPatternIterators::Iterator it_end = sp.end();
83 
84  int i = 0;
85  for (; it!=it_end; ++it) {
86 
87  col[i] = it->column();
88  row[i] = it->row();
89  val[i] = deal_mat.el(it->row(),it->column());
90 
91  ++i;
92  }
93 
94  assert(i == nnz);
95 
96  mat->SetDataPtrCOO(&row, &col, &val,
97  "Deal II Matrix",
98  nnz, n, m);
99 
100  // mat->ConvertToCSR();
101 
102 } ;
103 
105 template <typename ValueType>
106 void import_dealii_vector(const dealii::Vector<ValueType> &deal_vec,
108 
109  vec->MoveToHost();
110 
111  ValueType *val = NULL;
112  paralution::allocate_host(int(deal_vec.size()), &val);
113 
114  for (unsigned int i=0; i<deal_vec.size(); ++i)
115  val[i] = deal_vec[i];
116 
117  vec->SetDataPtr(&val, "DealII vector", int(deal_vec.size()));
118 
119 
120  // assert(int(vec->get_size()) == int(deal_vec.size()));
121  //
122  // This is slower
123  //
124  // for (unsigned int i=0; i<deal_vec.size(); ++i)
125  // (*vec)[i] = deal_vec[i];
126 
127 };
128 
130 template <typename ValueType>
132  dealii::Vector<ValueType> *deal_vec) {
133 
134  vec.MoveToHost();
135  assert(int(vec.get_size()) == int(deal_vec->size()));
136 
137  int size = vec.get_size();
138  ValueType *val = NULL;
139 
140  vec.LeaveDataPtr(&val);
141 
142  for (int i=0; i<size; ++i)
143  (*deal_vec)[i] = val[i];
144 
145  paralution::free_host(&val);
146 
147  //
148  // This is slower
149  //
150  // for (unsigned int i=0; i<deal_vec->size(); ++i)
151  // (*deal_vec)[i] = vec[i];
152 
153 };
154 
156 template <typename ValueType>
157 void import_dealii_vector(const dealii::BlockVector<ValueType> &deal_vec,
159 
160  vec->MoveToHost();
161 
162  ValueType *val = NULL;
163  paralution::allocate_host(int(deal_vec.size()), &val);
164 
165  for (unsigned int i=0; i<deal_vec.size(); ++i)
166  val[i] = deal_vec[i];
167 
168  vec->SetDataPtr(&val, "DealII vector", int(deal_vec.size()));
169 
170 
171  // assert(int(vec->get_size()) == int(deal_vec.size()));
172  //
173  // This is slower
174  //
175  // for (unsigned int i=0; i<deal_vec.size(); ++i)
176  // (*vec)[i] = deal_vec[i];
177 
178 };
179 
181 template <typename ValueType>
183  dealii::BlockVector<ValueType> *deal_vec) {
184 
185  vec.MoveToHost();
186  assert(int(vec.get_size()) == int(deal_vec->size()));
187 
188  int size = vec.get_size();
189  ValueType *val = NULL;
190 
191  vec.LeaveDataPtr(&val);
192 
193  for (int i=0; i<size; ++i)
194  (*deal_vec)[i] = val[i];
195 
196  paralution::free_host(&val);
197 
198  //
199  // This is slower
200  //
201  // for (unsigned int i=0; i<deal_vec->size(); ++i)
202  // (*deal_vec)[i] = vec[i];
203 
204 };
void ConvertToCSR(void)
Convert the matrix to CSR structure.
Definition: local_matrix.cpp:1387
void import_dealii_matrix(dealii::BlockSparseMatrix< ValueType > &deal_mat, paralution::LocalMatrix< ValueType > *mat)
Import (copy) a Deal.II block sparse matrix using its iterator and accessor into a paralution Local M...
Definition: paralution_dealii.hpp:14
void import_dealii_vector(const dealii::Vector< ValueType > &deal_vec, paralution::LocalVector< ValueType > *vec)
Import (copy) a Deal.II vector into a local vector.
Definition: paralution_dealii.hpp:106
virtual int get_size(void) const
Return the size of the vector.
Definition: local_vector.cpp:46
void allocate_host(const int size, DataType **ptr)
Allocate buffer on the host.
Definition: allocate_free.cpp:26
nnz
Definition: pcg_example.m:8
virtual void MoveToHost(void)
Move the object to the Host backend.
Definition: local_vector.cpp:354
void LeaveDataPtr(ValueType **ptr)
Get a pointer from the vector data and free the vector object.
Definition: local_vector.cpp:162
void SetDataPtrCOO(int **row, int **col, ValueType **val, std::string name, const int nnz, const int nrow, const int ncol)
Initialize a COO matrix on the Host with externally allocated data.
Definition: local_matrix.cpp:492
for i
Definition: pcg_example.m:13
Definition: local_matrix.hpp:15
void export_dealii_vector(paralution::LocalVector< ValueType > &vec, dealii::Vector< ValueType > *deal_vec)
Export (copy) a local vector into a Deal.II vector.
Definition: paralution_dealii.hpp:131
Definition: host_vector.hpp:13
void SetDataPtr(ValueType **ptr, std::string name, const int size)
Initialize a vector with externally allocated data.
Definition: local_vector.cpp:146
void free_host(DataType **ptr)
Free buffer on the host.
Definition: allocate_free.cpp:96