Skip to content

[SYCL] Add support of multiple devices within a context #2343

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

Merged
merged 9 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions sycl/source/detail/context_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ class context_impl {
/// more details.
///
/// \returns a map with device library programs.
std::map<DeviceLibExt, RT::PiProgram> &getCachedLibPrograms() {
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram> &
getCachedLibPrograms() {
return MCachedLibPrograms;
}

Expand All @@ -155,7 +156,8 @@ class context_impl {
PlatformImplPtr MPlatform;
bool MHostContext;
bool MUseCUDAPrimaryContext;
std::map<DeviceLibExt, RT::PiProgram> MCachedLibPrograms;
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>
MCachedLibPrograms;
mutable KernelProgramCache MKernelProgramCache;
};

Expand Down
6 changes: 4 additions & 2 deletions sycl/source/detail/kernel_program_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ class KernelProgramCache {
using PiProgramT = std::remove_pointer<RT::PiProgram>::type;
using PiProgramPtrT = std::atomic<PiProgramT *>;
using ProgramWithBuildStateT = BuildResult<PiProgramT>;
using ProgramCacheKeyT = std::pair<SerializedObj, KernelSetId>;
using ProgramCacheKeyT =
std::pair<std::pair<SerializedObj, KernelSetId>, RT::PiDevice>;
using ProgramCacheT = std::map<ProgramCacheKeyT, ProgramWithBuildStateT>;
using ContextPtr = context_impl *;

using PiKernelT = std::remove_pointer<RT::PiKernel>::type;

using PiKernelPtrT = std::atomic<PiKernelT *>;
using KernelWithBuildStateT = BuildResult<PiKernelT>;
using KernelByNameT = std::map<string_class, KernelWithBuildStateT>;
using KernelByNameT =
std::map<std::pair<string_class, RT::PiDevice>, KernelWithBuildStateT>;
using KernelCacheT = std::map<RT::PiProgram, KernelByNameT>;

~KernelProgramCache();
Expand Down
34 changes: 28 additions & 6 deletions sycl/source/detail/program_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ program_impl::program_impl(ContextImplPtr Context)

program_impl::program_impl(ContextImplPtr Context,
vector_class<device> DeviceList)
: MContext(Context), MDevices(DeviceList) {}
: MContext(Context), MDevices(DeviceList) {
if (Context->getDevices().size() > 1) {
throw feature_not_supported(
"multiple devices within a context are not supported with "
"sycl::program and sycl::kernel",
PI_INVALID_OPERATION);
}
}

program_impl::program_impl(
vector_class<shared_ptr_class<program_impl>> ProgramList,
Expand All @@ -51,6 +58,12 @@ program_impl::program_impl(
}

MContext = ProgramList[0]->MContext;
if (MContext->getDevices().size() > 1) {
throw feature_not_supported(
"multiple devices within a context are not supported with "
"sycl::program and sycl::kernel",
PI_INVALID_OPERATION);
}
MDevices = ProgramList[0]->MDevices;
vector_class<device> DevicesSorted;
if (!is_host()) {
Expand Down Expand Up @@ -105,6 +118,13 @@ program_impl::program_impl(ContextImplPtr Context,
RT::PiProgram Program)
: MProgram(Program), MContext(Context), MLinkable(true) {

if (Context->getDevices().size() > 1) {
throw feature_not_supported(
"multiple devices within a context are not supported with "
"sycl::program and sycl::kernel",
PI_INVALID_OPERATION);
}

const detail::plugin &Plugin = getPlugin();
if (MProgram == nullptr) {
assert(InteropProgram &&
Expand Down Expand Up @@ -233,7 +253,7 @@ void program_impl::build_with_kernel_name(string_class KernelName,
if (is_cacheable_with_options(BuildOptions)) {
MProgramAndKernelCachingAllowed = true;
MProgram = ProgramManager::getInstance().getBuiltPIProgram(
Module, get_context(), KernelName, this,
Module, get_context(), get_devices()[0], KernelName, this,
/*JITCompilationIsRequired=*/(!BuildOptions.empty()));
const detail::plugin &Plugin = getPlugin();
Plugin.call<PiApiKind::piProgramRetain>(MProgram);
Expand Down Expand Up @@ -356,7 +376,7 @@ void program_impl::build(const string_class &Options) {
check_device_feature_support<info::device::is_compiler_available>(MDevices);
vector_class<RT::PiDevice> Devices(get_pi_devices());
const detail::plugin &Plugin = getPlugin();
ProgramManager::getInstance().flushSpecConstants(*this);
ProgramManager::getInstance().flushSpecConstants(*this, get_pi_devices()[0]);
RT::PiResult Err = Plugin.call_nocheck<PiApiKind::piProgramBuild>(
MProgram, Devices.size(), Devices.data(), Options.c_str(), nullptr,
nullptr);
Expand Down Expand Up @@ -404,7 +424,8 @@ RT::PiKernel program_impl::get_pi_kernel(const string_class &KernelName) const {
if (is_cacheable()) {
std::tie(Kernel, std::ignore) =
ProgramManager::getInstance().getOrCreateKernel(
MProgramModuleHandle, get_context(), KernelName, this);
MProgramModuleHandle, get_context(), get_devices()[0], KernelName,
this);
getPlugin().call<PiApiKind::piKernelRetain>(Kernel);
} else {
const detail::plugin &Plugin = getPlugin();
Expand Down Expand Up @@ -453,9 +474,10 @@ void program_impl::create_pi_program_with_kernel_name(
bool JITCompilationIsRequired) {
assert(!MProgram && "This program already has an encapsulated PI program");
ProgramManager &PM = ProgramManager::getInstance();
const device FirstDevice = get_devices()[0];
RTDeviceBinaryImage &Img = PM.getDeviceImage(
Module, KernelName, get_context(), JITCompilationIsRequired);
MProgram = PM.createPIProgram(Img, get_context());
Module, KernelName, get_context(), FirstDevice, JITCompilationIsRequired);
MProgram = PM.createPIProgram(Img, get_context(), FirstDevice);
}

template <>
Expand Down
Loading