From 0393941064f6c3789dd62a73d0e6112a585bcdc0 Mon Sep 17 00:00:00 2001 From: Lizzzka007 <gashchuk2011@mail.ru> Date: Wed, 29 Nov 2023 22:23:29 +0300 Subject: [PATCH] some bugs fixed --- main.cpp | 149 +++++++++++++++++++++++++++++++++++++++--- mesh/include/mesh.hpp | 3 +- mesh/src/matvec.cpp | 8 ++- mesh/src/mesh.cpp | 16 ++--- 4 files changed, 150 insertions(+), 26 deletions(-) diff --git a/main.cpp b/main.cpp index 29ca3fa..a87c0a5 100644 --- a/main.cpp +++ b/main.cpp @@ -1,25 +1,156 @@ #include "mesh.hpp" #include "TemplateParameters.h" +#include "MemoryProcessing.h" + +#ifdef INCLUDE_CUDA + #include "MemoryProcessing.cuh" +#endif + using namespace icethermo; int main(void) { - Mesh<float, MemType::GPU> mesh1(10, 1.0f); + // Mesh<float, MemType::CPU> mesh1(10, 1.0f); + + // auto cells_temp = mesh1.CreateCellsData("cells_temperature"); + // auto cells_capacity = mesh1.CreateCellsData("cells_capacity", true); + // auto cells_enthalpy = mesh1.CreateCellsData("cells_enthalpy", false); + // auto cells_thickness = mesh1.GetCellsThickness(); + + // const int n = mesh1.GetCellsNum(); + // mesh1.PullCPUArray(*cells_thickness.get(), n); + + // for (int i = 0; i < n; i++) + // { + // printf("%f\n", mesh1.SubBuffer[i]); + // } + + // mesh1.SaveTXT("./mesh1"); + + // auto res = std::accumulate(mesh1.SubBuffer, mesh1.SubBuffer + n, float(0)); + // printf("accumulate %f\n", res); + + // ### examples of Mesh class ### + // constructor with given total thickness (default is 10 cells) +#ifdef INCLUDE_CUDA + Mesh<float, MemType::GPU> mesh1(1.0f); +#else + Mesh<float, MemType::CPU> mesh1(1.0f); +#endif + + // how to create cells data (lhs is shared ptr to vector) auto cells_temp = mesh1.CreateCellsData("cells_temperature"); auto cells_capacity = mesh1.CreateCellsData("cells_capacity", true); auto cells_enthalpy = mesh1.CreateCellsData("cells_enthalpy", false); - auto cells_thickness = mesh1.GetCellsThickness(); - const int n = mesh1.GetCellsNum(); - mesh1.PullCPUArray(*cells_thickness.get(), n); + // one can modify cell data + +#ifdef INCLUDE_CUDA + float* farray; + size_t farray_size = 0; + memproc::realloc<MemType::CPU>((void *&)(farray), farray_size, mesh1.GetCellsNum() * sizeof(float)); + farray[0] = 1.0f; farray[1] = 2.0f; farray[2] = 3.0f; + memproc::memcopy<MemType::GPU, MemType::CPU>((*cells_temp), farray, farray_size); +#else + (*cells_temp)[0] = 1.0f; (*cells_temp)[1] = 2.0f; (*cells_temp)[2] = 3.0f; +#endif + + // how to create nodes data (lhs is shared ptr to vector) + auto nodes_k = mesh1.CreateNodesData("nodes_k"); + auto nodes_enthalpy = mesh1.CreateNodesData("nodes_enthalpy", false); + + // one can modify nodes data +#ifdef INCLUDE_CUDA + memproc::realloc<MemType::CPU>((void *&)(farray), farray_size, mesh1.GetNodesNum() * sizeof(float)); + farray[0] = -5.0f; farray[mesh1.GetNodesNum() - 1] = -3.0f; + memproc::memcopy<MemType::GPU, MemType::CPU>((*nodes_k), farray, farray_size); +#else + (*nodes_k)[0] = -5.0f; (*nodes_k)[mesh1.GetNodesNum() - 1] = -3.0f; +#endif + + // how to create single data + auto temp_ib = mesh1.CreateSingleData("temp_ib"); + auto temp_is = mesh1.CreateSingleData("temp_is"); + + // one can modify single data + (*temp_ib) = -1.0f; (*temp_is) = 2.0f; + + // one can delete cells, nodes or single data + mesh1.DeleteCellsData("cells_enthalpy"); + mesh1.DeleteNodesData("nodes_enthalpy"); + mesh1.DeleteSingleData("temp_is"); + + // its is better to avoid this, but one can get another pointer to created data + + auto another_cells_temp = mesh1.GetCellsData("cells_temperature"); + auto another_nodes_k = mesh1.GetNodesData("nodes_k"); + auto another_temp_ib = mesh1.GetSingleData("temp_ib"); + +#ifdef INCLUDE_CUDA + memproc::realloc<MemType::CPU>((void *&)(farray), farray_size, mesh1.GetCellsNum() * sizeof(float)); + farray[0] = -5.0f; + memproc::memcopy<MemType::GPU, MemType::CPU>((*another_cells_temp), farray, farray_size); + + memproc::realloc<MemType::CPU>((void *&)(farray), farray_size, mesh1.GetNodesNum() * sizeof(float)); + farray[0] *= 2.0f; + memproc::memcopy<MemType::GPU, MemType::CPU>((*another_nodes_k), farray, farray_size); +#else + (*another_cells_temp)[0] = -5.0f; + (*another_nodes_k)[0] *= 2.0f; + (*another_temp_ib) = -30.0f; +#endif + + // one can get vector with cell thickness + // std::cout << "current cell thickness array: " << (*mesh1.GetCellsThickness()) << std::endl; + + // one can get total thickness + std::cout << "current total cell thickness: " << mesh1.GetTotalThickness() << std::endl; + + // one could manually mute and unmute variables (muted variables will not be writed to the output) + mesh1.MuteCellData("cells_temperature"); + mesh1.UnmuteCellData("cells_temperature"); + + mesh1.MuteNodeData("nodes_enthalpy"); + mesh1.UnmuteNodeData("nodes_enthalpy"); - for (int i = 0; i < n; i++) - { - printf("%f\n", mesh1.SubBuffer[i]); - } + // one can save mesh to .txt file + mesh1.SaveTXT("./mesh"); + + // one can save mesh to .txt file with postfix number (relevant for time series) + mesh1.SaveTXT("./mesh", 1488); + + // ### examples of another Mesh class constructor ### + + // construct uniform mesh with given cells num and total thickness +#ifdef INCLUDE_CUDA + Mesh<double, MemType::GPU> mesh2(15, 1.0); +#else + Mesh<double, MemType::CPU> mesh2(15, 1.0); +#endif + mesh2.SaveTXT("./mesh2"); + + // construct arbitrary mesh with given unit segment partition and total thickness +#ifdef INCLUDE_CUDA + double dsegment_partition[2] = {0.5, 0.5}; + double *dev_segment_partition; + size_t dev_segment_partition_size = 0; + + memproc::realloc<MemType::GPU>((void *&)(dev_segment_partition), dev_segment_partition_size, 2 * sizeof(double)); + memproc::memcopy<MemType::GPU, MemType::CPU>(dev_segment_partition, dsegment_partition, dev_segment_partition_size); + + Mesh<double, MemType::GPU> mesh3(dev_segment_partition, 2, 5.0); +#else + double dsegment_partition[2] = {0.5, 0.5}; + Mesh<double, MemType::CPU> mesh3(dsegment_partition, 2, 5.0); +#endif + + mesh3.SaveTXT("./mesh3"); - mesh1.SaveTXT("./mesh1"); +#ifdef INCLUDE_CUDA + memproc::dealloc<MemType::GPU>((void *&)(farray)); + memproc::dealloc<MemType::GPU>((void *&)(dev_segment_partition)); +#endif return 0; } \ No newline at end of file diff --git a/mesh/include/mesh.hpp b/mesh/include/mesh.hpp index 3bef1cf..c8eb45d 100644 --- a/mesh/include/mesh.hpp +++ b/mesh/include/mesh.hpp @@ -96,8 +96,7 @@ namespace icethermo std::map<std::string, std::pair<std::shared_ptr<NumType*>, bool>> nodes_data; std::map<std::string, size_t> nodes_data_size_t; - // Check GPU results subtools - public: + // sub-bufer for data output (needs if data on the GPU) NumType* SubBuffer; size_t SubBuffer_size; }; diff --git a/mesh/src/matvec.cpp b/mesh/src/matvec.cpp index c7d10c8..66a9681 100644 --- a/mesh/src/matvec.cpp +++ b/mesh/src/matvec.cpp @@ -20,11 +20,12 @@ namespace icethermo size_t cpu_holder_size = 0; memproc::realloc<MemType::CPU>((void *&)(cpu_holder), cpu_holder_size, size * sizeof(NumType)); - memproc::memcopy<MemType::CPU, MemType::CPU>(cpu_holder, vec, cpu_holder_size); - + memproc::memcopy<MemType::CPU, MemType::GPU>(cpu_holder, vec, cpu_holder_size); for (int i = 0; i < size; i++) + { accumulate_res += cpu_holder[i]; + } memproc::dealloc<MemType::CPU>((void *&)(cpu_holder)); @@ -50,7 +51,7 @@ namespace icethermo const int size = end - start; memproc::realloc<MemType::CPU>((void *&)(cpu_holder), cpu_holder_size, size * sizeof(NumType)); - memproc::memcopy<MemType::CPU, MemType::CPU>(cpu_holder, vec + start, cpu_holder_size); + memproc::memcopy<MemType::CPU, MemType::GPU>(cpu_holder, vec + start, cpu_holder_size); for (int i = start; i < end; i++) accumulate_res += cpu_holder[i]; @@ -99,6 +100,7 @@ namespace icethermo if(memtype == MemType::GPU) { icethermo_gpu::mul_vec(vec, num, size); + return; } #endif diff --git a/mesh/src/mesh.cpp b/mesh/src/mesh.cpp index 6bf847f..fd1788b 100644 --- a/mesh/src/mesh.cpp +++ b/mesh/src/mesh.cpp @@ -61,14 +61,10 @@ namespace icethermo Mesh<NumType, memtype>::Mesh(const NumType* unit_segment_decomposition, const int unit_segment_decomposition_size, NumType thickness) { NumType sum_decomp = sum_vec<NumType, memtype>(unit_segment_decomposition, unit_segment_decomposition_size); - if (std::abs(sum_decomp - 1.0) > 1e-5) { THERMO_ERR("Unit segment decomposition of length 1.0 should be given!"); } - - // NumType* thicknesses = unit_segment_decomposition*thickness; - // cells_thickness = std::make_shared<NumType*>(thicknesses); NumType* thicknesses; cells_thickness_size_t = 0; @@ -77,6 +73,9 @@ namespace icethermo memproc::memcopy<memtype, memtype>(thicknesses, unit_segment_decomposition, cells_thickness_size_t); mul_vec<NumType, memtype>(thicknesses, thickness, unit_segment_decomposition_size); cells_thickness = std::make_shared<NumType*>(std::move(thicknesses)); + + SubBuffer = nullptr; + SubBuffer_size = 0; } template <typename NumType, MemType memtype> @@ -136,13 +135,6 @@ namespace icethermo Mesh<NumType, memtype>::~Mesh() { memproc::dealloc<memtype>((void*&)(*cells_thickness.get()), cells_thickness_size_t); - - //for (auto item: this->single_data) - //{ - // auto value = item.second; - - // delete (value.first).get(); - //} for (auto item: this->cells_data) { @@ -173,7 +165,6 @@ namespace icethermo int Mesh<NumType, memtype>::GetNodesNum() const { return int(cells_thickness_size_t / sizeof(NumType)) + 1; - // return cells_thickness->size() + 1; } template <typename NumType, MemType memtype> @@ -374,6 +365,7 @@ namespace icethermo int count = cells_thickness_size_t / sizeof(NumType); PullCPUArray(*cells_thickness.get(), count); *ofs << "### cells_thickness_array ###\n"; + for (int i = 0; i < count; i++) { if (i != count - 1) -- GitLab