TestWelfordAggregate.hpp 5 KB
Newer Older
niklas.baumgarten's avatar
niklas.baumgarten committed
1
2
3
4
5
#ifndef TESTWELFORDAGGREGATE_HPP
#define TESTWELFORDAGGREGATE_HPP

#include "TestEnvironment.hpp"

niklas.baumgarten's avatar
niklas.baumgarten committed
6
#include "WelfordAggregate.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
7
#include "MeshesCreator.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
8
#include "NormalDistribution.hpp"
niklas.baumgarten's avatar
niklas.baumgarten committed
9
10
11
12


class TestWelfordAggregate : public Test {
protected:
niklas.baumgarten's avatar
niklas.baumgarten committed
13
  int numSamples;
niklas.baumgarten's avatar
niklas.baumgarten committed
14

niklas.baumgarten's avatar
niklas.baumgarten committed
15
  WelfordAggregate aggregate;
niklas.baumgarten's avatar
niklas.baumgarten committed
16

niklas.baumgarten's avatar
niklas.baumgarten committed
17
18
19
20
21
  std::unique_ptr<Meshes> meshes;

  NormalDistributionReal normalDist;

  TestWelfordAggregate(int numSamples) :
22
    aggregate(WelfordAggregate(numSamples)),
niklas.baumgarten's avatar
niklas.baumgarten committed
23
24
25
26
27
    meshes(MeshesCreator("Interval").CreateUnique()),
    normalDist(NormalDistributionReal(*meshes)), numSamples(numSamples) {
  }

  void TestSampleCounterParallel() {
28
    pout << "Before update: " << aggregate.ctr << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
29
    while (aggregate.ctr.dMcomm != 0) aggregate.Update(0.0, 0.0, 0.0);
30
    pout << "After update: " << aggregate.ctr << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
31
    aggregate.UpdateParallel();
32
    pout << "After parallel update: " << aggregate.ctr << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
33
34
    EXPECT_EQ(aggregate.ctr.M, numSamples);
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
35

36
37
38
39
  /*
   * Todo maybe use color
   */

niklas.baumgarten's avatar
niklas.baumgarten committed
40
  void TestMeanParallel() {
41
42
43
    pout << "Before update: " << aggregate.mean << endl;
    while (aggregate.ctr.dMcomm != 0) aggregate.Update(0.0, PPM->Proc(0), 0.0);
    pout << "After update: " << aggregate.mean << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
44
    aggregate.UpdateParallel();
45
46
47
48
49
50
51
52
53
54
55
56
57
    pout << "After parallel update: " << aggregate.mean << endl;
    double sumGauss = (pow((PPM->Size(0) - 1), 2) + (PPM->Size(0) - 1)) / 2.0;
    EXPECT_EQ(aggregate.mean.Q, sumGauss / PPM->Size(0));
  }

  void TestSVarParallel() {
//    pout << "Before update: " << aggregate.sVar << endl;
//    while (aggregate.ctr.dMcomm != 0) aggregate.Update(0.0, PPM->Proc(0), 0.0);
//    pout << "After update: " << aggregate.sVar << endl;
//    aggregate.UpdateParallel();
//    pout << "After parallel update: " << aggregate.sVar << endl;
//    double sumGauss = (pow((PPM->Size(0) - 1), 2) + (PPM->Size(0) - 1)) / 2.0;
//    EXPECT_EQ(aggregate.sVar.Q, sumGauss / PPM->Size(0));
niklas.baumgarten's avatar
niklas.baumgarten committed
58
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
59

niklas.baumgarten's avatar
niklas.baumgarten committed
60
61
62
  void TearDown() {
    PPM->Barrier(0);
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
63
64
};

niklas.baumgarten's avatar
niklas.baumgarten committed
65
66
class TestWelfordAggregate4Samples : public TestWelfordAggregate {
public:
67
  TestWelfordAggregate4Samples() : TestWelfordAggregate(4) {}
niklas.baumgarten's avatar
niklas.baumgarten committed
68
69
70
71
72
};

TEST_F(TestWelfordAggregate4Samples, TestSampleCounter) {
  switch(PPM->Size(0)) {
    case 8:
73
      pout << aggregate.ctr << " commSplit " << aggregate.commSplit << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
74
75
76
77
      EXPECT_EQ(aggregate.ctr.dMcomm, 1);
      EXPECT_EQ(aggregate.commSplit, 2);
      break;
    case 4:
78
      pout << aggregate.ctr << " commSplit " << aggregate.commSplit << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
79
80
81
82
      EXPECT_EQ(aggregate.ctr.dMcomm, 1);
      EXPECT_EQ(aggregate.commSplit, 2);
      break;
    case 2:
83
      pout << aggregate.ctr << " commSplit " << aggregate.commSplit << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
84
85
86
87
      EXPECT_EQ(aggregate.ctr.dMcomm, 2);
      EXPECT_EQ(aggregate.commSplit, 1);
      break;
    case 1:
88
      pout << aggregate.ctr << " commSplit " << aggregate.commSplit << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
89
90
91
92
93
      EXPECT_EQ(aggregate.ctr.dMcomm, 4);
      EXPECT_EQ(aggregate.commSplit, 0);
      break;
    default: Warning("No test case for this amount of processes")
      break;
niklas.baumgarten's avatar
niklas.baumgarten committed
94
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
95
96
97
  TestSampleCounterParallel();
}

98
TEST_F(TestWelfordAggregate4Samples, TestMean) { TestMeanParallel(); }
niklas.baumgarten's avatar
niklas.baumgarten committed
99

100
TEST_F(TestWelfordAggregate4Samples, TestSVar) { TestSVarParallel(); }
niklas.baumgarten's avatar
niklas.baumgarten committed
101
102
103

class TestWelfordAggregate8Samples : public TestWelfordAggregate {
public:
104
  TestWelfordAggregate8Samples() : TestWelfordAggregate(8) {}
niklas.baumgarten's avatar
niklas.baumgarten committed
105
106
107
108
109
};

TEST_F(TestWelfordAggregate8Samples, TestSampleCounter) {
  switch(PPM->Size(0)) {
    case 8:
110
      pout << aggregate.ctr << " commSplit " << aggregate.commSplit << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
111
112
113
114
      EXPECT_EQ(aggregate.ctr.dMcomm, 1);
      EXPECT_EQ(aggregate.commSplit, 3);
      break;
    case 4:
115
      pout << aggregate.ctr << " commSplit " << aggregate.commSplit << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
116
117
118
119
      EXPECT_EQ(aggregate.ctr.dMcomm, 2);
      EXPECT_EQ(aggregate.commSplit, 2);
      break;
    case 2:
120
      pout << aggregate.ctr << " commSplit " << aggregate.commSplit << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
121
122
123
124
      EXPECT_EQ(aggregate.ctr.dMcomm, 4);
      EXPECT_EQ(aggregate.commSplit, 1);
      break;
    case 1:
125
      pout << aggregate.ctr << " commSplit " << aggregate.commSplit << endl;
niklas.baumgarten's avatar
niklas.baumgarten committed
126
127
128
129
130
      EXPECT_EQ(aggregate.ctr.dMcomm, 8);
      EXPECT_EQ(aggregate.commSplit, 0);
      break;
    default: Warning("No test case for this amount of processes")
      break;
niklas.baumgarten's avatar
niklas.baumgarten committed
131
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
132
133
134
  TestSampleCounterParallel();
}

135
136
TEST_F(TestWelfordAggregate8Samples, TestMean) { TestMeanParallel(); }

niklas.baumgarten's avatar
niklas.baumgarten committed
137
138
class TestWelfordAggregate1e6Samples : public TestWelfordAggregate {
public:
139
  TestWelfordAggregate1e6Samples() : TestWelfordAggregate(1e6) {}
niklas.baumgarten's avatar
niklas.baumgarten committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
};

TEST_F(TestWelfordAggregate1e6Samples, TestSampleCounter) {
  switch(PPM->Size(0)) {
    case 8:
      EXPECT_EQ(aggregate.commSplit, 3);
      break;
    case 4:
      EXPECT_EQ(aggregate.commSplit, 2);
      break;
    case 2:
      EXPECT_EQ(aggregate.commSplit, 1);
      break;
    case 1:
      EXPECT_EQ(aggregate.commSplit, 0);
      break;
    default: Warning("No test case for this amount of processes")
niklas.baumgarten's avatar
niklas.baumgarten committed
157
  }
niklas.baumgarten's avatar
niklas.baumgarten committed
158
159
  EXPECT_EQ(aggregate.ctr.dMcomm, numSamples / PPM->Size(0));
  TestSampleCounterParallel();
niklas.baumgarten's avatar
niklas.baumgarten committed
160
161
}

162
TEST_F(TestWelfordAggregate1e6Samples, TestMean) { TestMeanParallel(); }
niklas.baumgarten's avatar
niklas.baumgarten committed
163

niklas.baumgarten's avatar
niklas.baumgarten committed
164
#endif //TESTWELFORDAGGREGATE_HPP