From 1f891c76289640fdd25ac27f451a1c6d7be06c3b Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Tue, 21 Jan 2025 17:34:37 +0100 Subject: [PATCH 1/4] add global routines to dump Idefix arrays (useful for debugging or checking that everything works as expected) --- src/global.hpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/global.hpp b/src/global.hpp index 8651f974..0cf57e15 100644 --- a/src/global.hpp +++ b/src/global.hpp @@ -44,6 +44,34 @@ IdefixArray1D ConvertVectorToIdefixArray(std::vector &inputVector) { return(outArr); } +///< dump Idefix array to a numpy array on disk +template +void dumpArray(std::string filename, IdefixArray3D array) { + IdefixHostArray3D hArray = Kokkos::create_mirror(array); + Kokkos::deep_copy(hArray,array); + + std::array shape; + bool fortran_order{false}; + shape[0] = array.extent(0); + shape[1] = array.extent(1); + shape[2] = array.extent(2); + npy::SaveArrayAsNumpy(filename, fortran_order, 3, shape.data(), hArray.data()); +} + +template +void dumpArray(std::string filename, IdefixArray4D array) { + IdefixHostArray4D hArray = Kokkos::create_mirror(array); + Kokkos::deep_copy(hArray,array); + + std::array shape; + bool fortran_order{false}; + shape[0] = array.extent(0); + shape[1] = array.extent(1); + shape[2] = array.extent(2); + shape[3] = array.extent(3); + npy::SaveArrayAsNumpy(filename, fortran_order, 4, shape.data(), hArray.data()); +} + } // namespace idfx class idfx::IdefixOutStream { From afc3e4dbb2cd052360cdbbe2ca4473e128491752 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Tue, 21 Jan 2025 17:37:24 +0100 Subject: [PATCH 2/4] add missing header --- src/global.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/global.hpp b/src/global.hpp index 0cf57e15..1a894416 100644 --- a/src/global.hpp +++ b/src/global.hpp @@ -11,6 +11,7 @@ #include #include #include "arrays.hpp" +#include "npy.hpp" namespace idfx { int initialize(); // Initialisation routine for idefix From 898fcf3e91f416395f3d9ced590ce1df8f365e08 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Sun, 9 Feb 2025 21:36:47 +0100 Subject: [PATCH 3/4] add some doc --- doc/source/programmingguide.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/source/programmingguide.rst b/doc/source/programmingguide.rst index 18795b50..4bd7c69f 100644 --- a/doc/source/programmingguide.rst +++ b/doc/source/programmingguide.rst @@ -645,6 +645,27 @@ It may also be useful to implement debug-only safeguards with custom logic that fit `RUNTIME_CHECK_*` macros. This can be achieved by using the compiler directive `#ifdef RUNTIME_CHECKS` directly. +Dump an array to a file +----------------------- + +It is usually difficult to know what Idefix arrays effectively contains, especially when running on GPU. +To help with this difficulty, Idefix provides a global function ``DumpArray`` that can be used +to dump a ``IdefixArray`` to a numpy file (that can be read from python). This feature can be used +for debugging purpose as: + + +.. code-block:: c++ + + #include "idefix.hpp" + + IdefixArray3D myArray("debugMe",10,10,10); + + idfx::DumpArray("myFilename.npy",myArray); // Dump the array content to a numpy file named "myFilename.npy" + + +Note that the array is automatically transfered from the GPU, if needed. + + Minimal skeleton ================ From 241c69c8b524caeea33c73cdc4f20db6f0151470 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Mon, 10 Feb 2025 08:52:58 +0100 Subject: [PATCH 4/4] refactor DumpArray --- src/global.hpp | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/global.hpp b/src/global.hpp index 1a894416..265699b2 100644 --- a/src/global.hpp +++ b/src/global.hpp @@ -46,31 +46,17 @@ IdefixArray1D ConvertVectorToIdefixArray(std::vector &inputVector) { } ///< dump Idefix array to a numpy array on disk -template -void dumpArray(std::string filename, IdefixArray3D array) { - IdefixHostArray3D hArray = Kokkos::create_mirror(array); - Kokkos::deep_copy(hArray,array); +template +void DumpArray(std::string filename, ArrayType array) { + auto hArray = Kokkos::create_mirror(array); + Kokkos::deep_copy(hArray, array); - std::array shape; + std::array shape; bool fortran_order{false}; - shape[0] = array.extent(0); - shape[1] = array.extent(1); - shape[2] = array.extent(2); - npy::SaveArrayAsNumpy(filename, fortran_order, 3, shape.data(), hArray.data()); -} - -template -void dumpArray(std::string filename, IdefixArray4D array) { - IdefixHostArray4D hArray = Kokkos::create_mirror(array); - Kokkos::deep_copy(hArray,array); - - std::array shape; - bool fortran_order{false}; - shape[0] = array.extent(0); - shape[1] = array.extent(1); - shape[2] = array.extent(2); - shape[3] = array.extent(3); - npy::SaveArrayAsNumpy(filename, fortran_order, 4, shape.data(), hArray.data()); + for (size_t i = 0; i < ArrayType::rank; ++i) { + shape[i] = array.extent(i); + } + npy::SaveArrayAsNumpy(filename, fortran_order, ArrayType::rank, shape.data(), hArray.data()); } } // namespace idfx