From b80856491cd223149ed024520689a940d50b45cf Mon Sep 17 00:00:00 2001 From: Samuel Ayala Date: Sat, 8 Jun 2024 22:25:54 -0400 Subject: [PATCH] wip better structs --- vlm/backends/cpu/include/vlm_backend_cpu.hpp | 2 +- vlm/include/vlm_allocator.hpp | 20 +++++++++--- vlm/include/vlm_backend.hpp | 26 ++++++++++----- vlm/include/vlm_fwd.hpp | 1 + vlm/include/vlm_mesh.hpp | 19 +++-------- vlm/src/vlm_backend.cpp | 33 ++++++++++++++++++++ vlm/src/vlm_mesh.cpp | 27 ++++++++++------ 7 files changed, 89 insertions(+), 39 deletions(-) diff --git a/vlm/backends/cpu/include/vlm_backend_cpu.hpp b/vlm/backends/cpu/include/vlm_backend_cpu.hpp index de03732..b66435a 100644 --- a/vlm/backends/cpu/include/vlm_backend_cpu.hpp +++ b/vlm/backends/cpu/include/vlm_backend_cpu.hpp @@ -7,7 +7,7 @@ namespace vlm { class BackendCPU : public Backend { public: - BackendCPU(const MeshGeom* mesh_geom, int timesteps); + BackendCPU(); ~BackendCPU(); void reset() override; void lhs_assemble() override; diff --git a/vlm/include/vlm_allocator.hpp b/vlm/include/vlm_allocator.hpp index be96ce1..ae72014 100644 --- a/vlm/include/vlm_allocator.hpp +++ b/vlm/include/vlm_allocator.hpp @@ -2,12 +2,22 @@ namespace vlm { -struct Allocator { - Allocator() = default; - virtual ~Allocator() = default; +typedef void* (*malloc_f)(unsigned long long size); +typedef void (*free_f)(void* ptr); +typedef void* (*memcpy_f)(void* dst, const void* src, unsigned long long size); +typedef void* (*memset_f)(void* dst, int value, unsigned long long size); - virtual void* malloc(unsigned long long size) const = 0; - virtual void free(void* ptr) const = 0; +struct Allocator { + malloc_f h_malloc; + malloc_f d_malloc; + free_f h_free; + free_f d_free; + memcpy_f hh_memcpy; + memcpy_f hd_memcpy; + memcpy_f dh_memcpy; + memcpy_f dd_memcpy; + memset_f h_memset; + memset_f d_memset; }; } // namespace vlm \ No newline at end of file diff --git a/vlm/include/vlm_backend.hpp b/vlm/include/vlm_backend.hpp index 19faebc..148316b 100644 --- a/vlm/include/vlm_backend.hpp +++ b/vlm/include/vlm_backend.hpp @@ -4,20 +4,30 @@ #include "vlm_types.hpp" #include "vlm_mesh.hpp" #include "vlm_data.hpp" +#include "vlm_allocator.hpp" namespace vlm { class Backend { public: - Allocator* h_allocator = nullptr; - Allocator* d_allocator = nullptr; - MeshProxy* h_mesh; - MeshProxy* d_mesh; - Data* h_data; // host (CPU) data - Data* d_data; // device (accelerator) data - f32 sigma_vatistas = 0.0f; + Allocator allocator; + + // Constant inital position meshes (for kinematics) + MeshGeom* hh_mesh_geom; // borrowed ptr + MeshGeom* hd_mesh_geom; + MeshGeom* dd_mesh_geom; + + // Mutable meshes (temporal state) + Mesh2* hh_mesh; // host ptr to host buffers for io + Mesh2* hd_mesh; // host ptr to device buffers + Mesh2* dd_mesh; // device ptr to device buffers for kernels - Backend(const MeshGeom* mesh_geom, int timesteps) {}; + Data* hd_data; + Data* dd_data; + + f32 sigma_vatistas = 0.0f; + Backend(MeshGeom* mesh_geom); + void init(u64 timesteps); virtual void reset() = 0; virtual void lhs_assemble() = 0; virtual void compute_rhs() = 0; diff --git a/vlm/include/vlm_fwd.hpp b/vlm/include/vlm_fwd.hpp index 4b3792b..4d1ea6a 100644 --- a/vlm/include/vlm_fwd.hpp +++ b/vlm/include/vlm_fwd.hpp @@ -7,6 +7,7 @@ struct MeshView; struct MeshGeom; struct Data; struct Allocator; +struct Memory; class Mesh; class Backend; template class Database2D; diff --git a/vlm/include/vlm_mesh.hpp b/vlm/include/vlm_mesh.hpp index 727be2b..b996911 100644 --- a/vlm/include/vlm_mesh.hpp +++ b/vlm/include/vlm_mesh.hpp @@ -9,7 +9,7 @@ namespace vlm { // C interface -struct MeshProxy { +struct Mesh2 { f32* vertices = nullptr; // (nc+nw+1)*(ns+1)*3 // TODO: evaluate if we really need those values or we can just compute on the fly in the kernels f32* normals = nullptr; // nc*ns*3 @@ -36,21 +36,10 @@ struct MeshGeom { u64 ns = 0; }; -struct System { - MeshProxy* meshes = nullptr; // meshes - // Data data; // unified (linearized) data for the whole system - u32 n_meshes = 0; -}; - void mesh_read_file(const std::string& filename, MeshGeom* mesh_geom); -void mesh_alloc(const Allocator* allocator, MeshProxy* mesh, u64 nc, u64 ns, u64 nw, u64 nwa); -void mesh_free(const Allocator* allocator, MeshProxy* mesh); - -struct AllocatorCPU : Allocator { - void* malloc(u64 size) const override { return std::malloc(size); } - void free(void* ptr) const override { std::free(ptr); } -}; - +void mesh_alloc(const malloc_f malloc, Mesh2* mesh, u64 nc, u64 ns, u64 nw, u64 nwa); +void mesh_free(const free_f free, Mesh2* mesh); +void mesh_copy(const memcpy_f memcpy, Mesh2* dst, const Mesh2* src); // === STRUCTURED MESH === // nc : number of panels chordwise diff --git a/vlm/src/vlm_backend.cpp b/vlm/src/vlm_backend.cpp index 7407755..033df5c 100644 --- a/vlm/src/vlm_backend.cpp +++ b/vlm/src/vlm_backend.cpp @@ -51,4 +51,37 @@ std::vector vlm::get_available_backends() { backends.push_back("cuda"); #endif return backends; +} + +Backend::Backend(MeshGeom* mesh_geom) : hh_mesh_geom(mesh_geom) {}; + +void Backend::init(u64 timesteps) { + u64 nb_vertices_wing = (hh_mesh_geom->nc+1)*(hh_mesh_geom->ns+1); + + // Mesh structs allocation + hd_mesh_geom = (MeshGeom*)allocator.h_malloc(sizeof(MeshGeom)); + dd_mesh_geom = (MeshGeom*)allocator.d_malloc(sizeof(MeshGeom)); + hh_mesh = (Mesh2*)allocator.h_malloc(sizeof(Mesh2)); + hd_mesh = (Mesh2*)allocator.h_malloc(sizeof(Mesh2)); + dd_mesh = (Mesh2*)allocator.d_malloc(sizeof(Mesh2)); + + // Allocate mesh buffers on device and host + hd_mesh_geom->vertices = (f32*) allocator.d_malloc(nb_vertices_wing*3*sizeof(f32)); + // mesh_alloc(allocator.h_malloc, hh_mesh, hh_mesh_geom->nc, hh_mesh_geom->ns, timesteps, 0); + mesh_alloc(allocator.d_malloc, hd_mesh, hh_mesh_geom->nc, hh_mesh_geom->ns, timesteps, 0); + + // Copy indices + hd_mesh_geom->nc = hh_mesh_geom->nc; + hd_mesh_geom->ns = hh_mesh_geom->ns; + hd_mesh->nc = hh_mesh_geom->nc; + hd_mesh->ns = hh_mesh_geom->ns; + hd_mesh->nw = timesteps; + + // Copy raw vertex geometry directly to device + allocator.hd_memcpy(hd_mesh_geom->vertices, hh_mesh_geom->vertices, nb_vertices_wing*3*sizeof(f32)); + allocator.hd_memcpy(hd_mesh->vertices, hh_mesh_geom->vertices, nb_vertices_wing*3*sizeof(f32)); + + // Copy host-device mesh ptr to device-device + allocator.hd_memcpy(dd_mesh_geom, hd_mesh_geom, sizeof(*hd_mesh_geom)); + allocator.hd_memcpy(dd_mesh, hd_mesh, sizeof(*hd_mesh)); } \ No newline at end of file diff --git a/vlm/src/vlm_mesh.cpp b/vlm/src/vlm_mesh.cpp index b59dd32..9d44386 100644 --- a/vlm/src/vlm_mesh.cpp +++ b/vlm/src/vlm_mesh.cpp @@ -14,22 +14,29 @@ using namespace vlm; constexpr f32 EPS = std::numeric_limits::epsilon(); -void vlm::mesh_alloc(const Allocator* allocator, MeshProxy* mesh, u64 nc, u64 ns, u64 nw, u64 nwa) { - mesh->vertices = (f32*)allocator->malloc((nc+nw+1)*(ns+1)*3*sizeof(f32)); - mesh->normals = (f32*)allocator->malloc(nc*ns*3*sizeof(f32)); - mesh->colloc = (f32*)allocator->malloc(nc*ns*3*sizeof(f32)); - mesh->area = (f32*)allocator->malloc(nc*ns*sizeof(f32)); +void vlm::mesh_alloc(const malloc_f malloc, Mesh2* mesh, u64 nc, u64 ns, u64 nw, u64 nwa) { + mesh->vertices = (f32*)malloc((nc+nw+1)*(ns+1)*3*sizeof(f32)); + mesh->normals = (f32*)malloc(nc*ns*3*sizeof(f32)); + mesh->colloc = (f32*)malloc(nc*ns*3*sizeof(f32)); + mesh->area = (f32*)malloc(nc*ns*sizeof(f32)); mesh->ns = nc; mesh->ns = ns; mesh->nw = nw; mesh->nwa = nwa; } -void vlm::mesh_free(const Allocator* allocator, MeshProxy* mesh) { - allocator->free(mesh->vertices); - allocator->free(mesh->normals); - allocator->free(mesh->colloc); - allocator->free(mesh->area); +void vlm::mesh_copy(const memcpy_f memcpy, Mesh2* dst, const Mesh2* src) { + memcpy(dst->vertices, src->vertices, (dst->nc+dst->nw+1)*(dst->ns+1)*3*sizeof(f32)); + memcpy(dst->normals, src->normals, dst->nc*dst->ns*3*sizeof(f32)); + memcpy(dst->colloc, src->colloc, dst->nc*dst->ns*3*sizeof(f32)); + memcpy(dst->area, src->area, dst->nc*dst->ns*sizeof(f32)); +} + +void vlm::mesh_free(const free_f free, Mesh2* mesh) { + free(mesh->vertices); + free(mesh->normals); + free(mesh->colloc); + free(mesh->area); } // Reads the Plot3D Structured file, allocates vertex buffer and fills it with the file data