generic_mpi_test.cc File Reference
#include "generic.h"
#include "poisson.h"
#include "meshes/one_d_mesh.h"

Classes

class  OneDPoissonProblem< ELEMENT >
 1D Poisson problem in unit interval. More...
 

Namespaces

 FishSolnOneDPoisson
 Namespace for fish-shaped solution of 1D Poisson equation.
 

Functions

void FishSolnOneDPoisson::get_exact_u (const Vector< double > &x, Vector< double > &u)
 Exact, fish-shaped solution as a 1D vector. More...
 
void FishSolnOneDPoisson::source_function (const Vector< double > &x, double &source)
 Source function required to make the fish shape an exact solution. More...
 
int main (int argc, char *argv[])
 Driver code for generic mpi stuff. More...
 

Function Documentation

◆ main()

int main ( int argc  ,
char argv[] 
)

Driver code for generic mpi stuff.

///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////

262 {
263 
264  // Initialise MPI
265  MPI_Helpers::init(argc,argv);
266 
267  // Switch off output modifier
269 
270  // Define processor-labeled output file for all on-screen stuff
271  std::ofstream output_stream;
272  char filename[100];
273  sprintf(filename,"OUTPUT.%i",MPI_Helpers::communicator_pt()->my_rank());
274  output_stream.open(filename);
275  oomph_info.stream_pt() = &output_stream;
276  OomphLibWarning::set_stream_pt(&output_stream);
277  OomphLibError::set_stream_pt(&output_stream);
278 
279  // Get the global oomph-lib communicator
280  const OomphCommunicator* const comm_pt = MPI_Helpers::communicator_pt();
281 
282  // Get rank and total number of processors.
283  unsigned my_rank = comm_pt->my_rank();
284  unsigned nproc = comm_pt->nproc();
285 
286  // Tell us who you are...
287  oomph_info << "I'm rank " << my_rank << " on a total of "
288  << nproc << " processors" << std::endl;
289 
290 
291 
292 
293  // Create a uniformly distributed LinearAlgebraDistribution
294  // 100 global rows are uniformly distributed accross the processes of
295  // comm_pt
296  unsigned nrow_global = 100;
297  LinearAlgebraDistribution distributed_distribution(comm_pt,
298  nrow_global);
299 
300  // Show us how many rows this processor holds
301  oomph_info << "distributed distribution: first_row and nrow_local: "
302  << distributed_distribution.first_row() << " "
303  << distributed_distribution.nrow_local()
304  << std::endl;
305 
306 
307  // Construct an empty distribution object (does not specify a distribution)
308  LinearAlgebraDistribution locally_replicated_distribution;
309 
310  // Build a locally replicated distribution such that every row is available
311  // on every process
312  bool distributed=false;
313  locally_replicated_distribution.build(comm_pt,nrow_global,distributed);
314 
315  // Show us how many rows this processor holds
316  oomph_info << "locally replicated distribution: first_row and nrow_local: "
317  << locally_replicated_distribution.first_row() << " "
318  << locally_replicated_distribution.nrow_local()
319  << std::endl;
320 
321  // Get the number of local rows on this process
322  unsigned nrow_local = distributed_distribution.nrow_local();
323 
324  // Get the first row on this process
325  unsigned first_row = distributed_distribution.first_row();
326 
327  // Get the number of global rows
328  nrow_global = distributed_distribution.nrow();
329 
330  // Is this distributed (true) or locally replicated (false)
331  distributed = distributed_distribution.distributed();
332 
333  // Does this object specify a distribution
334  bool built = distributed_distribution.built();
335 
336 
337 
338  // Construct a uniformly distributed DoubleVector with unit elements
339  DoubleVector my_vector(distributed_distribution,1.0);
340 
341  // Increment every element of my_vector on this process by 1
342  nrow_local = my_vector.distribution_pt()->nrow_local();
343  for (unsigned i = 0; i < nrow_local; i++)
344  {
345  my_vector[i]+=1.0;
346  }
347 
348  // Document elements on this process in my_vector
349  nrow_local = my_vector.distribution_pt()->nrow_local();
350  first_row = my_vector.distribution_pt()->first_row();
351  for (unsigned i = 0; i < nrow_local; i++)
352  {
353  oomph_info << "local row " << i
354  << " is global row " << first_row+i
355  << " and has value " << my_vector[i] << std::endl;
356  }
357 
358  // Redistribute my_vector such that it is locally replicated on all processes
359  my_vector.redistribute(&locally_replicated_distribution);
360 
361  // (Re)build my_vector such that it is uniformly distributed over all
362  // processes
363  my_vector.build(distributed_distribution,1.0);
364 
365  // Construct an empty DoubleVector
366  DoubleVector another_vector;
367 
368  // Clear all data from an existing DoubleVector
369  my_vector.clear();
370 
371 
372  // Construct an empty CRDoubleMatrix
373  CRDoubleMatrix my_matrix;
374 
375  // Specify that the rows be uniformly distributed
376  my_matrix.build(&distributed_distribution);
377 
378  // Vector of coefficient of value 1.0
379  Vector<double> values(nrow_local,1.0);
380 
381  // Column indices corresponding to values
382  Vector<int> column_indices(nrow_local);
383 
384  // Index of vectors values and column_indices where the i-th row starts
385  // (each row contains one coefficient)
386  Vector<int> row_start(nrow_local+1);
387 
388  // populate column_indices and row_start
389  for (unsigned i = 0; i < nrow_local; ++i)
390  {
391  column_indices[i]=first_row+nrow_local;
392  row_start[i]=i;
393  }
394  row_start[nrow_local]=nrow_local;
395 
396  // Build the (square) matrix
397  unsigned ncol = nrow_global;
398  my_matrix.build(ncol,values,column_indices,row_start);
399 
400  CRDoubleMatrix my_matrix2(&distributed_distribution,ncol,values,
401  column_indices,row_start);
402 
403  // Get the first (global) row of my_matrix on this process
404  first_row = my_matrix2.distribution_pt()->first_row();
405 
406  // Get the first (global) row of my_matrix on this process
407  first_row = my_matrix2.first_row();
408 
409 
410 
411 
412 
413  // Set up a problem:
414  // Solve a 1D Poisson problem using a source function that generates
415  // a fish shaped exact solution
416  unsigned n_element=40;
419 
420  // Get the residual and Jacobian, by default both are uniformly distributed
421  // over all available processes
422  my_vector.clear();
423  my_matrix.clear();
424 
425  // Get the Jacobian
426  problem.get_jacobian(my_vector,my_matrix);
427 
428  oomph_info
429  << "Uniformly distributed residual vector: first_row and nrow_local: "
430  << my_vector.first_row() << " "
431  << my_vector.nrow_local() << std::endl
432  << "Uniformly distributed jacobian matrix: first_row, nrow_local and nnz: "
433  << my_matrix.first_row() << " "
434  << my_matrix.nrow_local() << " "
435  << my_matrix.nnz() << " "
436  << std::endl;
437 
438 
439  // Request locally replicated residual and Jacobian from the problem
440  distributed=false;
441  LinearAlgebraDistribution locally_replicated_distribution_for_jac(
442  comm_pt,problem.ndof(),distributed);
443 
444  // Show us how many rows this processor holds
445  oomph_info
446  << "locally replicated distribution_for_jac: first_row and nrow_local: "
447  << locally_replicated_distribution_for_jac.first_row() << " "
448  << locally_replicated_distribution_for_jac.nrow_local()
449  << std::endl;
450 
451  my_vector.build(locally_replicated_distribution_for_jac);
452  my_matrix.build(&locally_replicated_distribution_for_jac);
453 
454  // Get the Jacobian
455  problem.get_jacobian(my_vector,my_matrix);
456 
457  oomph_info
458  << "Replicated residual vector: first_row and nrow_local: "
459  << my_vector.first_row() << " "
460  << my_vector.nrow_local() << std::endl
461  << "Replicated jacobian matrix: first_row, nrow_local and nnz: "
462  << my_matrix.first_row() << " "
463  << my_matrix.nrow_local() << " "
464  << my_matrix.nnz() << " "
465  << std::endl;
466 
467  // Finalize MPI
468  MPI_Helpers::finalize();
469 
470 
471  // dummy line to suppress warning about unused built variable
472  if (built)
473  {
474  built=false;
475  }
476 
477 } // end_of_main
int i
Definition: BiCGSTAB_step_by_step.cpp:9
1D Poisson problem in unit interval.
Definition: linear_solvers/direct_solver_test.cc:81
Definition: matrices.h:888
void clear()
clear
Definition: matrices.cc:1657
unsigned long nnz() const
Return the number of nonzero entries (the local nnz)
Definition: matrices.h:1096
void build(const LinearAlgebraDistribution *distribution_pt, const unsigned &ncol, const Vector< double > &value, const Vector< int > &column_index, const Vector< int > &row_start)
Definition: matrices.cc:1672
unsigned nrow_local() const
access function for the num of local rows on this processor.
Definition: linear_algebra_distribution.h:469
unsigned first_row() const
access function for the first row on this processor
Definition: linear_algebra_distribution.h:481
Definition: double_vector.h:58
void clear()
wipes the DoubleVector
Definition: double_vector.h:142
Definition: linear_algebra_distribution.h:64
unsigned first_row() const
Definition: linear_algebra_distribution.h:261
void build(const OomphCommunicator *const comm_pt, const unsigned &first_row, const unsigned &nrow_local, const unsigned &nrow=0)
Definition: linear_algebra_distribution.cc:35
unsigned nrow_local() const
Definition: linear_algebra_distribution.h:193
Definition: communicator.h:54
int my_rank() const
my rank
Definition: communicator.h:176
int nproc() const
number of processors
Definition: communicator.h:157
std::ostream *& stream_pt()
Access function for the stream pointer.
Definition: oomph_definitions.h:464
OutputModifier *& output_modifier_pt()
Access function for the output modifier pointer.
Definition: oomph_definitions.h:476
void source_function(const Vector< double > &x, double &source)
Source function required to make the fish shape an exact solution.
Definition: linear_solvers/direct_solver_test.cc:63
string filename
Definition: MergeRestartFiles.py:39
OutputModifier default_output_modifier
Single global instatiation of the default output modifier.
Definition: oomph_definitions.cc:325
OomphInfo oomph_info
Definition: oomph_definitions.cc:319
Constructor for SteadyAxisymAdvectionDiffusion problem
Definition: steady_axisym_advection_diffusion.cc:213

References oomph::DoubleVector::build(), oomph::CRDoubleMatrix::build(), oomph::LinearAlgebraDistribution::build(), oomph::LinearAlgebraDistribution::built(), oomph::DoubleVector::clear(), oomph::CRDoubleMatrix::clear(), oomph::default_output_modifier, oomph::LinearAlgebraDistribution::distributed(), oomph::DistributableLinearAlgebraObject::distribution_pt(), MergeRestartFiles::filename, oomph::LinearAlgebraDistribution::first_row(), oomph::DistributableLinearAlgebraObject::first_row(), i, oomph::OomphCommunicator::my_rank(), oomph::CRDoubleMatrix::nnz(), oomph::OomphCommunicator::nproc(), oomph::LinearAlgebraDistribution::nrow(), oomph::LinearAlgebraDistribution::nrow_local(), oomph::DistributableLinearAlgebraObject::nrow_local(), oomph::oomph_info, oomph::OomphInfo::output_modifier_pt(), problem, oomph::DoubleVector::redistribute(), FishSolnOneDPoisson::source_function(), and oomph::OomphInfo::stream_pt().