Skip to content

Commit

Permalink
Added dynamic load policy parallel inline tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuyaWelling2801 committed Jun 24, 2024
1 parent 441e311 commit 2bf82df
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ main()
std::vector<sycl::queue> u;
std::unordered_map<sycl::queue, int> map;
build_dl_universe(u, map);

int best_resource=0;
auto n = u.size();

//If building the universe is not a success, return
Expand All @@ -315,12 +315,12 @@ main()
constexpr bool just_call_submit = false;
constexpr bool call_select_before_submit = true;

auto actual = test_submit_and_wait_on_event<call_select_before_submit, policy_t>(u, map, 0);
actual = test_submit_and_wait_on_event<call_select_before_submit, policy_t>(u, map, 0);
actual = test_submit_and_wait_on_group<just_call_submit, policy_t>(u, map, 0);
actual = test_submit_and_wait_on_group<call_select_before_submit, policy_t>(u, map, 0);
actual = test_submit_and_wait<just_call_submit, policy_t>(u, map, 0);
actual = test_submit_and_wait<call_select_before_submit, policy_t>(u, map, 0);
auto actual = test_submit_and_wait_on_event<call_select_before_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_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);
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);

bProcessed = true;
}
Expand Down
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();
}
3 changes: 0 additions & 3 deletions test/support/test_fixed_resource_policy_parallel_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ test_submit_and_wait(UniverseContainer u, UniverseMapping map, std::vector<int>
using my_policy_t = Policy;
my_policy_t p(u, offset);
std::vector<int> result(u.size(), 0);
const int N = 5;

std::vector<std::thread> threads;
auto func = [&result,&map](typename oneapi::dpl::experimental::policy_traits<Policy>::resource_type e) {
Expand Down Expand Up @@ -79,7 +78,6 @@ test_submit_and_wait_on_group(UniverseContainer u, UniverseMapping map, std::vec
using my_policy_t = Policy;
my_policy_t p(u, offset);
std::vector<int> result(u.size(), 0);
const int N = 5;

std::vector<std::thread> threads;
auto func = [&result,&map](typename oneapi::dpl::experimental::policy_traits<Policy>::resource_type e) {
Expand Down Expand Up @@ -137,7 +135,6 @@ test_submit_and_wait_on_event(UniverseContainer u, UniverseMapping map, std::vec
using my_policy_t = Policy;
my_policy_t p(u, offset);
std::vector<int> result(u.size(), 0);
const int N = 5;

auto func = [&result,&map](typename oneapi::dpl::experimental::policy_traits<Policy>::resource_type e) {
int x = map[e];
Expand Down

0 comments on commit 2bf82df

Please sign in to comment.