Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
projection.hip
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
35#include <hip/hip_runtime.h>
37#include <device/hip/check.h>
38#include <device/hip/buffer.h>
39#include <device/hip/unified.h>
40
41#include "projection_kernel.h"
43
44
45/*
46 * Device-only reduction buffer, owned by the device layer and
47 * released on device teardown (hip_buffer_free_all in hip_finalize)
48 */
50
51extern "C" {
52
55
56 void hip_project_on(void *alpha, void * b, void *xx, void *bb, void *mult,
57 void *xbar, int *j, int *n){
58
59 int pow2 = 1;
60 while(pow2 < (*j)){
61 pow2 = 2*pow2;
62 }
63 const int nt = 1024/pow2;
64 const dim3 glsc3_nthrds(pow2, nt, 1);
65 const dim3 glsc3_nblcks(((*n)+nt - 1)/nt, 1, 1);
66 const int glsc3_nb = ((*n) + nt - 1)/nt;
67 hip_buffer_reserve(&proj_bufred, (*j)*glsc3_nb*sizeof(real));
68 real *proj_bufred_d = (real *) proj_bufred.dev;
69
70 /* First glsc3_many call */
71 hipLaunchKernelGGL(HIP_KERNEL_NAME( glsc3_many_kernel<real> ),
72 glsc3_nblcks, glsc3_nthrds,
73 0, (hipStream_t) glb_cmd_queue,
74 (const real *) b, (const real **) xx,
75 (const real *) mult, proj_bufred_d, *j, *n);
76 HIP_CHECK(hipGetLastError());
77 hipLaunchKernelGGL(HIP_KERNEL_NAME( glsc3_reduce_kernel<real> ),
78 (*j), 1024, 0 , (hipStream_t) glb_cmd_queue,
79 proj_bufred_d, glsc3_nb, *j);
80 HIP_CHECK(hipGetLastError());
81 HIP_CHECK(hipMemcpyAsync(alpha, proj_bufred_d, (*j) * sizeof(real),
82 hipMemcpyDeviceToDevice,
83 (hipStream_t) glb_cmd_queue));
84 if (hip_zerocopy()) {
85 /* xbar may alias pageable host memory, which hipMemset rejects;
86 zero with a kernel instead (works on any pointer) */
87 const dim3 zero_nthrds(1024, 1, 1);
88 const dim3 zero_nblcks((*n) > 0 ? ((*n) + 1024 - 1) / 1024 : 1, 1, 1);
89
90 hipLaunchKernelGGL(HIP_KERNEL_NAME(cfill_kernel<real>),
91 zero_nblcks, zero_nthrds, 0,
92 (hipStream_t) glb_cmd_queue,
93 (real *) xbar, (real) 0.0, *n);
94 HIP_CHECK(hipGetLastError());
95 }
96 else {
97 HIP_CHECK(hipMemsetAsync(xbar, 0, (*n) * sizeof(real)));
98 }
99
100 HIP_CHECK(hipStreamSynchronize((hipStream_t) glb_cmd_queue));
102
103 const dim3 vec_nthrds(1024, 1, 1);
104 const dim3 vec_nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
105
106 /* First vector operation block */
107 hipLaunchKernelGGL(HIP_KERNEL_NAME( project_on_vec_kernel<real> ),
108 vec_nblcks, vec_nthrds,
109 0, (hipStream_t) glb_cmd_queue, (real *) xbar,
110 (const real **) xx, (real *) b, (const real **) bb,
111 (const real *) alpha, *j, *n);
112 /* Second glsc3_many call */
113 hipLaunchKernelGGL(HIP_KERNEL_NAME( glsc3_many_kernel<real> ),
114 glsc3_nblcks, glsc3_nthrds,
115 0, (hipStream_t) glb_cmd_queue,
116 (const real *) b, (const real **) xx,
117 (const real *) mult, proj_bufred_d, *j, *n);
118 HIP_CHECK(hipGetLastError());
119 hipLaunchKernelGGL(HIP_KERNEL_NAME( glsc3_reduce_kernel<real> ),
120 (*j), 1024, 0, (hipStream_t) glb_cmd_queue,
121 proj_bufred_d, glsc3_nb, *j);
122 HIP_CHECK(hipGetLastError());
123 HIP_CHECK(hipMemcpyAsync(alpha, proj_bufred_d, (*j) * sizeof(real),
124 hipMemcpyDeviceToDevice,
125 (hipStream_t) glb_cmd_queue));
126
127 HIP_CHECK(hipStreamSynchronize((hipStream_t) glb_cmd_queue));
129
130 /* Second vector operation block */
131 hipLaunchKernelGGL(HIP_KERNEL_NAME(project_on_vec_kernel<real> ),
132 vec_nblcks, vec_nthrds,
133 0, (hipStream_t) glb_cmd_queue, (real *) xbar,
134 (const real **) xx, (real *) b, (const real **) bb,
135 (const real *) alpha, *j, *n);
136 }
137
138 void hip_project_ortho(void *alpha, void * b, void *xx, void *bb,
139 void *w, void *xm, int *j, int *n, real *nrm){
140
141 int pow2 = 1;
142 while(pow2 < (*j)){
143 pow2 = 2*pow2;
144 }
145 const int nt = 1024/pow2;
146 const dim3 glsc3_nthrds(pow2, nt, 1);
147 const dim3 glsc3_nblcks(((*n)+nt - 1)/nt, 1, 1);
148 const int glsc3_nb = ((*n) + nt - 1)/nt;
149 hip_buffer_reserve(&proj_bufred, (*j)*glsc3_nb*sizeof(real));
150 real *proj_bufred_d = (real *) proj_bufred.dev;
151
152 /* First glsc3_many call */
153 hipLaunchKernelGGL(HIP_KERNEL_NAME( glsc3_many_kernel<real> ),
154 glsc3_nblcks, glsc3_nthrds,
155 0, (hipStream_t) glb_cmd_queue,
156 (const real *) b, (const real **) xx,
157 (const real *) w, proj_bufred_d, *j, *n);
158 HIP_CHECK(hipGetLastError());
159 hipLaunchKernelGGL(HIP_KERNEL_NAME( glsc3_reduce_kernel<real> ),
160 (*j), 1024, 0 , (hipStream_t) glb_cmd_queue,
161 proj_bufred_d, glsc3_nb, *j);
162 HIP_CHECK(hipGetLastError());
163 HIP_CHECK(hipMemcpyAsync(alpha, proj_bufred_d, (*j) * sizeof(real),
164 hipMemcpyDeviceToDevice,
165 (hipStream_t) glb_cmd_queue));
166
167 HIP_CHECK(hipStreamSynchronize((hipStream_t) glb_cmd_queue));
169
170 HIP_CHECK(hipMemcpyAsync(nrm, (real *) alpha + (*j - 1),
171 sizeof(real), hipMemcpyDeviceToHost,
172 (hipStream_t) glb_cmd_queue));
173 (*nrm) = sqrt(*nrm);
174
175
176 const dim3 vec_nthrds(1024, 1, 1);
177 const dim3 vec_nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
178
179 /* First vector operation block */
180 hipLaunchKernelGGL( HIP_KERNEL_NAME( project_ortho_vec_kernel<real> ),
181 vec_nblcks, vec_nthrds, 0, (hipStream_t) glb_cmd_queue,
182 (real *) xm, (const real **) xx,
183 (real *) b, (const real **) bb,
184 (const real *) alpha, *j, *n);
185
186 /* Second glsc3_many call */
187 hipLaunchKernelGGL(HIP_KERNEL_NAME( glsc3_many_kernel<real> ),
188 glsc3_nblcks, glsc3_nthrds, 0, (hipStream_t) glb_cmd_queue,
189 (const real *) b, (const real **) xx,
190 (const real *) w, proj_bufred_d, *j, *n);
191 HIP_CHECK(hipGetLastError());
192 hipLaunchKernelGGL(HIP_KERNEL_NAME( glsc3_reduce_kernel<real> ),
193 (*j), 1024, 0 , (hipStream_t) glb_cmd_queue,
194 proj_bufred_d, glsc3_nb, *j);
195 HIP_CHECK(hipGetLastError());
196 HIP_CHECK(hipMemcpyAsync(alpha, proj_bufred_d, (*j) * sizeof(real),
197 hipMemcpyDeviceToDevice,
198 (hipStream_t) glb_cmd_queue));
199
200 HIP_CHECK(hipStreamSynchronize((hipStream_t) glb_cmd_queue));
202
203 /* Second vector operation block */
204 hipLaunchKernelGGL( HIP_KERNEL_NAME( project_ortho_vec_kernel<real> ),
205 vec_nblcks, vec_nthrds, 0, (hipStream_t) glb_cmd_queue,
206 (real *) xm, (const real **) xx,
207 (real *) b, (const real **) bb,
208 (const real *) alpha, *j, *n);
209
210 }
211
212}
213
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ w
const int j
__global__ void glsc3_many_kernel(const T *a, const T **b, const T *c, T_acc *buf_h, const int j, const int n)
__global__ void glsc3_reduce_kernel(T_acc *bufred, const int n, const int j)
__global__ void cfill_kernel(T *__restrict__ a, const T c, const int n)
__global__ void project_ortho_vec_kernel(T *__restrict__ x, const T **xx, T *__restrict__ y, const T **yy, const T *__restrict__ alpha, const int p_cur, const int n)
__global__ void project_on_vec_kernel(T *__restrict__ x, const T **xx, T *__restrict__ y, const T **yy, const T *__restrict__ alpha, const int p_cur, const int n)
double real
void * glb_cmd_queue
#define DEVICE_MPI_SUM
void device_mpi_allreduce_inplace(void *buf_d, int count, int nbytes, int op)
void hip_buffer_reserve(hip_buffer_t *buf, size_t size)
Definition buffer.hip:47
struct hip_buffer hip_buffer_t
Definition buffer.h:59
#define HIP_BUFFER_INIT_DEV
Definition buffer.h:62
#define HIP_CHECK(err)
Definition check.h:8
cuda_buffer_t proj_bufred
Definition projection.cu:47
void hip_project_ortho(void *alpha, void *b, void *xx, void *bb, void *w, void *xm, int *j, int *n, real *nrm)
void hip_project_on(void *alpha, void *b, void *xx, void *bb, void *mult, void *xbar, int *j, int *n)
int hip_zerocopy(void)
Definition unified.hip:122