Neko 1.99.6
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
projection.cu
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, The Neko Authors
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16
17 * Neither the name of the authors nor the names of its
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 POSSIBILITY OF SUCH DAMAGE.
33*/
34
36#include <device/cuda/check.h>
37#include <device/cuda/buffer.h>
38
39#include "projection_kernel.h"
41
42
43/*
44 * Device-only reduction buffer, owned by the device layer and
45 * released on device teardown (cuda_buffer_free_all in cuda_finalize)
46 */
48
49extern "C" {
50
53
54 void cuda_project_on(void *alpha, void * b, void *xx, void *bb, void *mult,
55 void *xbar, int *j, int *n){
56
57 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
58 int pow2 = 1;
59 while(pow2 < (*j)){
60 pow2 = 2*pow2;
61 }
62 const int nt = 1024/pow2;
63 const dim3 glsc3_nthrds(pow2, nt, 1);
64 const dim3 glsc3_nblcks(((*n)+nt - 1)/nt, 1, 1);
65 const int glsc3_nb = ((*n) + nt - 1)/nt;
68
69 /* First glsc3_many call */
71 <<<glsc3_nblcks, glsc3_nthrds, 0, stream>>>((const real *) b,
72 (const real **) xx,
73 (const real *) mult,
74 proj_bufred_d, *j, *n);
77 <<<(*j), 1024, 0, stream>>>(proj_bufred_d, glsc3_nb, *j);
79 CUDA_CHECK(cudaMemcpyAsync(alpha, proj_bufred_d, (*j) * sizeof(real),
81 CUDA_CHECK(cudaMemsetAsync(xbar, 0, (*n) * sizeof(real), stream));
82
85
86 const dim3 vec_nthrds(1024, 1, 1);
87 const dim3 vec_nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
88
89 /* First vector operation block */
91 <<<vec_nblcks, vec_nthrds, 0, stream>>>((real *) xbar,
92 (const real **) xx,
93 (real *) b,
94 (const real **) bb,
95 (const real *) alpha,
96 *j, *n);
97 /* Second glsc3_many call */
99 <<<glsc3_nblcks, glsc3_nthrds, 0, stream>>>((const real *) b,
100 (const real **) xx,
101 (const real *) mult,
102 proj_bufred_d, *j, *n);
105 <<<(*j), 1024, 0, stream>>>(proj_bufred_d, glsc3_nb, *j);
107 CUDA_CHECK(cudaMemcpyAsync(alpha, proj_bufred_d, (*j) * sizeof(real),
108 cudaMemcpyDeviceToDevice, stream));
109
110 cudaStreamSynchronize(stream);
112
113 /* Second vector operation block */
115 <<<vec_nblcks, vec_nthrds, 0, stream>>>((real *) xbar,
116 (const real **) xx,
117 (real *) b,
118 (const real **) bb,
119 (const real *) alpha,
120 *j, *n);
121 }
122
123 void cuda_project_ortho(void *alpha, void * b, void *xx, void *bb,
124 void *w, void *xm, int *j, int *n, real *nrm){
125
126 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
127
128 int pow2 = 1;
129 while(pow2 < (*j)){
130 pow2 = 2*pow2;
131 }
132 const int nt = 1024/pow2;
133 const dim3 glsc3_nthrds(pow2, nt, 1);
134 const dim3 glsc3_nblcks(((*n)+nt - 1)/nt, 1, 1);
135 const int glsc3_nb = ((*n) + nt - 1)/nt;
138
139 /* First glsc3_many call */
141 <<<glsc3_nblcks, glsc3_nthrds, 0, stream>>>((const real *) b,
142 (const real **) xx,
143 (const real *) w,
144 proj_bufred_d, *j, *n);
147 <<<(*j), 1024, 0, stream>>>(proj_bufred_d, glsc3_nb, *j);
149 CUDA_CHECK(cudaMemcpyAsync(alpha, proj_bufred_d, (*j) * sizeof(real),
150 cudaMemcpyDeviceToDevice, stream));
151
152 cudaStreamSynchronize(stream);
154
155 CUDA_CHECK(cudaMemcpyAsync(nrm, (real *) alpha + (*j - 1),
156 sizeof(real), cudaMemcpyDeviceToHost, stream));
157 (*nrm) = sqrt(*nrm);
158
159
160 const dim3 vec_nthrds(1024, 1, 1);
161 const dim3 vec_nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
162
163 /* First vector operation block */
165 <<<vec_nblcks, vec_nthrds, 0, stream>>>((real *) xm, (const real **) xx,
166 (real *) b, (const real **) bb,
167 (const real *) alpha, *j, *n);
168
169 /* Second glsc3_many call */
171 <<<glsc3_nblcks, glsc3_nthrds, 0, stream>>>((const real *) b,
172 (const real **) xx,
173 (const real *) w,
174 proj_bufred_d, *j, *n);
177 <<<(*j), 1024, 0, stream>>> (proj_bufred_d, glsc3_nb, *j);
179 CUDA_CHECK(cudaMemcpyAsync(alpha, proj_bufred_d, (*j) * sizeof(real),
180 cudaMemcpyDeviceToDevice, stream));
181
182 cudaStreamSynchronize(stream);
184
185 /* Second vector operation block */
187 <<<vec_nblcks, vec_nthrds, 0, stream>>>((real *) xm, (const real **) xx,
188 (real *) b, (const real **) bb,
189 (const real *) alpha, *j, *n);
190
191 }
192
193}
194
void cuda_buffer_reserve(cuda_buffer_t *buf, size_t size)
Definition buffer.cu:50
__global__ void ale_add_kinematics_kernel(const int n, T *__restrict__ wx, T *__restrict__ wy, T *__restrict__ wz, const T *__restrict__ x_ref, const T *__restrict__ y_ref, const T *__restrict__ z_ref, const T *__restrict__ phi, const T *__restrict__ x, const T *__restrict__ y, const T *__restrict__ z, const kinematics_params_t kin_params)
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ w
const int j
#define CUDA_BUFFER_INIT_DEV
Definition buffer.h:64
#define CUDA_CHECK(err)
Definition check.h:6
double real
#define DEVICE_MPI_SUM
void device_mpi_allreduce_inplace(void *buf_d, int count, int nbytes, int op)
cuda_buffer_t proj_bufred
Definition projection.cu:47
void cuda_project_ortho(void *alpha, void *b, void *xx, void *bb, void *w, void *xm, int *j, int *n, real *nrm)
void cuda_project_on(void *alpha, void *b, void *xx, void *bb, void *mult, void *xbar, int *j, int *n)
Definition projection.cu:54
void * dev
Definition buffer.h:53