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

[draft] New host task #4

Closed
wants to merge 5 commits into from
Closed
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
11 changes: 10 additions & 1 deletion sycl/include/sycl/detail/cg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,16 @@ class CGHostTask : public CG {
std::move(SharedPtrStorage), std::move(Requirements),
std::move(Events), std::move(loc)),
MHostTask(std::move(HostTask)), MQueue(Queue), MContext(Context),
MArgs(std::move(Args)) {}
MArgs(std::move(Args)) {
if (MHostTask) {
std::cout << "CGHostTask(...) with HostTask has property: " << std::flush;
std::cout << MHostTask->hasProperty<
sycl::property::host_task::manual_interop_sync>()
<< std::endl;
std::cout << "MHostTask->has_native_events(): "
<< MHostTask->hasNativeEvents() << std::endl;
}
}
};

class CGBarrier : public CG {
Expand Down
68 changes: 63 additions & 5 deletions sycl/include/sycl/detail/cg_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,74 @@ class InteropTask {
class HostTask {
std::function<void()> MHostTask;
std::function<void(interop_handle)> MInteropTask;
std::shared_ptr<property_list> MPropertyList;

public:
HostTask() : MHostTask([]() {}) {}
HostTask(std::function<void()> &&Func) : MHostTask(Func) {}
HostTask(std::function<void(interop_handle)> &&Func) : MInteropTask(Func) {}
HostTask() : MHostTask([]() {}) { std::cout << "HostTask()\n"; }
HostTask(std::function<void()> &&Func, std::shared_ptr<property_list>(PL))
: MHostTask(Func),
MPropertyList(PL) {
std::cout << "HostTask(Func(), PL)\n";
}
HostTask(std::function<void(interop_handle)> &&Func,
std::shared_ptr<property_list>(PL))
: MInteropTask(Func), MPropertyList(PL) {
std::cout << "HostTask(func(ih), PL)\n";
}
HostTask(std::shared_ptr<property_list>(PL))
: MPropertyList(PL) {
std::cout << "HostTask(PL)\n";
}

template <typename T>
bool hasProperty() {
if (MPropertyList) {
return MPropertyList->has_property<T>();
}
return false;
}

bool isInteropTask() const { return !!MInteropTask; }
virtual bool isInteropTask() const {
return !!MInteropTask; }

void call() { MHostTask(); }
void call(interop_handle handle) { MInteropTask(handle); }
virtual void call(interop_handle handle) { MInteropTask(handle); }
virtual bool hasNativeEvents() const { return false; }

friend class sycl::handler;
friend class DispatchHostTask;
friend class CGHostTask;
};

template <backend Backend> class NativeEventsHostTask : public HostTask {
std::function<backend_return_t<Backend, event>(interop_handle)> MHostTask;
backend_return_t<Backend, event> MNativeEvents;

public:
NativeEventsHostTask() = delete;
NativeEventsHostTask(
std::function<backend_return_t<Backend, event>(interop_handle)> &&Func,
std::shared_ptr<property_list> PL)
: MHostTask(Func), HostTask(PL) {
std::cout << "NativeEventsHostTask(std::function<backend_return_"
"t<Backend, event>(interop_handle)> &&Func)"
<< std::endl;
std::cout << "PL.has_property: "
<< hasProperty<property::host_task::manual_interop_sync>()
<< std::endl;
};

void call(interop_handle handle) override {
MNativeEvents = MHostTask(handle);
std::cout << "Got native events from kernel: " << MNativeEvents.size()
<< std::endl;
}
bool hasNativeEvents() const override { return true; }
backend_return_t<Backend, event> getNativeEvents() const {
return MNativeEvents;
}
bool isInteropTask() const override { return true; }
backend getBackend() const { return Backend; };
};

// Class which stores specific lambda object.
Expand Down
4 changes: 3 additions & 1 deletion sycl/include/sycl/detail/property_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ enum DataLessPropKind {
UseDefaultStream = 8,
DiscardEvents = 9,
DeviceReadOnly = 10,
HostTaskExecOnSubmit = 11,
HostTaskManualInteropSync = 12,
// Indicates the last known dataless property.
LastKnownDataLessPropKind = 10,
LastKnownDataLessPropKind = 12,
// Exceeding 32 may cause ABI breaking change on some of OSes.
DataLessPropKindSize = 32
};
Expand Down
1 change: 1 addition & 0 deletions sycl/include/sycl/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class __SYCL_EXPORT event {
template <backend BackendName, class SyclObjectT>
friend auto get_native(const SyclObjectT &Obj)
-> backend_return_t<BackendName, SyclObjectT>;
friend class interop_handle;
};

} // __SYCL_INLINE_VER_NAMESPACE(_V1)
Expand Down
36 changes: 31 additions & 5 deletions sycl/include/sycl/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,13 +1289,32 @@ class __SYCL_EXPORT handler {
void()>::value ||
detail::check_fn_signature<detail::remove_reference_t<FuncT>,
void(interop_handle)>::value>
host_task_impl(FuncT &&Func) {
host_task_impl(FuncT &&Func, const property_list &PropList) {
throwIfActionIsCreated();

MNDRDesc.set(range<1>(1));
MArgs = std::move(MAssociatedAccesors);

MHostTask.reset(new detail::HostTask(std::move(Func)));
MHostTask.reset(new detail::HostTask(
std::move(Func), std::make_shared<property_list>(PropList)));

setType(detail::CG::CodeplayHostTask);
}

template <typename FuncT, backend Backend = backend::opencl>
detail::enable_if_t<detail::check_fn_signature<
detail::remove_reference_t<FuncT>,
backend_return_t<Backend, event>(interop_handle)>::value>
host_task_impl(FuncT &&Func, const property_list &PropList) {
throwIfActionIsCreated();

std::cout << "Calling this special one!\n";

MNDRDesc.set(range<1>(1));
MArgs = std::move(MAssociatedAccesors);

MHostTask.reset(new detail::NativeEventsHostTask<Backend>(
std::move(Func), std::make_shared<property_list>(PropList)));

setType(detail::CG::CodeplayHostTask);
}
Expand Down Expand Up @@ -1495,16 +1514,23 @@ class __SYCL_EXPORT handler {
}

/// Enqueues a command to the SYCL runtime to invoke \p Func once.
template <typename FuncT>
template <typename FuncT, backend Backend = backend::opencl>
detail::enable_if_t<
detail::check_fn_signature<detail::remove_reference_t<FuncT>,
void()>::value ||
detail::check_fn_signature<detail::remove_reference_t<FuncT>,
void(interop_handle)>::value>
host_task(FuncT &&Func) {
host_task_impl(Func);
host_task(FuncT &&Func, const property_list &PropList = {}) {
host_task_impl(Func, PropList);
}

template <typename FuncT, backend Backend = backend::opencl>
detail::enable_if_t<detail::check_fn_signature<
detail::remove_reference_t<FuncT>,
backend_return_t<Backend, event>(interop_handle)>::value>
host_task(FuncT &&Func, const property_list &PropList = {}) {
host_task_impl(Func, PropList);
}
// replace _KERNELFUNCPARAM(KernelFunc) with KernelType KernelFunc
// or const KernelType &KernelFunc
#ifdef __SYCL_NONCONST_FUNCTOR__
Expand Down
60 changes: 58 additions & 2 deletions sycl/include/sycl/interop_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <sycl/detail/common.hpp>
#include <sycl/detail/defines.hpp>
#include <sycl/detail/pi.hpp>
#include <sycl/properties/host_task_properties.hpp>

#include <memory>

Expand All @@ -27,6 +28,13 @@ class DispatchHostTask;
class queue_impl;
class device_impl;
class context_impl;

using EventImplPtr = std::shared_ptr<event_impl>;

extern bool hasNativeInteropEvents(EventImplPtr);
template <backend Backend>
extern backend_return_t<Backend, event> getNativeInteropEvents(EventImplPtr);
extern void waitOnNativeInteropEvents(EventImplPtr);
} // namespace detail

class queue;
Expand Down Expand Up @@ -142,6 +150,49 @@ class interop_handle {
#endif
}

template <backend Backend = backend::opencl>
backend_return_t<Backend, event> get_native_events() {
#ifndef __SYCL_DEVICE_ONLY__
if (!MPropertyList
->has_property<property::host_task::manual_interop_sync>()) {
throw sycl::exception(make_error_code(errc::feature_not_supported),
"get_native_events can only be used in host task "
"with manual_interop_sync property");
}
if (Backend != backend::opencl) {
throw sycl::exception(make_error_code(errc::feature_not_supported),
"Get native events is only supported in openCL ");
}
std::cout << "ih.get_native_events(): MEventImplPtrs.size() = "
<< MDepEventImplPtrs.size() << std::endl;
backend_return_t<Backend, event> native_events;
for (auto &eventImplPtr : MDepEventImplPtrs) {
if (eventImplPtr) {
auto eventImplPtrNativeEvents =
get_native<Backend, event>(event(eventImplPtr));
native_events.insert(native_events.end(),
eventImplPtrNativeEvents.begin(),
eventImplPtrNativeEvents.end());

// If previously enqueued host_tasks have returned native events,
// add them to the vector of events
if (hasNativeInteropEvents(eventImplPtr)) {
std::cout << "In here!!12345\n";
waitOnNativeInteropEvents(eventImplPtr);
auto nativeInteropEvents =
detail::getNativeInteropEvents<Backend>(eventImplPtr);
native_events.insert(native_events.end(), nativeInteropEvents.begin(),
nativeInteropEvents.end());
}
}
}
std::cout << "native_events.size(): " << native_events.size() << std::endl;
return native_events;
#endif
throw sycl::exception(make_error_code(errc::feature_not_supported),
"get_native_events should never be called on device");
}

private:
friend class detail::ExecCGCommand;
friend class detail::DispatchHostTask;
Expand All @@ -150,9 +201,12 @@ class interop_handle {
interop_handle(std::vector<ReqToMem> MemObjs,
const std::shared_ptr<detail::queue_impl> &Queue,
const std::shared_ptr<detail::device_impl> &Device,
const std::shared_ptr<detail::context_impl> &Context)
const std::shared_ptr<detail::context_impl> &Context,
std::vector<detail::EventImplPtr> EventImplPtrs,
std::shared_ptr<property_list> PropList = {})
: MQueue(Queue), MDevice(Device), MContext(Context),
MMemObjs(std::move(MemObjs)) {}
MPropertyList(PropList), MMemObjs(std::move(MemObjs)),
MDepEventImplPtrs(EventImplPtrs) {}

template <backend Backend, typename DataT, int Dims>
backend_return_t<Backend, buffer<DataT, Dims>>
Expand All @@ -171,8 +225,10 @@ class interop_handle {
std::shared_ptr<detail::queue_impl> MQueue;
std::shared_ptr<detail::device_impl> MDevice;
std::shared_ptr<detail::context_impl> MContext;
std::shared_ptr<property_list> MPropertyList;

std::vector<ReqToMem> MMemObjs;
std::vector<detail::EventImplPtr> MDepEventImplPtrs;
};

} // __SYCL_INLINE_VER_NAMESPACE(_V1)
Expand Down
1 change: 1 addition & 0 deletions sycl/include/sycl/properties/all_properties.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <sycl/properties/accessor_properties.hpp>
#include <sycl/properties/buffer_properties.hpp>
#include <sycl/properties/context_properties.hpp>
#include <sycl/properties/host_task_properties.hpp>
#include <sycl/properties/image_properties.hpp>
#include <sycl/properties/queue_properties.hpp>
#include <sycl/properties/reduction_properties.hpp>
46 changes: 46 additions & 0 deletions sycl/include/sycl/properties/host_task_properties.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//==----------- buffer_properties.hpp --- SYCL buffer properties -----------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#pragma once

#include <sycl/context.hpp>
#include <sycl/detail/property_helper.hpp>
#include <sycl/properties/property_traits.hpp>

namespace sycl {
__SYCL_INLINE_VER_NAMESPACE(_V1) {

namespace property {
namespace host_task {
class exec_on_submit
: public detail::DataLessProperty<detail::HostTaskExecOnSubmit> {};

class manual_interop_sync
: public detail::DataLessProperty<detail::HostTaskManualInteropSync> {};

} // namespace host_task
} // namespace property

// Forward declaration
class host_task;

template <>
struct is_property<property::host_task::exec_on_submit> : std::true_type {};
template <>
struct is_property<property::host_task::manual_interop_sync> : std::true_type {
};

template <>
struct is_property_of<property::host_task::exec_on_submit, host_task>
: std::true_type {};
template <>
struct is_property_of<property::host_task::manual_interop_sync, host_task>
: std::true_type {};

} // __SYCL_INLINE_VER_NAMESPACE(_V1)
} // namespace sycl
14 changes: 14 additions & 0 deletions sycl/source/detail/event_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,18 @@ void event_impl::cleanDepEventsThroughOneLevel() {

} // namespace detail
} // __SYCL_INLINE_VER_NAMESPACE(_V1)
__SYCL_EXPORT bool
detail::hasNativeInteropEvents(std::shared_ptr<detail::event_impl> EPtr) {
std::cout << "hasNativeInteropEvents: " << EPtr->hasNativeEvents() << std::endl;
return EPtr->hasNativeEvents();
}
__SYCL_EXPORT void
detail::waitOnNativeInteropEvents(std::shared_ptr<detail::event_impl> EPtr) {
return EPtr->waitOnNativeInteropEvents();
}

template __SYCL_EXPORT backend_return_t<backend::opencl, event>
detail::getNativeInteropEvents<backend::opencl>(
std::shared_ptr<event_impl>);

} // namespace sycl
Loading