TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
kokkos_test.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 <View_Types.h>
17#include <TRUSTTab.h>
18#include <iostream>
19#include <Device.h>
20#include <ParserView.h>
21#define KOKKOS_IMPL_PUBLIC_INCLUDE
22#include <Kokkos_UnorderedMap.hpp>
23#include <Kokkos_UniqueToken.hpp>
24#include <TRUSTTrav.h>
25
26/*! Teste les methodes de l'interface Kokkos utilisees dans TRUST
27 */
28void kokkos_self_test()
29{
30#ifndef NDEBUG
31#ifdef TRUST_USE_GPU
32 static bool kokkos_self_tested_ = false;
33
34 if (kokkos_self_tested_) return;
35 kokkos_self_tested_=true;
36
37 // Tab dimensions:
38 int nb_elem = 3;
39 int nb_compo = 2;
40 DoubleTab a(nb_elem, nb_compo);
41 a=1.0;
42 DoubleTab b(nb_elem, nb_compo);
43 b=2.0;
44
45 // Check DoubleTabView:
46 {
47 DoubleTabView b_v = b.view_rw();
48 Kokkos::parallel_for(nb_elem, KOKKOS_LAMBDA(const int i)
49 {
50 assert(b_v(i, 0) == 2);
51 for (int j = 0; j < nb_compo; j++)
52 b_v(i, j) = 2 * b_v(i, j);
53 });
54 Kokkos::fence();
55 assert(b(0, 0) == 4); // Retour sur le host et verification
56 }
57 // Check CDoubleTabView:
58 {
59 CDoubleTabView a_v = a.view_ro();
60 Kokkos::parallel_for(nb_elem, KOKKOS_LAMBDA(const int i)
61 {
62 assert(a_v(i, 0) == 1);
63 });
64 Kokkos::fence();
65 assert(a(0, 0) == 1); // Verification sur le host
66 }
67 // Check basic loop:
68 {
69 a = 1;
70 b = 2;
71 CDoubleTabView a_v = a.view_ro();
72 DoubleTabView b_v = b.view_rw();
73 Kokkos::parallel_for(nb_elem, KOKKOS_LAMBDA(
74 const int i)
75 {
76 for (int j = 0; j < nb_compo; j++)
77 {
78 b_v(i, j) = 2 * b_v(i, j) + a_v(i, j);
79 //printf("KOKKOS: %p %e\n", b_v.data(), b_v(i, j));
80 }
81 });
82 Kokkos::fence();
83 assert(est_egal(b(0, 0), 5)); // Retour sur le host et verification
84 }
85
86 // Check basic loop on vect
87 {
88 DoubleVect u(nb_elem);
89 u = 1.0;
90 ArrOfDouble v(nb_elem);
91 v = 2.0;
92 CDoubleArrView a_v = u.view_ro();
93 DoubleArrView b_v = v.view_rw();
94 Kokkos::parallel_for(nb_elem, KOKKOS_LAMBDA(const int i)
95 {
96 b_v(i) = 2 * b_v(i) + a_v(i);
97 });
98 Kokkos::fence();
99 assert(est_egal(v(0), 5)); // Retour sur le host et verification
100 }
101 // Check DoubleVect built as a DoubleTab
102 {
103 DoubleTab tab(nb_elem);
104 DoubleArrView vect_v = static_cast<DoubleVect&>(tab).view_rw(); // It works but ToDo Kokkos, try to do better ? DoubleArrView vect_v = tab.view_rw(); ?
105 Kokkos::parallel_for(nb_elem, KOKKOS_LAMBDA(const int i)
106 {
107 vect_v(i) = 2;
108 });
109 }
110 // Check DoubleTab aliased by a DoubleVect
111 {
112 int dim = 3;
113 DoubleTab tab(nb_elem, dim);
114 DoubleVect& vect = tab;
115 DoubleArrView vect_v = vect.view_rw();
116 int size = vect.size_array();
117 Kokkos::parallel_for(size, KOKKOS_LAMBDA(const int i)
118 {
119 vect_v(i) = 2;
120 });
121 }
122 // Verification des adresses memoire:
123 {
124 DoubleTab tab(nb_elem,2);
125 for (int i=0; i<nb_elem; i++)
126 for (int j=0; j<2; j++)
127 tab(i,j) = i + 0.1*j;
128 CDoubleTabView tab_v = tab.view_ro();
129 // La creation de la vue fait une copie de u sur le device:
130 //printf("Provisoire OpenMP adresse host: [%p] device: [%p]\n",(void*)tab.data(), (void*)addrOnDevice(tab));
131 assert(a.data() != addrOnDevice(a));
132 // On verifie les adresses memoires de la vue:
133 //printf("Provisoire Kokkos adresse device: [%p]\n",(void*)tab_v.data());
134 //assert(tab_v.h_view.view_host()==tab.data());
135 assert(tab_v.data()==addrOnDevice(tab));
136 //debug_device_view(tab_v, tab);
137 }
138 // Changement de la zone memoire sur le device avec OpenMP:
139 {
140 DoubleTab tab(nb_elem,2);
141 for (int i=0; i<nb_elem; i++)
142 for (int j=0; j<2; j++)
143 tab(i,j) = i + 0.1*j;
144 CDoubleTabView tab_v = tab.view_ro();
145 assert(tab_v.data()==addrOnDevice(tab)); // Meme adresse
146 //printf("Provisoire OpenMP device before: [%p]\n", (void*)addrOnDevice(tab));
147 deleteOnDevice(tab);
148 allocateOnDevice(tab);
149 //printf("Provisoire OpenMP device after: [%p]\n", (void*)addrOnDevice(tab));
150 CDoubleTabView tab_v2 = tab.view_ro();
151 assert(tab_v2.data()==addrOnDevice(tab)); // Meme adresse
152 }
153 // C++ object in Kokkos region
154 {
155 ArrOfDouble f(nb_elem);
156 f = 0;
157 std::string expr("2*x+2");
158 // Parser sur le device;
159 ParserView parser(expr, 1);
160 parser.addVar("x");
161 parser.parseString();
162 DoubleArrView f_v = f.view_rw();
163 Kokkos::parallel_for(nb_elem, KOKKOS_LAMBDA(
164 const int i)
165 {
166 int threadId = parser.acquire();
167 double x = (double) i;
168 parser.setVar(0, x, threadId);
169 f_v(i) = parser.eval(threadId);
170 parser.release(threadId);
171 });
172 assert(f(0) == 2);
173 assert(f(nb_elem - 1) == 2 * nb_elem);
174 }
175 {
176 // How to use VECT of TRUSTArray in Kokkos region ?
177 using DoubleTravs = TRUST_Vector<DoubleTrav>;
178 const int nb = 5;
179 DoubleTravs tab(nb);
180 Kokkos::Array<CDoubleTabView, nb> tabs;
181 for (int n=0; n<nb; n++)
182 {
183 tab[n].resize(nb_elem);
184 tab[n] = (double)n; // Initialize on host
185 auto tab_view = tab[n].view_ro(); // Copy on the device
186 tabs[n] = tab_view;
187 }
188 Kokkos::parallel_for(nb_elem, KOKKOS_LAMBDA(const int i)
189 {
190 for (int n=0; n<nb; n++)
191 {
192 auto tab_v = tabs[n];
193 assert(tab_v(i,0)==n);
194 }
195 });
196 }
197// Some check:
198 /*
199 assert(a_v.is_allocated());
200 bool check = std::is_same<decltype(a_v)::memory_space, Kokkos::CudaSpace>::value;
201 assert(check);
202 */
203
204// ToDo implement reference counter for a view on tab
205 /*
206 DoubleTab tab(N);
207 DoubleArrView view = tab.ro(); // Data on device
208 copyFromeDevice(tab); // Data on host !
209 // How to detect that now view is invalid and can't be used in a Kokkos region now ?
210 Kokkos::parallel_for(N, KOKKOS_LAMBDA(const int elem) { view(elem) = 1.0; });
211 */
212#endif
213#endif
214}
215
_SIZE_ size_array() const
classe TRUST_Vector