summaryrefslogtreecommitdiff
path: root/nn/runtime/test/TestOpenmpSettings.cpp
blob: 549473b46edcf532750acafba753907b7cea9084 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "CpuExecutor.h"

#include <algorithm>
#include <gtest/gtest.h>
#include <memory>
#include <omp.h>
#include <random>
#include <thread>
#include <unistd.h>
#include <vector>

namespace {

class OpenmpSettingsTest : public ::testing::Test {
protected:
    virtual void SetUp() override {
        const int blocktimeInitial = kmp_get_blocktime();
        ASSERT_EQ(blocktimeInitial, kOpenmpDefaultBlockTime);
    }
    virtual void TearDown() override {
        const int blocktimeRestored = kmp_get_blocktime();
        ASSERT_EQ(blocktimeRestored, kOpenmpDefaultBlockTime);
    }
    static const int kOpenmpDefaultBlockTime;
};

const int OpenmpSettingsTest::kOpenmpDefaultBlockTime = 200;

using ::android::nn::ScopedOpenmpSettings;

TEST_F(OpenmpSettingsTest, Test1) {
    ScopedOpenmpSettings s;
    const int blocktimeSet = kmp_get_blocktime();
    ASSERT_EQ(blocktimeSet, 1);
}

TEST_F(OpenmpSettingsTest, Test2) {
    ScopedOpenmpSettings s1;
    const int blocktimeSet1 = kmp_get_blocktime();
    ASSERT_EQ(blocktimeSet1, 1);

    ScopedOpenmpSettings s2;
    const int blocktimeSet2 = kmp_get_blocktime();
    ASSERT_EQ(blocktimeSet2, 1);
}

TEST_F(OpenmpSettingsTest, TestThreaded) {
    // Threaded test to validate that each thread gets its own settings.
    std::vector<std::thread> threads;
    std::mt19937 randGen;
    std::uniform_int_distribution<> rand(1, 20);
    for (int i = 0; i < 10; i++) {
        const int sleepFor = rand(randGen);
        threads.push_back(std::thread([sleepFor]() {
            const int blocktimeSet1 = kmp_get_blocktime();
            ASSERT_EQ(blocktimeSet1, kOpenmpDefaultBlockTime);

            ScopedOpenmpSettings s;

            const int blocktimeSet2 = kmp_get_blocktime();
            ASSERT_EQ(blocktimeSet2, 1);

            usleep(sleepFor);

            const int blocktimeSet3 = kmp_get_blocktime();
            ASSERT_EQ(blocktimeSet3, 1);
        }));
    }
    std::for_each(threads.begin(), threads.end(), [](std::thread& t) {
        t.join();
    });
}

}  // end namespace