libMesh
parallel_sort_test.C
Go to the documentation of this file.
1 #include <libmesh/parallel_sort.h>
2 #include <libmesh/parallel.h>
3 
4 #include <algorithm>
5 
6 #include "test_comm.h"
7 #include "libmesh_cppunit.h"
8 
9 
10 using namespace libMesh;
11 
12 class ParallelSortTest : public CppUnit::TestCase {
13 public:
14  CPPUNIT_TEST_SUITE( ParallelSortTest );
15 
16  CPPUNIT_TEST( testSort );
17 
18  CPPUNIT_TEST_SUITE_END();
19 
20 public:
21  void setUp()
22  {}
23 
24  void tearDown()
25  {}
26 
27  void testSort()
28  {
29  const int size = TestCommWorld->size(),
30  rank = TestCommWorld->rank();
31  const int n_vals = size - rank;
32  std::vector<int> vals(n_vals);
33 
34  // Put all the numbers up to a triangular number into our data,
35  // scrambled and backwards
36  int val = rank+1, stride = size;
37  for (int i=0; i != n_vals; ++i)
38  {
39  vals[n_vals-i-1] = val;
40  val += stride;
41  stride -= 1;
42  }
43 
44  Parallel::Sort<int> sorter (*TestCommWorld, vals);
45 
46  sorter.sort();
47 
48  const std::vector<int> & my_bin = sorter.bin();
49 
50  // Our bins should be roughly the same size, but with that
51  // nbins*50 stuff in Parallel::BinSorter it's hard to predict the
52  // outcome exactly. We'll just make sure they're sorted and
53  // they've got everything.
54 
55  int total_size = cast_int<int>(my_bin.size());
56  TestCommWorld->sum(total_size);
57 
58  CPPUNIT_ASSERT_EQUAL(total_size, size*(size+1)/2);
59 
60  CPPUNIT_ASSERT(std::is_sorted(my_bin.begin(), my_bin.end()));
61 
62  int rank_with_i = -1;
63  for (int i=1; i <= total_size; ++i)
64  {
65  int count_i = std::count(my_bin.begin(), my_bin.end(), i);
66  CPPUNIT_ASSERT(count_i < 2);
67 
68  if (count_i)
69  {
70  CPPUNIT_ASSERT(rank_with_i <= rank);
71  rank_with_i = rank;
72  }
73  TestCommWorld->max(rank_with_i);
74 
75  TestCommWorld->sum(count_i);
76  CPPUNIT_ASSERT_EQUAL(count_i, 1);
77  }
78  }
79 };
80 
libMesh::Parallel::Sort
The parallel sorting method is templated on the type of data which is to be sorted.
Definition: parallel_sort.h:55
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
ParallelSortTest::testSort
void testSort()
Definition: parallel_sort_test.C:27
libMesh::Parallel::Sort::sort
void sort()
This is the only method which needs to be called by the user.
Definition: parallel_sort.C:64
libMesh::Parallel::Utils::is_sorted
bool is_sorted(const std::vector< KeyType > &v)
Definition: parallel_conversion_utils.h:52
libMesh::Parallel::Sort::bin
const std::vector< KeyType > & bin()
Return a constant reference to _my_bin.
Definition: parallel_sort.C:264
TestCommWorld
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:111
ParallelSortTest
Definition: parallel_sort_test.C:12
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(ParallelSortTest)
ParallelSortTest::setUp
void setUp()
Definition: parallel_sort_test.C:21
libmesh_cppunit.h
ParallelSortTest::tearDown
void tearDown()
Definition: parallel_sort_test.C:24
test_comm.h