-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added dynamic load policy parallel inline tests
- Loading branch information
1 parent
441e311
commit 2bf82df
Showing
3 changed files
with
196 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
...el_api/dynamic_selection/parallel_tests/test_dynamic_load_policy_parallel_inline.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// -*- C++ -*- | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Copyright (C) 2023 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "support/test_config.h" | ||
|
||
#include "oneapi/dpl/dynamic_selection" | ||
|
||
#include "support/inline_backend.h" | ||
#include "support/utils.h" | ||
|
||
template <bool call_select_before_submit, typename Policy, typename UniverseContainer, typename UniverseMapping> | ||
int | ||
test_submit_and_wait(UniverseContainer u, UniverseMapping map, int best_resource) | ||
{ | ||
using my_policy_t = Policy; | ||
my_policy_t p(u); | ||
std::vector<int> result(u.size(), 0); | ||
|
||
std::vector<std::thread> threads; | ||
auto func = [&result,&map](typename oneapi::dpl::experimental::policy_traits<Policy>::resource_type e) { | ||
int x = map[e]; | ||
result[x]++; | ||
return e; | ||
}; | ||
if(call_select_before_submit){ | ||
auto thread_func = [&p, &func](){ | ||
for(int i=0;i<10;i++){ | ||
auto s = oneapi::dpl::experimental::select(p); | ||
oneapi::dpl::experimental::submit_and_wait(s, func); | ||
|
||
} | ||
}; | ||
|
||
for(int i=0;i<5;i++){ | ||
threads.push_back(std::thread(thread_func)); | ||
} | ||
} | ||
else{ | ||
auto thread_func = [&p, &func](){ | ||
for(int i=0;i<10;i++){ | ||
oneapi::dpl::experimental::submit_and_wait(p, func); | ||
} | ||
}; | ||
|
||
for(int i=0;i<5;i++){ | ||
threads.push_back(std::thread(thread_func)); | ||
} | ||
} | ||
for(auto& thread : threads){ | ||
thread.join(); | ||
} | ||
auto result_element = std::distance(result.begin(),std::max_element(result.begin(), result.end())); | ||
EXPECT_TRUE(result_element==best_resource, "ERROR : did not select expected resources\n"); | ||
std::cout<<"Submit and wait on event : OK\n"; | ||
return 0; | ||
} | ||
template <bool call_select_before_submit, typename Policy, typename UniverseContainer, typename UniverseMapping> | ||
int | ||
test_submit_and_wait_on_group(UniverseContainer u, UniverseMapping map, int best_resource) | ||
{ | ||
using my_policy_t = Policy; | ||
my_policy_t p(u); | ||
std::vector<int> result(u.size(), 0); | ||
|
||
std::vector<std::thread> threads; | ||
auto func = [&result,&map](typename oneapi::dpl::experimental::policy_traits<Policy>::resource_type e) { | ||
int x = map[e]; | ||
result[x]++; | ||
return e; | ||
}; | ||
if(call_select_before_submit){ | ||
auto thread_func = [&p, &func](){ | ||
for(int i=0;i<10;i++){ | ||
auto s = oneapi::dpl::experimental::select(p); | ||
|
||
auto w = oneapi::dpl::experimental::submit(s, func); | ||
} | ||
}; | ||
|
||
for(int i=0;i<5;i++){ | ||
threads.push_back(std::thread(thread_func)); | ||
} | ||
} | ||
else{ | ||
auto thread_func = [&p, &func](){ | ||
for(int i=0;i<10;i++){ | ||
auto w = oneapi::dpl::experimental::submit(p, func); | ||
} | ||
}; | ||
|
||
for(int i=0;i<5;i++){ | ||
threads.push_back(std::thread(thread_func)); | ||
} | ||
} | ||
oneapi::dpl::experimental::wait(p.get_submission_group()); | ||
for(auto& thread : threads){ | ||
thread.join(); | ||
} | ||
|
||
auto result_element = std::distance(result.begin(),std::max_element(result.begin(), result.end())); | ||
EXPECT_TRUE(result_element==best_resource, "ERROR : did not select expected resources\n"); | ||
std::cout<<"Submit and wait on event : OK\n"; | ||
return 0; | ||
} | ||
template <bool call_select_before_submit, typename Policy, typename UniverseContainer, typename UniverseMap> | ||
int | ||
test_submit_and_wait_on_event(UniverseContainer u, UniverseMap&& map, int best_resource) | ||
{ | ||
using my_policy_t = Policy; | ||
my_policy_t p(u); | ||
std::vector<int> result(u.size(), 0); | ||
|
||
auto func = [&result,&map](typename oneapi::dpl::experimental::policy_traits<Policy>::resource_type e) { | ||
int x = map[e]; | ||
result[x]++; | ||
return e; | ||
}; | ||
|
||
std::vector<std::thread> threads; | ||
if(call_select_before_submit){ | ||
auto thread_func = [&p, &func](){ | ||
for(int i=0;i<10;i++){ | ||
auto s = oneapi::dpl::experimental::select(p); | ||
|
||
auto w = oneapi::dpl::experimental::submit(s, func); | ||
oneapi::dpl::experimental::wait(w); | ||
} | ||
}; | ||
|
||
for(int i=0;i<5;i++){ | ||
threads.push_back(std::thread(thread_func)); | ||
} | ||
} | ||
else{ | ||
auto thread_func = [&p, &func](){ | ||
for(int i=0;i<10;i++){ | ||
auto w = oneapi::dpl::experimental::submit(p, func); | ||
oneapi::dpl::experimental::wait(w); | ||
} | ||
}; | ||
|
||
for(int i=0;i<5;i++){ | ||
threads.push_back(std::thread(thread_func)); | ||
} | ||
} | ||
for(auto& thread : threads){ | ||
thread.join(); | ||
} | ||
|
||
auto result_element = std::distance(result.begin(),std::max_element(result.begin(), result.end())); | ||
EXPECT_TRUE(result_element==best_resource, "ERROR : did not select expected resources\n"); | ||
std::cout<<"Submit and wait on event : OK\n"; | ||
return 0; | ||
} | ||
#if TEST_DYNAMIC_SELECTION_AVAILABLE | ||
static inline void | ||
build_universe(std::vector<int>& u, std::unordered_map<int, int>& map) | ||
{ | ||
for(int i=0;i<u.size();i++){ | ||
map[u[i]]=i; | ||
} | ||
} | ||
#endif // TEST_DYNAMIC_SELECTION_AVAILABLE | ||
int | ||
main() | ||
{ | ||
using policy_t = oneapi::dpl::experimental::dynamic_load_policy<TestUtils::int_inline_backend_t>; | ||
std::unordered_map<int, int> map; | ||
std::vector<int> u{4, 5, 6, 7}; | ||
build_universe(u, map); | ||
int best_resource=0; | ||
constexpr bool just_call_submit = false; | ||
constexpr bool call_select_before_submit = true; | ||
|
||
auto actual = test_submit_and_wait_on_event<just_call_submit, policy_t>(u, map, best_resource); | ||
actual = test_submit_and_wait_on_event<call_select_before_submit, policy_t>(u, map, best_resource); | ||
actual = test_submit_and_wait<just_call_submit, policy_t>(u, map, best_resource); | ||
actual = test_submit_and_wait<call_select_before_submit, policy_t>(u, map, best_resource); | ||
actual = test_submit_and_wait_on_group<just_call_submit, policy_t>(u, map, best_resource); | ||
actual = test_submit_and_wait_on_group<call_select_before_submit, policy_t>(u, map, best_resource); | ||
|
||
return TestUtils::done(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters