TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Device.h
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#ifndef Device_included
17#define Device_included
18
19#include <TRUSTTabs_forward.h>
20#include <Nom.h>
21
22#ifndef LATATOOLS
23#include <Perf_counters.h>
24#include <kokkos++.h>
25#endif
26
27#ifdef TRUST_USE_CUDA
28// See https://nvidia.github.io/NVTX/
29// See https://stackoverflow.com/questions/23230003/something-between-func-and-pretty-function/29856690#29856690
30#include <nvtx3/nvToolsExt.h>
31#endif
32#ifdef TRUST_USE_ROCM
33// See https://nvidia.github.io/NVTX/
34// See https://stackoverflow.com/questions/23230003/something-between-func-and-pretty-function/29856690#29856690
35#include <rocprofiler-sdk-roctx/roctx.h>
36#endif
37
38/*
39extern bool init_device_, clock_on, timer;
40extern double clock_start;
41extern int timer_counter;
42*/
43
44void self_test();
45void init_device();
46void init_cuda();
47std::string ptrToString(const void* adr);
48
49#ifdef TRUST_USE_GPU
50#define ToDo_Kokkos(str) \
51 Cerr << "[Kokkos " << str << "] Warning, code running slow cause not ported yet: line " << __LINE__ << " in " << __FILE__ << finl;
52#else
53#define ToDo_Kokkos(str)
54#endif
55
56// Macro Kernel_Name
57#ifdef TRUST_USE_GPU
58inline const std::string methodName(const std::string& prettyFunction, const int line)
59{
60 size_t colons = prettyFunction.find("::");
61 if (colons != std::string::npos)
62 {
63 // method::class:line
64 size_t begin = prettyFunction.substr(0, colons).rfind(" ") + 1;
65 size_t end = prettyFunction.rfind("(") - begin;
66 return prettyFunction.substr(begin, end) + ":" + std::to_string(line);
67 }
68 else
69 {
70 // return_type function:line
71 colons = prettyFunction.find('(');
72 return prettyFunction.substr(0, colons) + ":" + std::to_string(line);
73 }
74}
75#define __KERNEL_NAME__ methodName(__PRETTY_FUNCTION__,__LINE__)
76#else
77#define __KERNEL_NAME__ ""
78#endif
79
80extern std::string start_gpu_timer(std::string str="kernel", int bytes=-1);
81
82extern void end_gpu_timer(const std::string& str, int onDevice=1, int bytes=-1); // Return in [ms]
83
84template <typename _TYPE_>
85extern _TYPE_* addrOnDevice(_TYPE_* ptr);
86
87template <typename _TYPE_, typename _SIZE_=int>
88extern _TYPE_* addrOnDevice(TRUSTArray<_TYPE_,_SIZE_>& tab);
89
90template <typename _TYPE_, typename _SIZE_=int>
91inline const _TYPE_* addrOnDevice(const TRUSTArray<_TYPE_,_SIZE_>& tab)
92{
93 return addrOnDevice(const_cast<TRUSTArray<_TYPE_,_SIZE_>&>(tab));
94}
95
96template <typename _TYPE_, typename _SIZE_=int>
97extern _TYPE_* allocateOnDevice(TRUSTArray<_TYPE_,_SIZE_>& tab);
98
99template <typename _TYPE_, typename _SIZE_=int>
100inline const _TYPE_* allocateOnDevice(const TRUSTArray<_TYPE_,_SIZE_>& tab)
101{
102 return allocateOnDevice(const_cast<TRUSTArray<_TYPE_,_SIZE_>&>(tab));
103}
104
105template <typename _TYPE_>
106extern bool isAllocatedOnDevice(_TYPE_* tab_addr);
107
108template <typename _TYPE_, typename _SIZE_=int>
109extern bool isAllocatedOnDevice(TRUSTArray<_TYPE_,_SIZE_>& tab);
110
111template <typename _TYPE_, typename _SIZE_=int>
112extern void deleteOnDevice(TRUSTArray<_TYPE_,_SIZE_>& tab);
113
114template <typename _TYPE_, typename _SIZE_=int>
115extern const _TYPE_* mapToDevice(const TRUSTArray<_TYPE_,_SIZE_>& tab);
116
117template <typename _TYPE_, typename _SIZE_=int>
118extern _TYPE_* mapToDevice_(TRUSTArray<_TYPE_,_SIZE_>& tab, DataLocation nextLocation);
119
120template <typename _TYPE_, typename _SIZE_=int>
121extern _TYPE_* computeOnTheDevice(TRUSTArray<_TYPE_,_SIZE_>& tab);
122
123template <typename _TYPE_, typename _SIZE_=int>
124extern void copyFromDevice(TRUSTArray<_TYPE_,_SIZE_>& tab);
125
126template <typename _TYPE_, typename _SIZE_=int>
127extern void copyFromDevice(const TRUSTArray<_TYPE_,_SIZE_>& tab);
128
129template <typename _TYPE_, typename _SIZE_=int>
130extern _TYPE_* allocateOnDevice(_TYPE_* ptr, _SIZE_ size);
131
132template <typename _TYPE_, typename _SIZE_=int>
133extern void deleteOnDevice(_TYPE_* ptr, _SIZE_ size);
134
135template <typename _TYPE_, typename _SIZE_=int>
136extern void copyToDevice(_TYPE_* ptr, _SIZE_ size);
137
138template <typename _TYPE_, typename _SIZE_=int>
139extern void copyFromDevice(_TYPE_* ptr, _SIZE_ size);
140
141#endif
Represents a an array of int/int64/double/... values.
Definition TRUSTArray.h:81