Neko  0.9.0
A portable framework for high-order spectral element flow simulations
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 
35 #include <device/device_config.h>
36 #include <device/hip/check.h>
37 #include "fusedcg_cpld_kernel.h"
38 
45 
46 extern "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  if (fusedcg_cpld_buf != NULL && fusedcg_cpld_buf_len < nb) {
115  HIP_CHECK(hipHostFree(fusedcg_cpld_buf));
116  HIP_CHECK(hipFree(fusedcg_cpld_buf_d));
117  fusedcg_cpld_buf = NULL;
118  }
119 
120  if (fusedcg_cpld_buf == NULL){
121  HIP_CHECK(hipHostMalloc(&fusedcg_cpld_buf, 2*sizeof(real)));
122  HIP_CHECK(hipMalloc(&fusedcg_cpld_buf_d, nb*sizeof(real)));
124  }
125 
126  /* Store alpha(p_cur) in pinned memory */
127  fusedcg_cpld_buf[1] = (*alpha);
128 
129  /* Update alpha_d(p_cur) = alpha(p_cur) */
130  real *alpha_d_p_cur = ((real *) alpha_d) + ((*p_cur - 1));
131  HIP_CHECK(hipMemcpyAsync(alpha_d_p_cur, &fusedcg_cpld_buf[1],
132  sizeof(real), hipMemcpyHostToDevice,
133  stream));
134 
135  hipLaunchKernelGGL(HIP_KERNEL_NAME(fusedcg_cpld_part2_kernel<real>),
136  nblcks, nthrds, 0, stream,
137  (real *) a1, (real *) a2, (real *) a3,
138  (real *) b, (real *) c1, (real *) c2,
139  (real *) c3, *alpha, fusedcg_cpld_buf_d,
140  *n);
141  HIP_CHECK(hipGetLastError());
142 
143  hipLaunchKernelGGL(HIP_KERNEL_NAME(reduce_kernel<real>),
144  1, 1024, 0, stream, fusedcg_cpld_buf_d, nb);
145  HIP_CHECK(hipGetLastError());
146 
147 #ifdef HAVE_DEVICE_MPI
148  hipStreamSynchronize(stream);
150  sizeof(real), DEVICE_MPI_SUM);
151 #else
152  HIP_CHECK(hipMemcpyAsync(fusedcg_cpld_buf, fusedcg_cpld_buf_d, sizeof(real),
153  hipMemcpyDeviceToHost, stream));
154  hipStreamSynchronize(stream);
155 #endif
156 
157  return fusedcg_cpld_buf[0];
158  }
159 }
160 
double real
Definition: device_config.h:12
void * glb_cmd_queue
#define DEVICE_MPI_SUM
Definition: device_mpi_op.h:9
void device_mpi_allreduce(void *buf_d, void *buf, int count, int nbytes, int op)
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)
int fusedcg_cpld_buf_len
real * fusedcg_cpld_buf
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)
real * fusedcg_cpld_buf_d
#define HIP_CHECK(err)
Definition: check.h:8