diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-11-13 14:12:49 +0100 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-11-13 14:12:49 +0100 |
| commit | e821d617802ebbfff756844e4d9720342fc78a4e (patch) | |
| tree | 1dcf33b84698b5074218e44ea491b77a9c23ee66 /examples/cavity_2d_gpu/cavity_2d_gpu.cpp | |
| parent | 0c0087ef9482634273ec1a23d3a915754f94926d (diff) | |
| download | libs-lbm-e821d617802ebbfff756844e4d9720342fc78a4e.tar.gz | |
Swarm POCdev
Diffstat (limited to 'examples/cavity_2d_gpu/cavity_2d_gpu.cpp')
| -rw-r--r-- | examples/cavity_2d_gpu/cavity_2d_gpu.cpp | 698 |
1 files changed, 389 insertions, 309 deletions
diff --git a/examples/cavity_2d_gpu/cavity_2d_gpu.cpp b/examples/cavity_2d_gpu/cavity_2d_gpu.cpp index 55225d1..6b71db3 100644 --- a/examples/cavity_2d_gpu/cavity_2d_gpu.cpp +++ b/examples/cavity_2d_gpu/cavity_2d_gpu.cpp @@ -1,384 +1,464 @@ +#include <kel/lbm/sycl/lbm.hpp> #include <kel/lbm/lbm.hpp> -#include <forstio/codec/data.hpp> -#include <AdaptiveCpp/sycl/sycl.hpp> - -#include <iostream> -#include <fstream> -#include <vector> -#include <chrono> -#include <cmath> +#include <forstio/io/io.hpp> +#include <forstio/remote/filesystem/easy.hpp> +#include <forstio/codec/json/json.hpp> +#include <forstio/codec/simple.hpp> +#include <forstio/codec/args.hpp> namespace kel { namespace lbm { -namespace sch { -/** - * Basic distribution function - * Base type - * D - * Q - * Scalar factor - * D factor - * Q factor - */ -using T = Float32; -using D2Q9 = Descriptor<2u,9u>; +// Square cavity, as this case has always used. +constexpr uint64_t dim_x = 128ul; +constexpr uint64_t dim_y = 128ul; -template<typename Desc> -using DfCell = Cell<T, Desc, 0u, 0u, 1u>; +// Lid velocity, tangential along +x on the top row. +constexpr double lid_velocity = 0.1; -template<typename Desc> -using CellInfo = Cell<UInt8, Desc, 1u, 0u, 0u>; - -/** - * Basic type for simulation - */ -template<typename Desc> -using CellStruct = CellFieldStruct< - Desc, - Struct< - Member<CellField<Desc,DfCell<Desc>>, "dfs">, - Member<CellField<Desc,DfCell<Desc>>, "dfs_old">, - Member<CellField<Desc,CellInfo<Desc>>, "info"> - > ->; +namespace sch { -template<typename T, uint64_t D> -using MacroStruct = Struct< - Member<FixedArray<T,D>, "velocity">, - Member<T, "pressure"> ->; +using InfoChunk = Chunk<UInt8, 0u, dim_x, dim_y>; -using CavityFieldD2Q9 = CellStruct<D2Q9>; -} +template<typename T, typename Desc> +using DfChunk = Chunk<FixedArray<T,Desc::Q>, 1u, dim_x, dim_y>; +template<typename T, typename Desc> +using ScalarChunk = Chunk<Scalar<T>, 0u, dim_x, dim_y>; -/* -template<typename T, typename Encode> -class df_cell_view; -*/ -/** - * Minor helper for the AA-Pull Pattern, so I can use only one lattice - * - * Am I sure I want to use AA this way? - * Esoteric Twist technically reduces the needed memory access footprint - */ -/* -template<typename Desc, size_t SN, size_t DN, size_t QN, typename Encode> -class df_cell_view<sch::Cell<sch::T, Desc, SN, DN, QN>, Encode> { -public: - using Schema = sch::Cell<sch::T,Desc,SN,DN,QN>; -private: - std::array<std::decay_t<typename kel::lbm::native_data_type<sch::T>::type>*, QN> view_; -public: - df_cell_view(const std::array<std::decay_t<typename kel::lbm::native_data_type<sch::T>::type>*, QN>& view): - view_{view} - {} -}; -*/ -namespace cmpt { -struct MovingWall {}; -} +template<typename T, typename Desc> +using VectorChunk = Chunk<Vector<T,Desc::D>, 0u, dim_x, dim_y>; -/** - * Full-Way moving wall Bounce back, something is not right here. - * Technically it should reflect properly. - */ -template<typename Desc> -class component<sch::T, Desc, cmpt::MovingWall> { -public: - std::array<typename kel::lbm::native_data_type<sch::T>::type, Desc::D> lid_vel; +template<typename T, typename Desc> +using ChunkStruct = Struct< + Member<InfoChunk, "info">, + Member<DfChunk<T,Desc>, "dfs">, + Member<DfChunk<T,Desc>, "dfs_old"> +>; -public: - void apply( - kel::lbm::data<sch::DfCell<Desc>>& dfs - ){ - using dfi = df_info<sch::T,Desc>; +template<typename T, typename Desc> +using VelChunk = Chunk<Vector<T,Desc::D>, 0u, dim_x, dim_y>; - // Technically use .copy() - /* - auto dfs_cpy = dfs; +template<typename T> +using RhoChunk = Chunk<Scalar<T>, 0u, dim_x, dim_y>; - for(uint64_t i = 0u; i < Desc::Q; ++i){ - dfs({dfi::opposite_index.at(i)}) = dfs_cpy({i}) - 2.0 * dfi::weights[i] * 1.0 * ( lid_vel[0] * dfi::directions[i][0] + lid_vel[1] * dfi::directions[i][1]) * dfi::inv_cs2; - } - */ - } -}; +template<typename T, typename Desc> +using MacroStruct = Struct< + Member<VelChunk<T,Desc>, "velocity">, + Member<RhoChunk<T>, "density"> +>; -constexpr size_t dim_x = 128; -constexpr size_t dim_y = 128; +//template<typename T, typename Desc> +//using ParticleArray = Array< +// Particle<T,Desc::D> +//>; +} -template<typename Desc> -void set_geometry( - kel::lbm::data<sch::CellInfo<Desc>>* info_field +template<typename T, typename Desc> +kel::lbm::error_or<void> setup_initial_conditions( + kel::lbm::data<sch::ChunkStruct<T,Desc>>& fields, + kel::lbm::data<sch::MacroStruct<T,Desc>>& macros ){ - using namespace kel::lbm; - using namespace acpp; - kel::lbm::data<sch::FixedArray<sch::UInt64,Desc::D>> meta{{dim_x,dim_y}}; - /** - * Set ghost - */ - iterate_over([&](const kel::lbm::data<sch::FixedArray<sch::UInt64,2u>>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& info = info_field[i]; - - info({0u}).set(0u); - - }, {{0u,0u}}, meta); - - /** - * Set wall - */ - iterate_over([&](const kel::lbm::data<sch::FixedArray<sch::UInt64,2u>>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& info = info_field[i]; - - info({0u}).set(1u); - - }, {{0u,0u}}, meta, {{1u,1u}}); - - /** - * Set fluid - */ - iterate_over([&](const kel::lbm::data<sch::FixedArray<sch::UInt64,2u>>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& info = info_field[i]; - - info({0u}).set(2u); - - }, {{0u,0u}}, meta, {{2u,2u}}); - - /** - * Set top lid - */ - iterate_over([&](const kel::lbm::data<sch::FixedArray<sch::UInt64,2u>>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& info = info_field[i]; - info({0u}).set(3u); + auto& info_f = fields.template get<"info">(); + // Set everything as walls + iterator<Desc::D>::apply( + [&](auto& index){ + info_f.at(index).set(1u); + }, + {}, + info_f.get_dims(), + {} + ); + // Fluid + iterator<Desc::D>::apply( + [&](auto& index){ + info_f.at(index).set(2u); + }, + {}, + info_f.get_dims(), + {{1u,1u}} + ); + + // The lid: the top row, moving tangentially. Corners stay plain walls. + iterator<Desc::D>::apply( + [&](auto& index){ + info_f.at(index).set(5u); + }, + {{0u,dim_y-1u}}, + {{dim_x,dim_y}}, + {{1u,0u}} + ); + // + auto& df_f = fields.template get<"dfs_old">(); + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + iterator<Desc::D>::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + rho.at({}) = {1}; + auto& vel = vel_f.at(index); + auto eq = equilibrium<T,Desc>(rho,vel); - }, {{0u,1u}}, {{meta.at({0u}), 2u}}, {{2u,0u}}); -} + df = eq; + }, + {},// 0-index + df_f.get_dims() + ); -template<typename Desc> -void set_initial_conditions( - kel::lbm::data<sch::CellInfo<Desc>>* info_field, - kel::lbm::data<sch::DfCell<Desc>>* dfs_field, - kel::lbm::data<sch::DfCell<Desc>>* dfs_old_field -){ - using namespace kel::lbm; - using namespace acpp; + iterator<Desc::D>::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + rho.at({}) = {1}; + auto& vel = vel_f.at(index); + if(info_f.at(index).get() == 2u){ + vel.at({{0u}}) = 0.0; + } + auto eq = equilibrium<T,Desc>(rho,vel); - kel::lbm::data<sch::T> rho{1.0}; - kel::lbm::data<sch::FixedArray<sch::T,Desc::D>> vel{{0.0,0.0}}; - auto eq = equilibrium<sch::T,Desc>(rho, vel); - kel::lbm::data<sch::FixedArray<sch::UInt64,Desc::D>> meta{{dim_x,dim_y}}; + df = eq; + }, + {},// 0-index + df_f.get_dims(), + {{1u,1u}} + ); + + iterator<Desc::D>::apply( + [&](auto& index){ + kel::lbm::data<sch::Vector<T,Desc::D>> middle, ind_vec; + middle.at({{0u}}) = dim_x * 0.25; + middle.at({{1u}}) = dim_y * 0.5; - /** - * Set distribution - */ - iterate_over([&](const kel::lbm::data<sch::FixedArray<sch::UInt64,Desc::D>>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& dfs = dfs_field[i]; - auto& dfs_old = dfs_old_field[i]; + ind_vec.at({{0u}}) = index.at({{0u}}).template cast_to<T>(); + ind_vec.at({{1u}}) = index.at({{1u}}).template cast_to<T>(); - for(kel::lbm::data<sch::UInt64> k = 0; k < kel::lbm::data<sch::UInt64>{Desc::Q}; ++k){ - dfs(k) = eq.at(k); - dfs_old(k) = eq.at(k); - } + auto dist = middle - ind_vec; + auto dist_2 = kel::lbm::math::dot(dist,dist); + if(dist_2.at({}).get() < dim_y*dim_y*0.01){ + info_f.at(index) = 1u; + } + }, + {},// 0-index + df_f.get_dims() + ); - }, {{0u,0u}}, meta); + return kel::lbm::make_void(); } template<typename T, typename Desc> -void lbm_step( - kel::lbm::data<sch::CellInfo<Desc>>* info_r, - kel::lbm::data<sch::DfCell<Desc>>* dfs_r, - kel::lbm::data<sch::DfCell<Desc>>* dfs_r_old, - bool is_even, - uint64_t time_step, - acpp::sycl::queue& sycl_q +kel::lbm::error_or<void> step( + kel::lbm::data<sch::Ptr<sch::ChunkStruct<T,Desc>>,encode::Sycl<kel::lbm::enc::Native>>& fields, + kel::lbm::data<sch::Ptr<sch::MacroStruct<T,Desc>>,encode::Sycl<kel::lbm::enc::Native>>& macros, + kel::lbm::data<sch::UInt64> t_i, + device& dev ){ - using namespace kel::lbm; - using namespace acpp; - - using dfi = df_info<T,Desc>; + auto& q = dev.get_handle(); + auto& info_f = fields.template get<"info">(); - //component<T, Desc, cmpt::BGK> coll{0.59}; - constexpr kel::lbm::data<T> frequency{1.0 / 0.59}; - //component<T, Desc, cmpt::BounceBack> bb; - component<T, Desc, cmpt::MovingWall> bb_lid; - bb_lid.lid_vel = {0.1, 0.0}; + // auto coll_ev = + q.submit([&](acpp::sycl::handler& h){ + // Need nicer things to handle the flow. I see improvement here + component<T,Desc,cmpt::BGK, encode::Sycl<kel::lbm::enc::Native>> collision{0.8}; + component<T,Desc,cmpt::BounceBack,encode::Sycl<kel::lbm::enc::Native>> bb; + component<T,Desc,cmpt::AntiBounceBack<0u>,encode::Sycl<kel::lbm::enc::Native>> abb; - // Submit collision kernel - sycl_q.submit([&](sycl::handler& cgh) { - // Accessor for latt with read/write + kel::lbm::data<sch::Scalar<T>> rho_b; + rho_b.at({}) = 1.0; + kel::lbm::data<sch::Vector<T,Desc::D>> vel_b; + vel_b.at({{0u}}) = 0.015; - cgh.parallel_for( - sycl::range<2>{dim_x, dim_y}, [=](sycl::id<2> idx) { - size_t i = idx[0]; - size_t j = idx[1]; + component<T,Desc,cmpt::Equilibrium,encode::Sycl<kel::lbm::enc::Native>> equi{rho_b,vel_b}; - size_t acc_id = i * dim_x + j; - - // Read cell info - auto& info = info_r[acc_id]; - - switch (info({0u}).get()) { - case 1u: { - // bb.apply(latt_acc, {i, j}, time_step); - - auto& dfs_old = is_even ? dfs_r_old[acc_id] : dfs_r[acc_id]; - auto df_cpy = dfs_old.copy(); - - for(uint64_t i = 1u; i < Desc::Q; ++i){ - dfs_old({i}) = df_cpy({dfi::opposite_index.at(i)}); + component<T,Desc,cmpt::ZouHeHorizontal<true>,encode::Sycl<kel::lbm::enc::Native>> flow_in{ + [&](){ + uint64_t target_t_i = 32u; + if(t_i.get() < target_t_i){ + return 1.0 + (0.0002 / target_t_i) * t_i.get(); } + return 1.0002; + }() + }; + component<T,Desc,cmpt::ZouHeHorizontal<false>,encode::Sycl<kel::lbm::enc::Native>> flow_out{1.0}; - break; - } - case 2u: { - // coll.apply(latt_acc, {i, j}, time_step); - auto& dfs_old = is_even ? dfs_r_old[acc_id] : dfs_r[acc_id]; - kel::lbm::data<T> rho; - kel::lbm::data<sch::FixedArray<T,Desc::D>> vel; - compute_rho_u<T,Desc>(dfs_old,rho,vel); - auto eq = equilibrium<T,Desc>(rho,vel); + h.parallel_for(acpp::sycl::range<Desc::D>{dim_x,dim_y}, [=](acpp::sycl::id<Desc::D> idx){ + kel::lbm::data<sch::FixedArray<sch::UInt64,Desc::D>> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } - for(uint64_t i = 0u; i < Desc::Q; ++i){ - dfs_old({i}) = dfs_old({i}) + frequency * (eq.at(i) - dfs_old({i})); - } + auto info = info_f.at(index); + + switch(info.get()){ + case 0u: break; - } - default: - // Do nothing + case 1u: + bb.apply(fields,index,t_i); break; - } - }); - }); - - // Submit streaming kernel - sycl_q.submit([&](sycl::handler& cgh) { - - cgh.parallel_for( - sycl::range<2>{dim_x - 2, dim_y - 2}, [=](sycl::id<2> idx) { - size_t i = idx[0] + 1; // avoid boundary - size_t j = idx[1] + 1; - - size_t acc_id = i * dim_x + j; - - auto& dfs_new = is_even ? dfs_r[acc_id] : dfs_r_old[acc_id]; - auto& info = info_r[acc_id]; + case 2u: + collision.apply(fields,macros,index,t_i); + break; + case 5u: { + /** + * Moving wall: bounce back plus the tangential momentum the + * lid injects. + * + * f_i = f_opp + 2 w_i rho_w (e_i . u_w) / cs^2 + * + * written from f_opp because that is the direction the + * library's BounceBack writes in. There is no component for a + * y normal moving wall, and the original example had this + * commented out, so it is implemented here. + */ + using dfi = df_info<T,Desc>; + const bool is_even = ((t_i.get() % 2u) == 0u); + auto& dfs_f = (is_even) + ? fields.template get<"dfs_old">() + : fields.template get<"dfs">(); + auto& dfs = dfs_f.at(index); + auto df_cpy = dfs; - if (info({0u}).get() > 0u && info({0u}).get() != 3u) { - for (uint64_t k = 0u; k < Desc::Q; ++k) { - auto dir = dfi::directions[dfi::opposite_index[k]]; - // auto& cell_dir_old = latt_acc({i + dir[0], j + dir[1]}); - // - size_t acc_old_id = (i+dir[0]) * dim_x + (j+dir[1]); + for(uint64_t i = 0u; i < Desc::Q; ++i){ + const auto e_dot_u = + static_cast<typename kel::lbm::native_data_type<T>::type>( + dfi::directions[i][0]) * lid_velocity; - auto& dfs_old = is_even ? dfs_r_old[acc_old_id] : dfs_r[acc_old_id]; - auto& info_old = info_r[acc_old_id]; - - if (info_old({0}).get() == 3u) { - auto& dfs_old_loc = is_even ? dfs_r_old[acc_id] : dfs_r[acc_id]; - dfs_new({k}) = dfs_old_loc({dfi::opposite_index.at(k)}) - 2.0 * dfi::inv_cs2 * dfi::weights.at(k) * 1.0 * (bb_lid.lid_vel[0] * dir[0] + bb_lid.lid_vel[1] * dir[1]); - } else { - dfs_new({k}) = dfs_old({k}); + dfs.at({i}) = df_cpy.at({dfi::opposite_index.at(i)}) + + kel::lbm::data<T>{ + static_cast<typename kel::lbm::native_data_type<T>::type>( + 2.0 * dfi::weights[i] * dfi::inv_cs2 * e_dot_u)}; } - } + } break; + default: + break; } }); - }); + }).wait(); + + + // Step + /* + q.submit([&](acpp::sycl::handler& h){ + // h.depends_on(collision_ev); + }).wait(); + */ + + return kel::lbm::make_void(); } } } -int main(){ +template<typename T, typename Desc> +kel::lbm::error_or<void> lbm_main(int argc, char** argv){ using namespace kel::lbm; - using namespace acpp; - kel::lbm::data<sch::FixedArray<sch::UInt64,sch::D2Q9::D>> meta{{dim_x, dim_y}}; - constexpr size_t dim_size = dim_x * dim_y; - - converter<sch::T> conv{ - {0.1}, - {0.1} - }; + /* + auto eo_lbm_args = setup_lbm_env<sch::Args<sch::Struct<>,sch::Tuple<>>>(argc,argv); + if(eo_lbm_args.is_error()){ + return std::move(eo_lbm_args.get_error()); + } + auto& lbm_args = eo_lbm_args.get_value(); + */ - // Lid driven cavity: the lid velocity and the cavity height are the - // characteristic scales. - print_lbm_meta<sch::T, sch::D2Q9>(conv, {1e-3}, {0.1}, {dim_y}); + using dfi = df_info<T,Desc>; auto eo_lbm_dir = output_directory(); if(eo_lbm_dir.is_error()){ - return -1; + return std::move(eo_lbm_dir.get_error()); } auto& lbm_dir = eo_lbm_dir.get_value(); - auto out_dir = lbm_dir / "cavity_gpu_2d"; - sycl::queue sycl_q{sycl::default_selector_v, sycl::property::queue::in_order{}}; + auto out_dir = lbm_dir / "cavity_2d_gpu"; - constexpr size_t num_cells = dim_x * dim_y; + { + std::error_code ec; + std::filesystem::create_directories(out_dir,ec); + if(ec != std::errc{}){ + return kel::lbm::make_error<kel::lbm::err::critical>("Could not create output directory"); + } + } - // Allocate USM shared memory for your main data - auto* info = sycl::malloc_shared<kel::lbm::data<sch::CellInfo<sch::D2Q9>>>(num_cells, sycl_q); - auto* dfs = sycl::malloc_shared<kel::lbm::data<sch::DfCell<sch::D2Q9>>>(num_cells, sycl_q); - auto* dfs_old = sycl::malloc_shared<kel::lbm::data<sch::DfCell<sch::D2Q9>>>(num_cells, sycl_q); + converter<T> conv { + // delta_x + {1.0}, + // delta_t + {1.0} + }; + + print_lbm_meta<T,Desc>(conv,{0.1},{1e-4},{0.4 * dim_y}); - /** - * Set meta information describing what this cell is - */ - set_geometry(info); + // kel::lbm::data<sch::FixedArray<sch::UInt64,Desc::D>> meta{{dim_x,dim_y}}; + auto lbm_data_ptr = kel::lbm::heap<kel::lbm::data<sch::ChunkStruct<T,Desc>>>(); + auto lbm_macro_data_ptr = kel::lbm::heap<kel::lbm::data<sch::MacroStruct<T,Desc>>>(); + + std::cout<<"Estimated Bytes: "<<memory_estimate<sch::ChunkStruct<T,Desc>,sch::MacroStruct<T,Desc>>().get()<<std::endl; - /** - * - */ - set_initial_conditions(info,dfs,dfs_old); + auto eo_aio = saw::setup_async_io(); + if(eo_aio.is_error()){ + return kel::lbm::make_error<kel::lbm::err::critical>( + std::string{eo_aio.get_error().get_message()}); + } + auto& aio = eo_aio.get_value(); + saw::wait_scope wait{aio.event_loop}; - /** - * Timeloop - */ - uint64_t lattice_steps = 512000u; - bool even_step = true; + bool krun = true; + bool print_status = false; + aio.event_port.on_signal(saw::Signal::Terminate).then([&](){ + krun = false; + }).detach(); + aio.event_port.on_signal(saw::Signal::User1).then([&](){ + print_status = true; + }).detach(); - uint64_t print_every = 256u; - uint64_t file_no = 0u; + device dev; - kel::lbm::data<sch::Array<sch::MacroStruct<sch::T,sch::D2Q9::D>,sch::D2Q9::D>> macros{meta}; + auto& sycl_q = dev.get_handle(); - std::cout<<"Start"<<std::endl; + sycl_q.wait(); + { + auto eov = setup_initial_conditions<T,Desc>(*lbm_data_ptr,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_vtk_file(out_dir,"initial_state",0u,*lbm_data_ptr); + if(eov.is_error()){ + return eov; + } + } - auto start = std::chrono::steady_clock::now(); + kel::lbm::data<sch::ChunkStruct<T,Desc>, encode::Sycl<kel::lbm::enc::Native>> lbm_sycl_data{sycl_q}; + kel::lbm::data<sch::MacroStruct<T,Desc>, encode::Sycl<kel::lbm::enc::Native>> lbm_sycl_macro_data{sycl_q}; + sycl_q.wait(); + + { + auto eov = dev.copy_to_device(*lbm_data_ptr,lbm_sycl_data); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_device(*lbm_macro_data_ptr,lbm_sycl_macro_data); + if(eov.is_error()){ + return eov; + } + } sycl_q.wait(); - for(uint64_t i = 0u; i < lattice_steps; ++i){ - lbm_step<sch::T,sch::D2Q9>(info, dfs, dfs_old, even_step, i, sycl_q); - if(i % 1024u == 0u){ - sycl_q.wait(); - iterate_over([&](const kel::lbm::data<sch::FixedArray<sch::UInt64,sch::D2Q9::D>>& index){ - size_t j = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto dfs_field = even_step ? dfs_old : dfs; + auto lsd_view = make_view(lbm_sycl_data); + auto lsdm_view = make_view(lbm_sycl_macro_data); + kel::lbm::data<sch::UInt64> time_steps{16u*4096ul}; + + auto& info_f = lsd_view.template get<"info">(); + + for(kel::lbm::data<sch::UInt64> i{0u}; i < time_steps and krun; ++i){ + // BC + Collision + { + auto eov = step<T,Desc>(lsd_view,lsdm_view,i,dev); + if(eov.is_error()){ + return eov; + } + } + sycl_q.wait(); + /* + if(i.get() % 128u == 0u){ + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_vtk_file(out_dir,"m",i.get(), *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + } + */ + /* + if(i.get() % 32u == 0u){ + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_csv_file(out_dir,"m",i.get(), *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + } + */ + // Stream + sycl_q.submit([&](acpp::sycl::handler& h){ + component<T,Desc,cmpt::Stream,encode::Sycl<kel::lbm::enc::Native>> stream; - auto& rho = macros.at(index).template get<"pressure">(); - auto& vel = macros.at(index).template get<"velocity">(); - compute_rho_u<sch::T,sch::D2Q9>(dfs_field[j],rho,vel); - }, {{0u,0u}}, meta); - std::string vtk_f_name{"tmp/cavity_2d_gpu_"}; - vtk_f_name += std::to_string(i) + ".vtk"; - write_vtk_file(vtk_f_name, macros); + h.parallel_for(acpp::sycl::range<Desc::D>{dim_x,dim_y}, [=](acpp::sycl::id<Desc::D> idx){ + kel::lbm::data<sch::FixedArray<sch::UInt64,Desc::D>> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto info = info_f.at(index); + + if(info.get() > 0u){ + stream.apply(lsd_view,index,i); + } + }); + }).wait(); + wait.poll(); + if(print_status){ + std::cout<<"Status: "<<i.get()<<" of "<<time_steps.get()<<" - "<<(i.template cast_to<sch::Float64>().get() * 100 / time_steps.get())<<"%"<<std::endl; + print_status = false; } + print_progress_bar(i.get(), time_steps.get()-1u); + } - even_step = not even_step; + // After Loop + sycl_q.wait(); + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_vtk_file(out_dir,"m",time_steps.get(), *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_csv_file(out_dir,"m",time_steps.get(), *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } } - auto stop = std::chrono::steady_clock::now(); - std::cout<<std::format("{:%H:%M:%S}",(stop-start))<<std::endl; sycl_q.wait(); - sycl::free(info,sycl_q); - sycl::free(dfs,sycl_q); - sycl::free(dfs_old,sycl_q); + return kel::lbm::make_void(); +} + +using FloatT = kel::lbm::sch::Float32; + +int main(int argc, char** argv){ + auto eov = lbm_main<FloatT,kel::lbm::sch::D2Q9>(argc, argv); + if(eov.is_error()){ + auto& err = eov.get_error(); + std::cerr<<"[Error] "<<err.get_category(); + auto err_msg = err.get_message(); + if(err_msg.size() > 0u){ + std::cerr<<" - "<<err_msg; + } + std::cerr<<std::endl; + return err.get_id(); + } return 0; -}_ +} |
