Skip to content

Commit

Permalink
Merge branch 'master' of github.com:taichi-dev/taichi into llvm_aot_c…
Browse files Browse the repository at this point in the history
…api_8
  • Loading branch information
jim19930609 committed Jul 19, 2022
2 parents 69315ad + 2e1675e commit fef07b7
Show file tree
Hide file tree
Showing 21 changed files with 416 additions and 135 deletions.
16 changes: 16 additions & 0 deletions c_api/include/taichi/taichi_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ typedef struct TiRuntime_t *TiRuntime;
// handle.aot_module
typedef struct TiAotModule_t *TiAotModule;

// handle.event
typedef struct TiEvent_t *TiEvent;

// handle.memory
typedef struct TiMemory_t *TiMemory;

Expand Down Expand Up @@ -160,6 +163,12 @@ TI_DLL_EXPORT void *TI_API_CALL ti_map_memory(TiRuntime runtime,
TI_DLL_EXPORT void TI_API_CALL ti_unmap_memory(TiRuntime runtime,
TiMemory memory);

// function.create_event
TI_DLL_EXPORT TiEvent TI_API_CALL ti_create_event(TiRuntime runtime);

// function.destroy_event
TI_DLL_EXPORT void TI_API_CALL ti_destroy_event(TiEvent event);

// function.copy_memory_device_to_device
TI_DLL_EXPORT void TI_API_CALL
ti_copy_memory_device_to_device(TiRuntime runtime,
Expand All @@ -179,6 +188,13 @@ ti_launch_compute_graph(TiRuntime runtime,
uint32_t arg_count,
const TiNamedArgument *args);

// function.signal_event
TI_DLL_EXPORT void TI_API_CALL ti_signal_event(TiRuntime runtime,
TiEvent event);

// function.reset_event
TI_DLL_EXPORT void TI_API_CALL ti_reset_event(TiRuntime runtime, TiEvent event);

// function.submit
TI_DLL_EXPORT void TI_API_CALL ti_submit(TiRuntime runtime);

Expand Down
16 changes: 16 additions & 0 deletions c_api/include/taichi/taichi_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ typedef struct TiVulkanMemoryInteropInfo {
VkBufferUsageFlags usage;
} TiVulkanMemoryInteropInfo;

// structure.vulkan_event_interop_info
typedef struct TiVulkanEventInteropInfo {
VkEvent event;
} TiVulkanEventInteropInfo;

// function.create_vulkan_runtime
TI_DLL_EXPORT TiRuntime TI_API_CALL
ti_create_vulkan_runtime_ext(uint32_t api_version,
Expand Down Expand Up @@ -53,6 +58,17 @@ ti_export_vulkan_memory(TiRuntime runtime,
TiMemory memory,
TiVulkanMemoryInteropInfo *interop_info);

// function.import_vulkan_event
TI_DLL_EXPORT TiEvent TI_API_CALL
ti_import_vulkan_event(TiRuntime runtime,
const TiVulkanEventInteropInfo *interop_info);

// function.export_vulkan_event
TI_DLL_EXPORT void TI_API_CALL
ti_export_vulkan_event(TiRuntime runtime,
TiEvent event,
TiVulkanEventInteropInfo *interop_info);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
42 changes: 41 additions & 1 deletion c_api/src/taichi_core_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void Runtime::deallocate_memory(TiMemory devmem) {
}

AotModule::AotModule(Runtime &runtime,
std::unique_ptr<taichi::lang::aot::Module> &&aot_module)
std::unique_ptr<taichi::lang::aot::Module> aot_module)
: runtime_(&runtime), aot_module_(std::move(aot_module)) {
}

Expand All @@ -50,6 +50,17 @@ Runtime &AotModule::runtime() {
return *runtime_;
}

Event::Event(Runtime &runtime, std::unique_ptr<taichi::lang::DeviceEvent> event)
: runtime_(&runtime), event_(std::move(event)) {
}

taichi::lang::DeviceEvent &Event::get() {
return *event_;
}
Runtime &Event::runtime() {
return *runtime_;
}

// -----------------------------------------------------------------------------

TiRuntime ti_create_runtime(TiArch arch) {
Expand Down Expand Up @@ -160,6 +171,22 @@ void ti_unmap_memory(TiRuntime runtime, TiMemory devmem) {
runtime2->get().unmap(devmem2devalloc(*runtime2, devmem));
}

TiEvent ti_create_event(TiRuntime runtime) {
Runtime *runtime2 = (Runtime *)runtime;
std::unique_ptr<taichi::lang::DeviceEvent> event =
runtime2->get().create_event();
Event *event2 = new Event(*runtime2, std::move(event));
return (TiEvent)event2;
}
void ti_destroy_event(TiEvent event) {
if (event == nullptr) {
TI_WARN("ignored attempt to destroy event of null handle");
return;
}

delete (Event *)event;
}

void ti_copy_memory_device_to_device(TiRuntime runtime,
const TiMemorySlice *dst_memory,
const TiMemorySlice *src_memory) {
Expand Down Expand Up @@ -407,6 +434,19 @@ void ti_launch_compute_graph(TiRuntime runtime,
}
((taichi::lang::aot::CompiledGraph *)compute_graph)->run(arg_map);
}

void ti_signal_event(TiRuntime runtime, TiEvent event) {
((Runtime *)runtime)->signal_event(&((Event *)event)->get());
}

void ti_reset_event(TiRuntime runtime, TiEvent event) {
((Runtime *)runtime)->reset_event(&((Event *)event)->get());
}

void ti_wait_event(TiRuntime runtime, TiEvent event) {
((Runtime *)runtime)->wait_event(&((Event *)event)->get());
}

void ti_submit(TiRuntime runtime) {
if (runtime == nullptr) {
TI_WARN("ignored attempt to submit to runtime of null handle");
Expand Down
22 changes: 21 additions & 1 deletion c_api/src/taichi_core_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class Runtime {
virtual void buffer_copy(const taichi::lang::DevicePtr &dst,
const taichi::lang::DevicePtr &src,
size_t size) = 0;
virtual void signal_event(taichi::lang::DeviceEvent *event) {
TI_NOT_IMPLEMENTED
}
virtual void reset_event(taichi::lang::DeviceEvent *event) {
TI_NOT_IMPLEMENTED
}
virtual void wait_event(taichi::lang::DeviceEvent *event) {
TI_NOT_IMPLEMENTED
}
virtual void submit() = 0;
virtual void wait() = 0;

Expand All @@ -49,13 +58,24 @@ class AotModule {

public:
AotModule(Runtime &runtime,
std::unique_ptr<taichi::lang::aot::Module> &&aot_module);
std::unique_ptr<taichi::lang::aot::Module> aot_module);

taichi::lang::aot::CompiledGraph &get_cgraph(const std::string &name);
taichi::lang::aot::Module &get();
Runtime &runtime();
};

class Event {
Runtime *runtime_;
std::unique_ptr<taichi::lang::DeviceEvent> event_;

public:
Event(Runtime &runtime, std::unique_ptr<taichi::lang::DeviceEvent> event);

taichi::lang::DeviceEvent &get();
Runtime &runtime();
};

namespace {

[[maybe_unused]] taichi::lang::DeviceAllocation devmem2devalloc(
Expand Down
48 changes: 48 additions & 0 deletions c_api/src/taichi_vulkan_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ void VulkanRuntime::buffer_copy(const taichi::lang::DevicePtr &dst,
void VulkanRuntime::submit() {
get_gfx_runtime().flush();
}
void VulkanRuntime::signal_event(taichi::lang::DeviceEvent *event) {
get_gfx_runtime().signal_event(event);
}
void VulkanRuntime::reset_event(taichi::lang::DeviceEvent *event) {
get_gfx_runtime().reset_event(event);
}
void VulkanRuntime::wait_event(taichi::lang::DeviceEvent *event) {
get_gfx_runtime().wait_event(event);
}
void VulkanRuntime::wait() {
// (penguinliong) It's currently waiting for the entire runtime to stop.
// Should be simply waiting for its fence to finish.
Expand Down Expand Up @@ -252,4 +261,43 @@ void ti_export_vulkan_memory(TiRuntime runtime,
interop_info->usage = buffer.get()->usage;
}

TiEvent ti_import_vulkan_event(TiRuntime runtime,
const TiVulkanEventInteropInfo *interop_info) {
if (runtime == nullptr) {
TI_WARN("ignored attempt to import vulkan event to runtime of null handle");
return TI_NULL_HANDLE;
}
Runtime *runtime2 = (Runtime *)runtime;
if (runtime2->arch != taichi::Arch::vulkan) {
TI_WARN("ignored attempt to import vulkan memory to non-vulkan runtime");
return TI_NULL_HANDLE;
}

vkapi::IVkEvent event = std::make_unique<vkapi::DeviceObjVkEvent>();
event->device = runtime2->as_vk()->get_vk().vk_device();
event->event = interop_info->event;
event->external = true;

std::unique_ptr<taichi::lang::DeviceEvent> event2(
new taichi::lang::vulkan::VulkanDeviceEvent(std::move(event)));

return (TiEvent) new Event(*runtime2, std::move(event2));
}
void ti_export_vulkan_event(TiRuntime runtime,
TiEvent event,
TiVulkanEventInteropInfo *interop_info) {
if (runtime == nullptr) {
TI_WARN(
"ignored attempt to export vulkan memory from runtime of null handle");
return;
}
if (event == nullptr) {
TI_WARN("ignored attempt to export vulkan memory of null handle");
return;
}
auto event2 =
(taichi::lang::vulkan::VulkanDeviceEvent *)(&((Event *)event)->get());
interop_info->event = event2->vkapi_ref->event;
}

#endif // TI_WITH_VULKAN
3 changes: 3 additions & 0 deletions c_api/src/taichi_vulkan_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class VulkanRuntime : public Runtime {
virtual void buffer_copy(const taichi::lang::DevicePtr &dst,
const taichi::lang::DevicePtr &src,
size_t size) override final;
virtual void signal_event(taichi::lang::DeviceEvent *event) override final;
virtual void reset_event(taichi::lang::DeviceEvent *event) override final;
virtual void wait_event(taichi::lang::DeviceEvent *event) override final;
virtual void submit() override final;
virtual void wait() override final;
};
Expand Down
Loading

0 comments on commit fef07b7

Please sign in to comment.