Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
bicgstab_aux.hip
Go to the documentation of this file.
1/*
2 Copyright (c) 2026, 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 <stdio.h>
36#include <hip/hip_runtime.h>
37#include "bicgstab_kernel.h"
39#include <device/hip/check.h>
40#include <device/hip/buffer.h>
41
42extern "C" {
43
46
47#ifdef HAVE_RCCL
50#endif
51
57
62 static void bicgstab_reduce(real_xp *host, real_xp *dev, const int count,
63 hipStream_t stream) {
64#ifdef HAVE_RCCL
65 device_nccl_allreduce(dev, dev, count, sizeof(real_xp),
66 DEVICE_NCCL_SUM, stream);
67 HIP_CHECK(hipMemcpyAsync(host, dev, count * sizeof(real_xp),
68 hipMemcpyDeviceToHost, stream));
69 HIP_CHECK(hipStreamSynchronize(stream));
70#elif HAVE_DEVICE_MPI
71 HIP_CHECK(hipStreamSynchronize(stream));
72 device_mpi_allreduce(dev, host, count, sizeof(real_xp), DEVICE_MPI_SUM);
73#else
74 HIP_CHECK(hipMemcpyAsync(host, dev, count * sizeof(real_xp),
75 hipMemcpyDeviceToHost, stream));
76 HIP_CHECK(hipStreamSynchronize(stream));
77#endif
78 }
79
84 void hip_bicgstab_update_p(void *p, void *r, void *v, real *beta,
85 real *omega, int *n) {
86
87 if (*n <= 0)
88 return;
89
90 const dim3 nthrds(1024, 1, 1);
91 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
92 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
93
94 hipLaunchKernelGGL(HIP_KERNEL_NAME(bicgstab_update_p_kernel<real>),
95 nblcks, nthrds, 0, stream,
96 (real *) p, (real *) r, (real *) v,
97 *beta, *omega, *n);
98 HIP_CHECK(hipGetLastError());
99 }
100
105 void hip_bicgstab_product_and_norm(void *a, void *b, void *mult,
106 real_xp *res, int *n) {
107
108 const dim3 nthrds(1024, 1, 1);
109 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
110 const int nb = ((*n) + 1024 - 1) / 1024;
111 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
112
114 2 * (nb > 1 ? nb : 1) * sizeof(real_xp));
115 real_xp *host = (real_xp *) bicgstab_redbuf.host;
116 real_xp *dev = (real_xp *) bicgstab_redbuf.dev;
117
118 if (*n > 0) {
119 hipLaunchKernelGGL(
121 nblcks, nthrds, 0, stream,
122 (real *) a, (real *) b, (real *) mult, dev, *n);
123 HIP_CHECK(hipGetLastError());
124 hipLaunchKernelGGL(HIP_KERNEL_NAME(glsc3_reduce_kernel<real_xp>),
125 2, 1024, 0, stream, dev, nb, 2);
126 HIP_CHECK(hipGetLastError());
127 }
128 else {
129 HIP_CHECK(hipMemsetAsync(dev, 0, 2 * sizeof(real_xp), stream));
130 }
131
132 bicgstab_reduce(host, dev, 2, stream);
133 res[0] = host[0];
134 res[1] = host[1];
135 }
136
141 real_xp hip_bicgstab_part1(void *s, void *r, void *v, void *mult,
142 real *alpha, int *n) {
143
144 const dim3 nthrds(1024, 1, 1);
145 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
146 const int nb = ((*n) + 1024 - 1) / 1024;
147 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
148
150 2 * (nb > 1 ? nb : 1) * sizeof(real_xp));
151 real_xp *host = (real_xp *) bicgstab_redbuf.host;
152 real_xp *dev = (real_xp *) bicgstab_redbuf.dev;
153
154 if (*n > 0) {
155 hipLaunchKernelGGL(HIP_KERNEL_NAME(bicgstab_part1_kernel<real, real_xp>),
156 nblcks, nthrds, 0, stream,
157 (real *) s, (real *) r, (real *) v,
158 (real *) mult, dev, *alpha, *n);
159 HIP_CHECK(hipGetLastError());
160 hipLaunchKernelGGL(HIP_KERNEL_NAME(reduce_kernel<real_xp>),
161 1, 1024, 0, stream, dev, nb);
162 HIP_CHECK(hipGetLastError());
163 }
164 else {
165 HIP_CHECK(hipMemsetAsync(dev, 0, sizeof(real_xp), stream));
166 }
167
168 bicgstab_reduce(host, dev, 1, stream);
169 return host[0];
170 }
171
178 void hip_bicgstab_part2(void *x, void *r, void *p_hat, void *s_hat,
179 void *s, void *t, void *f, void *mult,
180 real *alpha, real *omega, real_xp *res, int *n) {
181
182 const dim3 nthrds(1024, 1, 1);
183 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
184 const int nb = ((*n) + 1024 - 1) / 1024;
185 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
186
188 2 * (nb > 1 ? nb : 1) * sizeof(real_xp));
189 real_xp *host = (real_xp *) bicgstab_redbuf.host;
190 real_xp *dev = (real_xp *) bicgstab_redbuf.dev;
191
192 if (*n > 0) {
193 hipLaunchKernelGGL(HIP_KERNEL_NAME(bicgstab_part2_kernel<real, real_xp>),
194 nblcks, nthrds, 0, stream,
195 (real *) x, (real *) r,
196 (real *) p_hat, (real *) s_hat,
197 (real *) s, (real *) t, (real *) f,
198 (real *) mult, dev, *alpha, *omega, *n);
199 HIP_CHECK(hipGetLastError());
200 hipLaunchKernelGGL(HIP_KERNEL_NAME(glsc3_reduce_kernel<real_xp>),
201 2, 1024, 0, stream, dev, nb, 2);
202 HIP_CHECK(hipGetLastError());
203 }
204 else {
205 HIP_CHECK(hipMemsetAsync(dev, 0, 2 * sizeof(real_xp), stream));
206 }
207
208 bicgstab_reduce(host, dev, 2, stream);
209 res[0] = host[0];
210 res[1] = host[1];
211 }
212}
cuda_buffer_t bicgstab_redbuf
void hip_bicgstab_product_and_norm(void *a, void *b, void *mult, real_xp *res, int *n)
void hip_bicgstab_update_p(void *p, void *r, void *v, real *beta, real *omega, int *n)
void hip_bicgstab_part2(void *x, void *r, void *p_hat, void *s_hat, void *s, void *t, void *f, void *mult, real *alpha, real *omega, real_xp *res, int *n)
static void bicgstab_reduce(real_xp *host, real_xp *dev, const int count, hipStream_t stream)
real_xp hip_bicgstab_part1(void *s, void *r, void *v, void *mult, real *alpha, int *n)
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
__global__ void bicgstab_update_p_kernel(T *__restrict__ p, const T *__restrict__ r, const T *__restrict__ v, const T beta, const T omega, const int n)
__global__ void bicgstab_product_and_norm_kernel(const T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ mult, T_acc *__restrict__ buf_h, const int n)
__global__ void bicgstab_part1_kernel(T *__restrict__ s, const T *__restrict__ r, const T *__restrict__ v, const T *__restrict__ mult, T_acc *__restrict__ buf_h, const T alpha, const int n)
__global__ void bicgstab_part2_kernel(T *__restrict__ x, T *__restrict__ r, const T *__restrict__ p_hat, const T *__restrict__ s_hat, const T *__restrict__ s, const T *__restrict__ t, const T *__restrict__ f, const T *__restrict__ mult, T_acc *__restrict__ buf_h, const T alpha, const T omega, const int n)
__global__ void const T *__restrict__ x
__global__ void reduce_kernel(T *bufred, const int n)
__global__ void glsc3_reduce_kernel(T_acc *bufred, const int n, const int j)
double real
void * glb_cmd_queue
double real_xp
#define DEVICE_MPI_SUM
void device_mpi_allreduce(void *buf_d, void *buf, int count, int nbytes, int op)
#define DEVICE_NCCL_SUM
void device_nccl_allreduce(void *sbuf_d, void *rbuf_d, int count, int nbytes, int op, void *stream)
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
Definition buffer.h:61
#define HIP_CHECK(err)
Definition check.h:8