VM2D 1.14
Vortex methods for 2D flows simulation
Loading...
Searching...
No Matches
Gpu2D.h
Go to the documentation of this file.
1/*--------------------------------*- VM2D -*-----------------*---------------*\
2| ## ## ## ## #### ##### | | Version 1.14 |
3| ## ## ### ### ## ## ## ## | VM2D: Vortex Method | 2026/03/06 |
4| ## ## ## # ## ## ## ## | for 2D Flow Simulation *----------------*
5| #### ## ## ## ## ## | Open Source Code |
6| ## ## ## ###### ##### | https://www.github.com/vortexmethods/VM2D |
7| |
8| Copyright (C) 2017-2026 I. Marchevsky, K. Sokol, E. Ryatina, A. Kolganova |
9*-----------------------------------------------------------------------------*
10| File name: Gpu2D.h |
11| Info: Source code of VM2D |
12| |
13| This file is part of VM2D. |
14| VM2D is free software: you can redistribute it and/or modify it |
15| under the terms of the GNU General Public License as published by |
16| the Free Software Foundation, either version 3 of the License, or |
17| (at your option) any later version. |
18| |
19| VM2D is distributed in the hope that it will be useful, but WITHOUT |
20| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21| FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
22| for more details. |
23| |
24| You should have received a copy of the GNU General Public License |
25| along with VM2D. If not, see <http://www.gnu.org/licenses/>. |
26\*---------------------------------------------------------------------------*/
27
28
41#ifndef GPU2D_H
42#define GPU2D_H
43
44#include <limits>
45#include <memory>
46
47#include "cuLib2D.cuh"
48#include "Gpudefs.h"
49
50#include "cudaTreeInfo.h"
51//#include "Gmres2D.h"
52
53namespace VM2D
54{
55
56 class World2D;
57 class GmresSolver;
58
70 class Gpu
71 {
72 private:
74 const World2D& W;
75
76 public:
77
78 //static int nReserve; //Для контроля паритета выделения и освобождения памяти
79
80#if defined(__CUDACC__) || defined(USE_CUDA)
81
82 int blocks;
83 int* dev_blocks;
84
87 template<typename T>
88 void ReleaseDevMem(T* ptr, int code)
89 {
90 cuDeleteFromDev(ptr, code);
91 //--nReserve;
92 }
93
94
105 template<typename T, size_t dim>
106 T* ReserveDevMem(size_t n, size_t& new_n, int code = 0)
107 {
108 size_t nBlocks = n / CUBLOCK;
109 if (n % CUBLOCK)
110 nBlocks++;
111
112 new_n = nBlocks * CUBLOCK;
113
114 void* ptr;
115
116 cuReserveDevMem(ptr, new_n * dim * sizeof(T), code);
117 //++nReserve;
118
119 return (T*)ptr;
120 } //ReserveDevMem(...)
121
122
132 template<typename T>
133 T* ReserveDevMemAndCopyFixedArray(size_t n, T* host_src)
134 {
135 void* dev_ptr;
136
137 cuReserveDevMem(dev_ptr, n * sizeof(T));
138 //++nReserve;
139
140 cuCopyFixedArray(dev_ptr, host_src, sizeof(T) * n, 116);
141
142 return (T*)dev_ptr;
143 }//ReserveDevMemAndCopyFixedArray(...)
144
145
153 template<typename T, size_t dim>
154 void CopyMemFromDev(size_t n, T* dev_ptr, T* host_ptr, int code = 0) const
155 {
156 cuCopyMemFromDev((void*)host_ptr, (void*)dev_ptr, sizeof(T) * n * dim, code);
157 };//CopyMemFromDev(...)
158
166 template<typename T, size_t dim>
167 void CopyMemToDev(size_t n, T* host_ptr, T* dev_ptr) const
168 {
169 cuCopyFixedArray((void*)dev_ptr, (void*)host_ptr, sizeof(T) * n * dim, 117);
170 };//CopyMemToDev(...)
171
172
174 void RefreshWake(int code = 0);
175
177 void RefreshAfls(int code = 0);
178 void RefreshVirtualWakes(int code = 0);
179
181 void RefreshVP(int code = 0);
182
183
184 // Ниже - данные для вычисления скоростей
185
186
187
189 size_t* dev_ptr_nPanels;
190 size_t* dev_ptr_nVortices;
191
192
193 //Переменная, которая лежит на хосте и хранит адрес на видеокарте массива, в котором хранятся указатели на соответствующие массивы
194 double** dev_ptr_ptr_vtx;
195 double** dev_ptr_ptr_vel;
196 double** dev_ptr_ptr_rad;
197 double** dev_ptr_ptr_i0;
198 float** dev_ptr_ptr_i0f;
199 double** dev_ptr_ptr_i1;
200 double** dev_ptr_ptr_i2;
201 double** dev_ptr_ptr_i3;
202 float** dev_ptr_ptr_i3f;
203
204 double** dev_ptr_ptr_r;
205 double** dev_ptr_ptr_rhs;
206
207 double** dev_ptr_ptr_freeVortexSheet;
208 double** dev_ptr_ptr_attachedVortexSheet;
209 double** dev_ptr_ptr_attachedSourceSheet;
210
211 double** dev_ptr_ptr_meanEpsOverPanel;
212
213 double** dev_ptr_ptr_viscousStresses;
214
216 std::vector<size_t> n_CUDA_virtWake;
217 size_t n_CUDA_totalVirtWake;
218
220 size_t n_CUDA_source;
221
223 size_t n_CUDA_velVP;
224
226
228 size_t n_CUDA_wake;
229
231 size_t n_CUDA_afls;
232
234 size_t n_CUDA_pnls;
235
236
238 std::unique_ptr<BHcu::CudaTreeInfo> inflTreeWake;
239 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreeWake;
240#ifdef TURB
241 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreeNut;
242#endif
243
244 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreePnl;
245 std::unique_ptr<BHcu::CudaTreeInfo> inflTreePnlVortex;
246 std::unique_ptr<BHcu::CudaTreeInfo> inflTreePnlSource;
247
248 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreeVP;
249
250 //для вспомогательных операций
251 std::unique_ptr<BHcu::CudaTreeInfo> auxTreePnl;
252 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreePoint; //точки Point2, для которых ищутся ближайшие панели
253 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreeSegment; //отрезки 2 х Point2, для которых контролируются пересечения с профилем
254
255
257 std::unique_ptr<GmresSolver> Gmres;
258
259 void AllocateSolution(double*& dev_sol, size_t n);
260 void SetSolution(double* sol, double* dev_sol, size_t n);
261 void ReleaseSolution(double* dev_sol);
262
263 double* dev_sol;
264 double* dev_solLin;
265
266
267#endif
268
272 Gpu(const World2D& W_);
273
275 ~Gpu();
276
277
281 void setAccelCoeff(double cft_)
282 {
283#if defined(__CUDACC__) || defined(USE_CUDA)
284 cuSetAccelCoeff(cft_);
285#endif
286 }
287
288
293 void setCollapseCoeff(double pos_, double refLength_)
294 {
295#if defined(__CUDACC__) || defined(USE_CUDA)
296 cuSetCollapseCoeff(pos_, refLength_);
297#endif
298 }
299
300
304 void setMaxGamma(double gam_)
305 {
306#if defined(__CUDACC__) || defined(USE_CUDA)
307 cuSetMaxGamma(gam_);
308#endif
309 }
310
311
318 void setSchemeSwitcher(int schemeSwitcher_)
319 {
320#if defined(__CUDACC__) || defined(USE_CUDA)
321 cuSetSchemeSwitcher(schemeSwitcher_, 1);
322#endif
323 }
324 };
325
326}//namespace VM2D
327
328
329/*
330namespace VM2D
331{
332 template <class T>
333 class MyAlloc {
334 public:
335 // type definitions
336 typedef T value_type;
337 typedef T* pointer;
338 typedef const T* const_pointer;
339 typedef T& reference;
340 typedef const T& const_reference;
341 typedef std::size_t size_type;
342 typedef std::ptrdiff_t difference_type;
343
344 // rebind allocator to type U
345 template <class U>
346 struct rebind {
347 typedef MyAlloc<U> other;
348 };
349
350 // return address of values
351 pointer address(reference value) const {
352 return &value;
353 }
354 const_pointer address(const_reference value) const {
355 return &value;
356 }
357
358 // constructors and destructor
359 // nothing to do because the allocator has no state
360 MyAlloc() throw() {
361 }
362 MyAlloc(const MyAlloc&) throw() {
363 }
364 template <class U>
365 MyAlloc(const MyAlloc<U>&) throw() {
366 }
367 ~MyAlloc() throw() {
368 }
369
370 // return maximum number of elements that can be allocated
371 size_type max_size() const throw() {
372 return std::numeric_limits<std::size_t>::max() / sizeof(T);
373 }
374
375 // allocate but don't initialize num elements of type T
376 pointer allocate(size_type num, const void* = 0) {
377 // print message and allocate memory with global new
378 //std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl;
379
380 pointer ret = (pointer)(::operator new(num * sizeof(T)));
381
382 //pointer ret;
383 //cudaHostAlloc((void**)&ret, num * sizeof(T), cudaHostAllocDefault);
384 cuAlloc((void**)&ret, num * sizeof(T));
385
386 //std::cerr << " allocated at: " << (void*)ret << std::endl;
387 return ret;
388 }
389
390 // initialize elements of allocated storage p with value value
391 void construct(pointer p, const T& value) {
392 // initialize memory with placement new
393 new((void*)p)T(value);
394 //std::cerr << " construct " << std::endl;
395 }
396
397 // destroy elements of initialized storage p
398 void destroy(pointer p) {
399 // destroy objects by calling their destructor
400 p->~T();
401 //std::cerr << " destroy " << std::endl;
402 }
403
404 // deallocate storage p of deleted elements
405 void deallocate(pointer p, size_type num) {
406 // print message and deallocate memory with global delete
407
408 //std::cerr << "deallocate " << num << " element(s)" << " of size " << sizeof(T) << " at: " << (void*)p << std::endl;
409
410 //::operator delete((void*)p);
411 cuDalloc((void*)p);
412 }
413 };
414
415 // return that all specializations of this allocator are interchangeable
416 template <class T1, class T2>
417 bool operator== (const MyAlloc<T1>&,
418 const MyAlloc<T2>&) throw() {
419 return true;
420 }
421 template <class T1, class T2>
422 bool operator!= (const MyAlloc<T1>&,
423 const MyAlloc<T2>&) throw() {
424 return false;
425 }
426
427
428 //{
429 // // create a vector, using MyAlloc<> as allocator
430 // std::vector<int, VM2D::MyAlloc<int> > v(5, 37);
431
432 // v.resize(0);
433
434 // // insert elements
435 // // - causes reallocations
436 // v.push_back(42);
437 // v.push_back(56);
438
439 // v.reserve(10);
440
441 // v.push_back(11);
442 // v.push_back(22);
443 // v.push_back(33);
444 // v.push_back(44);
445 //}
446
447}
448*/
449#endif
Описание констант и параметров для взаимодействия с графическим ускорителем
#define CUBLOCK
Definition Gpudefs.h:82
Класс, обеспечивающий возможность выполнения вычислений на GPU по технологии Nvidia CUDA.
Definition Gpu2D.h:71
void setAccelCoeff(double cft_)
Установка коэффициента разгона потока
Definition Gpu2D.h:281
void setCollapseCoeff(double pos_, double refLength_)
Установка правой границы самого правого профиля (для организации увеличения радиуса коллапса)
Definition Gpu2D.h:293
void setMaxGamma(double gam_)
Установка максимально допустимой циркуляции вихря
Definition Gpu2D.h:304
const World2D & W
Константная ссылка на решаемую задачу
Definition Gpu2D.h:74
Gpu(const World2D &W_)
Конструктор
Definition Gpu2D.cpp:57
void setSchemeSwitcher(int schemeSwitcher_)
Установка переключателя расчетных схем
Definition Gpu2D.h:318
Класс, опеделяющий текущую решаемую задачу
Definition World2D.h:77