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

[AOT] C-APIs for Taichi runtime distribution #5150

Merged
merged 8 commits into from
Jun 14, 2022
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
1 change: 1 addition & 0 deletions c_api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!taichi.json
10 changes: 10 additions & 0 deletions c_api/include/taichi/taichi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "taichi/taichi_platform.h"

#include "taichi/taichi_core.h"

#if TI_WITH_VULKAN
#define VK_NO_PROTOTYPES 1
#include "taichi/taichi_vulkan.h"
#endif // TI_WITH_VULKAN
172 changes: 172 additions & 0 deletions c_api/include/taichi/taichi_core.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#pragma once
#include <taichi/taichi_platform.h>

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

// alias.bool
typedef uint32_t TiBool;

// definition.false
#define FALSE 0

// definition.true
#define TRUE 1

// alias.flags
typedef uint32_t TiFlags;

// definition.null_handle
#define NULL_HANDLE 0

// handle.runtime
typedef struct TiRuntime_t *TiRuntime;

// handle.aot_module
typedef struct TiAotModule_t *TiAotModule;

// handle.memory
typedef struct TiMemory_t *TiMemory;

// handle.kernel
typedef struct TiKernel_t *TiKernel;

// handle.compute_graph
typedef struct TiComputeGraph_t *TiComputeGraph;

// enumeration.arch
typedef enum TiArch {
TI_ARCH_X64 = 0,
TI_ARCH_ARM64 = 1,
TI_ARCH_JS = 2,
TI_ARCH_CC = 3,
TI_ARCH_WASM = 4,
TI_ARCH_CUDA = 5,
TI_ARCH_METAL = 6,
TI_ARCH_OPENGL = 7,
TI_ARCH_DX11 = 8,
TI_ARCH_OPENCL = 9,
TI_ARCH_AMDGPU = 10,
TI_ARCH_VULKAN = 11,
TI_ARCH_MAX_ENUM = 0xffffffff,
} TiArch;

// enumeration.argument_type
typedef enum TiArgumentType {
TI_ARGUMENT_TYPE_I32 = 0,
TI_ARGUMENT_TYPE_F32 = 1,
TI_ARGUMENT_TYPE_NDARRAY = 2,
TI_ARGUMENT_TYPE_MAX_ENUM = 0xffffffff,
} TiArgumentType;

// bit_field.memory_usage
typedef enum TiMemoryUsageFlagBits {
TI_MEMORY_USAGE_STORAGE_BIT = 0,
TI_MEMORY_USAGE_UNIFORM_BIT = 1,
TI_MEMORY_USAGE_VERTEX_BIT = 2,
TI_MEMORY_USAGE_INDEX_BIT = 3,
} TiMemoryUsageFlagBits;
typedef TiFlags TiMemoryUsageFlags;

// structure.memory_allocate_info
typedef struct TiMemoryAllocateInfo {
uint64_t size;
bool host_write;
bool host_read;
bool export_sharing;
TiMemoryUsageFlagBits usage;
} TiMemoryAllocateInfo;

// structure.nd_shape
typedef struct TiNdShape {
uint32_t dim_count;
uint32_t dims[16];
} TiNdShape;

// structure.nd_array
typedef struct TiNdArray {
TiMemory memory;
TiNdShape shape;
TiNdShape elem_shape;
} TiNdArray;

// union.argument_value
typedef union TiArgumentValue {
int32_t i32;
float f32;
TiNdArray ndarray;
} TiArgumentValue;

// structure.argument
typedef struct TiArgument {
TiArgumentType type;
TiArgumentValue value;
} TiArgument;

// structure.named_argument
typedef struct TiNamedArgument {
const char *name;
TiArgument argument;
} TiNamedArgument;

// function.create_runtime
TI_DLL_EXPORT TiRuntime TI_API_CALL ti_create_runtime(TiArch arch);

// function.destroy_runtime
TI_DLL_EXPORT void TI_API_CALL ti_destroy_runtime(TiRuntime runtime);

// function.allocate_memory
TI_DLL_EXPORT TiMemory TI_API_CALL
ti_allocate_memory(TiRuntime runtime,
const TiMemoryAllocateInfo *allocate_info);

// function.free_memory
TI_DLL_EXPORT void TI_API_CALL ti_free_memory(TiRuntime runtime,
TiMemory memory);

// function.map_memory
TI_DLL_EXPORT void *TI_API_CALL ti_map_memory(TiRuntime runtime,
TiMemory memory);

// function.unmap_memory
TI_DLL_EXPORT void TI_API_CALL ti_unmap_memory(TiRuntime runtime,
TiMemory memory);

// function.launch_kernel
TI_DLL_EXPORT void TI_API_CALL ti_launch_kernel(TiRuntime runtime,
TiKernel kernel,
uint32_t arg_count,
const TiArgument *args);

// function.launch_compute_graph
TI_DLL_EXPORT void TI_API_CALL
ti_launch_compute_graph(TiRuntime runtime,
TiComputeGraph compute_graph,
uint32_t arg_count,
const TiNamedArgument *args);

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

// function.wait
TI_DLL_EXPORT void TI_API_CALL ti_wait(TiRuntime runtime);

// function.load_aot_module
TI_DLL_EXPORT TiAotModule TI_API_CALL
ti_load_aot_module(TiRuntime runtime, const char *module_path);

// function.destroy_aot_module
TI_DLL_EXPORT void TI_API_CALL ti_destroy_aot_module(TiAotModule aot_module);

// function.get_aot_module_kernel
TI_DLL_EXPORT TiKernel TI_API_CALL
ti_get_aot_module_kernel(TiAotModule aot_module, const char *name);

// function.get_aot_module_compute_graph
TI_DLL_EXPORT TiComputeGraph TI_API_CALL
ti_get_aot_module_compute_graph(TiAotModule aot_module, const char *name);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
55 changes: 55 additions & 0 deletions c_api/include/taichi/taichi_platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

// TO KEEP THE INCLUDE DEPENDENCY CLEAN, PLEASE DO NOT INCLUDE ANY OTHER
// TAICHI HEADERS INTO THIS ONE.
//
// TODO(#2196): Once we can slim down "taichi/common/core.h", consider moving
// the contents back to core.h and delete this file.
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

// https://gcc.gnu.org/wiki/Visibility
#if defined _WIN32 || defined _WIN64 || defined __CYGWIN__
#ifdef __GNUC__
#define TI_DLL_EXPORT __attribute__((dllexport))
#define TI_API_CALL
#else
#define TI_DLL_EXPORT __declspec(dllexport)
#define TI_API_CALL __stdcall
#endif // __GNUC__
#else
#define TI_DLL_EXPORT __attribute__((visibility("default")))
#define TI_API_CALL
#endif // defined _WIN32 || defined _WIN64 || defined __CYGWIN__

// Windows
#if defined(_WIN64)
#define TI_PLATFORM_WINDOWS
#endif

#if defined(_WIN32) && !defined(_WIN64)
static_assert(false, "32-bit Windows systems are not supported")
#endif

// Linux
#if defined(__linux__)
#if defined(ANDROID)
#define TI_PLATFORM_ANDROID
#else
#define TI_PLATFORM_LINUX
#endif
#endif

// OSX
#if defined(__APPLE__)
#define TI_PLATFORM_OSX
#endif

#if (defined(TI_PLATFORM_LINUX) || defined(TI_PLATFORM_OSX) || \
defined(__unix__))
#define TI_PLATFORM_UNIX
#endif

#include <stddef.h>
#include <stdint.h>
58 changes: 58 additions & 0 deletions c_api/include/taichi/taichi_vulkan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once
#include <taichi/taichi_core.h>
#include <vulkan/vulkan.h>

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

// structure.vulkan_runtime_interop_info
typedef struct TiVulkanRuntimeInteropInfo {
uint32_t api_version;
VkInstance instance;
VkPhysicalDevice physical_device;
VkDevice device;
VkQueue compute_queue;
uint32_t compute_queue_family_index;
VkQueue graphics_queue;
uint32_t graphics_queue_family_index;
} TiVulkanRuntimeInteropInfo;

// structure.vulkan_memory_interop_info
typedef struct TiVulkanMemoryInteropInfo {
VkBuffer buffer;
size_t size;
VkBufferUsageFlags usage;
} TiVulkanMemoryInteropInfo;

// function.create_vulkan_runtime
TI_DLL_EXPORT TiRuntime TI_API_CALL
ti_create_vulkan_runtime_ext(uint32_t api_version,
uint32_t instance_extension_count,
const char **instance_extensions,
uint32_t device_extension_count,
const char **device_extensions);

// function.import_vulkan_runtime
TI_DLL_EXPORT TiRuntime TI_API_CALL
ti_import_vulkan_runtime(const TiVulkanRuntimeInteropInfo *interop_info);

// function.export_vulkan_runtime
TI_DLL_EXPORT void TI_API_CALL
ti_export_vulkan_runtime(TiRuntime runtime,
TiVulkanRuntimeInteropInfo *interop_info);

// function.import_vulkan_memory
TI_DLL_EXPORT TiMemory TI_API_CALL
ti_import_vulkan_memory(TiRuntime runtime,
const TiVulkanMemoryInteropInfo *interop_info);

// function.export_vulkan_memory
TI_DLL_EXPORT void TI_API_CALL
ti_export_vulkan_memory(TiRuntime runtime,
TiMemory memory,
TiVulkanMemoryInteropInfo *interop_info);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Loading