TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
DeviceMemory.cpp
1/****************************************************************************
2* Copyright (c) 2025, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#include <DeviceMemory.h>
17#include <iostream>
18#include <Process.h>
19#include <Nom.h>
20#include <Device.h>
21#ifndef LATATOOLS
22#ifdef TRUST_USE_ROCM
23#include <hip/hip_runtime.h>
24#endif
25#endif
26
27map_t DeviceMemory::memory_map_;
28size_t DeviceMemory::initial_free_ = 0;
29// Typical size of internal_items (elem,face,som) used as threshold to detect excessive H2D/D2H copies or host array allocation
32
33// Memory:
34size_t DeviceMemory::deviceMemGetInfo(bool print_total) // free or total bytes of the device
35{
36 size_t free=0, total=0;
37#ifndef LATATOOLS
38#ifdef TRUST_USE_CUDA
39 cudaMemGetInfo(&free, &total);
40#endif
41#ifdef TRUST_USE_ROCM
42 auto err = hipMemGetInfo(&free, &total);
43 if (err) Process::exit("Error during hipMemGetInfo");
44#endif
45#endif
46 return print_total ? total : free;
47}
49{
50 size_t free = deviceMemGetInfo(0);
51 initial_free_ = (initial_free_==0) ? free : initial_free_;
52 return initial_free_ - free;
53}
54
55/** Check if a chunk (even empty) is allocated on the device for the host ptr
56 * Return true/false
57*/
59{
60 if (memory_map_.empty()) return false;
61 auto it = memory_map_.find(ptr);
62 return (it != memory_map_.end());
63}
64
65/** Return the device address of the host ptr
66 * Return nullptr if ptr null or map empty
67 * Fatal error if address not found on the device
68*/
69#ifndef LATATOOLS
71{
72 if (ptr==nullptr || memory_map_.empty())
73 return nullptr;
74 trustIdType bytes;
75 void* device_ptr;
76 void* host_ptr;
77 for (auto it = memory_map_.begin(); it != memory_map_.end(); ++it)
78 {
79 host_ptr = it->first;
80 std::tie(bytes, device_ptr) = it->second;
81 if (ptr==host_ptr)
82 return device_ptr;
83 else
84 {
85 // Cas de buffer_base (ptr n'est pas forcement l'adresse de debut du bloc memoire...) ou de DoubleTab_parts
86 if (ptr >= host_ptr && ptr < static_cast<char *>(host_ptr) + bytes)
87 return static_cast<char *>(device_ptr) + (static_cast<char *>(ptr) - static_cast<char *>(host_ptr));
88 }
89 }
90 // Reproduce OpenMP-target spec: FATAL ERROR: data in use_device clause was not found on device 1: host:0x3dc75600
91 Process:: Journal() << "Error! Device address for host address " << ptrToString(ptr) << " not found:" << finl;
93 Process::exit("Error! Device address for host address not found. See log.");
94 return nullptr;
95}
96
97/** Add a new line to the memory_map_
98 * ptr should be non-null, device_ptr and size may be null
99 */
100void DeviceMemory::add(void * ptr, void * device_ptr, trustIdType bytes)
101{
102 if (ptr==nullptr) return;
103 DeviceMemory::getMemoryMap()[ptr] = {bytes, device_ptr};
104 if (statistics().is_gpu_verbose_on())
105 {
106 Process::Journal() << "Adding Host ptr: " << ptrToString(ptr) << " Device ptr: " << ptrToString(device_ptr)
107 << " bytes: " << bytes << finl;
109 }
110}
111
112/** Delete a line into the memory_map_
113 *
114 */
115void DeviceMemory::del(void * ptr)
116{
117 DeviceMemory::getMemoryMap().erase(ptr);
118 if (statistics().is_gpu_verbose_on())
119 {
120 Process::Journal() << "Deleting Host ptr: " << ptrToString(ptr) << finl;
122 }
123}
124
125/** Print the memory map for debug
126 *
127 */
129{
130 trustIdType bytes;
131 void* device_ptr;
132 Process::Journal() << "=== Memory blocks on the device ===" << finl;
133 for (const auto& block : memory_map_)
134 {
135 void* ptr = block.first;
136 std::tie(bytes, device_ptr) = block.second;
137 Process::Journal() << "Host ptr: " << ptrToString(ptr) << " Device ptr: " << ptrToString(device_ptr) << " bytes: " << bytes << finl;
138 }
139 Process::Journal() << "===================================" << finl;
140}
141
142bool DeviceMemory::warning(trustIdType nb_items)
143{
144 return statistics().is_gpu_verbose_on() && nb_pas_dt_>1 && nb_items>=internal_items_size_;
145}
146#endif
static Sortie & Journal(int message_level=0)
Renvoie un objet statique de type Sortie qui sert de journal d'evenements.
Definition Process.cpp:588
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
static bool isAllocatedOnDevice(void *)
static size_t allocatedBytesOnDevice()
static void add(void *ptr, void *device_ptr, trustIdType bytes)
static void printMemoryMap()
static map_t & getMemoryMap()
static bool warning(trustIdType nb_items)
static trustIdType internal_items_size_
static int nb_pas_dt_
static void del(void *ptr)
static size_t deviceMemGetInfo(bool)
static void * addrOnDevice(void *)