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

[llvm] [aot] Add LLVM to CAPI part 5: Added C-API tests for Vulkan and Cuda backend #5440

Merged
merged 16 commits into from
Jul 18, 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
24 changes: 24 additions & 0 deletions c_api/src/c_api_test_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "c_api_test_utils.h"
#include "taichi/platform/cuda/detect_cuda.h"

#ifdef TI_WITH_VULKAN
#include "taichi/rhi/vulkan/vulkan_loader.h"
#endif

namespace capi {
namespace utils {

bool is_vulkan_available() {
#ifdef TI_WITH_VULKAN
return taichi::lang::vulkan::is_vulkan_api_available();
#else
return false;
#endif
}

bool is_cuda_available() {
return taichi::is_cuda_api_available();
}

} // namespace utils
} // namespace capi
11 changes: 11 additions & 0 deletions c_api/src/c_api_test_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <taichi/taichi_platform.h>

namespace capi {
namespace utils {

TI_DLL_EXPORT bool TI_API_CALL is_vulkan_available();
TI_DLL_EXPORT bool TI_API_CALL is_cuda_available();

} // namespace utils
} // namespace capi
79 changes: 79 additions & 0 deletions c_api/tests/c_api_interface_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "gtest/gtest.h"
#include "c_api_test_utils.h"
#include "taichi/taichi_core.h"

TEST(CapiDryRun, Runtime) {
Expand All @@ -8,6 +9,20 @@ TEST(CapiDryRun, Runtime) {
TiRuntime runtime = ti_create_runtime(arch);
ti_destroy_runtime(runtime);
}

if (capi::utils::is_vulkan_available()) {
// Vulkan Runtime
TiArch arch = TiArch::TI_ARCH_VULKAN;
TiRuntime runtime = ti_create_runtime(arch);
ti_destroy_runtime(runtime);
}

if (capi::utils::is_cuda_available()) {
// Vulkan Runtime
TiArch arch = TiArch::TI_ARCH_CUDA;
TiRuntime runtime = ti_create_runtime(arch);
ti_destroy_runtime(runtime);
}
}

TEST(CapiDryRun, MemoryAllocation) {
Expand All @@ -30,6 +45,28 @@ TEST(CapiDryRun, MemoryAllocation) {

ti_destroy_runtime(runtime);
}

if (capi::utils::is_vulkan_available()) {
// Vulkan Runtime
TiArch arch = TiArch::TI_ARCH_VULKAN;
TiRuntime runtime = ti_create_runtime(arch);

TiMemory memory = ti_allocate_memory(runtime, &alloc_info);
ti_free_memory(runtime, memory);

ti_destroy_runtime(runtime);
}

if (capi::utils::is_cuda_available()) {
// Cuda Runtime
TiArch arch = TiArch::TI_ARCH_CUDA;
TiRuntime runtime = ti_create_runtime(arch);

TiMemory memory = ti_allocate_memory(runtime, &alloc_info);
ti_free_memory(runtime, memory);

ti_destroy_runtime(runtime);
}
}

TEST(CapiDryRun, CpuAotModule) {
Expand All @@ -49,3 +86,45 @@ TEST(CapiDryRun, CpuAotModule) {
ti_destroy_runtime(runtime);
}
}

TEST(CapiDryRun, VulkanAotModule) {
if (capi::utils::is_vulkan_available()) {
const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH");

std::stringstream aot_mod_ss;
aot_mod_ss << folder_dir;

{
// Vulkan Runtime
TiArch arch = TiArch::TI_ARCH_VULKAN;
TiRuntime runtime = ti_create_runtime(arch);

TiAotModule aot_mod =
ti_load_aot_module(runtime, aot_mod_ss.str().c_str());
ti_destroy_aot_module(aot_mod);

ti_destroy_runtime(runtime);
}
}
}

TEST(CapiDryRun, CudaAotModule) {
if (capi::utils::is_cuda_available()) {
const auto folder_dir = getenv("TAICHI_AOT_FOLDER_PATH");

std::stringstream aot_mod_ss;
aot_mod_ss << folder_dir;

{
// Vulkan Runtime
TiArch arch = TiArch::TI_ARCH_CUDA;
TiRuntime runtime = ti_create_runtime(arch);

TiAotModule aot_mod =
ti_load_aot_module(runtime, aot_mod_ss.str().c_str());
ti_destroy_aot_module(aot_mod);

ti_destroy_runtime(runtime);
}
}
}
4 changes: 4 additions & 0 deletions cmake/TaichiCAPI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ cmake_minimum_required(VERSION 3.0)
set(TAICHI_C_API_NAME taichi_c_api)

file(GLOB_RECURSE C_API_SOURCE "c_api/src/*.cpp")
if(NOT TI_BUILD_TESTS)
list(REMOVE_ITEM C_API_SOURCE "c_api/src/c_api_test_utils.cpp")
endif()

add_library(${TAICHI_C_API_NAME} SHARED ${C_API_SOURCE})
target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_isolated_core)

Expand Down
1 change: 1 addition & 0 deletions cmake/TaichiCAPITests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ target_include_directories(${C_API_TESTS_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/c_api/include
${PROJECT_SOURCE_DIR}/c_api/src
)

add_test(NAME ${C_API_TESTS_NAME} COMMAND ${C_API_TESTS_NAME})
2 changes: 2 additions & 0 deletions tests/cpp/aot/llvm/kernel_aot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
compile_kernel_aot(arch=ti.cpu)
elif args.arch == "cuda":
compile_kernel_aot(arch=ti.cuda)
elif args.arch == "vulkan":
compile_kernel_aot(arch=ti.vulkan)
else:
assert False
6 changes: 6 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
__capi_aot_test_cases = {
"CapiDryRun.CpuAotModule":
[os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), "--arch=cpu"],
"CapiDryRun.VulkanAotModule": [
os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'),
"--arch=vulkan"
],
"CapiDryRun.CudaAotModule":
[os.path.join('cpp', 'aot', 'llvm', 'kernel_aot_test.py'), "--arch=cuda"],
}


Expand Down