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  LIBMESH_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  LOG_UNIT_TEST;
30 
31  const int size = TestCommWorld->size(),
32  rank = TestCommWorld->rank();
33  const int n_vals = size - rank;
34  std::vector<int> vals(n_vals);
35 
36  // Put all the numbers up to a triangular number into our data,
37  // scrambled and backwards
38  int val = rank+1, stride = size;
39  for (int i=0; i != n_vals; ++i)
40  {
41  vals[n_vals-i-1] = val;
42  val += stride;
43  stride -= 1;
44  }
45 
46  Parallel::Sort<int> sorter (*TestCommWorld, vals);
47 
48  sorter.sort();
49 
50  const std::vector<int> & my_bin = sorter.bin();
51 
52  // Our bins should be roughly the same size, but with that
53  // nbins*50 stuff in Parallel::BinSorter it's hard to predict the
54  // outcome exactly. We'll just make sure they're sorted and
55  // they've got everything.
56 
57  int total_size = cast_int<int>(my_bin.size());
58  TestCommWorld->sum(total_size);
59 
60  CPPUNIT_ASSERT_EQUAL(total_size, size*(size+1)/2);
61 
62  CPPUNIT_ASSERT(std::is_sorted(my_bin.begin(), my_bin.end()));
63 
64  int rank_with_i = -1;
65  for (int i=1; i <= total_size; ++i)
66  {
67  int count_i = std::count(my_bin.begin(), my_bin.end(), i);
68  CPPUNIT_ASSERT(count_i < 2);
69 
70  if (count_i)
71  {
72  CPPUNIT_ASSERT(rank_with_i <= rank);
73  rank_with_i = rank;
74  }
75  TestCommWorld->max(rank_with_i);
76 
77  TestCommWorld->sum(count_i);
78  CPPUNIT_ASSERT_EQUAL(count_i, 1);
79  }
80  }
81 };
82 
const std::vector< KeyType > & bin()
Return a constant reference to _my_bin.
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
void sort()
This is the only method which needs to be called by the user.
Definition: parallel_sort.C:64
void sum(T &r) const
processor_id_type rank() const
The libMesh namespace provides an interface to certain functionality in the library.
processor_id_type size() const
bool is_sorted(const std::vector< KeyType > &v)
The parallel sorting method is templated on the type of data which is to be sorted.
Definition: parallel_sort.h:55
void max(const T &r, T &o, Request &req) const
CPPUNIT_TEST_SUITE_REGISTRATION(ParallelSortTest)