libMesh
petsc_matrix_test.C
Go to the documentation of this file.
1 #include <libmesh/petsc_matrix.h>
2 
3 #ifdef LIBMESH_HAVE_PETSC
4 
5 #include <libmesh/parallel.h>
6 #include <libmesh/dense_matrix.h>
7 
8 #include "libmesh_cppunit.h"
9 #include "test_comm.h"
10 
11 #include <vector>
12 #ifdef LIBMESH_HAVE_CXX11_THREAD
13 #include <thread>
14 #include <algorithm>
15 #endif
16 
17 using namespace libMesh;
18 
19 class PetscMatrixTest : public CppUnit::TestCase
20 {
21 public:
22  CPPUNIT_TEST_SUITE(PetscMatrixTest);
23 
24  CPPUNIT_TEST(testGetAndSet);
25 
26  CPPUNIT_TEST_SUITE_END();
27 
28 public:
29  void setUp()
30  {
31  _comm = TestCommWorld;
32  _matrix = libmesh_make_unique<PetscMatrix<Number>>(*_comm);
33 
34  numeric_index_type root_block_size = 2;
35  _local_size = root_block_size + static_cast<numeric_index_type>(_comm->rank());
36  _global_size = 0;
37  _i.push_back(0);
38 
39  for (processor_id_type p = 0; p < _comm->size(); ++p)
40  {
41  numeric_index_type block_size = root_block_size + static_cast<numeric_index_type>(p);
42  _global_size += block_size;
43  _i.push_back(_global_size);
44  }
45 
46  _matrix->init(_global_size,
47  _global_size,
48  _local_size,
49  _local_size,
50  /*nnz=*/_local_size,
51  /*noz=*/0);
52  }
53 
54  void tearDown() {}
55 
57  {
58  std::vector<numeric_index_type> rows(_local_size);
59  std::vector<numeric_index_type> cols(_local_size);
60  DenseMatrix<Number> local(_local_size, _local_size);
61 
62  numeric_index_type index = _i[_comm->rank()], count = 0;
63  for (; count < _local_size; ++count, ++index)
64  {
65  rows[count] = index;
66  cols[count] = index;
67  for (numeric_index_type j = 0; j < _local_size; ++j)
68  local(count, j) = (count + 1) * (j + 1) * (_comm->rank() + 1);
69  }
70 
71  _matrix->add_matrix(local, rows, cols);
72  _matrix->close();
73 
74  index = _i[_comm->rank()], count = 0;
75 
76  auto functor = [this]()
77  {
78  std::vector<numeric_index_type> cols_to_get;
79  std::vector<Number> values;
80  numeric_index_type local_index = _i[_comm->rank()], local_count = 0;
81  for (; local_count < _local_size; ++local_count, ++local_index)
82  {
83  _matrix->get_row(local_index, cols_to_get, values);
84  for (numeric_index_type j = 0; j < _local_size; ++j)
85  {
86  LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(values[j]),
87  (local_count + 1) * (j + 1) * (_comm->rank() + 1),
88  _tolerance);
89  CPPUNIT_ASSERT_EQUAL(cols_to_get[local_count], local_index);
90  }
91  }
92  };
93 
94 #ifdef LIBMESH_HAVE_CXX11_THREAD
95  auto num_threads = std::min(unsigned(2),
96  std::max(
97  std::thread::hardware_concurrency(),
98  unsigned(1)));
99  std::vector<std::thread> threads(num_threads);
100  for (unsigned int thread = 0; thread < num_threads; ++thread)
101  threads[thread] = std::thread(functor);
102  std::for_each(threads.begin(), threads.end(),
103  [](std::thread & x){x.join();});
104 #else
105  functor();
106 #endif
107  }
108 
109 private:
110 
111  Parallel::Communicator * _comm;
112  std::unique_ptr<PetscMatrix<Number>> _matrix;
114  std::vector<numeric_index_type> _i;
115  const Real _tolerance = TOLERANCE * TOLERANCE;
116 
117 };
118 
120 
121 #endif
libMesh::libmesh_real
T libmesh_real(T a)
Definition: libmesh_common.h:166
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::TOLERANCE
static const Real TOLERANCE
Definition: libmesh_common.h:128
libMesh::DenseMatrix< Number >
PetscMatrixTest::testGetAndSet
void testGetAndSet()
Definition: petsc_matrix_test.C:56
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
libMesh::processor_id_type
uint8_t processor_id_type
Definition: id_types.h:104
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
PetscMatrixTest
Definition: petsc_matrix_test.C:19
libmesh_cppunit.h
PetscMatrixTest::_matrix
std::unique_ptr< PetscMatrix< Number > > _matrix
Definition: petsc_matrix_test.C:112
PetscMatrixTest::setUp
void setUp()
Definition: petsc_matrix_test.C:29
PetscMatrixTest::tearDown
void tearDown()
Definition: petsc_matrix_test.C:54
PetscMatrixTest::_comm
Parallel::Communicator * _comm
Definition: petsc_matrix_test.C:111
PetscMatrixTest::_local_size
numeric_index_type _local_size
Definition: petsc_matrix_test.C:113
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
test_comm.h
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(PetscMatrixTest)
PetscMatrixTest::_i
std::vector< numeric_index_type > _i
Definition: petsc_matrix_test.C:114