Neko  0.8.99
A portable framework for high-order spectral element flow simulations
pipecg_aux.cu
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 "pipecg_kernel.h"
36 #include <device/device_config.h>
37 #include <device/cuda/check.h>
38 
42 real *buf = NULL;
43 real *buf_d1 = NULL;
44 real *buf_d2 = NULL;
45 real *buf_d3 = NULL;
46 int buf_len = 0;
47 
48 extern "C" {
49 
50  void cuda_cg_update_xp(void *x, void *p, void *u, void *alpha, void *beta,
51  int *p_cur, int *p_space, int *n) {
52 
53  const dim3 nthrds(1024, 1, 1);
54  const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
55  const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
56 
57  cg_update_xp_kernel<real>
58  <<<nblcks, nthrds, 0, stream>>>((real *) x, (real *) p,
59  (real **) u, (real *) alpha,
60  (real *) beta, *p_cur, *p_space, *n);
61  CUDA_CHECK(cudaGetLastError());
62 
63  }
64 
65  void cuda_pipecg_vecops(void *p, void *q, void *r, void *s, void *u1,
66  void *u2, void *w, void *z,
67  void *ni, void *mi, real *alpha,
68  real *beta, void *mult,
69  real *reduction, int *n) {
70 
71  const dim3 nthrds(1024, 1, 1);
72  const dim3 nblcks(((*n)+1024 - 1)/ 1024, 1, 1);
73  const int nb = ((*n) + 1024 - 1)/ 1024;
74  const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
75 
76  if (buf != NULL && buf_len < nb) {
77  CUDA_CHECK(cudaFreeHost(buf));
78  CUDA_CHECK(cudaFree(buf_d1));
79  CUDA_CHECK(cudaFree(buf_d2));
80  CUDA_CHECK(cudaFree(buf_d3));
81  buf = NULL;
82  }
83 
84  if (buf == NULL){
85  CUDA_CHECK(cudaMallocHost(&buf, 3*sizeof(real)));
86  CUDA_CHECK(cudaMalloc(&buf_d1, nb*sizeof(real)));
87  CUDA_CHECK(cudaMalloc(&buf_d2, nb*sizeof(real)));
88  CUDA_CHECK(cudaMalloc(&buf_d3, nb*sizeof(real)));
89  buf_len = nb;
90  }
91 
92  pipecg_vecops_kernel<real>
93  <<<nblcks, nthrds, 0, stream>>>((real *) p, (real *) q,
94  (real *) r, (real *) s,
95  (real *) u1, (real *) u2,
96  (real *) w, (real *) z,
97  (real *) ni, (real *) mi,
98  *alpha, *beta, (real *)mult,
99  buf_d1, buf_d2, buf_d3, *n);
100 
101  CUDA_CHECK(cudaGetLastError());
102 
103  reduce_kernel<real><<<1, 1024, 0, stream>>>(buf_d1, nb);
104  CUDA_CHECK(cudaGetLastError());
105  reduce_kernel<real><<<1, 1024, 0, stream>>>(buf_d2, nb);
106  CUDA_CHECK(cudaGetLastError());
107  reduce_kernel<real><<<1, 1024, 0, stream>>>(buf_d3, nb);
108  CUDA_CHECK(cudaGetLastError());
109 
110  CUDA_CHECK(cudaMemcpyAsync(&buf[0], buf_d1, sizeof(real),
111  cudaMemcpyDeviceToHost, stream));
112  CUDA_CHECK(cudaGetLastError());
113  CUDA_CHECK(cudaMemcpyAsync(&buf[1], buf_d2, sizeof(real),
114  cudaMemcpyDeviceToHost, stream));
115  CUDA_CHECK(cudaGetLastError());
116  CUDA_CHECK(cudaMemcpyAsync(&buf[2], buf_d3, sizeof(real),
117  cudaMemcpyDeviceToHost, stream));
118  CUDA_CHECK(cudaGetLastError());
119 
120  CUDA_CHECK(cudaStreamSynchronize(stream));
121 
122  reduction[0] = buf[0];
123  reduction[1] = buf[1];
124  reduction[2] = buf[2];
125 
126  }
127 }
128 
__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
#define CUDA_CHECK(err)
Definition: check.h:6
double real
Definition: device_config.h:12
void * glb_cmd_queue
void cuda_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.cu:65
real * buf
Definition: pipecg_aux.cu:42
int buf_len
Definition: pipecg_aux.cu:46
void cuda_cg_update_xp(void *x, void *p, void *u, void *alpha, void *beta, int *p_cur, int *p_space, int *n)
Definition: pipecg_aux.cu:50
real * buf_d2
Definition: pipecg_aux.cu:44
real * buf_d1
Definition: pipecg_aux.cu:43
real * buf_d3
Definition: pipecg_aux.cu:45