1
2
3
4
5
6
7
8
9
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD+Patents license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cstdio>
#include <cstdlib>
#include <gtest/gtest.h>
#include <omp.h>
TEST(Threading, openmp) {
omp_set_num_threads(10);
EXPECT_EQ(omp_get_max_threads(), 10);
std::vector<int> nt_per_thread(10);
size_t sum = 0;
#pragma omp parallel reduction(+: sum)
{
int nt = omp_get_num_threads ();
int rank = omp_get_thread_num ();
nt_per_thread[rank] = nt;
#pragma omp for
for(int i = 0; i < 1000 * 1000 * 10; i++) {
sum += i;
}
}
EXPECT_EQ (nt_per_thread[0], 10);
EXPECT_GT (sum, 0);
}