xref: /libCEED/backends/cuda-ref/ceed-cuda-ref-restriction.c (revision cf8cbdd67b26059bcd4f184d0db9bf05297bc21d)
1ff1e7120SSebastian Grimberg // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2ff1e7120SSebastian Grimberg // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3ff1e7120SSebastian Grimberg //
4ff1e7120SSebastian Grimberg // SPDX-License-Identifier: BSD-2-Clause
5ff1e7120SSebastian Grimberg //
6ff1e7120SSebastian Grimberg // This file is part of CEED:  http://github.com/ceed
7ff1e7120SSebastian Grimberg 
8ff1e7120SSebastian Grimberg #include <ceed.h>
9ff1e7120SSebastian Grimberg #include <ceed/backend.h>
10ff1e7120SSebastian Grimberg #include <ceed/jit-tools.h>
11ff1e7120SSebastian Grimberg #include <cuda.h>
12ff1e7120SSebastian Grimberg #include <cuda_runtime.h>
13ff1e7120SSebastian Grimberg #include <stdbool.h>
14ff1e7120SSebastian Grimberg #include <stddef.h>
15ff1e7120SSebastian Grimberg #include <string.h>
16ff1e7120SSebastian Grimberg 
17ff1e7120SSebastian Grimberg #include "../cuda/ceed-cuda-common.h"
18ff1e7120SSebastian Grimberg #include "../cuda/ceed-cuda-compile.h"
19ff1e7120SSebastian Grimberg #include "ceed-cuda-ref.h"
20ff1e7120SSebastian Grimberg 
21ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
22*cf8cbdd6SSebastian Grimberg // Compile restriction kernels
23*cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
24*cf8cbdd6SSebastian Grimberg static inline int CeedElemRestrictionSetupCompile_Cuda(CeedElemRestriction rstr) {
25*cf8cbdd6SSebastian Grimberg   Ceed                      ceed;
26*cf8cbdd6SSebastian Grimberg   bool                      is_deterministic;
27*cf8cbdd6SSebastian Grimberg   CeedInt                   num_elem, num_comp, elem_size, comp_stride;
28*cf8cbdd6SSebastian Grimberg   CeedRestrictionType       rstr_type;
29*cf8cbdd6SSebastian Grimberg   char                     *restriction_kernel_path, *restriction_kernel_source;
30*cf8cbdd6SSebastian Grimberg   CeedElemRestriction_Cuda *impl;
31*cf8cbdd6SSebastian Grimberg 
32*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
33*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
34*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
35*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
36*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
37*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
38*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
39*cf8cbdd6SSebastian Grimberg   is_deterministic = impl->d_l_vec_indices != NULL;
40*cf8cbdd6SSebastian Grimberg 
41*cf8cbdd6SSebastian Grimberg   // Compile CUDA kernels
42*cf8cbdd6SSebastian Grimberg   switch (rstr_type) {
43*cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_STRIDED: {
44*cf8cbdd6SSebastian Grimberg       CeedInt strides[3] = {1, num_elem * elem_size, elem_size};
45*cf8cbdd6SSebastian Grimberg       bool    has_backend_strides;
46*cf8cbdd6SSebastian Grimberg 
47*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
48*cf8cbdd6SSebastian Grimberg       if (!has_backend_strides) {
49*cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedElemRestrictionGetStrides(rstr, &strides));
50*cf8cbdd6SSebastian Grimberg       }
51*cf8cbdd6SSebastian Grimberg 
52*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-restriction-strided.h", &restriction_kernel_path));
53*cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
54*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
55*cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
56*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Cuda(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
57*cf8cbdd6SSebastian Grimberg                                        "RSTR_NUM_COMP", num_comp, "RSTR_STRIDE_NODES", strides[0], "RSTR_STRIDE_COMP", strides[1], "RSTR_STRIDE_ELEM",
58*cf8cbdd6SSebastian Grimberg                                        strides[2]));
59*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "StridedNoTranspose", &impl->ApplyNoTranspose));
60*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "StridedTranspose", &impl->ApplyTranspose));
61*cf8cbdd6SSebastian Grimberg     } break;
62*cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_STANDARD: {
63*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-restriction-offset.h", &restriction_kernel_path));
64*cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
65*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
66*cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
67*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Cuda(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
68*cf8cbdd6SSebastian Grimberg                                        "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
69*cf8cbdd6SSebastian Grimberg                                        "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
70*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyNoTranspose));
71*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OffsetTranspose", &impl->ApplyTranspose));
72*cf8cbdd6SSebastian Grimberg     } break;
73*cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_ORIENTED: {
74*cf8cbdd6SSebastian Grimberg       char *offset_kernel_path;
75*cf8cbdd6SSebastian Grimberg 
76*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-restriction-oriented.h", &restriction_kernel_path));
77*cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
78*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
79*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-restriction-offset.h", &offset_kernel_path));
80*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &restriction_kernel_source));
81*cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
82*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Cuda(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
83*cf8cbdd6SSebastian Grimberg                                        "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
84*cf8cbdd6SSebastian Grimberg                                        "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
85*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OrientedNoTranspose", &impl->ApplyNoTranspose));
86*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnsignedNoTranspose));
87*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OrientedTranspose", &impl->ApplyTranspose));
88*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnsignedTranspose));
89*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedFree(&offset_kernel_path));
90*cf8cbdd6SSebastian Grimberg     } break;
91*cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_CURL_ORIENTED: {
92*cf8cbdd6SSebastian Grimberg       char *offset_kernel_path;
93*cf8cbdd6SSebastian Grimberg 
94*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-restriction-curl-oriented.h", &restriction_kernel_path));
95*cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
96*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
97*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-restriction-offset.h", &offset_kernel_path));
98*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, offset_kernel_path, &restriction_kernel_source));
99*cf8cbdd6SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
100*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedCompile_Cuda(ceed, restriction_kernel_source, &impl->module, 6, "RSTR_ELEM_SIZE", elem_size, "RSTR_NUM_ELEM", num_elem,
101*cf8cbdd6SSebastian Grimberg                                        "RSTR_NUM_COMP", num_comp, "RSTR_NUM_NODES", impl->num_nodes, "RSTR_COMP_STRIDE", comp_stride,
102*cf8cbdd6SSebastian Grimberg                                        "USE_DETERMINISTIC", is_deterministic ? 1 : 0));
103*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "CurlOrientedNoTranspose", &impl->ApplyNoTranspose));
104*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "CurlOrientedUnsignedNoTranspose", &impl->ApplyUnsignedNoTranspose));
105*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OffsetNoTranspose", &impl->ApplyUnorientedNoTranspose));
106*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "CurlOrientedTranspose", &impl->ApplyTranspose));
107*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "CurlOrientedUnsignedTranspose", &impl->ApplyUnsignedTranspose));
108*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnorientedTranspose));
109*cf8cbdd6SSebastian Grimberg       CeedCallBackend(CeedFree(&offset_kernel_path));
110*cf8cbdd6SSebastian Grimberg     } break;
111*cf8cbdd6SSebastian Grimberg     case CEED_RESTRICTION_POINTS: {
112*cf8cbdd6SSebastian Grimberg       // LCOV_EXCL_START
113*cf8cbdd6SSebastian Grimberg       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
114*cf8cbdd6SSebastian Grimberg       // LCOV_EXCL_STOP
115*cf8cbdd6SSebastian Grimberg     } break;
116*cf8cbdd6SSebastian Grimberg   }
117*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedFree(&restriction_kernel_path));
118*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedFree(&restriction_kernel_source));
119*cf8cbdd6SSebastian Grimberg   return CEED_ERROR_SUCCESS;
120*cf8cbdd6SSebastian Grimberg }
121*cf8cbdd6SSebastian Grimberg 
122*cf8cbdd6SSebastian Grimberg //------------------------------------------------------------------------------
123dce49693SSebastian Grimberg // Core apply restriction code
124ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
125dce49693SSebastian Grimberg static inline int CeedElemRestrictionApply_Cuda_Core(CeedElemRestriction rstr, CeedTransposeMode t_mode, bool use_signs, bool use_orients,
126dce49693SSebastian Grimberg                                                      CeedVector u, CeedVector v, CeedRequest *request) {
127ff1e7120SSebastian Grimberg   Ceed                      ceed;
128dce49693SSebastian Grimberg   CeedRestrictionType       rstr_type;
129ff1e7120SSebastian Grimberg   const CeedScalar         *d_u;
130ff1e7120SSebastian Grimberg   CeedScalar               *d_v;
131ca735530SJeremy L Thompson   CeedElemRestriction_Cuda *impl;
132ca735530SJeremy L Thompson 
133dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
134dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
135dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
136*cf8cbdd6SSebastian Grimberg 
137*cf8cbdd6SSebastian Grimberg   // Assemble kernel if needed
138*cf8cbdd6SSebastian Grimberg   if (!impl->module) {
139*cf8cbdd6SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionSetupCompile_Cuda(rstr));
140*cf8cbdd6SSebastian Grimberg   }
141ca735530SJeremy L Thompson 
142ca735530SJeremy L Thompson   // Get vectors
143ff1e7120SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
144ff1e7120SSebastian Grimberg   if (t_mode == CEED_TRANSPOSE) {
145ff1e7120SSebastian Grimberg     // Sum into for transpose mode, e-vec to l-vec
146ff1e7120SSebastian Grimberg     CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
147ff1e7120SSebastian Grimberg   } else {
148ff1e7120SSebastian Grimberg     // Overwrite for notranspose mode, l-vec to e-vec
149ff1e7120SSebastian Grimberg     CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
150ff1e7120SSebastian Grimberg   }
151ff1e7120SSebastian Grimberg 
152ff1e7120SSebastian Grimberg   // Restrict
153ff1e7120SSebastian Grimberg   if (t_mode == CEED_NOTRANSPOSE) {
154ff1e7120SSebastian Grimberg     // L-vector -> E-vector
155*cf8cbdd6SSebastian Grimberg     CeedInt elem_size;
156*cf8cbdd6SSebastian Grimberg 
157*cf8cbdd6SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
158dce49693SSebastian Grimberg     const CeedInt block_size = elem_size < 1024 ? (elem_size > 32 ? elem_size : 32) : 1024;
159*cf8cbdd6SSebastian Grimberg     const CeedInt grid       = CeedDivUpInt(impl->num_nodes, block_size);
16058549094SSebastian Grimberg 
161dce49693SSebastian Grimberg     switch (rstr_type) {
162dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
163*cf8cbdd6SSebastian Grimberg         void *args[] = {&d_u, &d_v};
16458549094SSebastian Grimberg 
165*cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyNoTranspose, grid, block_size, args));
166dce49693SSebastian Grimberg       } break;
167dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
168*cf8cbdd6SSebastian Grimberg         void *args[] = {&impl->d_ind, &d_u, &d_v};
169dce49693SSebastian Grimberg 
170*cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyNoTranspose, grid, block_size, args));
171dce49693SSebastian Grimberg       } break;
172dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
173dce49693SSebastian Grimberg         if (use_signs) {
174*cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &impl->d_orients, &d_u, &d_v};
175dce49693SSebastian Grimberg 
176*cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyNoTranspose, grid, block_size, args));
177dce49693SSebastian Grimberg         } else {
178*cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &d_u, &d_v};
179dce49693SSebastian Grimberg 
180*cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args));
181dce49693SSebastian Grimberg         }
182dce49693SSebastian Grimberg       } break;
183dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
184dce49693SSebastian Grimberg         if (use_signs && use_orients) {
185*cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
186dce49693SSebastian Grimberg 
187*cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyNoTranspose, grid, block_size, args));
188dce49693SSebastian Grimberg         } else if (use_orients) {
189*cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
190dce49693SSebastian Grimberg 
191*cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnsignedNoTranspose, grid, block_size, args));
192dce49693SSebastian Grimberg         } else {
193*cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &d_u, &d_v};
194dce49693SSebastian Grimberg 
195*cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnorientedNoTranspose, grid, block_size, args));
196dce49693SSebastian Grimberg         }
197dce49693SSebastian Grimberg       } break;
198b3d03e38SSebastian Grimberg       case CEED_RESTRICTION_POINTS: {
199b3d03e38SSebastian Grimberg         // LCOV_EXCL_START
200b3d03e38SSebastian Grimberg         return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
201b3d03e38SSebastian Grimberg         // LCOV_EXCL_STOP
202b3d03e38SSebastian Grimberg       } break;
203ff1e7120SSebastian Grimberg     }
204ff1e7120SSebastian Grimberg   } else {
205ff1e7120SSebastian Grimberg     // E-vector -> L-vector
206*cf8cbdd6SSebastian Grimberg     const bool    is_deterministic = impl->d_l_vec_indices != NULL;
207dce49693SSebastian Grimberg     const CeedInt block_size       = 32;
208*cf8cbdd6SSebastian Grimberg     const CeedInt grid             = CeedDivUpInt(impl->num_nodes, block_size);
209ca735530SJeremy L Thompson 
210dce49693SSebastian Grimberg     switch (rstr_type) {
211dce49693SSebastian Grimberg       case CEED_RESTRICTION_STRIDED: {
212*cf8cbdd6SSebastian Grimberg         void *args[] = {&d_u, &d_v};
213dce49693SSebastian Grimberg 
214*cf8cbdd6SSebastian Grimberg         CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyTranspose, grid, block_size, args));
215dce49693SSebastian Grimberg       } break;
216dce49693SSebastian Grimberg       case CEED_RESTRICTION_STANDARD: {
217*cf8cbdd6SSebastian Grimberg         if (!is_deterministic) {
218*cf8cbdd6SSebastian Grimberg           void *args[] = {&impl->d_ind, &d_u, &d_v};
21958549094SSebastian Grimberg 
220*cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyTranspose, grid, block_size, args));
221ff1e7120SSebastian Grimberg         } else {
22258549094SSebastian Grimberg           void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
22358549094SSebastian Grimberg 
224*cf8cbdd6SSebastian Grimberg           CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyTranspose, grid, block_size, args));
22558549094SSebastian Grimberg         }
226dce49693SSebastian Grimberg       } break;
227dce49693SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED: {
228dce49693SSebastian Grimberg         if (use_signs) {
229*cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
230*cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &impl->d_orients, &d_u, &d_v};
23158549094SSebastian Grimberg 
232*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyTranspose, grid, block_size, args));
233dce49693SSebastian Grimberg           } else {
2347aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_orients, &d_u, &d_v};
2357aa91133SSebastian Grimberg 
236*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyTranspose, grid, block_size, args));
2377aa91133SSebastian Grimberg           }
2387aa91133SSebastian Grimberg         } else {
239*cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
240*cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &d_u, &d_v};
241dce49693SSebastian Grimberg 
242*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
243dce49693SSebastian Grimberg           } else {
244dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
245dce49693SSebastian Grimberg 
246*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
247dce49693SSebastian Grimberg           }
248dce49693SSebastian Grimberg         }
249dce49693SSebastian Grimberg       } break;
250dce49693SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED: {
251dce49693SSebastian Grimberg         if (use_signs && use_orients) {
252*cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
253*cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
254dce49693SSebastian Grimberg 
255*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyTranspose, grid, block_size, args));
2567aa91133SSebastian Grimberg           } else {
2577aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
2587aa91133SSebastian Grimberg 
259*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyTranspose, grid, block_size, args));
2607aa91133SSebastian Grimberg           }
261dce49693SSebastian Grimberg         } else if (use_orients) {
262*cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
263*cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &impl->d_curl_orients, &d_u, &d_v};
264dce49693SSebastian Grimberg 
265*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
266dce49693SSebastian Grimberg           } else {
2677aa91133SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &impl->d_curl_orients, &d_u, &d_v};
2687aa91133SSebastian Grimberg 
269*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnsignedTranspose, grid, block_size, args));
2707aa91133SSebastian Grimberg           }
2717aa91133SSebastian Grimberg         } else {
272*cf8cbdd6SSebastian Grimberg           if (!is_deterministic) {
273*cf8cbdd6SSebastian Grimberg             void *args[] = {&impl->d_ind, &d_u, &d_v};
274dce49693SSebastian Grimberg 
275*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnorientedTranspose, grid, block_size, args));
276dce49693SSebastian Grimberg           } else {
277dce49693SSebastian Grimberg             void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
278dce49693SSebastian Grimberg 
279*cf8cbdd6SSebastian Grimberg             CeedCallBackend(CeedRunKernel_Cuda(ceed, impl->ApplyUnorientedTranspose, grid, block_size, args));
280dce49693SSebastian Grimberg           }
281dce49693SSebastian Grimberg         }
282dce49693SSebastian Grimberg       } break;
283b3d03e38SSebastian Grimberg       case CEED_RESTRICTION_POINTS: {
284b3d03e38SSebastian Grimberg         // LCOV_EXCL_START
285b3d03e38SSebastian Grimberg         return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement restriction CeedElemRestrictionAtPoints");
286b3d03e38SSebastian Grimberg         // LCOV_EXCL_STOP
287b3d03e38SSebastian Grimberg       } break;
288ff1e7120SSebastian Grimberg     }
289ff1e7120SSebastian Grimberg   }
290ff1e7120SSebastian Grimberg 
291ff1e7120SSebastian Grimberg   if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL;
292ff1e7120SSebastian Grimberg 
293ff1e7120SSebastian Grimberg   // Restore arrays
294ff1e7120SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
295ff1e7120SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
296ff1e7120SSebastian Grimberg   return CEED_ERROR_SUCCESS;
297ff1e7120SSebastian Grimberg }
298ff1e7120SSebastian Grimberg 
299ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
300dce49693SSebastian Grimberg // Apply restriction
301dce49693SSebastian Grimberg //------------------------------------------------------------------------------
302dce49693SSebastian Grimberg static int CeedElemRestrictionApply_Cuda(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) {
303dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Cuda_Core(rstr, t_mode, true, true, u, v, request);
304dce49693SSebastian Grimberg }
305dce49693SSebastian Grimberg 
306dce49693SSebastian Grimberg //------------------------------------------------------------------------------
307dce49693SSebastian Grimberg // Apply unsigned restriction
308dce49693SSebastian Grimberg //------------------------------------------------------------------------------
309dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnsigned_Cuda(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
310dce49693SSebastian Grimberg                                                  CeedRequest *request) {
311dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Cuda_Core(rstr, t_mode, false, true, u, v, request);
312dce49693SSebastian Grimberg }
313dce49693SSebastian Grimberg 
314dce49693SSebastian Grimberg //------------------------------------------------------------------------------
315dce49693SSebastian Grimberg // Apply unoriented restriction
316dce49693SSebastian Grimberg //------------------------------------------------------------------------------
317dce49693SSebastian Grimberg static int CeedElemRestrictionApplyUnoriented_Cuda(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector v,
318dce49693SSebastian Grimberg                                                    CeedRequest *request) {
319dce49693SSebastian Grimberg   return CeedElemRestrictionApply_Cuda_Core(rstr, t_mode, false, false, u, v, request);
320dce49693SSebastian Grimberg }
321dce49693SSebastian Grimberg 
322dce49693SSebastian Grimberg //------------------------------------------------------------------------------
323ff1e7120SSebastian Grimberg // Get offsets
324ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
325ff1e7120SSebastian Grimberg static int CeedElemRestrictionGetOffsets_Cuda(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
326ff1e7120SSebastian Grimberg   CeedElemRestriction_Cuda *impl;
327ff1e7120SSebastian Grimberg 
328ca735530SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
329ff1e7120SSebastian Grimberg   switch (mem_type) {
330ff1e7120SSebastian Grimberg     case CEED_MEM_HOST:
331ff1e7120SSebastian Grimberg       *offsets = impl->h_ind;
332ff1e7120SSebastian Grimberg       break;
333ff1e7120SSebastian Grimberg     case CEED_MEM_DEVICE:
334ff1e7120SSebastian Grimberg       *offsets = impl->d_ind;
335ff1e7120SSebastian Grimberg       break;
336ff1e7120SSebastian Grimberg   }
337ff1e7120SSebastian Grimberg   return CEED_ERROR_SUCCESS;
338ff1e7120SSebastian Grimberg }
339ff1e7120SSebastian Grimberg 
340ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
341dce49693SSebastian Grimberg // Get orientations
342dce49693SSebastian Grimberg //------------------------------------------------------------------------------
343dce49693SSebastian Grimberg static int CeedElemRestrictionGetOrientations_Cuda(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
344dce49693SSebastian Grimberg   CeedElemRestriction_Cuda *impl;
345dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
346dce49693SSebastian Grimberg 
347dce49693SSebastian Grimberg   switch (mem_type) {
348dce49693SSebastian Grimberg     case CEED_MEM_HOST:
349dce49693SSebastian Grimberg       *orients = impl->h_orients;
350dce49693SSebastian Grimberg       break;
351dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
352dce49693SSebastian Grimberg       *orients = impl->d_orients;
353dce49693SSebastian Grimberg       break;
354dce49693SSebastian Grimberg   }
355dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
356dce49693SSebastian Grimberg }
357dce49693SSebastian Grimberg 
358dce49693SSebastian Grimberg //------------------------------------------------------------------------------
359dce49693SSebastian Grimberg // Get curl-conforming orientations
360dce49693SSebastian Grimberg //------------------------------------------------------------------------------
361dce49693SSebastian Grimberg static int CeedElemRestrictionGetCurlOrientations_Cuda(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
362dce49693SSebastian Grimberg   CeedElemRestriction_Cuda *impl;
363dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
364dce49693SSebastian Grimberg 
365dce49693SSebastian Grimberg   switch (mem_type) {
366dce49693SSebastian Grimberg     case CEED_MEM_HOST:
367dce49693SSebastian Grimberg       *curl_orients = impl->h_curl_orients;
368dce49693SSebastian Grimberg       break;
369dce49693SSebastian Grimberg     case CEED_MEM_DEVICE:
370dce49693SSebastian Grimberg       *curl_orients = impl->d_curl_orients;
371dce49693SSebastian Grimberg       break;
372dce49693SSebastian Grimberg   }
373dce49693SSebastian Grimberg   return CEED_ERROR_SUCCESS;
374dce49693SSebastian Grimberg }
375dce49693SSebastian Grimberg 
376dce49693SSebastian Grimberg //------------------------------------------------------------------------------
377ff1e7120SSebastian Grimberg // Destroy restriction
378ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
379dce49693SSebastian Grimberg static int CeedElemRestrictionDestroy_Cuda(CeedElemRestriction rstr) {
380ff1e7120SSebastian Grimberg   Ceed                      ceed;
381ca735530SJeremy L Thompson   CeedElemRestriction_Cuda *impl;
382ca735530SJeremy L Thompson 
383dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
384dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
385*cf8cbdd6SSebastian Grimberg   if (impl->module) {
386ff1e7120SSebastian Grimberg     CeedCallCuda(ceed, cuModuleUnload(impl->module));
387*cf8cbdd6SSebastian Grimberg   }
388ff1e7120SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->h_ind_allocated));
389ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaFree(impl->d_ind_allocated));
390ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaFree(impl->d_t_offsets));
391ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaFree(impl->d_t_indices));
392ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaFree(impl->d_l_vec_indices));
393dce49693SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->h_orients_allocated));
394dce49693SSebastian Grimberg   CeedCallCuda(ceed, cudaFree(impl->d_orients_allocated));
395dce49693SSebastian Grimberg   CeedCallBackend(CeedFree(&impl->h_curl_orients_allocated));
396dce49693SSebastian Grimberg   CeedCallCuda(ceed, cudaFree(impl->d_curl_orients_allocated));
397ff1e7120SSebastian Grimberg   CeedCallBackend(CeedFree(&impl));
398ff1e7120SSebastian Grimberg   return CEED_ERROR_SUCCESS;
399ff1e7120SSebastian Grimberg }
400ff1e7120SSebastian Grimberg 
401ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
402ff1e7120SSebastian Grimberg // Create transpose offsets and indices
403ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
404dce49693SSebastian Grimberg static int CeedElemRestrictionOffset_Cuda(const CeedElemRestriction rstr, const CeedInt *indices) {
405ff1e7120SSebastian Grimberg   Ceed                      ceed;
406ca735530SJeremy L Thompson   bool                     *is_node;
407ff1e7120SSebastian Grimberg   CeedSize                  l_size;
408ca735530SJeremy L Thompson   CeedInt                   num_elem, elem_size, num_comp, num_nodes = 0;
409ca735530SJeremy L Thompson   CeedInt                  *ind_to_offset, *l_vec_indices, *t_offsets, *t_indices;
410ca735530SJeremy L Thompson   CeedElemRestriction_Cuda *impl;
411ca735530SJeremy L Thompson 
412dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
413dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
414dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
415dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
416dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
417dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
418ca735530SJeremy L Thompson   const CeedInt size_indices = num_elem * elem_size;
419ff1e7120SSebastian Grimberg 
420ff1e7120SSebastian Grimberg   // Count num_nodes
421ff1e7120SSebastian Grimberg   CeedCallBackend(CeedCalloc(l_size, &is_node));
422ca735530SJeremy L Thompson 
423ff1e7120SSebastian Grimberg   for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1;
424ff1e7120SSebastian Grimberg   for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i];
425ff1e7120SSebastian Grimberg   impl->num_nodes = num_nodes;
426ff1e7120SSebastian Grimberg 
427ff1e7120SSebastian Grimberg   // L-vector offsets array
428ff1e7120SSebastian Grimberg   CeedCallBackend(CeedCalloc(l_size, &ind_to_offset));
429ff1e7120SSebastian Grimberg   CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices));
430ca735530SJeremy L Thompson   for (CeedInt i = 0, j = 0; i < l_size; i++) {
431ff1e7120SSebastian Grimberg     if (is_node[i]) {
432ff1e7120SSebastian Grimberg       l_vec_indices[j] = i;
433ff1e7120SSebastian Grimberg       ind_to_offset[i] = j++;
434ff1e7120SSebastian Grimberg     }
435ff1e7120SSebastian Grimberg   }
436ff1e7120SSebastian Grimberg   CeedCallBackend(CeedFree(&is_node));
437ff1e7120SSebastian Grimberg 
438ff1e7120SSebastian Grimberg   // Compute transpose offsets and indices
439ff1e7120SSebastian Grimberg   const CeedInt size_offsets = num_nodes + 1;
440ca735530SJeremy L Thompson 
441ff1e7120SSebastian Grimberg   CeedCallBackend(CeedCalloc(size_offsets, &t_offsets));
442ff1e7120SSebastian Grimberg   CeedCallBackend(CeedMalloc(size_indices, &t_indices));
443ff1e7120SSebastian Grimberg   // Count node multiplicity
444ff1e7120SSebastian Grimberg   for (CeedInt e = 0; e < num_elem; ++e) {
445ff1e7120SSebastian Grimberg     for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1];
446ff1e7120SSebastian Grimberg   }
447ff1e7120SSebastian Grimberg   // Convert to running sum
448ff1e7120SSebastian Grimberg   for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1];
449ff1e7120SSebastian Grimberg   // List all E-vec indices associated with L-vec node
450ff1e7120SSebastian Grimberg   for (CeedInt e = 0; e < num_elem; ++e) {
451ff1e7120SSebastian Grimberg     for (CeedInt i = 0; i < elem_size; ++i) {
452ff1e7120SSebastian Grimberg       const CeedInt lid = elem_size * e + i;
453ff1e7120SSebastian Grimberg       const CeedInt gid = indices[lid];
454ca735530SJeremy L Thompson 
455ff1e7120SSebastian Grimberg       t_indices[t_offsets[ind_to_offset[gid]]++] = lid;
456ff1e7120SSebastian Grimberg     }
457ff1e7120SSebastian Grimberg   }
458ff1e7120SSebastian Grimberg   // Reset running sum
459ff1e7120SSebastian Grimberg   for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1];
460ff1e7120SSebastian Grimberg   t_offsets[0] = 0;
461ff1e7120SSebastian Grimberg 
462ff1e7120SSebastian Grimberg   // Copy data to device
463ff1e7120SSebastian Grimberg   // -- L-vector indices
464ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt)));
465ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), cudaMemcpyHostToDevice));
466ff1e7120SSebastian Grimberg   // -- Transpose offsets
467ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt)));
468ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), cudaMemcpyHostToDevice));
469ff1e7120SSebastian Grimberg   // -- Transpose indices
470ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt)));
471ff1e7120SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), cudaMemcpyHostToDevice));
472ff1e7120SSebastian Grimberg 
473ff1e7120SSebastian Grimberg   // Cleanup
474ff1e7120SSebastian Grimberg   CeedCallBackend(CeedFree(&ind_to_offset));
475ff1e7120SSebastian Grimberg   CeedCallBackend(CeedFree(&l_vec_indices));
476ff1e7120SSebastian Grimberg   CeedCallBackend(CeedFree(&t_offsets));
477ff1e7120SSebastian Grimberg   CeedCallBackend(CeedFree(&t_indices));
478ff1e7120SSebastian Grimberg   return CEED_ERROR_SUCCESS;
479ff1e7120SSebastian Grimberg }
480ff1e7120SSebastian Grimberg 
481ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
482ff1e7120SSebastian Grimberg // Create restriction
483ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
484fcbe8c06SSebastian Grimberg int CeedElemRestrictionCreate_Cuda(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *indices, const bool *orients,
485dce49693SSebastian Grimberg                                    const CeedInt8 *curl_orients, CeedElemRestriction rstr) {
486ca735530SJeremy L Thompson   Ceed                      ceed, ceed_parent;
487dce49693SSebastian Grimberg   bool                      is_deterministic;
488*cf8cbdd6SSebastian Grimberg   CeedInt                   num_elem, elem_size;
489ca735530SJeremy L Thompson   CeedRestrictionType       rstr_type;
490ff1e7120SSebastian Grimberg   CeedElemRestriction_Cuda *impl;
491ca735530SJeremy L Thompson 
492dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed));
493ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
494ca735530SJeremy L Thompson   CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic));
495dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
496dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
497ca735530SJeremy L Thompson   const CeedInt size      = num_elem * elem_size;
498*cf8cbdd6SSebastian Grimberg   CeedInt       layout[3] = {1, size, elem_size};
499ff1e7120SSebastian Grimberg 
500dce49693SSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
501dce49693SSebastian Grimberg   impl->num_nodes                = size;
502ff1e7120SSebastian Grimberg   impl->h_ind                    = NULL;
503ff1e7120SSebastian Grimberg   impl->h_ind_allocated          = NULL;
504ff1e7120SSebastian Grimberg   impl->d_ind                    = NULL;
505ff1e7120SSebastian Grimberg   impl->d_ind_allocated          = NULL;
506ff1e7120SSebastian Grimberg   impl->d_t_indices              = NULL;
507ff1e7120SSebastian Grimberg   impl->d_t_offsets              = NULL;
508dce49693SSebastian Grimberg   impl->h_orients                = NULL;
509dce49693SSebastian Grimberg   impl->h_orients_allocated      = NULL;
510dce49693SSebastian Grimberg   impl->d_orients                = NULL;
511dce49693SSebastian Grimberg   impl->d_orients_allocated      = NULL;
512dce49693SSebastian Grimberg   impl->h_curl_orients           = NULL;
513dce49693SSebastian Grimberg   impl->h_curl_orients_allocated = NULL;
514dce49693SSebastian Grimberg   impl->d_curl_orients           = NULL;
515dce49693SSebastian Grimberg   impl->d_curl_orients_allocated = NULL;
516dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionSetData(rstr, impl));
517dce49693SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionSetELayout(rstr, layout));
518ff1e7120SSebastian Grimberg 
519dce49693SSebastian Grimberg   // Set up device offset/orientation arrays
520*cf8cbdd6SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
521dce49693SSebastian Grimberg   if (rstr_type != CEED_RESTRICTION_STRIDED) {
522ff1e7120SSebastian Grimberg     switch (mem_type) {
523ff1e7120SSebastian Grimberg       case CEED_MEM_HOST: {
524ff1e7120SSebastian Grimberg         switch (copy_mode) {
525ff1e7120SSebastian Grimberg           case CEED_OWN_POINTER:
526ff1e7120SSebastian Grimberg             impl->h_ind_allocated = (CeedInt *)indices;
527ff1e7120SSebastian Grimberg             impl->h_ind           = (CeedInt *)indices;
528ff1e7120SSebastian Grimberg             break;
529ff1e7120SSebastian Grimberg           case CEED_USE_POINTER:
530ff1e7120SSebastian Grimberg             impl->h_ind = (CeedInt *)indices;
531ff1e7120SSebastian Grimberg             break;
532ff1e7120SSebastian Grimberg           case CEED_COPY_VALUES:
533dce49693SSebastian Grimberg             CeedCallBackend(CeedMalloc(size, &impl->h_ind_allocated));
534dce49693SSebastian Grimberg             memcpy(impl->h_ind_allocated, indices, size * sizeof(CeedInt));
535ff1e7120SSebastian Grimberg             impl->h_ind = impl->h_ind_allocated;
536ff1e7120SSebastian Grimberg             break;
537ff1e7120SSebastian Grimberg         }
538ff1e7120SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
539ff1e7120SSebastian Grimberg         impl->d_ind_allocated = impl->d_ind;  // We own the device memory
540ff1e7120SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), cudaMemcpyHostToDevice));
541dce49693SSebastian Grimberg         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Cuda(rstr, indices));
542dce49693SSebastian Grimberg       } break;
543ff1e7120SSebastian Grimberg       case CEED_MEM_DEVICE: {
544ff1e7120SSebastian Grimberg         switch (copy_mode) {
545ff1e7120SSebastian Grimberg           case CEED_COPY_VALUES:
546ff1e7120SSebastian Grimberg             CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
547ff1e7120SSebastian Grimberg             impl->d_ind_allocated = impl->d_ind;  // We own the device memory
548ff1e7120SSebastian Grimberg             CeedCallCuda(ceed, cudaMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), cudaMemcpyDeviceToDevice));
549ff1e7120SSebastian Grimberg             break;
550ff1e7120SSebastian Grimberg           case CEED_OWN_POINTER:
551ff1e7120SSebastian Grimberg             impl->d_ind           = (CeedInt *)indices;
552ff1e7120SSebastian Grimberg             impl->d_ind_allocated = impl->d_ind;
553ff1e7120SSebastian Grimberg             break;
554ff1e7120SSebastian Grimberg           case CEED_USE_POINTER:
555ff1e7120SSebastian Grimberg             impl->d_ind = (CeedInt *)indices;
556ff1e7120SSebastian Grimberg             break;
557ff1e7120SSebastian Grimberg         }
558dce49693SSebastian Grimberg         CeedCallBackend(CeedMalloc(size, &impl->h_ind_allocated));
559dce49693SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(impl->h_ind_allocated, impl->d_ind, size * sizeof(CeedInt), cudaMemcpyDeviceToHost));
560dce49693SSebastian Grimberg         impl->h_ind = impl->h_ind_allocated;
561dce49693SSebastian Grimberg         if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Cuda(rstr, indices));
562dce49693SSebastian Grimberg       } break;
563ff1e7120SSebastian Grimberg     }
564ff1e7120SSebastian Grimberg 
565dce49693SSebastian Grimberg     // Orientation data
566dce49693SSebastian Grimberg     if (rstr_type == CEED_RESTRICTION_ORIENTED) {
567dce49693SSebastian Grimberg       switch (mem_type) {
568dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
569dce49693SSebastian Grimberg           switch (copy_mode) {
570dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
571dce49693SSebastian Grimberg               impl->h_orients_allocated = (bool *)orients;
572dce49693SSebastian Grimberg               impl->h_orients           = (bool *)orients;
573dce49693SSebastian Grimberg               break;
574dce49693SSebastian Grimberg             case CEED_USE_POINTER:
575dce49693SSebastian Grimberg               impl->h_orients = (bool *)orients;
576dce49693SSebastian Grimberg               break;
577dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
578dce49693SSebastian Grimberg               CeedCallBackend(CeedMalloc(size, &impl->h_orients_allocated));
579dce49693SSebastian Grimberg               memcpy(impl->h_orients_allocated, orients, size * sizeof(bool));
580dce49693SSebastian Grimberg               impl->h_orients = impl->h_orients_allocated;
581dce49693SSebastian Grimberg               break;
582dce49693SSebastian Grimberg           }
583dce49693SSebastian Grimberg           CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_orients, size * sizeof(bool)));
584dce49693SSebastian Grimberg           impl->d_orients_allocated = impl->d_orients;  // We own the device memory
585dce49693SSebastian Grimberg           CeedCallCuda(ceed, cudaMemcpy(impl->d_orients, orients, size * sizeof(bool), cudaMemcpyHostToDevice));
586dce49693SSebastian Grimberg         } break;
587dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
588dce49693SSebastian Grimberg           switch (copy_mode) {
589dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
590dce49693SSebastian Grimberg               CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_orients, size * sizeof(bool)));
591dce49693SSebastian Grimberg               impl->d_orients_allocated = impl->d_orients;  // We own the device memory
592dce49693SSebastian Grimberg               CeedCallCuda(ceed, cudaMemcpy(impl->d_orients, orients, size * sizeof(bool), cudaMemcpyDeviceToDevice));
593dce49693SSebastian Grimberg               break;
594dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
595dce49693SSebastian Grimberg               impl->d_orients           = (bool *)orients;
596dce49693SSebastian Grimberg               impl->d_orients_allocated = impl->d_orients;
597dce49693SSebastian Grimberg               break;
598dce49693SSebastian Grimberg             case CEED_USE_POINTER:
599dce49693SSebastian Grimberg               impl->d_orients = (bool *)orients;
600dce49693SSebastian Grimberg               break;
601dce49693SSebastian Grimberg           }
602dce49693SSebastian Grimberg           CeedCallBackend(CeedMalloc(size, &impl->h_orients_allocated));
603dce49693SSebastian Grimberg           CeedCallCuda(ceed, cudaMemcpy(impl->h_orients_allocated, impl->d_orients, size * sizeof(bool), cudaMemcpyDeviceToHost));
604dce49693SSebastian Grimberg           impl->h_orients = impl->h_orients_allocated;
605dce49693SSebastian Grimberg         } break;
606dce49693SSebastian Grimberg       }
607dce49693SSebastian Grimberg     } else if (rstr_type == CEED_RESTRICTION_CURL_ORIENTED) {
608dce49693SSebastian Grimberg       switch (mem_type) {
609dce49693SSebastian Grimberg         case CEED_MEM_HOST: {
610dce49693SSebastian Grimberg           switch (copy_mode) {
611dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
612dce49693SSebastian Grimberg               impl->h_curl_orients_allocated = (CeedInt8 *)curl_orients;
613dce49693SSebastian Grimberg               impl->h_curl_orients           = (CeedInt8 *)curl_orients;
614dce49693SSebastian Grimberg               break;
615dce49693SSebastian Grimberg             case CEED_USE_POINTER:
616dce49693SSebastian Grimberg               impl->h_curl_orients = (CeedInt8 *)curl_orients;
617dce49693SSebastian Grimberg               break;
618dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
619dce49693SSebastian Grimberg               CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_allocated));
620dce49693SSebastian Grimberg               memcpy(impl->h_curl_orients_allocated, curl_orients, 3 * size * sizeof(CeedInt8));
621dce49693SSebastian Grimberg               impl->h_curl_orients = impl->h_curl_orients_allocated;
622dce49693SSebastian Grimberg               break;
623dce49693SSebastian Grimberg           }
624dce49693SSebastian Grimberg           CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_curl_orients, 3 * size * sizeof(CeedInt8)));
625dce49693SSebastian Grimberg           impl->d_curl_orients_allocated = impl->d_curl_orients;  // We own the device memory
626dce49693SSebastian Grimberg           CeedCallCuda(ceed, cudaMemcpy(impl->d_curl_orients, curl_orients, 3 * size * sizeof(CeedInt8), cudaMemcpyHostToDevice));
627dce49693SSebastian Grimberg         } break;
628dce49693SSebastian Grimberg         case CEED_MEM_DEVICE: {
629dce49693SSebastian Grimberg           switch (copy_mode) {
630dce49693SSebastian Grimberg             case CEED_COPY_VALUES:
631dce49693SSebastian Grimberg               CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_curl_orients, 3 * size * sizeof(CeedInt8)));
632dce49693SSebastian Grimberg               impl->d_curl_orients_allocated = impl->d_curl_orients;  // We own the device memory
633dce49693SSebastian Grimberg               CeedCallCuda(ceed, cudaMemcpy(impl->d_curl_orients, curl_orients, 3 * size * sizeof(CeedInt8), cudaMemcpyDeviceToDevice));
634dce49693SSebastian Grimberg               break;
635dce49693SSebastian Grimberg             case CEED_OWN_POINTER:
636dce49693SSebastian Grimberg               impl->d_curl_orients           = (CeedInt8 *)curl_orients;
637dce49693SSebastian Grimberg               impl->d_curl_orients_allocated = impl->d_curl_orients;
638dce49693SSebastian Grimberg               break;
639dce49693SSebastian Grimberg             case CEED_USE_POINTER:
640dce49693SSebastian Grimberg               impl->d_curl_orients = (CeedInt8 *)curl_orients;
641dce49693SSebastian Grimberg               break;
642dce49693SSebastian Grimberg           }
643dce49693SSebastian Grimberg           CeedCallBackend(CeedMalloc(3 * size, &impl->h_curl_orients_allocated));
644dce49693SSebastian Grimberg           CeedCallCuda(ceed, cudaMemcpy(impl->h_curl_orients_allocated, impl->d_curl_orients, 3 * size * sizeof(CeedInt8), cudaMemcpyDeviceToHost));
645dce49693SSebastian Grimberg           impl->h_curl_orients = impl->h_curl_orients_allocated;
646dce49693SSebastian Grimberg         } break;
647dce49693SSebastian Grimberg       }
648dce49693SSebastian Grimberg     }
649dce49693SSebastian Grimberg   }
650ca735530SJeremy L Thompson 
651ff1e7120SSebastian Grimberg   // Register backend functions
652dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Apply", CeedElemRestrictionApply_Cuda));
653dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnsigned", CeedElemRestrictionApplyUnsigned_Cuda));
654dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "ApplyUnoriented", CeedElemRestrictionApplyUnoriented_Cuda));
655dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOffsets", CeedElemRestrictionGetOffsets_Cuda));
656dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Cuda));
657dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Cuda));
658dce49693SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Cuda));
659ff1e7120SSebastian Grimberg   return CEED_ERROR_SUCCESS;
660ff1e7120SSebastian Grimberg }
661ff1e7120SSebastian Grimberg 
662ff1e7120SSebastian Grimberg //------------------------------------------------------------------------------
663