concurrenthistogramtest.cpp 8.62 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

/**
 * Author: Hossain Mahmud <mahmud@in.tum.de>
 * Date: April 2014
 */

#include "gtest/gtest.h"

#include "core/tools/concurrenthistogram.h"

#include <tbb/tbb.h>

using namespace campvis;


/**
 * Test class for ConcurrentGenericHistogramND. Initializes a uniform distribution
 * over ND dimensional space. Buckets are chosen in each unit, i.e. number of buckets
 * is range+1. 
 */

template<size_t ND>
46
class ConcurrentHistogramNDUniform {
47
protected:
48
    ConcurrentHistogramNDUniform()
49
50
51
52
    {
        max = new int[ND];
        min = new int[ND];
        numBuckets = new size_t[ND];
53
        for (size_t i = 0; i < ND; i++) { 
54
55
            min[i] = 0; 
            max[i] = 100;
56
57
58
            numBuckets[i] = max[i] - min[i] + 1; 
        }

59
60
        int nd = ND;
        numSamples = static_cast<int>(pow(static_cast<float>(max[0]-min[0]+1), nd));
61
62
        initSamples();

63
        histogram = new int[numSamples];
64
65
66
67
68
        computeHistogram();

        _cgh = new campvis::ConcurrentGenericHistogramND<int, ND>(min, max, numBuckets);
    }

69
    ~ConcurrentHistogramNDUniform() {
70
71
72
73
74
75
76
77
78
79
80
81
82
        delete max;
        delete min;
        delete numBuckets;
        delete histogram;
    }

    virtual void SetUp() {
    }

    virtual void TearDown() {
    }

    void initSamples() {
83
        double range = max[0] - min[0] + 1;
84
        for (int i = 0; i < numSamples; i++) {
85
86
            std::vector<int> sample;
            int x = i;
87
            for (int j = ND-1; j >= 0; j--) {
88
89
90
91
92
93
94
                sample.push_back(x / static_cast<int>(pow(range, j)));
                x = x % static_cast<int>(pow(range, j));
            }
            samples.push_back(sample);
        }
    }
    void computeHistogram() {
95
        int total = 0;
96
97
        for (int i = 0; i < numSamples; i++) {
            histogram[i] = 1;
98
            total += histogram[i];
99
        }
100
        EXPECT_EQ(numSamples, total);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    }

    int getND() {return ND;}

protected:
    int *max, *min;
    size_t *numBuckets;
    campvis::ConcurrentGenericHistogramND<int, ND>* _cgh;

    int numSamples;
    std::vector<std::vector<int> > samples;
    int *histogram;

};

/** 
 * Test class 1D system.
 */
119
class ConcurrentHistogram1DTest : public ConcurrentHistogramNDUniform<1>, public ::testing::Test {
120
121
122
123
124
125
126
127
128
129
130
public:
    ConcurrentHistogram1DTest() {
    }
};

/** 
 * Expected number of elements at each bucket should be 1
 */
TEST_F(ConcurrentHistogram1DTest, concurrentAddSampleTest) {
    tbb::parallel_for(tbb::blocked_range<size_t>(0, samples.size()), [&] (const tbb::blocked_range<size_t>& range) {
        for (size_t i = range.begin(); i != range.end(); ++i) {
131
            const std::vector<int>& vec = samples[i];
132
133
134
135
136
            _cgh->addSample(&(samples[i].front()));
        }
    });

    for (int i = 0; i < getND(); i++) {
137
        for (size_t j = 0; j < numBuckets[i]; j ++) {
138
139
            size_t expected = static_cast<size_t>(histogram[i]);
            EXPECT_EQ(expected, _cgh->getNumElements(j));
140
141
142
143
144
145
146
        }
    }
}

/** 
 * Test class 2D system.
 */
147
class ConcurrentHistogram2DTest : public ConcurrentHistogramNDUniform<2>, public ::testing::Test {
148
149
150
151
152
153
154
155
156
157
158
public:
    ConcurrentHistogram2DTest() {
    }
};

/** 
 * Expected number of elements at each bucket should be 1
 */
TEST_F(ConcurrentHistogram2DTest, concurrentAddSampleTest) {
    tbb::parallel_for(tbb::blocked_range<size_t>(0, samples.size()), [&] (const tbb::blocked_range<size_t>& range) {
        for (size_t i = range.begin(); i != range.end(); ++i) {
159
            const std::vector<int>& vec = samples[i];
160
161
162
163
164
            _cgh->addSample(&(samples[i].front()));
        }
    });

    for (int i = 0; i < getND(); i++) {
165
        for (size_t j = 0; j < numBuckets[i]; j ++) {
166
167
            size_t expected = static_cast<size_t>(histogram[i]);
            EXPECT_EQ(expected, _cgh->getNumElements(j));
168
169
170
        }
        break;
    }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
}


/**
 * Test class for ConcurrentGenericHistogramND. Initializes a uniform distribution
 * over ND dimensional space. Buckets are chosen in each unit, i.e. number of buckets
 * is range+1. 
 */

template<size_t ND>
class ConcurrentHistogramNDSpecific {
protected:
    ConcurrentHistogramNDSpecific()
    {
        max = new int[ND];
        min = new int[ND];
        numBuckets = new size_t[ND];
188
        for (size_t i = 0; i < ND; i++) { 
189
190
191
192
193
            min[i] = 0; 
            max[i] = 9999;
            numBuckets[i] = 2; 
        }

194
195
        int nd = ND;
        numSamples = static_cast<int>(pow(static_cast<double>(max[0]-min[0]+1), nd));
196
197
        initSamples();

198
        histogram = new int[numSamples];
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        computeHistogram();

        _cgh = new campvis::ConcurrentGenericHistogramND<int, ND>(min, max, numBuckets);
    }

    ~ConcurrentHistogramNDSpecific() {
        delete max;
        delete min;
        delete numBuckets;
        delete histogram;
    }

    virtual void SetUp() {
    }

    virtual void TearDown() {
    }

    void initSamples() {
218
        double range = static_cast<double>(max[0] - min[0] + 1);
219
220
221
222
223
224
225
226
227
228
229
230
        for (int i = 0; i < numSamples; i++) {
            std::vector<int> sample;
            int x = i;
            for (int j = ND-1; j >= 0; j--) {
                sample.push_back(x / static_cast<int>(pow(range, j)));
                x = x % static_cast<int>(pow(range, j));
            }
            samples.push_back(sample);
        }
    }
    void computeHistogram() {
        int total = 0;
231
232
233
        int nd = ND;
        for (int i = 0; i < pow(2.0, nd); i++) {
            histogram[i] = static_cast<int>(numSamples / pow(2.0, nd) );
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
            total += histogram[i];
        }
        EXPECT_EQ(numSamples, total);

    }

    int getND() {return ND;}

protected:
    int *max, *min;
    size_t *numBuckets;
    campvis::ConcurrentGenericHistogramND<int, ND>* _cgh;

    int numSamples;
    std::vector<std::vector<int> > samples;
    int *histogram;

};

/** 
 * Test class 1D system.
 */
class ConcurrentHistogram1DTestSpecific : public ConcurrentHistogramNDSpecific<1>, public ::testing::Test {
public:
    ConcurrentHistogram1DTestSpecific() {
    }
};

/** 
 * Expected number of elements at each bucket should be 1
 */
TEST_F(ConcurrentHistogram1DTestSpecific, concurrentAddSampleTest) {
    tbb::parallel_for(tbb::blocked_range<size_t>(0, samples.size()), [&] (const tbb::blocked_range<size_t>& range) {
        for (size_t i = range.begin(); i != range.end(); ++i) {
            std::vector<int> vec = samples[i];
            _cgh->addSample(&(samples[i].front()));
        }
    });

    for (int i = 0; i < getND(); i++) {
274
        for (size_t j = 0; j < numBuckets[i]; j ++) {
275
276
            size_t expected = static_cast<size_t>(histogram[i]);
            EXPECT_EQ(expected, _cgh->getNumElements(j));
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
        }
    }
}

/** 
 * Test class 2D system.
 */
class ConcurrentHistogram2DTestSpecific : public ConcurrentHistogramNDUniform<2>, public ::testing::Test {
public:
    ConcurrentHistogram2DTestSpecific() {
    }
};

/** 
 * Expected number of elements at each bucket should be 1
 */
TEST_F(ConcurrentHistogram2DTestSpecific, concurrentAddSampleTest) {
    tbb::parallel_for(tbb::blocked_range<size_t>(0, samples.size()), [&] (const tbb::blocked_range<size_t>& range) {
        for (size_t i = range.begin(); i != range.end(); ++i) {
            std::vector<int> vec = samples[i];
            _cgh->addSample(&(samples[i].front()));
        }
    });

    for (int i = 0; i < getND(); i++) {
302
        for (size_t j = 0; j < numBuckets[i]; j ++) {
303
304
            size_t expected = static_cast<size_t>(histogram[i]);
            EXPECT_EQ(expected, _cgh->getNumElements(j));
305
306
307
308
309
        }
        break;
    }
}