Skip to content

Commit

Permalink
wip better structs
Browse files Browse the repository at this point in the history
  • Loading branch information
samayala22 committed Jun 9, 2024
1 parent aac719b commit b808564
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 39 deletions.
2 changes: 1 addition & 1 deletion vlm/backends/cpu/include/vlm_backend_cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 15 additions & 5 deletions vlm/include/vlm_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 18 additions & 8 deletions vlm/include/vlm_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions vlm/include/vlm_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct MeshView;
struct MeshGeom;
struct Data;
struct Allocator;
struct Memory;
class Mesh;
class Backend;
template<typename Interpolator> class Database2D;
Expand Down
19 changes: 4 additions & 15 deletions vlm/include/vlm_mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions vlm/src/vlm_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,37 @@ std::vector<std::string> 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));
}
27 changes: 17 additions & 10 deletions vlm/src/vlm_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@ using namespace vlm;

constexpr f32 EPS = std::numeric_limits<f32>::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
Expand Down

0 comments on commit b808564

Please sign in to comment.