Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Fixed sparse tests

Android APP for SPH (#113)

step
  • Loading branch information
PENGUINLIONG committed Feb 13, 2023
1 parent 1333cda commit f659523
Show file tree
Hide file tree
Showing 38 changed files with 926 additions and 1,110 deletions.
97 changes: 97 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
Language: Cpp
#BasedOnStyle: Chromium
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: false
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 8
UseTab: Never
---
Language: ObjC
...
19 changes: 9 additions & 10 deletions 1_hello_world/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include "glm/glm.hpp"
#include "taichi/aot_demo/framework.hpp"
#include "taichi/aot_demo/shadow_buffer.hpp"

using namespace ti::aot_demo;

Expand All @@ -15,21 +16,19 @@ struct App1_hello_world : public App {
virtual AppConfig cfg() const override final {
AppConfig out {};
out.app_name = "1_hello_world";
out.supported_archs = {
TI_ARCH_VULKAN,
};
return out;
}
virtual void initialize(TiArch arch) override final{

if(arch != TI_ARCH_VULKAN) {
std::cout << "1_hello_world only supports vulkan backend" << std::endl;
exit(0);
}

GraphicsRuntime& runtime = F_->runtime();
virtual void initialize() override final{
Renderer& renderer = F_->renderer();
ti::Runtime &runtime = F_->runtime();

points = runtime.allocate_vertex_buffer(3, 2, true);
points = runtime.allocate_ndarray<float>({3}, {2}, true);
colors = runtime.allocate_ndarray<float>({3}, {4}, true);

draw_points = runtime.draw_points(points)
draw_points = renderer.draw_points(points)
.point_size(10.0f)
.color(colors)
.build();
Expand Down
40 changes: 9 additions & 31 deletions 1_hello_world_with_interop/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
#include <iostream>
#include "glm/glm.hpp"
#include "taichi/aot_demo/framework.hpp"
#include "taichi/aot_demo/interop/cross_device_copy.hpp"

using namespace ti::aot_demo;

struct App1_hello_world_with_interop : public App {
// Runtime/Ndarray to perform computations
TiArch arch_;
ti::Runtime runtime;
ti::NdArray<float> points;

Expand All @@ -22,31 +20,22 @@ struct App1_hello_world_with_interop : public App {
virtual AppConfig cfg() const override final {
AppConfig out {};
out.app_name = "1_hello_world_with_interop";
out.supported_archs = {
TI_ARCH_VULKAN,
TI_ARCH_CUDA,
TI_ARCH_X64,
TI_ARCH_OPENGL,
};
return out;
}
virtual void initialize(TiArch arch) override final{
if(arch != TI_ARCH_VULKAN && arch != TI_ARCH_X64 && arch != TI_ARCH_CUDA && arch != TI_ARCH_OPENGL) {
std::cout << "1_hello_world_with_interop only supports cuda, x64, vulkan, opengl backends" << std::endl;
exit(0);
}
arch_ = arch;
virtual void initialize() override final{
Renderer &renderer = F_->renderer();

// Prepare Ndarray to store computation results
if(arch_ == TI_ARCH_VULKAN) {
// Reuse the vulkan runtime from renderer framework
runtime = ti::Runtime(arch_, F_->runtime(), false);;
} else {
runtime = ti::Runtime(arch_);
}
points = runtime.allocate_ndarray<float>({3}, {2}, true);

// Prepare vertex buffers for the renderer
GraphicsRuntime& g_runtime = F_->runtime();
render_points = g_runtime.allocate_vertex_buffer(3, 2, true);
colors = g_runtime.allocate_ndarray<float>({3}, {4}, true);

// Renderer renders with data from "render_points" in each frame
draw_points = g_runtime.draw_points(render_points)
draw_points = renderer.draw_points(points)
.point_size(10.0f)
.color(colors)
.build();
Expand All @@ -69,17 +58,6 @@ struct App1_hello_world_with_interop : public App {
};
colors.write(colors_data);

// Copy data from "points" to "render_points"
if(arch_ == TI_ARCH_X64) {
InteropHelper<float>::copy_from_cpu(F_->runtime(), render_points, runtime, points);
} else if(arch_ == TI_ARCH_CUDA) {
InteropHelper<float>::copy_from_cuda(F_->runtime(), render_points, runtime, points);
} else if(arch_ == TI_ARCH_VULKAN) {
InteropHelper<float>::copy_from_vulkan(F_->runtime(), render_points, runtime, points);
} else if(arch_ == TI_ARCH_OPENGL) {
InteropHelper<float>::copy_from_opengl(F_->runtime(), render_points, runtime, points);
}

std::cout << "stepped! (fps=" << F_->fps() << ")" << std::endl;
return true;
}
Expand Down
84 changes: 17 additions & 67 deletions 2_mpm88/app.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <memory>
#include <thread>
#include <chrono>
#include <iostream>
#include "glm/glm.hpp"
#include "taichi/aot_demo/draws/draw_points.hpp"
#include "taichi/aot_demo/framework.hpp"
#include "taichi/aot_demo/interop/cross_device_copy.hpp"
#include "taichi/aot_demo/shadow_buffer.hpp"

using namespace ti::aot_demo;

Expand All @@ -27,42 +29,11 @@ static std::string get_aot_file_dir(TiArch arch) {
}
}

template<typename T>
static void copy_to_vulkan_ndarray(ti::NdArray<T>& dst,
GraphicsRuntime& dst_runtime,
ti::NdArray<T>& src,
ti::Runtime& src_runtime, TiArch src_arch) {

switch(src_arch) {
case TI_ARCH_VULKAN: {
InteropHelper<T>::copy_from_vulkan(dst_runtime, dst, src_runtime, src);
break;
}
case TI_ARCH_X64: {
InteropHelper<T>::copy_from_cpu(dst_runtime, dst, src_runtime, src);
break;
}
case TI_ARCH_CUDA: {
InteropHelper<T>::copy_from_cuda(dst_runtime, dst, src_runtime, src);
break;
}
case TI_ARCH_OPENGL: {
InteropHelper<T>::copy_from_opengl(dst_runtime, dst, src_runtime, src);
break;
}
default: {
throw std::runtime_error("Unable to perform NdArray memory copy");
}
}
}

struct App2_mpm88 : public App {
static const uint32_t NPARTICLE = 8192 * 2;
static const uint32_t GRID_SIZE = 128;

ti::Runtime runtime_;
ti::AotModule module_;
TiArch arch_;

ti::ComputeGraph g_init_;
ti::ComputeGraph g_update_;
Expand All @@ -74,8 +45,6 @@ struct App2_mpm88 : public App {
ti::NdArray<float> J_;
ti::NdArray<float> grid_v_;
ti::NdArray<float> grid_m_;

ti::NdArray<float> render_x_;

std::unique_ptr<GraphicsTask> draw_points;

Expand All @@ -87,46 +56,32 @@ struct App2_mpm88 : public App {
return out;
}


virtual void initialize(TiArch arch) override final{
if(arch != TI_ARCH_VULKAN && arch != TI_ARCH_X64 && arch != TI_ARCH_CUDA && arch != TI_ARCH_OPENGL) {
std::cout << "1_hello_world_with_interop only supports cuda, x64, vulkan, opengl backends" << std::endl;
exit(0);
}
arch_ = arch;

GraphicsRuntime& g_runtime = F_->runtime();
if(arch_ == TI_ARCH_VULKAN) {
// Reuse the vulkan runtime from renderer framework
runtime_ = ti::Runtime(arch_, F_->runtime(), false);;
} else {
runtime_ = ti::Runtime(arch_);
}
virtual void initialize() override final{
Renderer &renderer = F_->renderer();
ti::Runtime &runtime = F_->runtime();

// 2. Load AOT module
#ifdef TI_AOT_DEMO_WITH_ANDROID_APP
std::vector<uint8_t> tcm;
F_->asset_mgr().load_file("E2_mpm88.tcm", tcm);
module_ = runtime_.create_aot_module(tcm);
module_ = runtime.create_aot_module(tcm);
#else
auto aot_file_path = get_aot_file_dir(arch_);
module_ = runtime_.load_aot_module(aot_file_path);
auto aot_file_path = get_aot_file_dir(runtime.arch());
module_ = runtime.load_aot_module(aot_file_path);
#endif

g_init_ = module_.get_compute_graph("init");
g_update_ = module_.get_compute_graph("update");

render_x_ = g_runtime.allocate_vertex_buffer(NPARTICLE, 2, false/*host_access*/);
x_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2}, false/*host_access*/);
v_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2});
pos_ = runtime.allocate_ndarray<float>({NPARTICLE}, {3});
C_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2, 2});
J_ = runtime.allocate_ndarray<float>({NPARTICLE}, {});
grid_v_ = runtime.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {2});
grid_m_ = runtime.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {});

x_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2}, false/*host_access*/);
v_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2});
pos_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {3});
C_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2, 2});
J_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {});
grid_v_ = runtime_.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {2});
grid_m_ = runtime_.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {});

draw_points = g_runtime.draw_points(render_x_)
draw_points = renderer.draw_points(x_)
.point_size(3.0f)
.color(glm::vec3(0,0,1))
.build();
Expand All @@ -144,18 +99,13 @@ struct App2_mpm88 : public App {
g_update_["grid_v"] = grid_v_;
g_update_["grid_m"] = grid_m_;

Renderer& renderer = F_->renderer();
renderer.set_framebuffer_size(256, 256);

std::cout << "initialized!" << std::endl;
}
virtual bool update() override final {
g_update_.launch();

auto& g_runtime = F_->runtime();
copy_to_vulkan_ndarray<float>(render_x_, g_runtime, x_, runtime_, arch_);
runtime_.wait();

std::cout << "stepped! (fps=" << F_->fps() << ")" << std::endl;
return true;
}
Expand Down
17 changes: 6 additions & 11 deletions 3_implicit_fem/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@ struct App3_implicit_fem : public App {
out.framebuffer_height = 256;
return out;
}
virtual void initialize(TiArch arch) override final{

if(arch != TI_ARCH_VULKAN) {
std::cout << "3_implicit_fem only supports vulkan backend" << std::endl;
exit(0);
}
GraphicsRuntime& runtime = F_->runtime();
Renderer& renderer = F_->renderer();
virtual void initialize() override final{
Renderer &renderer = F_->renderer();
ti::Runtime& runtime = F_->runtime();

#ifdef TI_AOT_DEMO_WITH_ANDROID_APP
std::vector<uint8_t> tcm;
Expand Down Expand Up @@ -84,15 +79,15 @@ struct App3_implicit_fem : public App {

hes_edge_ = runtime.allocate_ndarray<float>({nedge});
hes_vert_ = runtime.allocate_ndarray<float>({ncell});
x_ = runtime.allocate_vertex_buffer(nvert, 3, true);
x_ = runtime.allocate_ndarray<float>({nvert}, {3}, true);
v_ = runtime.allocate_ndarray<float>({nvert}, {3});
f_ = runtime.allocate_ndarray<float>({nvert}, {3});
mul_ans_ = runtime.allocate_ndarray<float>({nvert}, {3});
c2e_ = runtime.allocate_ndarray<int>({ncell}, {6}, true);
b_ = runtime.allocate_ndarray<float>({nvert}, {3});
r0_ = runtime.allocate_ndarray<float>({nvert}, {3});
p0_ = runtime.allocate_ndarray<float>({nvert}, {3});
indices_ = runtime.allocate_index_buffer(nface, 3, true);
indices_ = runtime.allocate_ndarray<uint32_t>({nface}, {3}, true);
vertices_ = runtime.allocate_ndarray<int>({ncell}, {4}, true);
edges_ = runtime.allocate_ndarray<int>({nedge}, {2}, true);
ox_ = runtime.allocate_ndarray<float>({nvert}, {3}, true);
Expand All @@ -116,7 +111,7 @@ struct App3_implicit_fem : public App {
glm::mat4 world2camera = glm::lookAt(glm::vec3(0, 0, 10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
glm::mat4 world2view = camera2view * world2camera;

draw_mesh = runtime.draw_mesh(x_, indices_)
draw_mesh = renderer.draw_mesh(x_, indices_)
.model2world(model2world)
.world2view(world2view)
.color(glm::vec3(0,0,1))
Expand Down
Loading

0 comments on commit f659523

Please sign in to comment.