Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
bicgstab_aux.cu
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 "bicgstab_kernel.h"
38#include <device/cuda/check.h>
39#include <device/cuda/buffer.h>
40
41#ifdef HAVE_NVSHMEM
42#include <nvshmem.h>
43#include <nvshmemx.h>
44#endif
45
46extern "C" {
47
50
51#ifdef HAVE_NCCL
54#endif
55
61
66 static void bicgstab_reduce(real_xp *host, real_xp *dev, const int count,
67 cudaStream_t stream) {
68#ifdef HAVE_NCCL
69 device_nccl_allreduce(dev, dev, count, sizeof(real_xp),
70 DEVICE_NCCL_SUM, stream);
71 CUDA_CHECK(cudaMemcpyAsync(host, dev, count * sizeof(real_xp),
72 cudaMemcpyDeviceToHost, stream));
73 cudaStreamSynchronize(stream);
74#elif HAVE_NVSHMEM
75 if (sizeof(real_xp) == sizeof(float)) {
76 nvshmemx_float_sum_reduce_on_stream(NVSHMEM_TEAM_WORLD, (float *) dev,
77 (float *) dev, count, stream);
78 }
79 else if (sizeof(real_xp) == sizeof(double)) {
80 nvshmemx_double_sum_reduce_on_stream(NVSHMEM_TEAM_WORLD, (double *) dev,
81 (double *) dev, count, stream);
82 }
83 CUDA_CHECK(cudaMemcpyAsync(host, dev, count * sizeof(real_xp),
84 cudaMemcpyDeviceToHost, stream));
85 cudaStreamSynchronize(stream);
86#elif HAVE_DEVICE_MPI
87 cudaStreamSynchronize(stream);
88 device_mpi_allreduce(dev, host, count, sizeof(real_xp), DEVICE_MPI_SUM);
89#else
90 CUDA_CHECK(cudaMemcpyAsync(host, dev, count * sizeof(real_xp),
91 cudaMemcpyDeviceToHost, stream));
92 cudaStreamSynchronize(stream);
93#endif
94 }
95
100 void cuda_bicgstab_update_p(void *p, void *r, void *v, real *beta,
101 real *omega, int *n) {
102
103 if (*n <= 0)
104 return;
105
106 const dim3 nthrds(1024, 1, 1);
107 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
108 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
109
111 <<<nblcks, nthrds, 0, stream>>>((real *) p, (real *) r, (real *) v,
112 *beta, *omega, *n);
113 CUDA_CHECK(cudaGetLastError());
114 }
115
120 void cuda_bicgstab_product_and_norm(void *a, void *b, void *mult,
121 real_xp *res, int *n) {
122
123 const dim3 nthrds(1024, 1, 1);
124 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
125 const int nb = ((*n) + 1024 - 1) / 1024;
126 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
127
129 2 * (nb > 1 ? nb : 1) * sizeof(real_xp));
130 real_xp *host = (real_xp *) bicgstab_redbuf.host;
131 real_xp *dev = (real_xp *) bicgstab_redbuf.dev;
132
133 if (*n > 0) {
135 <<<nblcks, nthrds, 0, stream>>>((real *) a, (real *) b, (real *) mult,
136 dev, *n);
137 CUDA_CHECK(cudaGetLastError());
138 glsc3_reduce_kernel<real_xp><<<2, 1024, 0, stream>>>(dev, nb, 2);
139 CUDA_CHECK(cudaGetLastError());
140 }
141 else {
142 CUDA_CHECK(cudaMemsetAsync(dev, 0, 2 * sizeof(real_xp), stream));
143 }
144
145 bicgstab_reduce(host, dev, 2, stream);
146 res[0] = host[0];
147 res[1] = host[1];
148 }
149
154 real_xp cuda_bicgstab_part1(void *s, void *r, void *v, void *mult,
155 real *alpha, int *n) {
156
157 const dim3 nthrds(1024, 1, 1);
158 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
159 const int nb = ((*n) + 1024 - 1) / 1024;
160 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
161
163 2 * (nb > 1 ? nb : 1) * sizeof(real_xp));
164 real_xp *host = (real_xp *) bicgstab_redbuf.host;
165 real_xp *dev = (real_xp *) bicgstab_redbuf.dev;
166
167 if (*n > 0) {
169 <<<nblcks, nthrds, 0, stream>>>((real *) s, (real *) r, (real *) v,
170 (real *) mult, dev, *alpha, *n);
171 CUDA_CHECK(cudaGetLastError());
172 reduce_kernel<real_xp><<<1, 1024, 0, stream>>>(dev, nb);
173 CUDA_CHECK(cudaGetLastError());
174 }
175 else {
176 CUDA_CHECK(cudaMemsetAsync(dev, 0, sizeof(real_xp), stream));
177 }
178
179 bicgstab_reduce(host, dev, 1, stream);
180 return host[0];
181 }
182
189 void cuda_bicgstab_part2(void *x, void *r, void *p_hat, void *s_hat,
190 void *s, void *t, void *f, void *mult,
191 real *alpha, real *omega, real_xp *res, int *n) {
192
193 const dim3 nthrds(1024, 1, 1);
194 const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
195 const int nb = ((*n) + 1024 - 1) / 1024;
196 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
197
199 2 * (nb > 1 ? nb : 1) * sizeof(real_xp));
200 real_xp *host = (real_xp *) bicgstab_redbuf.host;
201 real_xp *dev = (real_xp *) bicgstab_redbuf.dev;
202
203 if (*n > 0) {
205 <<<nblcks, nthrds, 0, stream>>>((real *) x, (real *) r,
206 (real *) p_hat, (real *) s_hat,
207 (real *) s, (real *) t, (real *) f,
208 (real *) mult, dev,
209 *alpha, *omega, *n);
210 CUDA_CHECK(cudaGetLastError());
211 glsc3_reduce_kernel<real_xp><<<2, 1024, 0, stream>>>(dev, nb, 2);
212 CUDA_CHECK(cudaGetLastError());
213 }
214 else {
215 CUDA_CHECK(cudaMemsetAsync(dev, 0, 2 * sizeof(real_xp), stream));
216 }
217
218 bicgstab_reduce(host, dev, 2, stream);
219 res[0] = host[0];
220 res[1] = host[1];
221 }
222}
cuda_buffer_t bicgstab_redbuf
void cuda_bicgstab_product_and_norm(void *a, void *b, void *mult, real_xp *res, int *n)
static void bicgstab_reduce(real_xp *host, real_xp *dev, const int count, cudaStream_t stream)
void cuda_bicgstab_update_p(void *p, void *r, void *v, real *beta, real *omega, int *n)
void cuda_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)
real_xp cuda_bicgstab_part1(void *s, void *r, void *v, void *mult, real *alpha, int *n)
void cuda_buffer_reserve(cuda_buffer_t *buf, size_t size)
Definition buffer.cu:50
__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)
struct cuda_buffer cuda_buffer_t
Definition buffer.h:60
#define CUDA_BUFFER_INIT_SYMM
Definition buffer.h:63
__global__ void const T *__restrict__ x
#define CUDA_CHECK(err)
Definition check.h:6
__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)