diff --git a/c_api/include/taichi/taichi.h b/c_api/include/taichi/taichi.h index 38d6f1b987f45b..4f8a666e56b846 100644 --- a/c_api/include/taichi/taichi.h +++ b/c_api/include/taichi/taichi.h @@ -4,7 +4,7 @@ #include "taichi/taichi_core.h" -#if TI_WITH_VULKAN +#ifdef TI_WITH_VULKAN #ifndef TI_NO_VULKAN_INCLUDES #include #endif // TI_NO_VULKAN_INCLUDES @@ -12,7 +12,7 @@ #include "taichi/taichi_vulkan.h" #endif // TI_WITH_VULKAN -#if TI_WITH_OPENGL +#ifdef TI_WITH_OPENGL #ifndef TI_NO_OPENGL_INCLUDES #include #endif // TI_NO_OPENGL_INCLUDES diff --git a/c_api/include/taichi/taichi_core.h b/c_api/include/taichi/taichi_core.h index 4989ab8508f4a0..24e15f9162ca60 100644 --- a/c_api/include/taichi/taichi_core.h +++ b/c_api/include/taichi/taichi_core.h @@ -1,9 +1,15 @@ #pragma once -#include + +#ifndef TI_C_API_VERSION +#define TI_C_API_VERSION 1001004 +#endif // TI_C_API_VERSION + +#include #ifdef __cplusplus extern "C" { -#endif // __cplusplus +#endif // __cplusplus + // alias.bool typedef uint32_t TiBool; @@ -21,28 +27,28 @@ typedef uint32_t TiFlags; #define TI_NULL_HANDLE 0 // handle.runtime -typedef struct TiRuntime_t *TiRuntime; +typedef struct TiRuntime_t* TiRuntime; // handle.aot_module -typedef struct TiAotModule_t *TiAotModule; +typedef struct TiAotModule_t* TiAotModule; // handle.event -typedef struct TiEvent_t *TiEvent; +typedef struct TiEvent_t* TiEvent; // handle.memory -typedef struct TiMemory_t *TiMemory; +typedef struct TiMemory_t* TiMemory; // handle.image -typedef struct TiImage_t *TiImage; +typedef struct TiImage_t* TiImage; // handle.sampler -typedef struct TiSampler_t *TiSampler; +typedef struct TiSampler_t* TiSampler; // handle.kernel -typedef struct TiKernel_t *TiKernel; +typedef struct TiKernel_t* TiKernel; // handle.compute_graph -typedef struct TiComputeGraph_t *TiComputeGraph; +typedef struct TiComputeGraph_t* TiComputeGraph; // enumeration.error typedef enum TiError { @@ -311,128 +317,185 @@ typedef struct TiArgument { // structure.named_argument typedef struct TiNamedArgument { - const char *name; + const char* name; TiArgument argument; } TiNamedArgument; // function.get_last_error -TI_DLL_EXPORT TiError TI_API_CALL ti_get_last_error(uint64_t message_size, - char *message); +TI_DLL_EXPORT TiError TI_API_CALL ti_get_last_error( + uint64_t message_size, + char* message +); // function.set_last_error -TI_DLL_EXPORT void TI_API_CALL ti_set_last_error(TiError error, - const char *message); +TI_DLL_EXPORT void TI_API_CALL ti_set_last_error( + TiError error, + const char* message +); // function.create_runtime -TI_DLL_EXPORT TiRuntime TI_API_CALL ti_create_runtime(TiArch arch); +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); +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); +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); +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); +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); +TI_DLL_EXPORT void TI_API_CALL ti_unmap_memory( + TiRuntime runtime, + TiMemory memory +); // function.allocate_image -TI_DLL_EXPORT TiImage TI_API_CALL -ti_allocate_image(TiRuntime runtime, const TiImageAllocateInfo *allocate_info); +TI_DLL_EXPORT TiImage TI_API_CALL ti_allocate_image( + TiRuntime runtime, + const TiImageAllocateInfo* allocate_info +); // function.free_image -TI_DLL_EXPORT void TI_API_CALL ti_free_image(TiRuntime runtime, TiImage image); +TI_DLL_EXPORT void TI_API_CALL ti_free_image( + TiRuntime runtime, + TiImage image +); // function.create_sampler -TI_DLL_EXPORT TiSampler TI_API_CALL -ti_create_sampler(TiRuntime runtime, const TiSamplerCreateInfo *create_info); +TI_DLL_EXPORT TiSampler TI_API_CALL ti_create_sampler( + TiRuntime runtime, + const TiSamplerCreateInfo* create_info +); // function.destroy_sampler -TI_DLL_EXPORT void TI_API_CALL ti_destroy_sampler(TiRuntime runtime, - TiSampler sampler); +TI_DLL_EXPORT void TI_API_CALL ti_destroy_sampler( + TiRuntime runtime, + TiSampler sampler +); // function.create_event -TI_DLL_EXPORT TiEvent TI_API_CALL ti_create_event(TiRuntime runtime); +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); +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, - const TiMemorySlice *dst_memory, - const TiMemorySlice *src_memory); +TI_DLL_EXPORT void TI_API_CALL ti_copy_memory_device_to_device( + TiRuntime runtime, + const TiMemorySlice* dst_memory, + const TiMemorySlice* src_memory +); // function.copy_image_device_to_device -TI_DLL_EXPORT void TI_API_CALL -ti_copy_image_device_to_device(TiRuntime runtime, - const TiImageSlice *dst_image, - const TiImageSlice *src_image); +TI_DLL_EXPORT void TI_API_CALL ti_copy_image_device_to_device( + TiRuntime runtime, + const TiImageSlice* dst_image, + const TiImageSlice* src_image +); // function.track_image -TI_DLL_EXPORT void TI_API_CALL ti_track_image_ext(TiRuntime runtime, - TiImage image, - TiImageLayout layout); +TI_DLL_EXPORT void TI_API_CALL ti_track_image_ext( + TiRuntime runtime, + TiImage image, + TiImageLayout layout +); // function.transition_image -TI_DLL_EXPORT void TI_API_CALL ti_transition_image(TiRuntime runtime, - TiImage image, - TiImageLayout layout); +TI_DLL_EXPORT void TI_API_CALL ti_transition_image( + TiRuntime runtime, + TiImage image, + TiImageLayout layout +); // function.launch_kernel -TI_DLL_EXPORT void TI_API_CALL ti_launch_kernel(TiRuntime runtime, - TiKernel kernel, - uint32_t arg_count, - const TiArgument *args); +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); +TI_DLL_EXPORT void TI_API_CALL ti_launch_compute_graph( + TiRuntime runtime, + TiComputeGraph compute_graph, + uint32_t arg_count, + const TiNamedArgument* args +); // function.signal_event -TI_DLL_EXPORT void TI_API_CALL ti_signal_event(TiRuntime runtime, - TiEvent 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); +TI_DLL_EXPORT void TI_API_CALL ti_reset_event( + TiRuntime runtime, + TiEvent event +); // function.wait_event -TI_DLL_EXPORT void TI_API_CALL ti_wait_event(TiRuntime runtime, TiEvent event); +TI_DLL_EXPORT void TI_API_CALL ti_wait_event( + TiRuntime runtime, + TiEvent event +); // function.submit -TI_DLL_EXPORT void TI_API_CALL ti_submit(TiRuntime runtime); +TI_DLL_EXPORT void TI_API_CALL ti_submit( + TiRuntime runtime +); // function.wait -TI_DLL_EXPORT void TI_API_CALL ti_wait(TiRuntime runtime); +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); +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); +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); +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); +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 +} // extern "C" +#endif // __cplusplus diff --git a/c_api/include/taichi/taichi_opengl.h b/c_api/include/taichi/taichi_opengl.h index 848ed1225d4393..e5d29db1402007 100644 --- a/c_api/include/taichi/taichi_opengl.h +++ b/c_api/include/taichi/taichi_opengl.h @@ -1,9 +1,15 @@ #pragma once -#include + +#ifndef TI_WITH_OPENGL +#define TI_WITH_OPENGL 1 +#endif // TI_WITH_OPENGL + +#include #ifdef __cplusplus extern "C" { -#endif // __cplusplus +#endif // __cplusplus + // structure.opengl_memory_interop_info typedef struct TiOpenglMemoryInteropInfo { @@ -12,17 +18,19 @@ typedef struct TiOpenglMemoryInteropInfo { } TiOpenglMemoryInteropInfo; // function.import_opengl_memory -TI_DLL_EXPORT void TI_API_CALL -ti_import_opengl_memory(TiRuntime runtime, - TiMemory memory, - TiOpenglMemoryInteropInfo *interop_info); +TI_DLL_EXPORT void TI_API_CALL ti_import_opengl_memory( + TiRuntime runtime, + TiMemory memory, + TiOpenglMemoryInteropInfo* interop_info +); // function.export_opengl_memory -TI_DLL_EXPORT void TI_API_CALL -ti_export_opengl_memory(TiRuntime runtime, - TiMemory memory, - TiOpenglMemoryInteropInfo *interop_info); +TI_DLL_EXPORT void TI_API_CALL ti_export_opengl_memory( + TiRuntime runtime, + TiMemory memory, + TiOpenglMemoryInteropInfo* interop_info +); #ifdef __cplusplus -} // extern "C" -#endif // __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/c_api/include/taichi/taichi_unity.h b/c_api/include/taichi/taichi_unity.h index e82f63d8fb6e8c..99aa34a89c20cf 100644 --- a/c_api/include/taichi/taichi_unity.h +++ b/c_api/include/taichi/taichi_unity.h @@ -1,54 +1,64 @@ #pragma once -#include + +#include #ifdef __cplusplus extern "C" { -#endif // __cplusplus +#endif // __cplusplus + // handle.native_buffer -typedef struct TixNativeBufferUnity_t *TixNativeBufferUnity; +typedef struct TixNativeBufferUnity_t* TixNativeBufferUnity; // function.import_native_runtime -TI_DLL_EXPORT TiRuntime TI_API_CALL tix_import_native_runtime_unity(); +TI_DLL_EXPORT TiRuntime TI_API_CALL tix_import_native_runtime_unity( +); // function.launch_kernel_async -TI_DLL_EXPORT void TI_API_CALL -tix_launch_kernel_async_unity(TiRuntime runtime, - TiKernel kernel, - uint32_t arg_count, - const TiArgument *args); +TI_DLL_EXPORT void TI_API_CALL tix_launch_kernel_async_unity( + TiRuntime runtime, + TiKernel kernel, + uint32_t arg_count, + const TiArgument* args +); // function.launch_compute_graph_async -TI_DLL_EXPORT void TI_API_CALL -tix_launch_compute_graph_async_unity(TiRuntime runtime, - TiComputeGraph compute_graph, - uint32_t arg_count, - const TiNamedArgument *args); +TI_DLL_EXPORT void TI_API_CALL tix_launch_compute_graph_async_unity( + TiRuntime runtime, + TiComputeGraph compute_graph, + uint32_t arg_count, + const TiNamedArgument* args +); // function.copy_memory_to_native_buffer_async -TI_DLL_EXPORT void TI_API_CALL -tix_copy_memory_to_native_buffer_async_unity(TiRuntime runtime, - TixNativeBufferUnity dst, - uint64_t dst_offset, - const TiMemorySlice *src); +TI_DLL_EXPORT void TI_API_CALL tix_copy_memory_to_native_buffer_async_unity( + TiRuntime runtime, + TixNativeBufferUnity dst, + uint64_t dst_offset, + const TiMemorySlice* src +); // function.copy_memory_device_to_host -TI_DLL_EXPORT void TI_API_CALL -tix_copy_memory_device_to_host_unity(TiRuntime runtime, - void *dst, - uint64_t dst_offset, - const TiMemorySlice *src); +TI_DLL_EXPORT void TI_API_CALL tix_copy_memory_device_to_host_unity( + TiRuntime runtime, + void* dst, + uint64_t dst_offset, + const TiMemorySlice* src +); // function.copy_memory_host_to_device -TI_DLL_EXPORT void TI_API_CALL -tix_copy_memory_host_to_device_unity(TiRuntime runtime, - const TiMemorySlice *dst, - const void *src, - uint64_t src_offset); +TI_DLL_EXPORT void TI_API_CALL tix_copy_memory_host_to_device_unity( + TiRuntime runtime, + const TiMemorySlice* dst, + const void* src, + uint64_t src_offset +); // function.submit_async -TI_DLL_EXPORT void *TI_API_CALL tix_submit_async_unity(TiRuntime runtime); +TI_DLL_EXPORT void* TI_API_CALL tix_submit_async_unity( + TiRuntime runtime +); #ifdef __cplusplus -} // extern "C" -#endif // __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/c_api/include/taichi/taichi_vulkan.h b/c_api/include/taichi/taichi_vulkan.h index aa66b7efe0ab38..e5894a47e903da 100644 --- a/c_api/include/taichi/taichi_vulkan.h +++ b/c_api/include/taichi/taichi_vulkan.h @@ -1,9 +1,15 @@ #pragma once -#include + +#ifndef TI_WITH_VULKAN +#define TI_WITH_VULKAN 1 +#endif // TI_WITH_VULKAN + +#include #ifdef __cplusplus extern "C" { -#endif // __cplusplus +#endif // __cplusplus + // structure.vulkan_runtime_interop_info typedef struct TiVulkanRuntimeInteropInfo { @@ -44,57 +50,66 @@ typedef struct TiVulkanEventInteropInfo { } TiVulkanEventInteropInfo; // 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); +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); +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); +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); +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); +TI_DLL_EXPORT void TI_API_CALL ti_export_vulkan_memory( + TiRuntime runtime, + TiMemory memory, + TiVulkanMemoryInteropInfo* interop_info +); // function.import_vulkan_image -TI_DLL_EXPORT TiImage TI_API_CALL -ti_import_vulkan_image(TiRuntime runtime, - const TiVulkanImageInteropInfo *interop_info, - VkImageViewType view_type, - VkImageLayout layout); +TI_DLL_EXPORT TiImage TI_API_CALL ti_import_vulkan_image( + TiRuntime runtime, + const TiVulkanImageInteropInfo* interop_info, + VkImageViewType view_type, + VkImageLayout layout +); // function.export_vulkan_image -TI_DLL_EXPORT void TI_API_CALL -ti_export_vulkan_image(TiRuntime runtime, - TiImage image, - TiVulkanImageInteropInfo *interop_info); +TI_DLL_EXPORT void TI_API_CALL ti_export_vulkan_image( + TiRuntime runtime, + TiImage image, + TiVulkanImageInteropInfo* interop_info +); // function.import_vulkan_event -TI_DLL_EXPORT TiEvent TI_API_CALL -ti_import_vulkan_event(TiRuntime runtime, - const TiVulkanEventInteropInfo *interop_info); +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); +TI_DLL_EXPORT void TI_API_CALL ti_export_vulkan_event( + TiRuntime runtime, + TiEvent event, + TiVulkanEventInteropInfo* interop_info +); #ifdef __cplusplus -} // extern "C" -#endif // __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/c_api/src/c_api_test_utils.cpp b/c_api/src/c_api_test_utils.cpp index 19299f34d39ca7..14b142dac29b5a 100644 --- a/c_api/src/c_api_test_utils.cpp +++ b/c_api/src/c_api_test_utils.cpp @@ -1,6 +1,7 @@ #include "c_api_test_utils.h" #include "taichi_llvm_impl.h" #include "taichi/platform/cuda/detect_cuda.h" +#include "taichi/common/logging.h" #ifdef TI_WITH_VULKAN #include "taichi/rhi/vulkan/vulkan_loader.h" @@ -45,82 +46,5 @@ void check_runtime_error(TiRuntime runtime) { #endif } -TiNdarrayAndMem make_ndarray(TiRuntime runtime, - TiDataType dtype, - const int *arr_shape, - int arr_dims, - const int *element_shape, - int element_dims, - bool host_read, - bool host_write) { - size_t alloc_size = 1; - if (dtype == TiDataType::TI_DATA_TYPE_F64 || - dtype == TiDataType::TI_DATA_TYPE_I64 || - dtype == TiDataType::TI_DATA_TYPE_U64) { - alloc_size = 8; - - } else if (dtype == TiDataType::TI_DATA_TYPE_F32 || - dtype == TiDataType::TI_DATA_TYPE_I32 || - dtype == TiDataType::TI_DATA_TYPE_U32) { - alloc_size = 4; - - } else if (dtype == TI_DATA_TYPE_F16 || dtype == TI_DATA_TYPE_I16 || - dtype == TI_DATA_TYPE_U16) { - alloc_size = 2; - - } else if (dtype == TI_DATA_TYPE_I8 || dtype == TI_DATA_TYPE_U8) { - alloc_size = 1; - - } else { - TI_ASSERT(false); - } - - for (int i = 0; i < arr_dims; i++) { - alloc_size *= arr_shape[i]; - } - - for (int i = 0; i < element_dims; i++) { - alloc_size *= element_shape[i]; - } - - TiNdarrayAndMem res; - res.runtime_ = runtime; - - TiMemoryAllocateInfo alloc_info; - alloc_info.size = alloc_size; - alloc_info.host_write = host_write; - alloc_info.host_read = host_read; - alloc_info.export_sharing = false; - alloc_info.usage = TiMemoryUsageFlagBits::TI_MEMORY_USAGE_STORAGE_BIT; - - res.memory_ = ti_allocate_memory(res.runtime_, &alloc_info); - - TiNdShape shape; - shape.dim_count = (uint32_t)arr_dims; - for (size_t i = 0; i < shape.dim_count; i++) { - shape.dims[i] = arr_shape[i]; - } - - TiNdShape e_shape; - e_shape.dim_count = (uint32_t)element_dims; - for (size_t i = 0; i < e_shape.dim_count; i++) { - e_shape.dims[i] = element_shape[i]; - } - - TiNdArray arg_array{}; - arg_array.memory = res.memory_; - arg_array.shape = shape; - arg_array.elem_shape = e_shape; - arg_array.elem_type = dtype; - - TiArgumentValue arg_value{}; - arg_value.ndarray = arg_array; - - res.arg_.type = TiArgumentType::TI_ARGUMENT_TYPE_NDARRAY; - res.arg_.value = arg_value; - - return res; -} - } // namespace utils } // namespace capi diff --git a/c_api/src/c_api_test_utils.h b/c_api/src/c_api_test_utils.h index 0c13f244869901..dc19394effd6a6 100644 --- a/c_api/src/c_api_test_utils.h +++ b/c_api/src/c_api_test_utils.h @@ -1,6 +1,5 @@ #pragma once -#include -#include "taichi/taichi_core.h" +#include "taichi_core_impl.h" namespace capi { namespace utils { @@ -10,20 +9,5 @@ TI_DLL_EXPORT bool TI_API_CALL is_opengl_available(); TI_DLL_EXPORT bool TI_API_CALL is_cuda_available(); TI_DLL_EXPORT void TI_API_CALL check_runtime_error(TiRuntime runtime); -typedef struct TiNdarrayAndMem { - TiRuntime runtime_; - TiMemory memory_; - TiArgument arg_; -} TiNdarrayAndMem; - -TI_DLL_EXPORT TiNdarrayAndMem TI_API_CALL make_ndarray(TiRuntime runtime, - TiDataType dtype, - const int *arr_shape, - int arr_dims, - const int *element_shape, - int element_dims, - bool host_read = false, - bool host_write = false); - } // namespace utils } // namespace capi diff --git a/c_api/src/taichi_core_impl.cpp b/c_api/src/taichi_core_impl.cpp index e2af8c2d3f17f0..3269902084f320 100644 --- a/c_api/src/taichi_core_impl.cpp +++ b/c_api/src/taichi_core_impl.cpp @@ -3,6 +3,7 @@ #include "taichi_vulkan_impl.h" #include "taichi_llvm_impl.h" #include "taichi/program/ndarray.h" +#include "taichi/program/texture.h" struct ErrorCache { TiError error{TI_ERROR_SUCCESS}; diff --git a/c_api/src/taichi_core_impl.h b/c_api/src/taichi_core_impl.h index 5df9dfe9052281..a948753028eb41 100644 --- a/c_api/src/taichi_core_impl.h +++ b/c_api/src/taichi_core_impl.h @@ -1,16 +1,35 @@ #pragma once +#include +#include +#include #include -#include "taichi/taichi_core.h" -#include "taichi/aot/module_loader.h" -#include "taichi/rhi/device.h" -#include "taichi/program/texture.h" -#include "taichi/runtime/gfx/aot_module_loader_impl.h" +#include + +// Taichi runtime is not necessarily using the same 3rd-party headers as the +// user codes. For C-API implementations we only use the internal headers. +#ifdef TI_WITH_VULKAN +#include "taichi/rhi/vulkan/vulkan_loader.h" +#define TI_NO_VULKAN_INCLUDES 1 +#endif // TI_WITH_VULKAN + +#ifdef TI_WITH_OPENGL +#include "glad/gl.h" +#define TI_NO_OPENGL_INCLUDES 1 +#endif // TI_WITH_OPENGL +// Then Include all C-API symbols. +#include "taichi/taichi.h" + +// Include for the base types. +#include "taichi/rhi/arch.h" #define TI_RUNTIME_HOST 1 #include "taichi/program/context.h" #undef TI_RUNTIME_HOST +#include "taichi/rhi/device.h" +#include "taichi/aot/graph_data.h" +#include "taichi/aot/module_loader.h" -// Error reporting. +// Error reporting helpers. #define TI_CAPI_NOT_SUPPORTED(x) ti_set_last_error(TI_ERROR_NOT_SUPPORTED, #x); #define TI_CAPI_NOT_SUPPORTED_IF(x) \ if (x) { \ @@ -68,10 +87,6 @@ ti_set_last_error(TI_ERROR_INVALID_STATE, "c++ exception"); \ } -class Runtime; -class Context; -class AotModule; - class Runtime { protected: // 32 is a magic number in `taichi/inc/constants.h`. diff --git a/c_api/src/taichi_gfx_impl.cpp b/c_api/src/taichi_gfx_impl.cpp index 16daa7d1b00cb0..eb8651cd27e2c2 100644 --- a/c_api/src/taichi_gfx_impl.cpp +++ b/c_api/src/taichi_gfx_impl.cpp @@ -1,4 +1,7 @@ +#ifdef TI_WITH_GFX + #include "taichi_gfx_impl.h" +#include "taichi/runtime/gfx/aot_module_loader_impl.h" GfxRuntime::GfxRuntime(taichi::Arch arch) : Runtime(arch) { } @@ -54,3 +57,5 @@ void GfxRuntime::wait() { // Should be simply waiting for its fence to finish. get_gfx_runtime().synchronize(); } + +#endif // TI_WITH_GFX diff --git a/c_api/src/taichi_gfx_impl.h b/c_api/src/taichi_gfx_impl.h index d8935f203a3861..0be8a0a5c24fe4 100644 --- a/c_api/src/taichi_gfx_impl.h +++ b/c_api/src/taichi_gfx_impl.h @@ -1,4 +1,11 @@ #pragma once + +#ifdef TI_WITH_VULKAN +#define TI_WITH_GFX 1 +#endif // TI_WITH_VULKAN + +#ifdef TI_WITH_GFX + #include "taichi_core_impl.h" #include "taichi/runtime/gfx/runtime.h" @@ -30,3 +37,5 @@ class GfxRuntime : public Runtime { virtual void submit() override final; virtual void wait() override final; }; + +#endif // TI_WITH_GFX diff --git a/c_api/src/taichi_llvm_impl.cpp b/c_api/src/taichi_llvm_impl.cpp index 2918031c209a6a..03083dbf8f3838 100644 --- a/c_api/src/taichi_llvm_impl.cpp +++ b/c_api/src/taichi_llvm_impl.cpp @@ -1,3 +1,5 @@ +#ifdef TI_WITH_LLVM + #include "taichi_core_impl.h" #include "taichi_llvm_impl.h" @@ -128,3 +130,5 @@ void LlvmRuntime::wait() { } } // namespace capi + +#endif // TI_WITH_LLVM diff --git a/c_api/src/taichi_llvm_impl.h b/c_api/src/taichi_llvm_impl.h index a0c47eb360ce57..bcae14c336023d 100644 --- a/c_api/src/taichi_llvm_impl.h +++ b/c_api/src/taichi_llvm_impl.h @@ -1,4 +1,5 @@ #pragma once +#ifdef TI_WITH_LLVM #include "taichi_core_impl.h" @@ -41,3 +42,5 @@ class LlvmRuntime : public Runtime { }; } // namespace capi + +#endif // TI_WITH_LLVM diff --git a/c_api/src/taichi_opengl_impl.cpp b/c_api/src/taichi_opengl_impl.cpp index a3087c027e97a7..44de6e4c1b120c 100644 --- a/c_api/src/taichi_opengl_impl.cpp +++ b/c_api/src/taichi_opengl_impl.cpp @@ -1,4 +1,5 @@ #include "taichi_opengl_impl.h" +#ifdef TI_WITH_OPENGL OpenglRuntime::OpenglRuntime() : GfxRuntime(taichi::Arch::opengl), @@ -30,3 +31,5 @@ void ti_export_opengl_memory(TiRuntime runtime, interop_info->size = runtime2->get_gl().get_devalloc_size(devalloc); TI_CAPI_TRY_CATCH_END(); } + +#endif // TI_WITH_OPENGL diff --git a/c_api/src/taichi_opengl_impl.h b/c_api/src/taichi_opengl_impl.h index ee546406de4c23..fa8236a0181528 100644 --- a/c_api/src/taichi_opengl_impl.h +++ b/c_api/src/taichi_opengl_impl.h @@ -1,6 +1,8 @@ #pragma once +#ifdef TI_WITH_OPENGL + +#include "c_api_impl_common.h" #include "taichi_gfx_impl.h" -#include "taichi/taichi_opengl.h" #include "taichi/rhi/opengl/opengl_device.h" class OpenglRuntime : public GfxRuntime { @@ -16,3 +18,5 @@ class OpenglRuntime : public GfxRuntime { return device_; } }; + +#endif // TI_WITH_OPENGL diff --git a/c_api/src/taichi_vulkan_impl.cpp b/c_api/src/taichi_vulkan_impl.cpp index f429c01615b29d..1d07e21aae83a2 100644 --- a/c_api/src/taichi_vulkan_impl.cpp +++ b/c_api/src/taichi_vulkan_impl.cpp @@ -1,10 +1,5 @@ +#ifdef TI_WITH_VULKAN #include "taichi_vulkan_impl.h" -#include "taichi/rhi/vulkan/vulkan_loader.h" -#include "vulkan/vulkan.h" -#ifdef ANDROID -#define VK_KHR_android_surface 1 -#include "vulkan/vulkan_android.h" -#endif VulkanRuntime::VulkanRuntime() : GfxRuntime(taichi::Arch::vulkan) { } @@ -28,9 +23,7 @@ VulkanRuntimeImported::Workaround::Workaround( api_version); vk_device.set_cap(taichi::lang::DeviceCapability::spirv_version, 0x10000); - if (api_version >= VK_API_VERSION_1_3) { - vk_device.set_cap(taichi::lang::DeviceCapability::spirv_version, 0x10500); - } else if (api_version >= VK_API_VERSION_1_2) { + if (api_version >= VK_API_VERSION_1_2) { vk_device.set_cap(taichi::lang::DeviceCapability::spirv_version, 0x10500); } else if (api_version >= VK_API_VERSION_1_1) { vk_device.set_cap(taichi::lang::DeviceCapability::spirv_version, 0x10300); @@ -362,3 +355,5 @@ void ti_export_vulkan_event(TiRuntime runtime, interop_info->event = event2->vkapi_ref->event; TI_CAPI_TRY_CATCH_END(); } + +#endif // TI_WITH_VULKAN \ No newline at end of file diff --git a/c_api/src/taichi_vulkan_impl.h b/c_api/src/taichi_vulkan_impl.h index 25a149abf40e25..2cba127404e951 100644 --- a/c_api/src/taichi_vulkan_impl.h +++ b/c_api/src/taichi_vulkan_impl.h @@ -1,12 +1,11 @@ #pragma once +#ifdef TI_WITH_VULKAN -#define VK_NO_PROTOTYPES -#include "taichi/taichi_vulkan.h" +#include "taichi_core_impl.h" +#include "taichi_gfx_impl.h" #include "taichi/rhi/vulkan/vulkan_device.h" #include "taichi/rhi/vulkan/vulkan_device_creator.h" -#include "taichi_core_impl.h" -#include "taichi_gfx_impl.h" class VulkanRuntime; class VulkanRuntimeImported; @@ -52,3 +51,5 @@ class VulkanRuntimeOwned : public VulkanRuntime { virtual taichi::lang::Device &get() override final; virtual taichi::lang::gfx::GfxRuntime &get_gfx_runtime() override final; }; + +#endif // TI_WITH_VULKAN \ No newline at end of file diff --git a/c_api/taichi.json b/c_api/taichi.json index e5aa4b4c7aea08..0a35c2a42fca08 100644 --- a/c_api/taichi.json +++ b/c_api/taichi.json @@ -1,5 +1,4 @@ { - "version": "1.0.2", "modules": [ { "name": "taichi/taichi_platform.h", @@ -7,6 +6,11 @@ }, { "name": "taichi/taichi_core.h", + "default_definitions": [ + { + "name": "TI_C_API_VERSION" + } + ], "required_modules": [ "taichi/taichi_platform.h" ], @@ -869,6 +873,12 @@ }, { "name": "taichi/taichi_vulkan.h", + "default_definitions": [ + { + "name": "TI_WITH_VULKAN", + "value": "1" + } + ], "required_modules": [ "taichi/taichi_core.h" ], @@ -1162,6 +1172,12 @@ }, { "name": "taichi/taichi_opengl.h", + "default_definitions": [ + { + "name": "TI_WITH_OPENGL", + "value": "1" + } + ], "required_modules": [ "taichi/taichi_core.h" ], diff --git a/misc/generate_c_api.py b/misc/generate_c_api.py index e29a8c32b34312..ba372543e9812d 100644 --- a/misc/generate_c_api.py +++ b/misc/generate_c_api.py @@ -120,12 +120,21 @@ def get_human_readable_name(x: EntryBase): def print_module_header(module): - out = ["#pragma once"] + out = [ + "#pragma once", + "" + ] - for x in module.required_modules: - out += [f"#include <{x}>"] + for (name, value) in module.default_definitions: + out += [ + f"#ifndef {name}", + f"#define {name} {value}", + f"#endif // {name}", + "", + ] out += [ + "#include ", "", "#ifdef __cplusplus", 'extern "C" {', diff --git a/misc/taichi_json.py b/misc/taichi_json.py index ba238ae817acdf..d204d8611a6892 100644 --- a/misc/taichi_json.py +++ b/misc/taichi_json.py @@ -217,14 +217,21 @@ def __init__(self, j): class Module: all_modules = {} - def __init__(self, j, builtin_tys): + def __init__(self, version, j, builtin_tys): self.name = j["name"] self.is_built_in = False self.declr_reg = DeclarationRegistry(builtin_tys) self.required_modules = [] + self.default_definitions = [] DeclarationRegistry.set_current(self.declr_reg) + if "default_definitions" in j: + for jj in j["default_definitions"]: + name = jj["name"] + value = jj["value"] if "value" in jj else str(version) + self.default_definitions += [(name, value)] + if "is_built_in" in j: self.is_built_in = True # Built-in headers are hand-written so we can return right away. @@ -267,15 +274,28 @@ def __init__(self, j, builtin_tys): @staticmethod def load_all(builtin_tys): + def ver2int(ver: str) -> int: + xs = [int(x) for x in ver.split('.')] + assert len(xs) <= 3 + xs += ['0'] * (3 - len(xs)) + + version_number = 0 + for i in range(3): + version_number = version_number * 1000 + xs[i] + return version_number + j = None with open("c_api/taichi.json") as f: j = json.load(f) - version = j["version"] + version = None + with open("version.txt") as f: + version = ver2int(f.readlines()[0][1:]) + print("taichi c-api version is:", version) for k in j["modules"]: - module = Module(k, builtin_tys) + module = Module(version, k, builtin_tys) Module.all_modules[module.name] = module return list(Module.all_modules.values())