Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
tensor.hip
Go to the documentation of this file.
1/*
2 Copyright (c) 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>
37#include <device/hip/check.h>
38#include "tensor_kernel.h"
39#include <stdio.h>
40
41#define max(a,b) \
42 ({ __typeof__ (a) _a = (a); \
43 __typeof__ (b) _b = (b); \
44 _a > _b ? _a : _b; })
45
46
47/*
48 * Threads per block for tnsr3d.
49 *
50 * One thread per output point of the widest of the three phases, rounded up
51 * to whole wavefronts and capped at the block maximum. The three phases
52 * produce nu*nu*nv, nu*nv*nv and nv*nv*nv points, so the widest is nu*nu*nv
53 * when restricting and nv*nv*nv when prolonging.
54 *
55 * This used to be a flat 1024 regardless, which on a p-transfer left most of
56 * the block with nothing to do: an 8 -> 4 restriction has 256, 128 and 64
57 * points in its phases, so three quarters to fifteen sixteenths of the
58 * threads idled. p-multigrid transfers between adjacent levels constantly,
59 * which is where that showed up.
60 *
61 * Any block size is correct here: the phases are grid stride loops and the
62 * barriers between them sit outside the loop bodies, so a thread with no
63 * iterations still reaches every barrier.
64 */
65static int hip_tnsr3d_nthrds(const int nu, const int nv)
66{
67 const int w1 = nu * nu * nv;
68 const int w3 = nv * nv * nv;
69 int work = (w1 > w3) ? w1 : w3;
70
71 work = ((work + 64 - 1) / 64) * 64;
72 if (work > 1024) work = 1024;
73 if (work < 64) work = 64;
74 return work;
75}
76
77extern "C" {
78
80 void hip_tnsr3d(void *v, int *nv, void *u, int *nu,
81 void *A, void *Bt, void *Ct, int *nel) {
82
83 const dim3 nthrds(hip_tnsr3d_nthrds(*nu, *nv), 1, 1);
84 const dim3 nblcks(*nel, 1, 1);
85
86 int n = max(*nu,*nv);
87#define CASE(N) \
88 case N: \
89 hipLaunchKernelGGL(HIP_KERNEL_NAME(tnsr3d_kernel<real, N>), \
90 nblcks, nthrds, 0, \
91 (hipStream_t) glb_cmd_queue, \
92 (real *) v, *nv, \
93 (real *) u, *nu, \
94 (real *) A, (real *) Bt, (real *) Ct); \
95 HIP_CHECK(hipGetLastError()); \
96 break
97
98 switch(n) {
99 CASE(2);
100 CASE(3);
101 CASE(4);
102 CASE(5);
103 CASE(6);
104 CASE(7);
105 CASE(8);
106 CASE(9);
107 CASE(10);
108 CASE(11);
109 CASE(12);
110 CASE(13);
111 CASE(14);
112 CASE(15);
113 CASE(16);
114 default:
115 {
116 fprintf(stderr, __FILE__ ": size not supported: %d\n", n);
117 exit(1);
118 }
119 }
120 }
121
123 void hip_tnsr3d_el_list(void *v, int *nv, void *u, int *nu,
124 void *A, void *Bt, void *Ct, int * elements, int* n_points) {
125 const dim3 nthrds(hip_tnsr3d_nthrds(*nu, *nv), 1, 1);
126 const dim3 nblcks(*n_points, 1, 1);
127
128 int n = max(*nu,*nv);
129#define CASE2(N) \
130 case N: \
131 hipLaunchKernelGGL(HIP_KERNEL_NAME(tnsr3d_el_kernel<real, N>), \
132 nblcks, nthrds, 0, \
133 (hipStream_t) glb_cmd_queue, \
134 (real *) v, *nv, \
135 (real *) u, *nu, \
136 (real *) A, (real *) Bt, (real *) Ct, \
137 (int *) elements, *n_points); \
138 HIP_CHECK(hipGetLastError()); \
139 break
140
141 switch(n) {
142 CASE2(2);
143 CASE2(3);
144 CASE2(4);
145 CASE2(5);
146 CASE2(6);
147 CASE2(7);
148 CASE2(8);
149 CASE2(9);
150 CASE2(10);
151 CASE2(11);
152 CASE2(12);
153 CASE2(13);
154 CASE2(14);
155 default:
156 {
157 fprintf(stderr, __FILE__ ": size not supported: %d\n", n);
158 exit(1);
159 }
160 }
161 }
162
163
164}
__global__ void ale_add_kinematics_kernel(const int n, T *__restrict__ wx, T *__restrict__ wy, T *__restrict__ wz, const T *__restrict__ x_ref, const T *__restrict__ y_ref, const T *__restrict__ z_ref, const T *__restrict__ phi, const T *__restrict__ x, const T *__restrict__ y, const T *__restrict__ z, const kinematics_params_t kin_params)
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ u
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ w3
#define CASE(N)
static int hip_tnsr3d_nthrds(const int nu, const int nv)
Definition tensor.hip:65
#define CASE2(N)
void hip_tnsr3d(void *v, int *nv, void *u, int *nu, void *A, void *Bt, void *Ct, int *nel)
Definition tensor.hip:80
void hip_tnsr3d_el_list(void *v, int *nv, void *u, int *nu, void *A, void *Bt, void *Ct, int *elements, int *n_points)
Definition tensor.hip:123
#define max(a, b)
Definition tensor.hip:41