Neko  0.8.1
A portable framework for high-order spectral element flow simulations
fusedcg_aux.cu
Go to the documentation of this file.
1 /*
2  Copyright (c) 2021-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 "fusedcg_kernel.h"
36 #include <device/device_config.h>
37 #include <device/cuda/check.h>
38 
42 real *fusedcg_buf = NULL;
45 
46 extern "C" {
47 
50 
51 
52 
53  void cuda_fusedcg_update_p(void *p, void *z, void *po, real *beta, int *n) {
54 
55  const dim3 nthrds(1024, 1, 1);
56  const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
57  const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
58 
59  fusedcg_update_p_kernel<real>
60  <<<nblcks, nthrds, 0, stream>>>((real *) p, (real *) z,
61  (real *) po, *beta, *n);
62  CUDA_CHECK(cudaGetLastError());
63 
64  }
65 
66  void cuda_fusedcg_update_x(void *x, void *p, void *alpha, int *p_cur, int *n) {
67 
68  const dim3 nthrds(1024, 1, 1);
69  const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
70  const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
71 
72  fusedcg_update_x_kernel<real>
73  <<<nblcks, nthrds, 0, stream>>>((real *) x, (const real **) p,
74  (const real *) alpha, *p_cur, *n);
75  CUDA_CHECK(cudaGetLastError());
76  }
77 
78 
79  real cuda_fusedcg_part2(void *a, void *b, void *c,
80  void *alpha_d , real *alpha, int *p_cur, int * n) {
81 
82  const dim3 nthrds(1024, 1, 1);
83  const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
84  const int nb = ((*n) + 1024 - 1)/ 1024;
85  const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
86 
87  if (fusedcg_buf != NULL && fusedcg_buf_len < nb) {
88  CUDA_CHECK(cudaFreeHost(fusedcg_buf));
89  CUDA_CHECK(cudaFree(fusedcg_buf_d));
90  fusedcg_buf = NULL;
91  }
92 
93  if (fusedcg_buf == NULL){
94  CUDA_CHECK(cudaMallocHost(&fusedcg_buf, 2*sizeof(real)));
95  CUDA_CHECK(cudaMalloc(&fusedcg_buf_d, nb*sizeof(real)));
96  fusedcg_buf_len = nb;
97  }
98 
99  /* Store alpha(p_cur) in pinned memory */
100  fusedcg_buf[1] = (*alpha);
101 
102  /* Update alpha_d(p_cur) = alpha(p_cur) */
103  real *alpha_d_p_cur = ((real *) alpha_d) + ((*p_cur - 1));
104  CUDA_CHECK(cudaMemcpyAsync(alpha_d_p_cur, &fusedcg_buf[1], sizeof(real),
105  cudaMemcpyHostToDevice, stream));
106 
107 
108  fusedcg_part2_kernel<real>
109  <<<nblcks, nthrds, 0, stream>>>((real *) a, (real *) b,
110  (real *) c, *alpha,
111  fusedcg_buf_d, * n);
112  CUDA_CHECK(cudaGetLastError());
113 
114  reduce_kernel<real><<<1, 1024, 0, stream>>>(fusedcg_buf_d, nb);
115  CUDA_CHECK(cudaGetLastError());
116 
117 #ifdef HAVE_DEVICE_MPI
118  cudaStreamSynchronize(stream);
120  sizeof(real), DEVICE_MPI_SUM);
121 #else
122  CUDA_CHECK(cudaMemcpyAsync(fusedcg_buf, fusedcg_buf_d, sizeof(real),
123  cudaMemcpyDeviceToHost, stream));
124  cudaStreamSynchronize(stream);
125 #endif
126 
127  return fusedcg_buf[0];
128  }
129 }
130 
__global__ void const T *__restrict__ x
Definition: cdtp_kernel.h:109
#define CUDA_CHECK(err)
Definition: check.h:6
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)
real cuda_fusedcg_part2(void *a, void *b, void *c, void *alpha_d, real *alpha, int *p_cur, int *n)
Definition: fusedcg_aux.cu:79
int fusedcg_buf_len
Definition: fusedcg_aux.cu:44
real * fusedcg_buf
Definition: fusedcg_aux.cu:42
void cuda_fusedcg_update_x(void *x, void *p, void *alpha, int *p_cur, int *n)
Definition: fusedcg_aux.cu:66
real * fusedcg_buf_d
Definition: fusedcg_aux.cu:43
void cuda_fusedcg_update_p(void *p, void *z, void *po, real *beta, int *n)
Definition: fusedcg_aux.cu:53