TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
TRUSTArray_device.tpp
1/****************************************************************************
2 * Copyright (c) 2024, 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 TRUSTArr_device_TPP_included
17#define TRUSTArr_device_TPP_included
18
19#include <TRUSTArray.h>
20
21// Methodes de verification que le tableau est a jour sur le host:
22// ToDo OpenMP :Appels couteux (car non inlines?) depuis operator()[int] mais comment faire mieux ?
23template<typename _TYPE_, typename _SIZE_>
25{
26#if defined(TRUST_USE_GPU) && !defined(TRUST_USE_UVM)
27 if (get_data_location()==DataLocation::Device)
28 copyFromDevice(*this);
29#endif
30}
31
32template<typename _TYPE_, typename _SIZE_>
34{
35#if defined(TRUST_USE_GPU) && !defined(TRUST_USE_UVM)
36 const DataLocation& loc = get_data_location();
37 if (loc==DataLocation::Host || loc==DataLocation::HostOnly || loc==DataLocation::PartialHostDevice) return;
38 else if (loc==DataLocation::Device)
39 copyFromDevice(*this);
40 // On va modifier le tableau (non const) sur le host:
41 set_data_location(DataLocation::Host);
42#endif
43}
44
45// Fonction pour connaitre la localisation du tableau
46template<typename _TYPE_, typename _SIZE_>
48{
49 DataLocation loc = get_data_location();
50 return loc == DataLocation::Device || loc == DataLocation::HostDevice;
51}
52
53// Fonctions checkDataOnDevice pour lancement conditionnel de kernels sur le device:
54// -Si les tableaux passes en parametre sont sur a jour sur le device
55// -Si ce n'est pas le cas, les tableaux sont copies sur le host via ensureDataOnHost
56template<typename _TYPE_, typename _SIZE_>
58{
59#ifdef TRUST_USE_GPU
60 bool flag = isDataOnDevice();
61 if (!flag)
63 //else
64 // set_data_location(Device); // non const array will be computed on device
65 return flag;
66#else
67 return false;
68#endif
69}
70
71template<typename _TYPE_, typename _SIZE_>
73{
74#ifdef TRUST_USE_GPU
75 bool flag = isDataOnDevice();
76 if (!flag)
78 else
79 set_data_location(DataLocation::Device); // non const array will be computed on device
80 return flag;
81#else
82 return false;
83#endif
84}
85
86template<typename _TYPE_, typename _SIZE_>
88{
89#ifdef TRUST_USE_GPU
90 bool flag = isDataOnDevice() && tab_const.isDataOnDevice();
91 // Si un des deux tableaux n'est pas a jour sur le device alors l'operation se fera sur le host:
92 if (!flag)
93 {
94 this->ensureDataOnHost();
95 tab_const.ensureDataOnHost();
96 }
97 else
98 set_data_location(DataLocation::Device); // non const array will be computed on device
99 return flag;
100#else
101 return false;
102#endif
103}
104
105
106#endif
DataLocation get_data_location()
Definition TRUSTArray.h:245
void set_data_location(DataLocation flag)
Definition TRUSTArray.h:247
bool isDataOnDevice() const
friend class TRUSTArray
Definition TRUSTArray.h:108