Skip to content

Commit

Permalink
[example] Build cpp examples (taichi-dev#3705)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmesingFlank authored Dec 4, 2021
1 parent 17adcdb commit 9ed4b06
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ if (TI_BUILD_TESTS)
include(cmake/TaichiTests.cmake)
endif()

option(TI_BUILD_EXAMPLES "Build the CPP examples" ON)

if (TI_BUILD_EXAMPLES)
include(cmake/TaichiExamples.cmake)
endif()

include_directories(${PROJECT_SOURCE_DIR}/external/eigen)

message("C++ Flags: ${CMAKE_CXX_FLAGS}")
Expand Down
16 changes: 0 additions & 16 deletions chi_examples/CMakeLists.txt

This file was deleted.

23 changes: 0 additions & 23 deletions chi_examples/README.md

This file was deleted.

21 changes: 21 additions & 0 deletions cmake/TaichiExamples.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.0)

set(EXAMPLES_NAME taichi_cpp_examples)

file(GLOB_RECURSE TAICHI_EXAMPLES_SOURCE "cpp_examples/main.cpp")

include_directories(
${PROJECT_SOURCE_DIR},
)

add_executable(${EXAMPLES_NAME} ${TAICHI_EXAMPLES_SOURCE})
if (WIN32)
# Output the executable to bin/ instead of build/Debug/...
set(EXAMPLES_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/bin")
set_target_properties(${EXAMPLES_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${EXAMPLES_OUTPUT_DIR})
set_target_properties(${EXAMPLES_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${EXAMPLES_OUTPUT_DIR})
set_target_properties(${EXAMPLES_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${EXAMPLES_OUTPUT_DIR})
set_target_properties(${EXAMPLES_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${EXAMPLES_OUTPUT_DIR})
set_target_properties(${EXAMPLES_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${EXAMPLES_OUTPUT_DIR})
endif()
target_link_libraries(${EXAMPLES_NAME} taichi_isolated_core)
11 changes: 5 additions & 6 deletions chi_examples/main.cpp → cpp_examples/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <bits/stdc++.h>

#include "taichi/ir/ir_builder.h"
#include "taichi/ir/statements.h"
#include "taichi/program/program.h"
Expand Down Expand Up @@ -49,7 +47,7 @@ void run_snode() {
int n = 10;
program.materialize_runtime();
auto *root = new SNode(0, SNodeType::root);
auto *pointer = &root->pointer(Index(0), n);
auto *pointer = &root->pointer(Axis(0), n, false);
auto *place = &pointer->insert_children(SNodeType::place);
place->dt = PrimitiveType::i32;
program.add_snode_tree(std::unique_ptr<SNode>(root), /*compile_only=*/false);
Expand Down Expand Up @@ -131,7 +129,7 @@ void run_snode() {
auto ctx_ret = kernel_ret->make_launch_context();
auto ctx_ext = kernel_ext->make_launch_context();
std::vector<int> ext_arr(n);
ctx_ext.set_arg_external_array(0, taichi::uint64(ext_arr.data()), n);
ctx_ext.set_arg_external_array(0, taichi::uint64(ext_arr.data()), n, false);

(*kernel_init)(ctx_init);
(*kernel_ret)(ctx_ret);
Expand Down Expand Up @@ -211,10 +209,11 @@ void autograd() {
}
};

auto *snode = &root->dense(0, n).insert_children(SNodeType::place);
auto *snode =
&root->dense(Axis(0), n, false).insert_children(SNodeType::place);
snode->dt = PrimitiveType::f32;
snode->grad_info = std::make_unique<GradInfoPrimal>(
&root->dense(0, n).insert_children(SNodeType::place));
&root->dense(Axis(0), n, false).insert_children(SNodeType::place));
snode->get_grad()->dt = PrimitiveType::f32;
snode->get_grad()->grad_info = std::make_unique<GradInfoAdjoint>();
return snode;
Expand Down

0 comments on commit 9ed4b06

Please sign in to comment.