matrix_addition.cc File Reference
#include "generic.h"

Functions

template<typename myType >
void construct_vector (myType given_array[], unsigned given_arraysize, Vector< myType > &result_vector)
 
template<typename myType >
void output_vector (Vector< myType > &given_vector)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ construct_vector()

template<typename myType >
void construct_vector ( myType  given_array[],
unsigned  given_arraysize,
Vector< myType > &  result_vector 
)
36 {
37  // Clear and reserve the required memory.
38  result_vector.clear();
39  result_vector.reserve(given_arraysize);
40 
41  for (unsigned i = 0; i < given_arraysize; i++)
42  {
43  result_vector.push_back(given_array[i]);
44  }
45 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9

References i.

Referenced by main().

◆ main()

int main ( int argc  ,
char argv[] 
)

Driver code: Testing CRDoubleMatrixHelpers::add(...)

The script validate.sh should run this self test on 1, 2, 3 and 4 cores.

65 {
66 #ifdef OOMPH_HAS_MPI
67  // Initialise MPI
68  MPI_Helpers::init(argc,argv);
69 #endif
70 
71  // Get the global oomph-lib communicator
72  const OomphCommunicator* const comm_pt = MPI_Helpers::communicator_pt();
73 
74  // my rank and number of processors. This is used later for putting the data.
75  unsigned my_rank = comm_pt->my_rank();
76  unsigned nproc = comm_pt->nproc();
77 
78  // Matrix 0
79  // [1 2 0 0 0
80  // 3 0 4 0 0
81  // 0 0 0 5 6
82  // 7 8 0 0 9
83  // 0 0 0 0 0]
84  //
85  // values = 1 2 3 4 5 6 7 8 9
86  // col i = 0 1 0 2 3 4 0 1 4
87  // row s = 0 2 4 6 9 9
88  //
89  // Matrix 1
90  // [1 2 3 4 5
91  // 6 7 8 9 10
92  // 0 0 11 0 12
93  // 13 14 15 0 0
94  // 0 0 16 17 18]
95  //
96  // values = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
97  // col i = 0 1 2 3 4 0 1 2 3 4 2 4 0 1 2 2 3 4
98  // row s = 0 5 10 12 15 18
99  //
100  // Result matrix
101  // [2 4 3 4 5
102  // 9 7 12 9 10
103  // 0 0 11 5 18
104  // 20 22 15 0 9
105  // 0 0 16 17 18]
106  //
107  // values = 2 4 3 4 5 9 7 12 9 10 11 5 18 20 22 15 9 16 17 18
108  // col i = 0 1 2 3 4 0 1 2 3 4 2 3 4 0 1 2 4 2 3 4
109  // row s = 0 5 10 13 17 19
110  //
111 
112  // First matrix (t1m1)
113  unsigned nrow_global_t1m1 = 5;
114  unsigned ncol_t1m1 = 5;
115  unsigned nnz_t1m1 = 9;
116  Vector<double> val_t1m1;
117  Vector<int> col_i_t1m1;
118  Vector<int> row_s_t1m1;
119 
120  double val_array_t1m1[] = {1,2,3,4,5,6,7,8,9};
121  int col_i_array_t1m1[] = {0,1,0,2,3,4,0,1,4};
122  int row_s_array_t1m1[] = {0,2,4,6,9,9};
123 
124  construct_vector(val_array_t1m1,nnz_t1m1,val_t1m1);
125  construct_vector(col_i_array_t1m1,nnz_t1m1,col_i_t1m1);
126  construct_vector(row_s_array_t1m1,nrow_global_t1m1+1,row_s_t1m1);
127 
128  CRDoubleMatrix mat_t1m1;
130  nrow_global_t1m1,ncol_t1m1,comm_pt,
131  val_t1m1,col_i_t1m1,row_s_t1m1,mat_t1m1);
132 
133  std::ostringstream mat_t1m1_stream;
134  mat_t1m1_stream << "t1m1_NP"<<nproc<<"R"<<my_rank;
135  mat_t1m1.sparse_indexed_output(mat_t1m1_stream.str());
136 
137  // Next matrix (t1m2)
138  unsigned nrow_global_t1m2 = 5;
139  unsigned ncol_t1m2 = 5;
140  unsigned nnz_t1m2 = 18;
141  Vector<double> val_t1m2;
142  Vector<int> col_i_t1m2;
143  Vector<int> row_s_t1m2;
144 
145  double val_array_t1m2[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
146  int col_i_array_t1m2[] = {0,1,2,3,4,0,1,2,3,4,2,4,0,1,2,2,3,4};
147  int row_s_array_t1m2[] = {0,5,10,12,15,18};
148 
149  construct_vector(val_array_t1m2,nnz_t1m2,val_t1m2);
150  construct_vector(col_i_array_t1m2,nnz_t1m2,col_i_t1m2);
151  construct_vector(row_s_array_t1m2,nrow_global_t1m2+1,row_s_t1m2);
152 
153  CRDoubleMatrix mat_t1m2;
155  nrow_global_t1m2,ncol_t1m2,comm_pt,
156  val_t1m2,col_i_t1m2,row_s_t1m2,mat_t1m2);
157 
158  std::ostringstream mat_t1m2_stream;
159 
160  mat_t1m2_stream << "t1m2_NP"<<nproc<<"R"<<my_rank;
161  mat_t1m2.sparse_indexed_output(mat_t1m2_stream.str());
162 
163  // Now perform element-wise addition of t1m1 and t1m2, and put the result
164  // in mat_t1_result.
165  CRDoubleMatrix mat_t1_result;
166 
167  mat_t1m1.add(mat_t1m2,mat_t1_result);
168 
169  std::ostringstream mat_t1_result_stream;
170  mat_t1_result_stream << "t1_res_NP"<<nproc<<"R"<<my_rank;
171  mat_t1_result.sparse_indexed_output(mat_t1_result_stream.str());
172 
173 
174  // Now test with the following matrices: A + B = C where
175  // A = [1 2 3 4 5 6
176  // 7 8 9 10 11 12
177  // 13 14 15 16 17 18]
178  //
179  // B = [1 2 3 4 5 6
180  // 7 8 9 10 11 12
181  // 13 14 15 16 17 18]
182  //
183  // B = [2 4 6 8 10 12
184  // 14 16 18 20 22 24
185  // 26 28 30 32 34 36]
186 
187  // First matrix (t2m1)
188  unsigned nrow_global_t2m1 = 3;
189  unsigned ncol_t2m1 = 6;
190  unsigned nnz_t2m1 = 18;
191  Vector<double> val_t2m1;
192  Vector<int> col_i_t2m1;
193  Vector<int> row_s_t2m1;
194 
195  double val_array_t2m1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
196  int col_i_array_t2m1[] = {0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5};
197  int row_s_array_t2m1[] = {0,6,12,18};
198 
199  construct_vector(val_array_t2m1,nnz_t2m1,val_t2m1);
200  construct_vector(col_i_array_t2m1,nnz_t2m1,col_i_t2m1);
201  construct_vector(row_s_array_t2m1,nrow_global_t2m1+1,row_s_t2m1);
202 
203  CRDoubleMatrix mat_t2m1;
205  nrow_global_t2m1,ncol_t2m1,comm_pt,
206  val_t2m1,col_i_t2m1,row_s_t2m1,mat_t2m1);
207 
208  std::ostringstream mat_t2m1_stream;
209  mat_t2m1_stream << "t2m1_NP"<<nproc<<"R"<<my_rank;
210  mat_t2m1.sparse_indexed_output(mat_t2m1_stream.str());
211 
212  // Second matrix (t2m2)
213  unsigned nrow_global_t2m2 = 3;
214  unsigned ncol_t2m2 = 6;
215  unsigned nnz_t2m2 = 18;
216  Vector<double> val_t2m2;
217  Vector<int> col_i_t2m2;
218  Vector<int> row_s_t2m2;
219 
220  double val_array_t2m2[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
221  int col_i_array_t2m2[] = {0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,5};
222  int row_s_array_t2m2[] = {0,6,12,18};
223 
224  construct_vector(val_array_t2m2,nnz_t2m2,val_t2m2);
225  construct_vector(col_i_array_t2m2,nnz_t2m2,col_i_t2m2);
226  construct_vector(row_s_array_t2m2,nrow_global_t2m2+1,row_s_t2m2);
227 
228  CRDoubleMatrix mat_t2m2;
230  nrow_global_t2m2,ncol_t2m2,comm_pt,
231  val_t2m2,col_i_t2m2,row_s_t2m2,mat_t2m2);
232 
233  std::ostringstream mat_t2m2_stream;
234  mat_t2m2_stream << "t2m2_NP"<<nproc<<"R"<<my_rank;
235  mat_t2m2.sparse_indexed_output(mat_t2m2_stream.str());
236 
237  // Now perform element-wise addition of t2m1 and t2m2, and put the result
238  // in mat_t2_result.
239  CRDoubleMatrix mat_t2_result;
240 
241  mat_t2m1.add(mat_t2m2,mat_t2_result);
242 
243  std::ostringstream mat_t2_result_stream;
244  mat_t2_result_stream << "t2_res_NP"<<nproc<<"R"<<my_rank;
245  mat_t2_result.sparse_indexed_output(mat_t2_result_stream.str());
246 
247 
248  // Now test with the following matrices: A + B = C where
249  // A = [1 2 3
250  // 4 5 6
251  // 7 8 9
252  // 10 11 12
253  // 13 14 15
254  // 16 17 18]
255  //
256  // B = [1 2 3
257  // 4 5 6
258  // 7 8 9
259  // 10 11 12
260  // 13 14 15
261  // 16 17 18]
262  //
263  // C = [2 4 6
264  // 8 10 12
265  // 14 16 18
266  // 20 22 24
267  // 26 28 30
268  // 32 34 36]
269 
270  // First matrix (t3m1)
271  unsigned nrow_global_t3m1 = 6;
272  unsigned ncol_t3m1 = 3;
273  unsigned nnz_t3m1 = 18;
274  Vector<double> val_t3m1;
275  Vector<int> col_i_t3m1;
276  Vector<int> row_s_t3m1;
277 
278  double val_array_t3m1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
279  int col_i_array_t3m1[] = {0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2};
280  int row_s_array_t3m1[] = {0,3,6,9,12,15,18};
281 
282  construct_vector(val_array_t3m1,nnz_t3m1,val_t3m1);
283  construct_vector(col_i_array_t3m1,nnz_t3m1,col_i_t3m1);
284  construct_vector(row_s_array_t3m1,nrow_global_t3m1+1,row_s_t3m1);
285 
286  CRDoubleMatrix mat_t3m1;
288  nrow_global_t3m1,ncol_t3m1,comm_pt,
289  val_t3m1,col_i_t3m1,row_s_t3m1,mat_t3m1);
290 
291  std::ostringstream mat_t3m1_stream;
292  mat_t3m1_stream << "t3m1_NP"<<nproc<<"R"<<my_rank;
293  mat_t3m1.sparse_indexed_output(mat_t3m1_stream.str());
294 
295  // Second matrix (t3m2)
296  unsigned nrow_global_t3m2 = 6;
297  unsigned ncol_t3m2 = 3;
298  unsigned nnz_t3m2 = 18;
299  Vector<double> val_t3m2;
300  Vector<int> col_i_t3m2;
301  Vector<int> row_s_t3m2;
302 
303  double val_array_t3m2[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
304  int col_i_array_t3m2[] = {0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2};
305  int row_s_array_t3m2[] = {0,3,6,9,12,15,18};
306 
307 
308  construct_vector(val_array_t3m2,nnz_t3m2,val_t3m2);
309  construct_vector(col_i_array_t3m2,nnz_t3m2,col_i_t3m2);
310  construct_vector(row_s_array_t3m2,nrow_global_t3m2+1,row_s_t3m2);
311 
312  CRDoubleMatrix mat_t3m2;
314  nrow_global_t3m2,ncol_t3m2,comm_pt,
315  val_t3m2,col_i_t3m2,row_s_t3m2,mat_t3m2);
316 
317  std::ostringstream mat_t3m2_stream;
318  mat_t3m2_stream << "t3m2_NP"<<nproc<<"R"<<my_rank;
319  mat_t3m2.sparse_indexed_output(mat_t3m2_stream.str());
320 
321  // Now perform element-wise addition of t3m1 and t3m2, and put the result
322  // in mat_t3_result.
323  CRDoubleMatrix mat_t3_result;
324 
325  mat_t3m1.add(mat_t3m2,mat_t3_result);
326 
327  std::ostringstream mat_t3_result_stream;
328  mat_t3_result_stream << "t3_res_NP"<<nproc<<"R"<<my_rank;
329  mat_t3_result.sparse_indexed_output(mat_t3_result_stream.str());
330 
331 
332  // COLUMN INDICES NOT ORDERED.
333  // Now test with the following matrices: A + B = C where
334  // A = [1 2 3 4 5 6
335  // 7 8 9 10 11 12
336  // 13 14 15 16 17 18]
337  //
338  // B = [1 2 3 4 5 6
339  // 7 8 9 10 11 12
340  // 13 14 15 16 17 18]
341  //
342  // B = [2 4 6 8 10 12
343  // 14 16 18 20 22 24
344  // 26 28 30 32 34 36]
345  //
346  // In this test, the column indices (per row) is not ordered.
347 
348  // First matrix (t4m1)
349  unsigned nrow_global_t4m1 = 3;
350  unsigned ncol_t4m1 = 6;
351  unsigned nnz_t4m1 = 18;
352  Vector<double> val_t4m1;
353  Vector<int> col_i_t4m1;
354  Vector<int> row_s_t4m1;
355 
356  double val_array_t4m1[] = {1,5,4,3,2,6,7,8,9,10,11,12,13,17,16,15,14,18};
357  int col_i_array_t4m1[] = {0,4,3,2,1,5,0,1,2,3,4,5,0,4,3,2,1,5};
358  int row_s_array_t4m1[] = {0,6,12,18};
359 
360  construct_vector(val_array_t4m1,nnz_t4m1,val_t4m1);
361  construct_vector(col_i_array_t4m1,nnz_t4m1,col_i_t4m1);
362  construct_vector(row_s_array_t4m1,nrow_global_t4m1+1,row_s_t4m1);
363 
364  CRDoubleMatrix mat_t4m1;
366  nrow_global_t4m1,ncol_t4m1,comm_pt,
367  val_t4m1,col_i_t4m1,row_s_t4m1,mat_t4m1);
368 
369  std::ostringstream mat_t4m1_stream;
370  mat_t4m1_stream << "t4m1_NP"<<nproc<<"R"<<my_rank;
371  mat_t4m1.sparse_indexed_output(mat_t4m1_stream.str());
372 
373  // Second matrix (t4m2)
374  unsigned nrow_global_t4m2 = 3;
375  unsigned ncol_t4m2 = 6;
376  unsigned nnz_t4m2 = 18;
377  Vector<double> val_t4m2;
378  Vector<int> col_i_t4m2;
379  Vector<int> row_s_t4m2;
380 
381  double val_array_t4m2[] = {1,2,3,4,5,6,7,11,10,9,8,12,13,14,15,16,17,18};
382  int col_i_array_t4m2[] = {0,1,2,3,4,5,0,4,3,2,1,5,0,1,2,3,4,5};
383  int row_s_array_t4m2[] = {0,6,12,18};
384 
385  construct_vector(val_array_t4m2,nnz_t4m2,val_t4m2);
386  construct_vector(col_i_array_t4m2,nnz_t4m2,col_i_t4m2);
387  construct_vector(row_s_array_t4m2,nrow_global_t4m2+1,row_s_t4m2);
388 
389  CRDoubleMatrix mat_t4m2;
391  nrow_global_t4m2,ncol_t4m2,comm_pt,
392  val_t4m2,col_i_t4m2,row_s_t4m2,mat_t4m2);
393 
394  std::ostringstream mat_t4m2_stream;
395  mat_t4m2_stream << "t4m2_NP"<<nproc<<"R"<<my_rank;
396  mat_t4m2.sparse_indexed_output(mat_t4m2_stream.str());
397 
398  // Now perform element-wise addition of t4m1 and t4m2, and put the result
399  // in mat_t4_result.
400  CRDoubleMatrix mat_t4_result;
401 
402  mat_t4m1.add(mat_t4m2,mat_t4_result);
403 
404  std::ostringstream mat_t4_result_stream;
405  mat_t4_result_stream << "t4_res_NP"<<nproc<<"R"<<my_rank;
406  mat_t4_result.sparse_indexed_output(mat_t4_result_stream.str());
407 
408 #ifdef OOMPH_HAS_MPI
409  // finalize MPI
410  MPI_Helpers::finalize();
411 #endif
412  return(EXIT_SUCCESS);
413 } // end_of_main
Definition: matrices.h:888
void add(const CRDoubleMatrix &matrix_in, CRDoubleMatrix &result_matrix) const
element-wise addition of this matrix with matrix_in.
Definition: matrices.cc:3515
void sparse_indexed_output(std::ostream &outfile, const unsigned &precision=0, const bool &output_bottom_right_zero=false) const
Definition: matrices.h:182
Definition: communicator.h:54
int my_rank() const
my rank
Definition: communicator.h:176
int nproc() const
number of processors
Definition: communicator.h:157
void construct_vector(myType given_array[], unsigned given_arraysize, Vector< myType > &result_vector)
Definition: matrix_addition.cc:34
void create_uniformly_distributed_matrix(const unsigned &nrow, const unsigned &ncol, const OomphCommunicator *const comm_pt, const Vector< double > &values, const Vector< int > &column_indices, const Vector< int > &row_start, CRDoubleMatrix &matrix_out)
Definition: matrices.cc:3676

References oomph::CRDoubleMatrix::add(), oomph::MPI_Helpers::communicator_pt(), construct_vector(), oomph::CRDoubleMatrixHelpers::create_uniformly_distributed_matrix(), oomph::MPI_Helpers::finalize(), oomph::MPI_Helpers::init(), oomph::OomphCommunicator::my_rank(), oomph::OomphCommunicator::nproc(), and oomph::Matrix< T, MATRIX_TYPE >::sparse_indexed_output().

◆ output_vector()

template<typename myType >
void output_vector ( Vector< myType > &  given_vector)
50 {
51  typename Vector<myType>::iterator it;
52 
53  for(it = given_vector.begin(); it != given_vector.end(); ++it)
54  {
55  oomph_info << *it << std::endl;
56  }
57 }
OomphInfo oomph_info
Definition: oomph_definitions.cc:319

References oomph::oomph_info.