Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
test_omp_threads.cpp
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD+Patents license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <cstdio>
10 #include <cstdlib>
11 
12 #include <gtest/gtest.h>
13 
14 #include <omp.h>
15 
16 TEST(Threading, openmp) {
17 
18  omp_set_num_threads(10);
19 
20  EXPECT_EQ(omp_get_max_threads(), 10);
21 
22  std::vector<int> nt_per_thread(10);
23  size_t sum = 0;
24 #pragma omp parallel reduction(+: sum)
25  {
26  int nt = omp_get_num_threads ();
27  int rank = omp_get_thread_num ();
28 
29  nt_per_thread[rank] = nt;
30 #pragma omp for
31  for(int i = 0; i < 1000 * 1000 * 10; i++) {
32  sum += i;
33  }
34  }
35 
36 
37  EXPECT_EQ (nt_per_thread[0], 10);
38  EXPECT_GT (sum, 0);
39 }