Skip to content

Commit

Permalink
Fractal application (#6)
Browse files Browse the repository at this point in the history
* Fractal app

* gif and reorg

* use openmp when there is no gpus

* Fix
  • Loading branch information
Ahdhn authored Jul 22, 2022
1 parent d945fa9 commit 38b3834
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ add_subdirectory("libNeonSet")
add_subdirectory("libNeonDomain")
add_subdirectory("libNeonSkeleton")
add_subdirectory("libNeonSolver")
add_subdirectory("apps")


#-------------------------------------------------------------------#
Expand Down Expand Up @@ -107,5 +108,4 @@ message("|| CUDA Compile flags : ${CMAKE_CUDA_FLAGS}")
message("|| CUDA ARCHS : ${CMAKE_CUDA_ARCHITECTURES}")
#message("|| NeonCXXFlags : ${NeonCXXFlags}")
#message("|| NeonCUDAFlags : ${NeonCUDAFlags}")
message("||")
message("\\===================================================")
3 changes: 3 additions & 0 deletions apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

add_subdirectory("fractal")
17 changes: 17 additions & 0 deletions apps/fractal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

set (APP_NAME fractal)
file(GLOB_RECURSE SrcFiles fractal.cu)

add_executable(${APP_NAME} ${SrcFiles})

target_link_libraries(${APP_NAME}
PUBLIC libNeonSkeleton)

set_target_properties(${APP_NAME} PROPERTIES
CUDA_SEPARABLE_COMPILATION ON
CUDA_RESOLVE_DEVICE_SYMBOLS ON)
set_target_properties(${APP_NAME} PROPERTIES FOLDER "apps")
source_group(TREE ${CMAKE_CURRENT_LIST_DIR} PREFIX "${APP_NAME}" FILES ${SrcFiles})

add_test(NAME ${APP_NAME} COMMAND ${APP_NAME})
96 changes: 96 additions & 0 deletions apps/fractal/fractal.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <iomanip>
#include <sstream>

#include "Neon/Neon.h"
#include "Neon/domain/dGrid.h"
#include "Neon/skeleton/Skeleton.h"

template <typename FieldT>
inline void draw_pixels(const int t, FieldT& field)
{
printf("\n Exporting Frame =%d", t);
int precision = 4;
std::ostringstream oss;
oss << std::setw(precision) << std::setfill('0') << t;
std::string fname = "frame_" + oss.str();
field.ioToVtk(fname, "pixels");
}

NEON_CUDA_HOST_DEVICE inline Neon::float_2d complex_sqr(Neon::float_2d& z)
{
return Neon::float_2d(z.x * z.x - z.y * z.y, z.x * z.y * 2.0f);
}

NEON_CUDA_HOST_DEVICE inline Neon::float_2d complex_pow(Neon::float_2d& z, Neon::float_1d& n)
{
Neon::float_1d radius = pow(z.norm(), n);
Neon::float_1d angle = n * atan2(z.y, z.x);
return Neon::float_2d(radius * cos(angle), radius * sin(angle));
}

template <typename FieldT>
inline Neon::set::Container FractalsContainer(FieldT& pixels,
int32_t& time,
int32_t n)
{
return pixels.getGrid().getContainer(
"FractalContainer", [&, n](Neon::set::Loader& L) {
auto& px = L.load(pixels);
auto& t = time;

return [=] NEON_CUDA_HOST_DEVICE(
const typename FieldT::Cell& idx) mutable {
auto id = px.mapToGlobal(idx);

Neon::float_2d c(-0.8, cos(t * 0.03) * 0.2);
Neon::float_2d z((float(id.x) / float(n)) - 1.0f,
(float(id.y) / float(n)) - 0.5f);
z *= 2.0f;
float iterations = 0;
while (z.norm() < 20 && iterations < 50) {
z = complex_sqr(z) + c;
iterations += 1;
}
px(idx, 0) = 1.0f - iterations * 0.02;
};
});
}

int main(int argc, char** argv)
{
Neon::init();
if (Neon::sys::globalSpace::gpuSysObjStorage.numDevs() > 0) {
int32_t n = 320;
Neon::index_3d dim(2 * n, n, 1);
std::vector<int> gpu_ids{0};

auto runtime = Neon::Runtime::stream;

//runtime = Neon::Runtime::openmp;

Neon::Backend backend(gpu_ids, runtime);

using Grid = Neon::domain::dGrid;
Grid grid(
backend, dim,
[](const Neon::index_3d& idx) -> bool { return true; },
Neon::domain::Stencil::s7_Laplace_t());

int cardinality = 1;
float inactiveValue = 0.0f;
auto pixels = grid.template newField<float>("pixels", cardinality, inactiveValue);

Neon::skeleton::Skeleton skeleton(backend);

int32_t time;
skeleton.sequence({FractalsContainer(pixels, time, n)}, "fractal");


for (time = 0; time < 1000; ++time) {
skeleton.run();

pixels.updateIO(0);
//draw_pixels(time, pixels);
}
}
}
Binary file added apps/fractal/fractals.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions libNeonDomain/include/Neon/domain/internal/dGrid/dPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,18 @@ struct dPartition
}


NEON_CUDA_HOST_DEVICE inline auto mapToGlobal(const Neon::index_3d& local) const -> Neon::index_3d
NEON_CUDA_HOST_DEVICE inline auto mapToGlobal(const Cell& local) const -> Neon::index_3d
{
assert(local.x >= 0 &&
local.y >= 0 &&
local.z >= -m_zHaloRadius &&
local.x < m_dim.x &&
local.y < m_dim.y &&
local.z < m_dim.z + m_zHaloRadius);
assert(local.mLocation.x >= 0 &&
local.mLocation.y >= 0 &&
local.mLocation.z >= -m_zHaloRadius &&
local.mLocation.x < m_dim.x &&
local.mLocation.y < m_dim.y &&
local.mLocation.z < m_dim.z + m_zHaloRadius);

switch (m_dataView) {
case Neon::DataView::STANDARD: {
return local + m_origin;
return local.mLocation + m_origin;
}
default: {
}
Expand Down
2 changes: 1 addition & 1 deletion libNeonDomain/src/domain/internal/dGrid/dGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ auto dGrid::getLaunchParameters(const Neon::DataView dataView,
for (int32_t i = 0; i < dims.size(); ++i) {
dims[i] = m_data->partitionDims[i];
dims[i].z = dims[i].z - m_zBoundaryRadius * 2;
if (dims[i].z <= 0) {
if (dims[i].z <= 0 && dims.size() > 1) {
NeonException exp("dGrid");
exp << "The grid size is too small to support the data view model correctly";
NEON_THROW(exp);
Expand Down

0 comments on commit 38b3834

Please sign in to comment.