Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
fusedcg_cpld_aux.hip
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 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/hip/check.h>
37#include <device/hip/buffer.h>
38#include "fusedcg_cpld_kernel.h"
39
45
46extern "C" {
47
50
51 void hip_fusedcg_cpld_part1(void *a1, void *a2, void *a3,
52 void *b1, void *b2, void *b3,
53 void *tmp, int *n) {
54
55 const dim3 nthrds(1024, 1, 1);
56 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
57 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
58
59 hipLaunchKernelGGL(HIP_KERNEL_NAME(fusedcg_cpld_part1_kernel<real>),
60 nblcks, nthrds, 0, stream,
61 (real *) a1, (real *) a2, (real *) a3,
62 (real *) b1, (real *) b2, (real *) b3,
63 (real *) tmp, *n);
64 HIP_CHECK(hipGetLastError());
65 }
66
67 void hip_fusedcg_cpld_update_p(void *p1, void *p2, void *p3,
68 void *z1, void *z2, void *z3,
69 void *po1, void *po2, void *po3,
70 real *beta, int *n) {
71
72 const dim3 nthrds(1024, 1, 1);
73 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
74 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
75
76
77 hipLaunchKernelGGL(HIP_KERNEL_NAME(fusedcg_cpld_update_p_kernel<real>),
78 nblcks, nthrds, 0, stream,
79 (real *) p1, (real *) p2, (real *) p3,
80 (real *) z1, (real *) z2, (real *) z3,
81 (real *) po1, (real *) po2, (real *) po3,
82 *beta, *n);
83 HIP_CHECK(hipGetLastError());
84
85 }
86
87 void hip_fusedcg_cpld_update_x(void *x1, void *x2, void *x3,
88 void *p1, void *p2, void *p3,
89 void *alpha, int *p_cur, int *n) {
90
91 const dim3 nthrds(1024, 1, 1);
92 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
93 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
94
95
96 hipLaunchKernelGGL(HIP_KERNEL_NAME(fusedcg_cpld_update_x_kernel<real>),
97 nblcks, nthrds, 0, stream,
98 (real *) x1, (real *) x2, (real *) x3,
99 (const real **) p1, (const real **) p2,
100 (const real **) p3, (const real *) alpha,
101 *p_cur, *n);
102 HIP_CHECK(hipGetLastError());
103 }
104
105 real hip_fusedcg_cpld_part2(void *a1, void *a2, void *a3, void *b,
106 void *c1, void *c2, void *c3, void *alpha_d ,
107 real *alpha, int *p_cur, int * n) {
108
109 const dim3 nthrds(1024, 1, 1);
110 const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
111 const int nb = ((*n) + 1024 - 1)/ 1024;
112 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
113
114 /* nb + 1 to fit the two-element host staging area for any nb */
115 hip_buffer_reserve(&fusedcg_cpld_redbuf, (nb + 1)*sizeof(real));
116 real *fusedcg_cpld_buf = (real *) fusedcg_cpld_redbuf.host;
117 real *fusedcg_cpld_buf_d = (real *) fusedcg_cpld_redbuf.dev;
118
119 /* Store alpha(p_cur) in pinned memory */
120 fusedcg_cpld_buf[1] = (*alpha);
121
122 /* Update alpha_d(p_cur) = alpha(p_cur) */
123 real *alpha_d_p_cur = ((real *) alpha_d) + ((*p_cur - 1));
124 HIP_CHECK(hipMemcpyAsync(alpha_d_p_cur, &fusedcg_cpld_buf[1],
125 sizeof(real), hipMemcpyHostToDevice,
126 stream));
127
128 hipLaunchKernelGGL(HIP_KERNEL_NAME(fusedcg_cpld_part2_kernel<real>),
129 nblcks, nthrds, 0, stream,
130 (real *) a1, (real *) a2, (real *) a3,
131 (real *) b, (real *) c1, (real *) c2,
132 (real *) c3, *alpha, fusedcg_cpld_buf_d,
133 *n);
134 HIP_CHECK(hipGetLastError());
135
136 hipLaunchKernelGGL(HIP_KERNEL_NAME(reduce_kernel<real>),
137 1, 1024, 0, stream, fusedcg_cpld_buf_d, nb);
138 HIP_CHECK(hipGetLastError());
139
140#ifdef HAVE_DEVICE_MPI
141 HIP_CHECK(hipStreamSynchronize(stream));
142 device_mpi_allreduce(fusedcg_cpld_buf_d, fusedcg_cpld_buf, 1,
143 sizeof(real), DEVICE_MPI_SUM);
144#else
145 HIP_CHECK(hipMemcpyAsync(fusedcg_cpld_buf, fusedcg_cpld_buf_d, sizeof(real),
146 hipMemcpyDeviceToHost, stream));
147 HIP_CHECK(hipStreamSynchronize(stream));
148#endif
149
150 return fusedcg_cpld_buf[0];
151 }
152}
153
__global__ void fusedcg_cpld_update_p_kernel(T *__restrict__ p1, T *__restrict__ p2, T *__restrict__ p3, const T *__restrict__ z1, const T *__restrict__ z2, const T *__restrict__ z3, const T *__restrict__ po1, const T *__restrict__ po2, const T *__restrict__ po3, const T beta, const int n)
__global__ void fusedcg_cpld_part2_kernel(T *__restrict__ a1, T *__restrict__ a2, T *__restrict__ a3, const T *__restrict__ b, const T *__restrict__ c1, const T *__restrict__ c2, const T *__restrict__ c3, const T alpha, T *buf_h, const int n)
__global__ void fusedcg_cpld_update_x_kernel(T *__restrict__ x1, T *__restrict__ x2, T *__restrict__ x3, const T **p1, const T **p2, const T **p3, const T *__restrict__ alpha, const int p_cur, const int n)
__global__ void fusedcg_cpld_part1_kernel(T *__restrict__ a1, T *__restrict__ a2, T *__restrict__ a3, T *__restrict__ b1, T *__restrict__ b2, T *__restrict__ b3, T *__restrict__ tmp, const int n)
__global__ void reduce_kernel(T *bufred, const int n)
double real
void * glb_cmd_queue
#define DEVICE_MPI_SUM
void device_mpi_allreduce(void *buf_d, void *buf, int count, int nbytes, int op)
cuda_buffer_t fusedcg_cpld_redbuf
void hip_fusedcg_cpld_update_x(void *x1, void *x2, void *x3, void *p1, void *p2, void *p3, void *alpha, int *p_cur, int *n)
void hip_fusedcg_cpld_update_p(void *p1, void *p2, void *p3, void *z1, void *z2, void *z3, void *po1, void *po2, void *po3, real *beta, int *n)
real hip_fusedcg_cpld_part2(void *a1, void *a2, void *a3, void *b, void *c1, void *c2, void *c3, void *alpha_d, real *alpha, int *p_cur, int *n)
void hip_fusedcg_cpld_part1(void *a1, void *a2, void *a3, void *b1, void *b2, void *b3, void *tmp, int *n)
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