Neko  0.8.99
A portable framework for high-order spectral element flow simulations
pipecg_aux.hip
Go to the documentation of this file.
1 /*
2  Copyright (c) 2021-2022, 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 <hip/hip_runtime.h>
36 #include <device/device_config.h>
37 #include <device/hip/check.h>
38 #include "pipecg_kernel.h"
39 
43 real *buf = NULL;
44 real *buf_d1 = NULL;
45 real *buf_d2 = NULL;
46 real *buf_d3 = NULL;
47 int buf_len = 0;
48 
49 extern "C" {
50 
51  void hip_cg_update_xp(void *x, void *p, void *u, void *alpha, void *beta,
52  int *p_cur, int *p_space, int *n) {
53 
54  const dim3 nthrds(1024, 1, 1);
55  const dim3 nblcks(((*n) + 1024 - 1) / 1024, 1, 1);
56 
57  hipLaunchKernelGGL(HIP_KERNEL_NAME(cg_update_xp_kernel<real>),
58  nblcks, nthrds, 0, (hipStream_t) glb_cmd_queue,
59  (real *) x, (real *) p, (real **) u, (real *) alpha,
60  (real *) beta, *p_cur, *p_space, *n);
61  HIP_CHECK(hipGetLastError());
62  }
63 
64  void hip_pipecg_vecops(void *p, void *q, void *r, void *s, void *u1,
65  void *u2, void *w, void *z,
66  void *ni, void *mi, real *alpha,
67  real *beta, void *mult,
68  real *reduction, int *n) {
69 
70  const dim3 nthrds(1024, 1, 1);
71  const dim3 nblcks(((*n) + 1024 - 1)/ 1024, 1, 1);
72  const int nb = ((*n) + 1024 - 1)/ 1024;
73  const hipStream_t stream = (hipStream_t) glb_cmd_queue;
74 
75  if (buf != NULL && buf_len < nb) {
76  HIP_CHECK(hipHostFree(buf));
77  HIP_CHECK(hipFree(buf_d1));
78  HIP_CHECK(hipFree(buf_d2));
79  HIP_CHECK(hipFree(buf_d3));
80  buf = NULL;
81  }
82 
83  if (buf == NULL){
84  HIP_CHECK(hipHostMalloc(&buf, nb*sizeof(real)));
85  HIP_CHECK(hipMalloc(&buf_d1, nb*sizeof(real)));
86  HIP_CHECK(hipMalloc(&buf_d2, nb*sizeof(real)));
87  HIP_CHECK(hipMalloc(&buf_d3, nb*sizeof(real)));
88  buf_len = nb;
89  }
90 
91  hipLaunchKernelGGL(HIP_KERNEL_NAME(pipecg_vecops_kernel<real>),
92  nblcks, nthrds, 0, stream,
93  (real *) p, (real *) q, (real *) r, (real *) s,
94  (real *) u1, (real *) u2, (real *) w, (real *) z,
95  (real *) ni, (real *) mi, *alpha, *beta,
96  (real *) mult, buf_d1, buf_d2, buf_d3, *n);
97  HIP_CHECK(hipGetLastError());
98 
99  hipLaunchKernelGGL(HIP_KERNEL_NAME(reduce_kernel<real>),
100  1, 1024, 0 , stream, buf_d1, nb);
101  HIP_CHECK(hipGetLastError());
102  hipLaunchKernelGGL(HIP_KERNEL_NAME(reduce_kernel<real>),
103  1, 1024, 0 , stream, buf_d2, nb);
104  HIP_CHECK(hipGetLastError());
105  hipLaunchKernelGGL(HIP_KERNEL_NAME(reduce_kernel<real>),
106  1, 1024, 0 , stream, buf_d3, nb);
107  HIP_CHECK(hipGetLastError());
108 
109  HIP_CHECK(hipMemcpyAsync(&buf[0], buf_d1, sizeof(real),
110  hipMemcpyDeviceToHost, stream));
111  HIP_CHECK(hipGetLastError());
112  HIP_CHECK(hipMemcpyAsync(&buf[1], buf_d2, sizeof(real),
113  hipMemcpyDeviceToHost, stream));
114  HIP_CHECK(hipGetLastError());
115  HIP_CHECK(hipMemcpyAsync(&buf[2], buf_d3, sizeof(real),
116  hipMemcpyDeviceToHost, stream));
117  HIP_CHECK(hipGetLastError());
118 
119  HIP_CHECK(hipStreamSynchronize(stream));
120 
121  reduction[0] = buf[0];
122  reduction[1] = buf[1];
123  reduction[2] = buf[2];
124 
125  }
126 }
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ w
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ u
__global__ void const T *__restrict__ x
Definition: cdtp_kernel.h:106
double real
Definition: device_config.h:12
void * glb_cmd_queue
#define HIP_CHECK(err)
Definition: check.h:8
real * buf
Definition: pipecg_aux.hip:43
void hip_cg_update_xp(void *x, void *p, void *u, void *alpha, void *beta, int *p_cur, int *p_space, int *n)
Definition: pipecg_aux.hip:51
void hip_pipecg_vecops(void *p, void *q, void *r, void *s, void *u1, void *u2, void *w, void *z, void *ni, void *mi, real *alpha, real *beta, void *mult, real *reduction, int *n)
Definition: pipecg_aux.hip:64
int buf_len
Definition: pipecg_aux.hip:47
real * buf_d2
Definition: pipecg_aux.hip:45
real * buf_d1
Definition: pipecg_aux.hip:44
real * buf_d3
Definition: pipecg_aux.hip:46