File: test_numa_dist.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (164 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
    Copyright (c) 2005-2025 Intel Corporation

    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 "common/test.h"

#if !__TBB_WIN8UI_SUPPORT

#include <stdio.h>
#include "tbb/parallel_for.h"
#include "tbb/global_control.h"
#include "tbb/enumerable_thread_specific.h"

#include "common/config.h"
#include "common/utils.h"
#include "common/utils_concurrency_limit.h"
#include "common/utils_report.h"
#include "common/vector_types.h"
#include "common/cpu_usertime.h"
#include "common/spin_barrier.h"
#include "common/exception_handling.h"
#include "common/concepts_common.h"
#include "test_partitioner.h"

#include <cstddef>
#include <vector>

//! \file test_numa_dist.cpp
//! \brief Test for [internal] functionality
#if _MSC_VER
#pragma warning (push)
// Suppress conditional expression is constant
#pragma warning (disable: 4127)
#if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
    // Suppress pointless "unreachable code" warning.
    #pragma warning (disable: 4702)
#endif
#if defined(_Wp64)
    // Workaround for overzealous compiler warnings in /Wp64 mode
    #pragma warning (disable: 4267)
#endif
#define _SCL_SECURE_NO_WARNINGS
#endif //#if _MSC_VER


struct numa {
    WORD processorGroupCount;
    std::vector<DWORD> numaProcessors;
    DWORD maxProcessors;
    numa() : processorGroupCount(GetMaximumProcessorGroupCount()), maxProcessors(GetActiveProcessorCount(ALL_PROCESSOR_GROUPS)){   
        numaProcessors.resize(processorGroupCount);
       for (WORD i = 0; i < processorGroupCount; i++) {
            this->numaProcessors[i] = GetActiveProcessorCount((i));
        }        
    }
};


int TestNumaDistribution(std::vector<DWORD> &validateProcgrp, int additionalParallelism, bool allThreads){
    validateProcgrp.resize(GetMaximumProcessorGroupCount());
    struct numa nodes;
    int master_thread_proc_grp = [&]{
        PROCESSOR_NUMBER proc;
        GetThreadIdealProcessorEx(GetCurrentThread(), &proc);
        return proc.Group;
    }();
    int requested_parallelism;
    if (allThreads) 
        requested_parallelism = additionalParallelism;
    else 
        requested_parallelism = nodes.numaProcessors.at(master_thread_proc_grp) + additionalParallelism;
    tbb::global_control global_limit(oneapi::tbb::global_control::max_allowed_parallelism, 1024);
    tbb::enumerable_thread_specific< std::pair<int, int> > tls;
    tbb::enumerable_thread_specific< double > tls_dummy;
    tbb::static_partitioner s;
  
    utils::SpinBarrier sb(requested_parallelism);
    oneapi::tbb::task_arena limited(requested_parallelism);
    limited.execute([&]() {

        tbb::parallel_for(0, requested_parallelism, [&](int)
            {                    
                PROCESSOR_NUMBER proc;
                if (GetThreadIdealProcessorEx(GetCurrentThread(), &proc))
                {
                    tls.local() = std::pair<int, int>(proc.Group, proc.Number);
                    sb.wait();
                }
            }, s);
        for (const auto& it : tls) {
           validateProcgrp[it.first]++;
        }
      });
    

    return master_thread_proc_grp;
}

//! Testing Numa Thread Distribution Stability
//! \brief \ref stress
TEST_CASE("Numa stability for the same node") {
    numa example;
    std::vector<DWORD> validateProcgrp;
    
    int numaGrp = TestNumaDistribution(validateProcgrp,0, 0);
    std::vector<DWORD> result(GetMaximumProcessorGroupCount(), 0);
    result[numaGrp] = example.numaProcessors[numaGrp];
    REQUIRE(validateProcgrp == result);
}

//! Testing Numa Thread Distribution Overflow
//! \brief \ref stress
TEST_CASE("Numa overflow") {
    numa example;
    std::vector<DWORD> validateProcgrp;
    
    int numaGrp = TestNumaDistribution(validateProcgrp, 1, 0);
    std::vector<DWORD> result(GetMaximumProcessorGroupCount(), 0);
    if (example.processorGroupCount <= 1) { // for single Numa node
       result[numaGrp] = example.numaProcessors[numaGrp] + 1;
    } else {
       result[numaGrp] = example.numaProcessors[numaGrp];
       result[(numaGrp+1)% GetMaximumProcessorGroupCount()] = 1;
    }
    REQUIRE(validateProcgrp == result);
}

//! Testing Numa Thread Distribution Maximum
//! \brief \ref stress
TEST_CASE("Numa all threads") {
    numa example;
    std::vector<DWORD> validateProcgrp;
    TestNumaDistribution(validateProcgrp, example.maxProcessors, 1);
    REQUIRE(validateProcgrp == example.numaProcessors);
}

//! Testing Numa Thread Distribution Doubled Max
//! \brief \ref stress
TEST_CASE("Double threads") {
    numa example;
    std::vector<DWORD> validateProcgrp;
    std::vector<DWORD> result(example.numaProcessors.size(), 0);
    for (size_t i = 0; i < example.numaProcessors.size(); i++) result[i] = 2 * example.numaProcessors[i];
    TestNumaDistribution(validateProcgrp, example.maxProcessors * 2, 1);
    REQUIRE(validateProcgrp == result);
}

#if _MSC_VER
#pragma warning (pop)
#endif

#endif // !__TBB_WIN8UI_SUPPORT