Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing threading control interfaces (WIP) #2466

Draft
wants to merge 76 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
c06d49a
Add create_task_arena method
olegkkruglov Jun 15, 2023
a637a20
Add task_arena parameter to DAAL thread-pinner
olegkkruglov Jun 15, 2023
ea1f2c9
Rewrite dispatch_by_cpu method to send tasks into task_arena
olegkkruglov Jun 20, 2023
109410c
Add TBB_PREVIEW_TASK_ARENA_CONSTRAINTS_EXTENSION define
olegkkruglov Jun 21, 2023
717a140
Move create_task_arena to cpp, add namespaces, rewrite threading_poli…
olegkkruglov Jun 22, 2023
bdced8d
Remove unnecessary print, move TBB-related define to the right place
olegkkruglov Jun 28, 2023
1a5c3e1
Add returning value for thread_pinner.execute (WIP)
olegkkruglov Jul 11, 2023
55258d6
WIP
olegkkruglov Jul 12, 2023
36c53cb
Add thread_pinner and task_arena functionality to oneapi part
olegkkruglov Jul 12, 2023
4ec54ba
policy(false, 1)
olegkkruglov Jul 13, 2023
9920973
policy(false, 0)
olegkkruglov Jul 13, 2023
64f776e
policy(true, 0)
olegkkruglov Jul 13, 2023
30d2c0d
policy(true, 1)
olegkkruglov Jul 13, 2023
3139371
Add threading_policy to host_policy and cpu_context
olegkkruglov Aug 10, 2023
2c4c5b6
Changing interfaces to keep backward compatibility (WIP)
olegkkruglov Aug 19, 2023
6084f0a
Codefactor fixes
olegkkruglov Aug 20, 2023
f5fa870
Remove redundant include
olegkkruglov Aug 20, 2023
ebf746e
Change task variable name
olegkkruglov Aug 20, 2023
3e0c6be
Remove AVX and SSSE3 from dispatch_by_cpu
olegkkruglov Aug 20, 2023
3569055
Add create_task_arena method
olegkkruglov Jun 15, 2023
d8379b0
Add task_arena parameter to DAAL thread-pinner
olegkkruglov Jun 15, 2023
4b75be5
Rewrite dispatch_by_cpu method to send tasks into task_arena
olegkkruglov Jun 20, 2023
0ee81ea
Add TBB_PREVIEW_TASK_ARENA_CONSTRAINTS_EXTENSION define
olegkkruglov Jun 21, 2023
62a7ba5
Move create_task_arena to cpp, add namespaces, rewrite threading_poli…
olegkkruglov Jun 22, 2023
82e321f
Remove unnecessary print, move TBB-related define to the right place
olegkkruglov Jun 28, 2023
7a162ea
Add returning value for thread_pinner.execute (WIP)
olegkkruglov Jul 11, 2023
9be6702
WIP
olegkkruglov Jul 12, 2023
07b2128
Add thread_pinner and task_arena functionality to oneapi part
olegkkruglov Jul 12, 2023
9e21b9c
policy(false, 1)
olegkkruglov Jul 13, 2023
da9f2a7
policy(false, 0)
olegkkruglov Jul 13, 2023
69676bd
policy(true, 0)
olegkkruglov Jul 13, 2023
cf56876
policy(true, 1)
olegkkruglov Jul 13, 2023
cd89718
Add threading_policy to host_policy and cpu_context
olegkkruglov Aug 10, 2023
efab72e
Changing interfaces to keep backward compatibility (WIP)
olegkkruglov Aug 19, 2023
d46b695
Codefactor fixes
olegkkruglov Aug 20, 2023
d2587d1
Remove redundant include
olegkkruglov Aug 20, 2023
c363ae0
Change task variable name
olegkkruglov Aug 20, 2023
0d2176f
Apply clang-format
inteldimitrius Aug 23, 2023
39248a5
Minor fixes
inteldimitrius Aug 23, 2023
64e741b
Remove AVX and SSE3 dispatches
inteldimitrius Aug 23, 2023
2a1fa57
Merge from inteldimitrius
olegkkruglov Sep 4, 2023
898707f
Merge from master
olegkkruglov Sep 6, 2023
bf68169
Temporarily remove kmeans from BUILD file
olegkkruglov Sep 6, 2023
458291c
Fix format
olegkkruglov Sep 7, 2023
5c3a1ae
Return kmeans to BUILD file
olegkkruglov Sep 8, 2023
8c87583
Merge branch 'master' into tbb-arena
olegkkruglov Sep 18, 2023
f5072ac
Merge branch 'master' into pr/olegkkruglov/2466
Alexsandruss Sep 22, 2023
87abfdc
Merge branch 'tbb-arena' of https://github.com/olegkkruglov/oneDAL in…
olegkkruglov Sep 25, 2023
a64614c
Merge branch 'master' into pr/olegkkruglov/2466
Alexsandruss Sep 25, 2023
c0a1e4c
Merge branch 'master' into pr/olegkkruglov/2466
Alexsandruss Sep 27, 2023
65473b3
Change threading policy API, add max_concurrency
Alexsandruss Sep 28, 2023
afb9ece
Merge branch 'tbb-arena' of https://github.com/olegkkruglov/oneDAL in…
olegkkruglov Oct 1, 2023
ed98fbb
Make default host_policy non-static
olegkkruglov Oct 1, 2023
20d70e0
Fix clang-format
olegkkruglov Oct 1, 2023
0502f7c
Remove setters from default_host_policy
olegkkruglov Oct 1, 2023
b93e768
Fix max_concurrency setting
olegkkruglov Oct 2, 2023
ba87921
Return hyperthreading disabling to DAAL side
olegkkruglov Oct 2, 2023
6de90c2
Merge branch 'oneapi-src:master' into tbb-arena
olegkkruglov Oct 4, 2023
a782a6f
Revert "Return hyperthreading disabling to DAAL side"
Alexsandruss Oct 4, 2023
bb970ac
Enable external threading control in DAAL interface
Alexsandruss Oct 5, 2023
63b92dc
Remove extra variable
Alexsandruss Oct 5, 2023
457a681
Add macros to remove thread_pinner on macOS
olegkkruglov Oct 5, 2023
440c40b
Fix for init of cpu context
Alexsandruss Oct 5, 2023
e8830f9
Set default max_threads_per_core parameter to 1
olegkkruglov Oct 9, 2023
ddacb17
Merge branch 'oneapi-src:master' into tbb-arena
olegkkruglov Oct 10, 2023
bf9b006
Change task_executor interfaces
olegkkruglov Oct 11, 2023
7d61ae2
Disable new code on macOS
olegkkruglov Oct 12, 2023
de80db1
Fix
olegkkruglov Oct 12, 2023
2776392
clang-format fix
olegkkruglov Oct 12, 2023
a95276e
Merge branch 'oneapi-src:master' into tbb-arena
olegkkruglov Oct 23, 2023
4eb47a5
Remove new interfaces from windows
olegkkruglov Oct 23, 2023
bee0243
Add user_cpu_context and ability to provide host_policy via it
olegkkruglov Oct 31, 2023
ff5afff
Add forgotten pragma once
olegkkruglov Nov 2, 2023
85d0ff3
Replace tbb include and turn on new interfaces on windows
olegkkruglov Nov 3, 2023
8c8930a
Merge branch 'oneapi-src:master' into tbb-arena
olegkkruglov Nov 3, 2023
9734264
Add new lines before EOF
olegkkruglov Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/daal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ daal_module(
"@micromkl//:vml_ipp",
# TODO: Currently vml_ipp lib depends on TBB, but it shouldn't
# Remove TBB from deps once problem with vml_ipp is resolved
"@tbb//:tbb_binary",
"@tbb//:tbb",
],
}),
)
Expand Down
6 changes: 6 additions & 0 deletions cpp/daal/include/services/env_detect.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ class DAAL_EXPORT Environment : public Base

services::internal::sycl::ExecutionContextIface & getDefaultExecutionContext() { return *_executionContext; }

/**
* Enables external threading control
* \param[in] assumeExternalControl Flag to assume external threading control
*/
void modifyExternalThreadingControl(bool assumeExternalControl = true);

private:
Environment();
Environment(const Environment & e);
Expand Down
35 changes: 23 additions & 12 deletions cpp/daal/src/services/env_detect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

static daal::services::Environment::LibraryThreadingType daal_thr_set = (daal::services::Environment::LibraryThreadingType)-1;
static bool isInit = false;
static bool externalThreadingControl = false;

namespace daal
{
Expand Down Expand Up @@ -126,21 +127,22 @@ DAAL_EXPORT daal::services::Environment::Environment(const Environment & e) : da

DAAL_EXPORT void daal::services::Environment::initNumberOfThreads()
{
if (isInit) return;

/* if HT enabled - set _numThreads to physical cores num */
if (daal::internal::ServiceInst::serv_get_ht())
if (!isInit && !externalThreadingControl)
{
/* Number of cores = number of cpu packages * number of cores per cpu package */
int ncores = daal::internal::ServiceInst::serv_get_ncpus() * daal::internal::ServiceInst::serv_get_ncorespercpu();

/* Re-set number of threads if ncores is valid and different to _numThreads */
if ((ncores > 0) && (ncores < _daal_threader_get_max_threads()))
/* if HT enabled - set _numThreads to physical cores num */
if (daal::internal::ServiceInst::serv_get_ht())
{
daal::services::Environment::setNumberOfThreads(ncores);
/* Number of cores = number of cpu packages * number of cores per cpu package */
int ncores = daal::internal::ServiceInst::serv_get_ncpus() * daal::internal::ServiceInst::serv_get_ncorespercpu();

/* Re-set number of threads if ncores is valid and different to _numThreads */
if ((ncores > 0) && (ncores < _daal_threader_get_max_threads()))
{
daal::services::Environment::setNumberOfThreads(ncores);
}
}
isInit = true;
}
isInit = true;
}

DAAL_EXPORT daal::services::Environment::~Environment()
Expand All @@ -160,7 +162,6 @@ void daal::services::Environment::_cpu_detect(int enable)

DAAL_EXPORT void daal::services::Environment::setNumberOfThreads(const size_t numThreads)
{
isInit = true;
daal::setNumberOfThreads(numThreads, &_globalControl);
}

Expand Down Expand Up @@ -188,3 +189,13 @@ DAAL_EXPORT void daal::services::Environment::enableThreadPinning(const bool ena
#endif
return;
}

DAAL_EXPORT void daal::services::Environment::modifyExternalThreadingControl(bool assumeExternalControl)
{
externalThreadingControl = assumeExternalControl;
isInit = false;
if (externalThreadingControl)
{
daal::services::Environment::setNumberOfThreads(_daal_threader_get_max_threads());
}
}
49 changes: 49 additions & 0 deletions cpp/daal/src/threading/service_thread_pinner.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class thread_pinner_impl_t : public tbb::task_scheduler_observer

public:
thread_pinner_impl_t(void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *));
thread_pinner_impl_t(void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *), tbb::task_arena & task_arena);
void on_scheduler_entry(bool);
void on_scheduler_exit(bool);
void init_thread_pinner(int statusToSet, int nthreadsToSet, int max_threadsToSet, int * cpu_queueToSet);
Expand All @@ -230,6 +231,7 @@ class thread_pinner_impl_t : public tbb::task_scheduler_observer
int get_status();
bool get_pinning();
bool set_pinning(bool p);
tbb::task_arena * get_task_arena();
~thread_pinner_impl_t();
} * IMPL;

Expand All @@ -245,6 +247,18 @@ thread_pinner_impl_t::thread_pinner_impl_t(void (*read_topo)(int &, int &, int &
return;
} /* thread_pinner_impl_t() */

thread_pinner_impl_t::thread_pinner_impl_t(void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *), tbb::task_arena & task_arena)
: pinner_arena(task_arena), tbb::task_scheduler_observer(pinner_arena), topo_deleter(deleter)
{
do_pinning = (nthreads > 0) ? true : false;
is_pinning.set(0);

read_topo(status, nthreads, max_threads, &cpu_queue);
observe(true);

return;
} /* thread_pinner_impl_t() */

void thread_pinner_impl_t::on_scheduler_entry(bool) /*override*/
{
if (do_pinning == false || status < 0) return;
Expand Down Expand Up @@ -324,6 +338,11 @@ bool thread_pinner_impl_t::set_pinning(bool p)
return old_pinning;
} /* bool set_pinning(bool p) */

tbb::task_arena * thread_pinner_impl_t::get_task_arena()
{
return &(this->pinner_arena);
}

thread_pinner_impl_t::~thread_pinner_impl_t()
{
observe(false);
Expand Down Expand Up @@ -352,12 +371,37 @@ DAAL_EXPORT void * _getThreadPinner(bool create_pinner, void (*read_topo)(int &,
return NULL;
} /* thread_pinner_t* getThreadPinner() */

DAAL_EXPORT void * _getThreadPinnerFromTaskArena(bool create_pinner, void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *),
tbb::task_arena & task_arena)
{
static bool pinner_created = false;

if (create_pinner == true || pinner_created == true)
{
static daal::services::internal::thread_pinner_t thread_pinner(read_topo, deleter, task_arena);
if (thread_pinner.get_status() == 0)
{
pinner_created = true;
return (void *)&thread_pinner;
}
}

return NULL;
} /* thread_pinner_t* getThreadPinner() */

DAAL_EXPORT void _thread_pinner_thread_pinner_init(void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *))
{
static thread_pinner_impl_t impl(read_topo, deleter);
IMPL = &impl;
}

DAAL_EXPORT void _thread_pinner_init_with_task_arena(void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *),
tbb::task_arena & task_arena)
{
static thread_pinner_impl_t impl(read_topo, deleter, task_arena);
IMPL = &impl;
}

DAAL_EXPORT void _thread_pinner_execute(daal::services::internal::thread_pinner_task_t & task)
{
IMPL->execute(task);
Expand Down Expand Up @@ -388,6 +432,11 @@ DAAL_EXPORT void _thread_pinner_on_scheduler_exit(bool p)
IMPL->on_scheduler_exit(p);
}

DAAL_EXPORT tbb::task_arena * _thread_pinner_get_task_arena()
{
return IMPL->get_task_arena();
}

#else /* if __DO_TBB_LAYER__ is not defined */

DAAL_EXPORT void * _getThreadPinner(bool create_pinner, void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *))
Expand Down
19 changes: 19 additions & 0 deletions cpp/daal/src/threading/service_thread_pinner.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define __SERVICE_THREAD_PINNER_H__

#include "services/daal_defines.h"
#include "tbb/tbb.h"
#if !defined(DAAL_THREAD_PINNING_DISABLED)

#include <cstdlib>
Expand Down Expand Up @@ -89,6 +90,7 @@ class thread_pinner_task_t
extern "C"
{
DAAL_EXPORT void _thread_pinner_thread_pinner_init(void(int &, int &, int &, int **), void (*deleter)(void *));
DAAL_EXPORT void _thread_pinner_init_with_task_arena(void(int &, int &, int &, int **), void (*deleter)(void *), tbb::task_arena & task_arena);
DAAL_EXPORT void _thread_pinner_read_topology();
DAAL_EXPORT void _thread_pinner_on_scheduler_entry(bool);
DAAL_EXPORT void _thread_pinner_on_scheduler_exit(bool);
Expand All @@ -98,7 +100,11 @@ extern "C"
DAAL_EXPORT bool _thread_pinner_get_pinning();
DAAL_EXPORT bool _thread_pinner_set_pinning(bool p);

DAAL_EXPORT tbb::task_arena * _thread_pinner_get_task_arena();

DAAL_EXPORT void * _getThreadPinner(bool create_pinner, void(int &, int &, int &, int **), void (*deleter)(void *));
DAAL_EXPORT void * _getThreadPinnerFromTaskArena(bool create_pinner, void(int &, int &, int &, int **), void (*deleter)(void *),
tbb::task_arena & task_arena);
}

namespace daal
Expand All @@ -111,20 +117,33 @@ class thread_pinner_t
{
public:
thread_pinner_t(void (*f)(int &, int &, int &, int **), void (*deleter)(void *)) { _thread_pinner_thread_pinner_init(f, deleter); }
thread_pinner_t(void (*f)(int &, int &, int &, int **), void (*deleter)(void *), tbb::task_arena & task_arena)
{
_thread_pinner_init_with_task_arena(f, deleter, task_arena);
}
void read_topology() { _thread_pinner_read_topology(); }
void on_scheduler_entry(bool p) { _thread_pinner_on_scheduler_entry(p); }
void on_scheduler_exit(bool p) { _thread_pinner_on_scheduler_exit(p); }
void execute(thread_pinner_task_t & task) { _thread_pinner_execute(task); }
int get_status() { return _thread_pinner_get_status(); }
bool get_pinning() { return _thread_pinner_get_pinning(); }
bool set_pinning(bool p) { return _thread_pinner_set_pinning(p); }
tbb::task_arena * get_task_arena() { return _thread_pinner_get_task_arena(); }
};

inline thread_pinner_t * getThreadPinner(bool create_pinner, void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *))
{
return (thread_pinner_t *)_getThreadPinner(create_pinner, read_topo, deleter);
}

inline thread_pinner_t * getThreadPinnerFromTaskArena(bool create_pinner, void (*read_topo)(int &, int &, int &, int **), void (*deleter)(void *),
tbb::task_arena & task_arena)
{
return (thread_pinner_t *)_getThreadPinnerFromTaskArena(create_pinner, read_topo, deleter, task_arena);
}

class thread_pinner_impl_t;

} // namespace internal
} // namespace services
} // namespace daal
Expand Down
1 change: 1 addition & 0 deletions cpp/oneapi/dal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dal_module(
"@onedal//cpp/oneapi:include_root",
"@onedal//cpp/daal:services",
"@onedal//cpp/daal:data_management",
"@onedal//cpp/daal:threading_tbb",
],
dpc_deps = [
"@micromkl_dpc//:mkl_dpc",
Expand Down
4 changes: 4 additions & 0 deletions cpp/oneapi/dal/backend/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ struct global_context_cpu_init {
};

void context_cpu::global_init() {
using daal::services::Environment;

[[maybe_unused]] static volatile global_context_cpu_init init;

Environment::getInstance()->modifyExternalThreadingControl();
}

inline constexpr detail::cpu_extension from_daal_cpu_type(daal::CpuType cpu) {
Expand Down
Loading