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
52namespace VM2D
53{
54
55 class World2D;
56
68 class Gpu
69 {
70 private:
72 const World2D& W;
73
74 public:
75
76 //static int nReserve; //Для контроля паритета выделения и освобождения памяти
77
78#if defined(__CUDACC__) || defined(USE_CUDA)
79
80 int blocks;
81 int* dev_blocks;
82
85 template<typename T>
86 void ReleaseDevMem(T* ptr, int code)
87 {
88 cuDeleteFromDev(ptr, code);
89 //--nReserve;
90 }
91
92
103 template<typename T, size_t dim>
104 T* ReserveDevMem(size_t n, size_t& new_n, int code = 0)
105 {
106 size_t nBlocks = n / CUBLOCK;
107 if (n % CUBLOCK)
108 nBlocks++;
109
110 new_n = nBlocks * CUBLOCK;
111
112 void* ptr;
113
114 cuReserveDevMem(ptr, new_n * dim * sizeof(T), code);
115 //++nReserve;
116
117 return (T*)ptr;
118 } //ReserveDevMem(...)
119
120
130 template<typename T>
131 T* ReserveDevMemAndCopyFixedArray(size_t n, T* host_src)
132 {
133 void* dev_ptr;
134
135 cuReserveDevMem(dev_ptr, n * sizeof(T));
136 //++nReserve;
137
138 cuCopyFixedArray(dev_ptr, host_src, sizeof(T) * n, 116);
139
140 return (T*)dev_ptr;
141 }//ReserveDevMemAndCopyFixedArray(...)
142
143
151 template<typename T, size_t dim>
152 void CopyMemFromDev(size_t n, T* dev_ptr, T* host_ptr, int code = 0) const
153 {
154 cuCopyMemFromDev((void*)host_ptr, (void*)dev_ptr, sizeof(T) * n * dim, code);
155 };//CopyMemFromDev(...)
156
164 template<typename T, size_t dim>
165 void CopyMemToDev(size_t n, T* host_ptr, T* dev_ptr) const
166 {
167 cuCopyFixedArray((void*)dev_ptr, (void*)host_ptr, sizeof(T) * n * dim, 117);
168 };//CopyMemToDev(...)
169
170
172 void RefreshWake(int code = 0);
173
175 void RefreshAfls(int code = 0);
176 void RefreshVirtualWakes(int code = 0);
177
179 void RefreshVP(int code = 0);
180
181
182 // Ниже - данные для вычисления скоростей
183
184
185
187 size_t* dev_ptr_nPanels;
188 size_t* dev_ptr_nVortices;
189
190
191 //Переменная, которая лежит на хосте и хранит адрес на видеокарте массива, в котором хранятся указатели на соответствующие массивы
192 double** dev_ptr_ptr_vtx;
193 double** dev_ptr_ptr_vel;
194 double** dev_ptr_ptr_rad;
195 double** dev_ptr_ptr_i0;
196 float** dev_ptr_ptr_i0f;
197 double** dev_ptr_ptr_i1;
198 double** dev_ptr_ptr_i2;
199 double** dev_ptr_ptr_i3;
200 float** dev_ptr_ptr_i3f;
201
202 double** dev_ptr_ptr_r;
203 double** dev_ptr_ptr_rhs;
204
205 double** dev_ptr_ptr_freeVortexSheet;
206 double** dev_ptr_ptr_attachedVortexSheet;
207 double** dev_ptr_ptr_attachedSourceSheet;
208
209 double** dev_ptr_ptr_meanEpsOverPanel;
210
211 double** dev_ptr_ptr_viscousStresses;
212
214 std::vector<size_t> n_CUDA_virtWake;
215 size_t n_CUDA_totalVirtWake;
216
218 size_t n_CUDA_source;
219
221 size_t n_CUDA_velVP;
222
224
226 size_t n_CUDA_wake;
227
229 size_t n_CUDA_afls;
230
232 size_t n_CUDA_pnls;
233
235 std::unique_ptr<BHcu::CudaTreeInfo> inflTreeWake;
236 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreeWake;
237
238 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreePnl;
239 std::unique_ptr<BHcu::CudaTreeInfo> inflTreePnlVortex;
240 std::unique_ptr<BHcu::CudaTreeInfo> inflTreePnlSource;
241
242 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreeVP;
243
244 //для вспомогательных операций
245 std::unique_ptr<BHcu::CudaTreeInfo> auxTreePnl;
246 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreePoint; //точки Point2, для которых ищутся ближайшие панели
247 std::unique_ptr<BHcu::CudaTreeInfo> cntrTreeSegment; //отрезки 2 х Point2, для которых контролируются пересечения с профилем
248
249
250 void AllocateSolution(double*& dev_sol, size_t n);
251 void SetSolution(double* sol, double* dev_sol, size_t n);
252 void ReleaseSolution(double* dev_sol);
253
254 double* dev_sol;
255 double* dev_solLin;
256
257
258#endif
259
263 Gpu(const World2D& W_);
264
266 ~Gpu();
267
268
272 void setAccelCoeff(double cft_)
273 {
274#if defined(__CUDACC__) || defined(USE_CUDA)
275 cuSetAccelCoeff(cft_);
276#endif
277 }
278
279
284 void setCollapseCoeff(double pos_, double refLength_)
285 {
286#if defined(__CUDACC__) || defined(USE_CUDA)
287 cuSetCollapseCoeff(pos_, refLength_);
288#endif
289 }
290
291
295 void setMaxGamma(double gam_)
296 {
297#if defined(__CUDACC__) || defined(USE_CUDA)
298 cuSetMaxGamma(gam_);
299#endif
300 }
301
302
309 void setSchemeSwitcher(int schemeSwitcher_)
310 {
311#if defined(__CUDACC__) || defined(USE_CUDA)
312 cuSetSchemeSwitcher(schemeSwitcher_, 1);
313#endif
314 }
315 };
316
317}//namespace VM2D
318
319
320/*
321namespace VM2D
322{
323 template <class T>
324 class MyAlloc {
325 public:
326 // type definitions
327 typedef T value_type;
328 typedef T* pointer;
329 typedef const T* const_pointer;
330 typedef T& reference;
331 typedef const T& const_reference;
332 typedef std::size_t size_type;
333 typedef std::ptrdiff_t difference_type;
334
335 // rebind allocator to type U
336 template <class U>
337 struct rebind {
338 typedef MyAlloc<U> other;
339 };
340
341 // return address of values
342 pointer address(reference value) const {
343 return &value;
344 }
345 const_pointer address(const_reference value) const {
346 return &value;
347 }
348
349 // constructors and destructor
350 // nothing to do because the allocator has no state
351 MyAlloc() throw() {
352 }
353 MyAlloc(const MyAlloc&) throw() {
354 }
355 template <class U>
356 MyAlloc(const MyAlloc<U>&) throw() {
357 }
358 ~MyAlloc() throw() {
359 }
360
361 // return maximum number of elements that can be allocated
362 size_type max_size() const throw() {
363 return std::numeric_limits<std::size_t>::max() / sizeof(T);
364 }
365
366 // allocate but don't initialize num elements of type T
367 pointer allocate(size_type num, const void* = 0) {
368 // print message and allocate memory with global new
369 //std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl;
370
371 pointer ret = (pointer)(::operator new(num * sizeof(T)));
372
373 //pointer ret;
374 //cudaHostAlloc((void**)&ret, num * sizeof(T), cudaHostAllocDefault);
375 cuAlloc((void**)&ret, num * sizeof(T));
376
377 //std::cerr << " allocated at: " << (void*)ret << std::endl;
378 return ret;
379 }
380
381 // initialize elements of allocated storage p with value value
382 void construct(pointer p, const T& value) {
383 // initialize memory with placement new
384 new((void*)p)T(value);
385 //std::cerr << " construct " << std::endl;
386 }
387
388 // destroy elements of initialized storage p
389 void destroy(pointer p) {
390 // destroy objects by calling their destructor
391 p->~T();
392 //std::cerr << " destroy " << std::endl;
393 }
394
395 // deallocate storage p of deleted elements
396 void deallocate(pointer p, size_type num) {
397 // print message and deallocate memory with global delete
398
399 //std::cerr << "deallocate " << num << " element(s)" << " of size " << sizeof(T) << " at: " << (void*)p << std::endl;
400
401 //::operator delete((void*)p);
402 cuDalloc((void*)p);
403 }
404 };
405
406 // return that all specializations of this allocator are interchangeable
407 template <class T1, class T2>
408 bool operator== (const MyAlloc<T1>&,
409 const MyAlloc<T2>&) throw() {
410 return true;
411 }
412 template <class T1, class T2>
413 bool operator!= (const MyAlloc<T1>&,
414 const MyAlloc<T2>&) throw() {
415 return false;
416 }
417
418
419 //{
420 // // create a vector, using MyAlloc<> as allocator
421 // std::vector<int, VM2D::MyAlloc<int> > v(5, 37);
422
423 // v.resize(0);
424
425 // // insert elements
426 // // - causes reallocations
427 // v.push_back(42);
428 // v.push_back(56);
429
430 // v.reserve(10);
431
432 // v.push_back(11);
433 // v.push_back(22);
434 // v.push_back(33);
435 // v.push_back(44);
436 //}
437
438}
439*/
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455#endif
Описание констант и параметров для взаимодействия с графическим ускорителем
#define CUBLOCK
Definition Gpudefs.h:76
Класс, обеспечивающий возможность выполнения вычислений на GPU по технологии Nvidia CUDA.
Definition Gpu2D.h:69
void setAccelCoeff(double cft_)
Установка коэффициента разгона потока
Definition Gpu2D.h:272
void setCollapseCoeff(double pos_, double refLength_)
Установка правой границы самого правого профиля (для организации увеличения радиуса коллапса)
Definition Gpu2D.h:284
void setMaxGamma(double gam_)
Установка максимально допустимой циркуляции вихря
Definition Gpu2D.h:295
const World2D & W
Константная ссылка на решаемую задачу
Definition Gpu2D.h:72
Gpu(const World2D &W_)
Конструктор
Definition Gpu2D.cpp:56
void setSchemeSwitcher(int schemeSwitcher_)
Установка переключателя расчетных схем
Definition Gpu2D.h:309
Класс, опеделяющий текущую решаемую задачу
Definition World2D.h:74