Neko  0.8.99
A portable framework for high-order spectral element flow simulations
gs.cu
Go to the documentation of this file.
1 /*
2  Copyright (c) 2021, 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 <climits>
36 #include <cstdio>
37 #include <device/device_config.h>
38 #include <device/cuda/check.h>
39 #include "gs_kernels.h"
40 
41 #define GS_OP_ADD 1
42 #define GS_OP_MUL 2
43 #define GS_OP_MIN 3
44 #define GS_OP_MAX 4
45 
46 extern "C" {
47 
51  void cuda_gather_kernel(void *v, int *m, int *o, void *dg,
52  void *u, int *n, void *gd, int *nb,
53  void *b, void *bo, int *op,
54  cudaStream_t stream) {
55 
56  if ((*m) == 0) return;
57 
58  const dim3 nthrds(1024, 1, 1);
59  const dim3 nblcks(((*m)+ 1024 - 1)/ 1024, 1, 1);
60 
61  switch (*op) {
62  case GS_OP_ADD:
63  gather_kernel_add<real>
64  <<<nblcks, nthrds, 0, stream>>>((real *) v, *m, *o, (int *) dg,
65  (real *) u, *n, (int *) gd,
66  *nb, (int *) b, (int *) bo);
67  CUDA_CHECK(cudaGetLastError());
68  break;
69  case GS_OP_MUL:
70  gather_kernel_mul<real>
71  <<<nblcks, nthrds, 0, stream>>>((real *) v, *m, *o, (int *) dg,
72  (real *) u, *n, (int *) gd,
73  *nb, (int *) b, (int *) bo);
74  CUDA_CHECK(cudaGetLastError());
75  break;
76  case GS_OP_MIN:
77  gather_kernel_min<real>
78  <<<nblcks, nthrds, 0, stream>>>((real *) v, *m, *o, (int *) dg,
79  (real *) u, *n, (int *) gd,
80  *nb, (int *) b, (int *) bo);
81  CUDA_CHECK(cudaGetLastError());
82  break;
83  case GS_OP_MAX:
84  gather_kernel_max<real>
85  <<<nblcks, nthrds, 0, stream>>>((real *) v, *m, *o, (int *) dg,
86  (real *) u, *n, (int *) gd,
87  *nb, (int *) b, (int *) bo);
88  CUDA_CHECK(cudaGetLastError());
89  break;
90  }
91  }
92 
96  void cuda_scatter_kernel(void *v, int *m, void *dg,
97  void *u, int *n, void *gd,
98  int *nb, void *b, void *bo,
99  cudaStream_t stream) {
100 
101  if ((*m) == 0) return;
102 
103  const dim3 nthrds(1024, 1, 1);
104  const dim3 nblcks(((*m)+1024 - 1)/ 1024, 1, 1);
105 
106  scatter_kernel<real>
107  <<<nblcks, nthrds, 0, stream>>>((real *) v, *m, (int *) dg,
108  (real *) u, *n, (int *) gd,
109  *nb, (int *) b, (int *) bo);
110  CUDA_CHECK(cudaGetLastError());
111  }
112 
116  void cuda_gs_pack(void *u_d, void *buf_d, void *dof_d,
117  int offset, int n, cudaStream_t stream) {
118 
119  const int nthrds = 1024;
120  const int nblcks = (n + nthrds - 1) / nthrds;
121 
122  if (stream == NULL) {
123  gs_pack_kernel<real>
124  <<<nblcks, nthrds>>>((real *) u_d, (real *) buf_d + offset,
125  (int *) dof_d + offset, n);
126  }
127  else {
128  gs_pack_kernel<real>
129  <<<nblcks, nthrds, 0, stream>>>((real *) u_d, (real *) buf_d + offset,
130  (int *) dof_d + offset, n);
131  }
132 
133  CUDA_CHECK(cudaGetLastError());
134  }
135 
139  void cuda_gs_unpack(real *u_d, int op, real *buf_d, int *dof_d,
140  int offset, int n, cudaStream_t stream) {
141 
142  const int nthrds = 1024;
143  const int nblcks = (n + nthrds - 1) / nthrds;
144 
145  switch (op) {
146  case GS_OP_ADD:
147  if (stream == NULL) {
148  gs_unpack_add_kernel<real>
149  <<<nblcks, nthrds>>>(u_d, buf_d + offset, dof_d + offset, n);
150  }
151  else {
152  gs_unpack_add_kernel<real>
153  <<<nblcks, nthrds, 0, stream>>>(u_d, buf_d + offset,
154  dof_d + offset, n);
155  }
156  break;
157  default:
158  printf("%s: unknown gs op %d\n", __FILE__, op);
159  abort();
160  }
161 
162  CUDA_CHECK(cudaGetLastError());
163  }
164 }
#define CUDA_CHECK(err)
Definition: check.h:6
__global__ void const T *__restrict__ u
Definition: conv1_kernel.h:132
__global__ void const T *__restrict__ const T *__restrict__ v
double real
Definition: device_config.h:12
#define GS_OP_MAX
Definition: gs.cu:44
void cuda_gs_unpack(real *u_d, int op, real *buf_d, int *dof_d, int offset, int n, cudaStream_t stream)
Definition: gs.cu:139
#define GS_OP_ADD
Definition: gs.cu:41
void cuda_gs_pack(void *u_d, void *buf_d, void *dof_d, int offset, int n, cudaStream_t stream)
Definition: gs.cu:116
void cuda_gather_kernel(void *v, int *m, int *o, void *dg, void *u, int *n, void *gd, int *nb, void *b, void *bo, int *op, cudaStream_t stream)
Definition: gs.cu:51
void cuda_scatter_kernel(void *v, int *m, void *dg, void *u, int *n, void *gd, int *nb, void *b, void *bo, cudaStream_t stream)
Definition: gs.cu:96
#define GS_OP_MUL
Definition: gs.cu:42
#define GS_OP_MIN
Definition: gs.cu:43