Caffe2 - C++ API
A deep learning, cross platform ML framework
test_parallel.cpp
1 #include <gtest/gtest.h>
2 
3 #include <ATen/ATen.h>
4 #include <ATen/DLConvertor.h>
5 #include <ATen/Parallel.h>
6 
7 #include <iostream>
8 #include <string.h>
9 #include <sstream>
10 
11 using namespace at;
12 
13 TEST(TestParallel, TestParallel) {
14  manual_seed(123);
15  set_num_threads(1);
16 
17  Tensor a = rand({1, 3});
18  a[0][0] = 1;
19  a[0][1] = 0;
20  a[0][2] = 0;
21  Tensor as = rand({3});
22  as[0] = 1;
23  as[1] = 0;
24  as[2] = 0;
25  ASSERT_TRUE(a.sum(0).equal(as));
26 }
27 
28 TEST(TestParallel, NestedParallel) {
29  Tensor a = ones({1024, 1024});
30  auto expected = a.sum();
31  // check that calling sum() from within a parallel block computes the same result
32  at::parallel_for(0, 10, 1, [&](int64_t begin, int64_t end) {
33  if (begin == 0) {
34  ASSERT_TRUE(a.sum().equal(expected));
35  }
36  });
37 }
38 
39 TEST(TestParallel, Exceptions) {
40  // parallel case
41  ASSERT_THROW(
42  at::parallel_for(0, 10, 1, [&](int64_t begin, int64_t end) {
43  throw std::runtime_error("exception");
44  }),
45  std::runtime_error);
46 
47  // non-parallel case
48  ASSERT_THROW(
49  at::parallel_for(0, 1, 1000, [&](int64_t begin, int64_t end) {
50  throw std::runtime_error("exception");
51  }),
52  std::runtime_error);
53 }
Flush-To-Zero and Denormals-Are-Zero mode.