Home | History | Annotate | Download | only in test
      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 
     19 #include <algorithm>
     20 #include <gtest/gtest.h>
     21 #include <memory>
     22 #include <omp.h>
     23 #include <random>
     24 #include <thread>
     25 #include <unistd.h>
     26 #include <vector>
     27 
     28 namespace {
     29 
     30 class OpenmpSettingsTest : public ::testing::Test {
     31 protected:
     32     virtual void SetUp() override {
     33         const int blocktimeInitial = kmp_get_blocktime();
     34         ASSERT_EQ(blocktimeInitial, kOpenmpDefaultBlockTime);
     35     }
     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 
     49 TEST_F(OpenmpSettingsTest, TestkPreferredBlockTime) {
     50     ScopedOpenmpSettings s;
     51     const int blocktimeSet = kmp_get_blocktime();
     52     ASSERT_EQ(blocktimeSet, kPreferredBlockTime);
     53 }
     54 
     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 
     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 blocktimeSet1 = kmp_get_blocktime();
     74             ASSERT_EQ(blocktimeSet1, kOpenmpDefaultBlockTime);
     75 
     76             ScopedOpenmpSettings s;
     77 
     78             const int blocktimeSet2 = kmp_get_blocktime();
     79             ASSERT_EQ(blocktimeSet2, kPreferredBlockTime);
     80 
     81             usleep(sleepFor);
     82 
     83             const int blocktimeSet3 = kmp_get_blocktime();
     84             ASSERT_EQ(blocktimeSet3, kPreferredBlockTime);
     85         }));
     86     }
     87     std::for_each(threads.begin(), threads.end(), [](std::thread& t) {
     88         t.join();
     89     });
     90 }
     91 
     92 }  // end namespace
     93