xref: /oneTBB/src/tbb/market.cpp (revision acc5bdfe)
1 /*
2     Copyright (c) 2005-2023 Intel Corporation
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 "arena.h"
18 #include "market.h"
19 
20 #include <algorithm> // std::find
21 
22 namespace tbb {
23 namespace detail {
24 namespace r1 {
25 
26 
27 class tbb_permit_manager_client : public pm_client {
28 public:
29     tbb_permit_manager_client(arena& a) : pm_client(a) {}
30 
31     void set_allotment(unsigned allotment) {
32         my_arena.set_allotment(allotment);
33     }
34 };
35 
36 //------------------------------------------------------------------------
37 // market
38 //------------------------------------------------------------------------
39 
40 market::market(unsigned workers_soft_limit)
41     : my_num_workers_soft_limit(workers_soft_limit)
42 {}
43 
44 pm_client* market::create_client(arena& a) {
45     return new (cache_aligned_allocate(sizeof(tbb_permit_manager_client))) tbb_permit_manager_client(a);
46 }
47 
48 void market::register_client(pm_client* c) {
49     mutex_type::scoped_lock lock(my_mutex);
50     my_clients[c->priority_level()].push_back(c);
51 }
52 
53 void market::unregister_and_destroy_client(pm_client& c) {
54     {
55         mutex_type::scoped_lock lock(my_mutex);
56         auto& clients = my_clients[c.priority_level()];
57         auto it = std::find(clients.begin(), clients.end(), &c);
58         __TBB_ASSERT(it != clients.end(), "Destroying of an unregistered client");
59         clients.erase(it);
60     }
61 
62     auto client = static_cast<tbb_permit_manager_client*>(&c);
63     client->~tbb_permit_manager_client();
64     cache_aligned_deallocate(client);
65 }
66 
67 void market::update_allotment() {
68     int effective_soft_limit = my_mandatory_num_requested > 0 && my_num_workers_soft_limit == 0 ? 1 : my_num_workers_soft_limit;
69     int max_workers = min(my_total_demand, effective_soft_limit);
70     __TBB_ASSERT(max_workers >= 0, nullptr);
71 
72     int unassigned_workers = max_workers;
73     int assigned = 0;
74     int carry = 0;
75     unsigned max_priority_level = num_priority_levels;
76     for (unsigned list_idx = 0; list_idx < num_priority_levels; ++list_idx ) {
77         int assigned_per_priority = min(my_priority_level_demand[list_idx], unassigned_workers);
78         unassigned_workers -= assigned_per_priority;
79         // We use reverse iterator there to serve last added clients first
80         for (auto it = my_clients[list_idx].rbegin(); it != my_clients[list_idx].rend(); ++it) {
81             tbb_permit_manager_client& client = static_cast<tbb_permit_manager_client&>(**it);
82             if (client.max_workers() == 0) {
83                 client.set_allotment(0);
84                 continue;
85             }
86 
87             if (max_priority_level == num_priority_levels) {
88                 max_priority_level = list_idx;
89             }
90 
91             int allotted = 0;
92             if (my_num_workers_soft_limit == 0) {
93                 __TBB_ASSERT(max_workers == 0 || max_workers == 1, nullptr);
94                 allotted = client.min_workers() > 0 && assigned < max_workers ? 1 : 0;
95             } else {
96                 int tmp = client.max_workers() * assigned_per_priority + carry;
97                 allotted = tmp / my_priority_level_demand[list_idx];
98                 carry = tmp % my_priority_level_demand[list_idx];
99                 __TBB_ASSERT(allotted <= client.max_workers(), nullptr);
100             }
101             client.set_allotment(allotted);
102             client.set_top_priority(list_idx == max_priority_level);
103             assigned += allotted;
104         }
105     }
106     __TBB_ASSERT(assigned == max_workers, nullptr);
107 }
108 
109 void market::set_active_num_workers(int soft_limit) {
110     mutex_type::scoped_lock lock(my_mutex);
111     if (my_num_workers_soft_limit != soft_limit) {
112         my_num_workers_soft_limit = soft_limit;
113         update_allotment();
114     }
115 }
116 
117 void market::adjust_demand(pm_client& c, int mandatory_delta, int workers_delta) {
118     __TBB_ASSERT(-1 <= mandatory_delta && mandatory_delta <= 1, nullptr);
119 
120     int delta{};
121     {
122         mutex_type::scoped_lock lock(my_mutex);
123         // Update client's state
124         delta = c.update_request(mandatory_delta, workers_delta);
125 
126         // Update market's state
127         my_total_demand += delta;
128         my_priority_level_demand[c.priority_level()] += delta;
129         my_mandatory_num_requested += mandatory_delta;
130 
131         update_allotment();
132     }
133 
134     notify_thread_request(delta);
135 }
136 
137 } // namespace r1
138 } // namespace detail
139 } // namespace tbb
140