From 9ad0552b7b00ef5461ce7a72cec8ea1a95b537cb Mon Sep 17 00:00:00 2001 From: Lizzzka007 <gashchuk2011@mail.ru> Date: Wed, 29 Nov 2023 23:57:30 +0300 Subject: [PATCH] . --- CMakeLists.txt | 2 -- main.cpp | 74 +++++++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 45b0fc1..bf8cd89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,6 @@ if(INCLUDE_CUDA) endif(INCLUDE_CUDA) add_subdirectory(mesh/) -# set_property(DIRECTORY mesh/ APPEND PROPERTY COMPILE_DEFINITIONS "-DINCLUDE_CUDA=${INCLUDE_CUDA}") -# target_compile_definitions(mesh PRIVATE INCLUDE_CUDA=${INCLUDE_CUDA}) set(SOURCE main.cpp) add_executable(mesh_test ${SOURCE}) diff --git a/main.cpp b/main.cpp index a87c0a5..303cf69 100644 --- a/main.cpp +++ b/main.cpp @@ -10,29 +10,6 @@ using namespace icethermo; int main(void) { - // 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 @@ -45,11 +22,11 @@ int main(void) auto cells_enthalpy = mesh1.CreateCellsData("cells_enthalpy", false); // 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)); + memproc::memcopy<MemType::CPU, MemType::GPU>(farray, (*cells_temp), farray_size); farray[0] = 1.0f; farray[1] = 2.0f; farray[2] = 3.0f; memproc::memcopy<MemType::GPU, MemType::CPU>((*cells_temp), farray, farray_size); #else @@ -63,6 +40,7 @@ int main(void) // one can modify nodes data #ifdef INCLUDE_CUDA memproc::realloc<MemType::CPU>((void *&)(farray), farray_size, mesh1.GetNodesNum() * sizeof(float)); + memproc::memcopy<MemType::CPU, MemType::GPU>(farray, (*nodes_k), farray_size); farray[0] = -5.0f; farray[mesh1.GetNodesNum() - 1] = -3.0f; memproc::memcopy<MemType::GPU, MemType::CPU>((*nodes_k), farray, farray_size); #else @@ -89,10 +67,12 @@ int main(void) #ifdef INCLUDE_CUDA memproc::realloc<MemType::CPU>((void *&)(farray), farray_size, mesh1.GetCellsNum() * sizeof(float)); + memproc::memcopy<MemType::CPU, MemType::GPU>(farray, (*another_cells_temp), farray_size); 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)); + memproc::memcopy<MemType::CPU, MemType::GPU>(farray, (*another_nodes_k), farray_size); farray[0] *= 2.0f; memproc::memcopy<MemType::GPU, MemType::CPU>((*another_nodes_k), farray, farray_size); #else @@ -101,9 +81,6 @@ int main(void) (*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; @@ -148,8 +125,49 @@ int main(void) mesh3.SaveTXT("./mesh3"); #ifdef INCLUDE_CUDA - memproc::dealloc<MemType::GPU>((void *&)(farray)); + Mesh<double, MemType::GPU> mesh_vis(15, 4.0); +#else + Mesh<double, MemType::CPU> mesh_vis(15, 4.0); +#endif + + auto cells_thick = mesh_vis.CreateCellsData("cells_temp_array"); + int N = mesh_vis.GetCellsNum(); + +#ifdef INCLUDE_CUDA + double *darray; + size_t darray_size = 0; + + memproc::realloc<MemType::CPU>((void *&)(darray), darray_size, N * sizeof(double)); + memproc::memcopy<MemType::CPU, MemType::GPU>(darray, *cells_thick, N); + + for (int i = 0; i < N; ++i) + darray[i] = -5.0 + i*1.0/N * (-5.0); + + memproc::memcopy<MemType::GPU, MemType::CPU>(*cells_thick, darray, N); +#else + for (int i = 0; i < N; ++i) + (*cells_thick)[i] = -5.0 + i*1.0/N * (-5.0); +#endif + + mesh_vis.SaveTXT("./mesh_vis"); + + // wrong constructor (it should be unit segment partition 0.5 + 0.4 != 1.0) +#ifdef INCLUDE_CUDA + dsegment_partition[0] = 0.5; dsegment_partition[1] = 0.4; + + 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> mesh4(dev_segment_partition, 2, 5.0); +#else + dsegment_partition[0] = 0.5; dsegment_partition[1] = 0.4; + Mesh<double, MemType::CPU> mesh4(dsegment_partition, 2, 5.0); +#endif + +#ifdef INCLUDE_CUDA + memproc::dealloc<MemType::CPU>((void *&)(farray)); memproc::dealloc<MemType::GPU>((void *&)(dev_segment_partition)); + memproc::dealloc<MemType::CPU>((void *&)(darray)); #endif return 0; -- GitLab