• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <CpuExecutor.h>
18 #include <gtest/gtest.h>
19 #include <omp.h>
20 #include <unistd.h>
21 
22 #include <algorithm>
23 #include <memory>
24 #include <random>
25 #include <thread>
26 #include <vector>
27 
28 namespace {
29 
30 class OpenmpSettingsTest : public ::testing::Test {
31    protected:
SetUp()32     virtual void SetUp() override {
33         const int blocktimeInitial = kmp_get_blocktime();
34         ASSERT_EQ(blocktimeInitial, kOpenmpDefaultBlockTime);
35     }
TearDown()36     virtual void TearDown() override {
37         const int blocktimeRestored = kmp_get_blocktime();
38         ASSERT_EQ(blocktimeRestored, kOpenmpDefaultBlockTime);
39     }
40     static const int kOpenmpDefaultBlockTime;
41     static const int kPreferredBlockTime;
42 };
43 
44 const int OpenmpSettingsTest::kOpenmpDefaultBlockTime = 200;
45 const int OpenmpSettingsTest::kPreferredBlockTime = 20;
46 
47 using ::android::nn::ScopedOpenmpSettings;
48 
TEST_F(OpenmpSettingsTest,TestkPreferredBlockTime)49 TEST_F(OpenmpSettingsTest, TestkPreferredBlockTime) {
50     ScopedOpenmpSettings s;
51     const int blocktimeSet = kmp_get_blocktime();
52     ASSERT_EQ(blocktimeSet, kPreferredBlockTime);
53 }
54 
TEST_F(OpenmpSettingsTest,Test2)55 TEST_F(OpenmpSettingsTest, Test2) {
56     ScopedOpenmpSettings s1;
57     const int blocktimeSet1 = kmp_get_blocktime();
58     ASSERT_EQ(blocktimeSet1, kPreferredBlockTime);
59 
60     ScopedOpenmpSettings s2;
61     const int blocktimeSet2 = kmp_get_blocktime();
62     ASSERT_EQ(blocktimeSet2, kPreferredBlockTime);
63 }
64 
TEST_F(OpenmpSettingsTest,TestThreaded)65 TEST_F(OpenmpSettingsTest, TestThreaded) {
66     // Threaded test to validate that each thread gets its own settings.
67     std::vector<std::thread> threads;
68     std::mt19937 randGen;
69     std::uniform_int_distribution<> rand(1, 20);
70     for (int i = 0; i < 10; i++) {
71         const int sleepFor = rand(randGen);
72         threads.push_back(std::thread([sleepFor]() {
73             const int blocktimeInitial = kmp_get_blocktime();
74             // kmp_get_blocktime() in a new thread returns 0 instead of 200
75             // about 10% of time on-device
76             ASSERT_TRUE(blocktimeInitial == 0 || blocktimeInitial == kOpenmpDefaultBlockTime);
77 
78             ScopedOpenmpSettings s;
79 
80             const int blocktimeSet1 = kmp_get_blocktime();
81             ASSERT_EQ(blocktimeSet1, kPreferredBlockTime);
82 
83             usleep(sleepFor);
84 
85             const int blocktimeSet2 = kmp_get_blocktime();
86             ASSERT_EQ(blocktimeSet2, kPreferredBlockTime);
87         }));
88     }
89     std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
90 }
91 
92 }  // end namespace
93