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