xref: /libCEED/include/ceed/jit-source/cuda/cuda-ref-restriction-oriented.h (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2cf8cbdd6SSebastian Grimberg // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3cf8cbdd6SSebastian Grimberg //
4cf8cbdd6SSebastian Grimberg // SPDX-License-Identifier: BSD-2-Clause
5cf8cbdd6SSebastian Grimberg //
6cf8cbdd6SSebastian Grimberg // This file is part of CEED:  http://github.com/ceed
7cf8cbdd6SSebastian Grimberg 
8cf8cbdd6SSebastian Grimberg /// @file
9cf8cbdd6SSebastian Grimberg /// Internal header for CUDA oriented element restriction kernels
10cf8cbdd6SSebastian Grimberg #ifndef CEED_CUDA_REF_RESTRICTION_ORIENTED_H
11cf8cbdd6SSebastian Grimberg #define CEED_CUDA_REF_RESTRICTION_ORIENTED_H
12cf8cbdd6SSebastian Grimberg 
13cf8cbdd6SSebastian Grimberg #include <ceed.h>
14cf8cbdd6SSebastian Grimberg 
15cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
16cf8cbdd6SSebastian Grimberg // L-vector -> E-vector, oriented
17cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
18cf8cbdd6SSebastian Grimberg extern "C" __global__ void OrientedNoTranspose(const CeedInt *__restrict__ indices, const bool *__restrict__ orients,
19cf8cbdd6SSebastian Grimberg                                                const CeedScalar *__restrict__ u, CeedScalar *__restrict__ v) {
20cf8cbdd6SSebastian Grimberg   for (CeedInt node = blockIdx.x * blockDim.x + threadIdx.x; node < RSTR_NUM_ELEM * RSTR_ELEM_SIZE; node += blockDim.x * gridDim.x) {
21cf8cbdd6SSebastian Grimberg     const CeedInt ind      = indices[node];
22cf8cbdd6SSebastian Grimberg     const bool    orient   = orients[node];
23cf8cbdd6SSebastian Grimberg     const CeedInt loc_node = node % RSTR_ELEM_SIZE;
24cf8cbdd6SSebastian Grimberg     const CeedInt elem     = node / RSTR_ELEM_SIZE;
25cf8cbdd6SSebastian Grimberg 
26cf8cbdd6SSebastian Grimberg     for (CeedInt comp = 0; comp < RSTR_NUM_COMP; comp++) {
27cf8cbdd6SSebastian Grimberg       v[loc_node + comp * RSTR_ELEM_SIZE * RSTR_NUM_ELEM + elem * RSTR_ELEM_SIZE] = u[ind + comp * RSTR_COMP_STRIDE] * (orient ? -1.0 : 1.0);
28cf8cbdd6SSebastian Grimberg     }
29cf8cbdd6SSebastian Grimberg   }
30cf8cbdd6SSebastian Grimberg }
31cf8cbdd6SSebastian Grimberg 
32cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
33cf8cbdd6SSebastian Grimberg // E-vector -> L-vector, oriented
34cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
35cf8cbdd6SSebastian Grimberg #if !USE_DETERMINISTIC
36cf8cbdd6SSebastian Grimberg extern "C" __global__ void OrientedTranspose(const CeedInt *__restrict__ indices, const bool *__restrict__ orients, const CeedScalar *__restrict__ u,
37cf8cbdd6SSebastian Grimberg                                              CeedScalar *__restrict__ v) {
38cf8cbdd6SSebastian Grimberg   for (CeedInt node = blockIdx.x * blockDim.x + threadIdx.x; node < RSTR_NUM_ELEM * RSTR_ELEM_SIZE; node += blockDim.x * gridDim.x) {
39cf8cbdd6SSebastian Grimberg     const CeedInt ind      = indices[node];
40cf8cbdd6SSebastian Grimberg     const bool    orient   = orients[node];
41cf8cbdd6SSebastian Grimberg     const CeedInt loc_node = node % RSTR_ELEM_SIZE;
42cf8cbdd6SSebastian Grimberg     const CeedInt elem     = node / RSTR_ELEM_SIZE;
43cf8cbdd6SSebastian Grimberg 
44cf8cbdd6SSebastian Grimberg     for (CeedInt comp = 0; comp < RSTR_NUM_COMP; comp++) {
45cf8cbdd6SSebastian Grimberg       atomicAdd(v + ind + comp * RSTR_COMP_STRIDE,
46cf8cbdd6SSebastian Grimberg                 u[loc_node + comp * RSTR_ELEM_SIZE * RSTR_NUM_ELEM + elem * RSTR_ELEM_SIZE] * (orient ? -1.0 : 1.0));
47cf8cbdd6SSebastian Grimberg     }
48cf8cbdd6SSebastian Grimberg   }
49cf8cbdd6SSebastian Grimberg }
50cf8cbdd6SSebastian Grimberg #else
51cf8cbdd6SSebastian Grimberg extern "C" __global__ void OrientedTranspose(const CeedInt *__restrict__ l_vec_indices, const CeedInt *__restrict__ t_indices,
52cf8cbdd6SSebastian Grimberg                                              const CeedInt *__restrict__ t_offsets, const bool *__restrict__ orients,
53cf8cbdd6SSebastian Grimberg                                              const CeedScalar *__restrict__ u, CeedScalar *__restrict__ v) {
54cf8cbdd6SSebastian Grimberg   CeedScalar value[RSTR_NUM_COMP];
55cf8cbdd6SSebastian Grimberg 
56cf8cbdd6SSebastian Grimberg   for (CeedInt i = blockIdx.x * blockDim.x + threadIdx.x; i < RSTR_NUM_NODES; i += blockDim.x * gridDim.x) {
57cf8cbdd6SSebastian Grimberg     const CeedInt ind     = l_vec_indices[i];
58cf8cbdd6SSebastian Grimberg     const CeedInt range_1 = t_offsets[i];
59cf8cbdd6SSebastian Grimberg     const CeedInt range_N = t_offsets[i + 1];
60cf8cbdd6SSebastian Grimberg 
61cf8cbdd6SSebastian Grimberg     for (CeedInt comp = 0; comp < RSTR_NUM_COMP; comp++) value[comp] = 0.0;
62cf8cbdd6SSebastian Grimberg 
63cf8cbdd6SSebastian Grimberg     for (CeedInt j = range_1; j < range_N; j++) {
64cf8cbdd6SSebastian Grimberg       const CeedInt t_ind    = t_indices[j];
65cf8cbdd6SSebastian Grimberg       const bool    orient   = orients[t_ind];
66cf8cbdd6SSebastian Grimberg       const CeedInt loc_node = t_ind % RSTR_ELEM_SIZE;
67cf8cbdd6SSebastian Grimberg       const CeedInt elem     = t_ind / RSTR_ELEM_SIZE;
68cf8cbdd6SSebastian Grimberg 
69cf8cbdd6SSebastian Grimberg       for (CeedInt comp = 0; comp < RSTR_NUM_COMP; comp++) {
70cf8cbdd6SSebastian Grimberg         value[comp] += u[loc_node + comp * RSTR_ELEM_SIZE * RSTR_NUM_ELEM + elem * RSTR_ELEM_SIZE] * (orient ? -1.0 : 1.0);
71cf8cbdd6SSebastian Grimberg       }
72cf8cbdd6SSebastian Grimberg     }
73cf8cbdd6SSebastian Grimberg 
74cf8cbdd6SSebastian Grimberg     for (CeedInt comp = 0; comp < RSTR_NUM_COMP; comp++) v[ind + comp * RSTR_COMP_STRIDE] += value[comp];
75cf8cbdd6SSebastian Grimberg   }
76cf8cbdd6SSebastian Grimberg }
77cf8cbdd6SSebastian Grimberg #endif
78cf8cbdd6SSebastian Grimberg 
79cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
80cf8cbdd6SSebastian Grimberg 
81cf8cbdd6SSebastian Grimberg #endif  // CEED_CUDA_REF_RESTRICTION_ORIENTED_H
82