QMCPACK
test_partition.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2017 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, mdewing@anl.gov Argonne National Laboratory
8 //
9 // File created by: Mark Dewing, mdewing@anl.gov Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include <iostream>
16 #include "Utilities/FairDivide.h"
17 #include <stdio.h>
18 #include <string>
19 #include <vector>
20 
21 using std::string;
22 using std::vector;
23 namespace qmcplusplus
24 {
25 void print_vector(vector<int>& out)
26 {
27  for (int i = 0; i < out.size(); i++)
28  {
29  std::cout << out[i] << " ";
30  }
31  std::cout << std::endl;
32 }
33 
34 TEST_CASE("FairDivideLow_one", "[utilities]")
35 {
36  std::vector<int> out;
37  FairDivideLow(1, 1, out);
38  REQUIRE(out.size() == 2);
39  REQUIRE(out[0] == 0);
40  REQUIRE(out[1] == 1);
41 }
42 
43 TEST_CASE("FairDivideLow_two", "[utilities]")
44 {
45  std::vector<int> out;
46  FairDivideLow(2, 1, out);
47  REQUIRE(out.size() == 2);
48  REQUIRE(out[0] == 0);
49  REQUIRE(out[1] == 2);
50 
51  FairDivideLow(2, 2, out);
52  REQUIRE(out.size() == 3);
53  REQUIRE(out[0] == 0);
54  REQUIRE(out[1] == 1);
55  REQUIRE(out[2] == 2);
56 }
57 
58 TEST_CASE("FairDivideLow_three", "[utilities]")
59 {
60  std::vector<int> out;
61  FairDivideLow(3, 1, out);
62  REQUIRE(out.size() == 2);
63  REQUIRE(out[0] == 0);
64  REQUIRE(out[1] == 3);
65 
66  FairDivideLow(3, 2, out);
67  REQUIRE(out.size() == 3);
68  REQUIRE(out[0] == 0);
69  REQUIRE(out[1] == 1);
70  REQUIRE(out[2] == 3);
71 
72  FairDivideLow(3, 3, out);
73  REQUIRE(out.size() == 4);
74  REQUIRE(out[0] == 0);
75  REQUIRE(out[1] == 1);
76  REQUIRE(out[2] == 2);
77  REQUIRE(out[3] == 3);
78 }
79 
80 TEST_CASE("FairDivideLow_four", "[utilities]")
81 {
82  std::vector<int> out;
83  FairDivideLow(4, 2, out);
84  REQUIRE(out.size() == 3);
85  REQUIRE(out[0] == 0);
86  REQUIRE(out[1] == 2);
87  REQUIRE(out[2] == 4);
88 }
89 
90 TEST_CASE("FairDivideAligned", "[utilities]")
91 {
92  int first, last;
93 
94  FairDivideAligned(37, 6, 5, 2, first, last);
95  REQUIRE(first == 24);
96  REQUIRE(last == 36);
97 
98  FairDivideAligned(37, 6, 5, 4, first, last);
99  REQUIRE(first == 37);
100  REQUIRE(last == 37);
101 
102  FairDivideAligned(37, 6, 1, 0, first, last);
103  REQUIRE(first == 0);
104  REQUIRE(last == 37);
105 }
106 
107 TEST_CASE("fairDivide_one", "[utilities]")
108 {
109  auto out = fairDivide(1, 1);
110  REQUIRE(out.size() == 1);
111  REQUIRE(out[0] == 1);
112 }
113 
114 TEST_CASE("fairDivide_empties", "[utilities]")
115 {
116  auto out = fairDivide(2, 3);
117  REQUIRE(out.size() == 3);
118  REQUIRE(out[0] == 1);
119  REQUIRE(out[1] == 1);
120  REQUIRE(out[2] == 0);
121 }
122 
123 TEST_CASE("fairDivide_typical", "[utilities]")
124 {
125  auto out = fairDivide(7, 3);
126  REQUIRE(out.size() == 3);
127  REQUIRE(out[0] == 3);
128  REQUIRE(out[1] == 2);
129  REQUIRE(out[2] == 2);
130 }
131 
132 TEST_CASE("fairDivide_even", "[utilities]")
133 {
134  auto out = fairDivide(6, 3);
135  REQUIRE(out.size() == 3);
136  REQUIRE(out[0] == 2);
137  REQUIRE(out[1] == 2);
138  REQUIRE(out[2] == 2);
139 }
140 
141 
142 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
void FairDivideLow(int ntot, int npart, IV &adist)
partition ntot elements among npart
Definition: FairDivide.h:114
void print_vector(vector< int > &out)
A collection of functions for dividing fairly.
REQUIRE(std::filesystem::exists(filename))
void FairDivideAligned(const int ntot, const int base, const int npart, const int me, int &first, int &last)
Partition ntot over npart and the size of each partition is a multiple of base size.
Definition: FairDivide.h:96
std::vector< IV > fairDivide(IV ntot, IV npart)
return the occupation vector for ntot entities partitioned npart ways.
Definition: FairDivide.h:77