Neko 1.99.9
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));
70#elif HAVE_DEVICE_MPI
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));
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
95 nblcks, nthrds, 0, stream,
96 (real *) p, (real *) r, (real *) v,
97 *beta, *omega, *n);
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));
117
118 if (*n > 0) {
121 nblcks, nthrds, 0, stream,
122 (real *) a, (real *) b, (real *) mult, dev, *n);
125 2, 1024, 0, stream, dev, nb, 2);
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));
153
154 if (*n > 0) {
156 nblcks, nthrds, 0, stream,
157 (real *) s, (real *) r, (real *) v,
158 (real *) mult, dev, *alpha, *n);
161 1, 1024, 0, stream, dev, nb);
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));
191
192 if (*n > 0) {
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);
201 2, 1024, 0, stream, dev, nb, 2);
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}
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)
hip_buffer_t bicgstab_redbuf
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 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__ v
__global__ void const T *__restrict__ x
double real
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
#define HIP_BUFFER_INIT
Definition buffer.h:61
#define HIP_CHECK(err)
Definition check.h:8
void * host
Definition buffer.h:52
void * dev
Definition buffer.h:53