#include <ceed.h>
#include <ceed/backend.h>
#include <ceed/jit-tools.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <hip/hip_runtime.h>
#include "../hip/ceed-hip-common.h"
#include "../hip/ceed-hip-compile.h"
#include "ceed-hip-ref.h"
static int CeedElemRestrictionApply_Hip(CeedElemRestriction r, CeedTransposeMode t_mode, CeedVector u, CeedVector v, CeedRequest *request) {
Ceed ceed;
Ceed_Hip *data;
CeedInt num_elem, elem_size;
const CeedScalar *d_u;
CeedScalar *d_v;
CeedElemRestriction_Hip *impl;
hipFunction_t kernel;
CeedCallBackend(CeedElemRestrictionGetData(r, &impl));
CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed));
CeedCallBackend(CeedGetData(ceed, &data));
CeedElemRestrictionGetNumElements(r, &num_elem);
CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size));
const CeedInt num_nodes = impl->num_nodes;
CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
if (t_mode == CEED_TRANSPOSE) {
CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
} else {
CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
}
if (t_mode == CEED_NOTRANSPOSE) {
if (impl->d_ind) {
kernel = impl->OffsetNoTranspose;
void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v};
CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256;
CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
} else {
kernel = impl->StridedNoTranspose;
void *args[] = {&num_elem, &d_u, &d_v};
CeedInt block_size = elem_size < 256 ? (elem_size > 64 ? elem_size : 64) : 256;
CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
}
} else {
if (impl->d_ind) {
CeedInt block_size = 64;
if (impl->OffsetTranspose) {
kernel = impl->OffsetTranspose;
void *args[] = {&num_elem, &impl->d_ind, &d_u, &d_v};
CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
} else {
kernel = impl->OffsetTransposeDet;
void *args[] = {&impl->d_l_vec_indices, &impl->d_t_indices, &impl->d_t_offsets, &d_u, &d_v};
CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
}
} else {
kernel = impl->StridedTranspose;
void *args[] = {&num_elem, &d_u, &d_v};
CeedInt block_size = 64;
CeedCallBackend(CeedRunKernel_Hip(ceed, kernel, CeedDivUpInt(num_nodes, block_size), block_size, args));
}
}
if (request != CEED_REQUEST_IMMEDIATE && request != CEED_REQUEST_ORDERED) *request = NULL;
CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
return CEED_ERROR_SUCCESS;
}
static int CeedElemRestrictionGetOffsets_Hip(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
CeedElemRestriction_Hip *impl;
CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl));
switch (mem_type) {
case CEED_MEM_HOST:
*offsets = impl->h_ind;
break;
case CEED_MEM_DEVICE:
*offsets = impl->d_ind;
break;
}
return CEED_ERROR_SUCCESS;
}
static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction r) {
Ceed ceed;
CeedElemRestriction_Hip *impl;
CeedCallBackend(CeedElemRestrictionGetData(r, &impl));
CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed));
CeedCallHip(ceed, hipModuleUnload(impl->module));
CeedCallBackend(CeedFree(&impl->h_ind_allocated));
CeedCallHip(ceed, hipFree(impl->d_ind_allocated));
CeedCallHip(ceed, hipFree(impl->d_t_offsets));
CeedCallHip(ceed, hipFree(impl->d_t_indices));
CeedCallHip(ceed, hipFree(impl->d_l_vec_indices));
CeedCallBackend(CeedFree(&impl));
return CEED_ERROR_SUCCESS;
}
static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction r, const CeedInt *indices) {
Ceed ceed;
bool *is_node;
CeedSize l_size;
CeedInt num_elem, elem_size, num_comp, num_nodes = 0, *ind_to_offset, *l_vec_indices, *t_offsets, *t_indices;
CeedElemRestriction_Hip *impl;
CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed));
CeedCallBackend(CeedElemRestrictionGetData(r, &impl));
CeedCallBackend(CeedElemRestrictionGetNumElements(r, &num_elem));
CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size));
CeedCallBackend(CeedElemRestrictionGetLVectorSize(r, &l_size));
CeedCallBackend(CeedElemRestrictionGetNumComponents(r, &num_comp));
const CeedInt size_indices = num_elem * elem_size;
CeedCallBackend(CeedCalloc(l_size, &is_node));
for (CeedInt i = 0; i < size_indices; i++) is_node[indices[i]] = 1;
for (CeedInt i = 0; i < l_size; i++) num_nodes += is_node[i];
impl->num_nodes = num_nodes;
CeedCallBackend(CeedCalloc(l_size, &ind_to_offset));
CeedCallBackend(CeedCalloc(num_nodes, &l_vec_indices));
for (CeedInt i = 0, j = 0; i < l_size; i++) {
if (is_node[i]) {
l_vec_indices[j] = i;
ind_to_offset[i] = j++;
}
}
CeedCallBackend(CeedFree(&is_node));
const CeedInt size_offsets = num_nodes + 1;
CeedCallBackend(CeedCalloc(size_offsets, &t_offsets));
CeedCallBackend(CeedMalloc(size_indices, &t_indices));
for (CeedInt e = 0; e < num_elem; ++e) {
for (CeedInt i = 0; i < elem_size; ++i) ++t_offsets[ind_to_offset[indices[elem_size * e + i]] + 1];
}
for (CeedInt i = 1; i < size_offsets; ++i) t_offsets[i] += t_offsets[i - 1];
for (CeedInt e = 0; e < num_elem; ++e) {
for (CeedInt i = 0; i < elem_size; ++i) {
const CeedInt lid = elem_size * e + i;
const CeedInt gid = indices[lid];
t_indices[t_offsets[ind_to_offset[gid]]++] = lid;
}
}
for (int i = size_offsets - 1; i > 0; --i) t_offsets[i] = t_offsets[i - 1];
t_offsets[0] = 0;
CeedCallHip(ceed, hipMalloc((void **)&impl->d_l_vec_indices, num_nodes * sizeof(CeedInt)));
CeedCallHip(ceed, hipMemcpy(impl->d_l_vec_indices, l_vec_indices, num_nodes * sizeof(CeedInt), hipMemcpyHostToDevice));
CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_offsets, size_offsets * sizeof(CeedInt)));
CeedCallHip(ceed, hipMemcpy(impl->d_t_offsets, t_offsets, size_offsets * sizeof(CeedInt), hipMemcpyHostToDevice));
CeedCallHip(ceed, hipMalloc((void **)&impl->d_t_indices, size_indices * sizeof(CeedInt)));
CeedCallHip(ceed, hipMemcpy(impl->d_t_indices, t_indices, size_indices * sizeof(CeedInt), hipMemcpyHostToDevice));
CeedCallBackend(CeedFree(&ind_to_offset));
CeedCallBackend(CeedFree(&l_vec_indices));
CeedCallBackend(CeedFree(&t_offsets));
CeedCallBackend(CeedFree(&t_indices));
return CEED_ERROR_SUCCESS;
}
int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *indices, const bool *orients,
const CeedInt8 *curl_orients, CeedElemRestriction r) {
Ceed ceed, ceed_parent;
bool is_deterministic, is_strided;
char *restriction_kernel_path, *restriction_kernel_source;
CeedInt num_elem, num_comp, elem_size, comp_stride = 1;
CeedRestrictionType rstr_type;
CeedElemRestriction_Hip *impl;
CeedCallBackend(CeedElemRestrictionGetCeed(r, &ceed));
CeedCallBackend(CeedCalloc(1, &impl));
CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic));
CeedCallBackend(CeedElemRestrictionGetNumElements(r, &num_elem));
CeedCallBackend(CeedElemRestrictionGetNumComponents(r, &num_comp));
CeedCallBackend(CeedElemRestrictionGetElementSize(r, &elem_size));
CeedInt size = num_elem * elem_size;
CeedInt strides[3] = {1, size, elem_size};
CeedInt layout[3] = {1, elem_size * num_elem, elem_size};
CeedCallBackend(CeedElemRestrictionGetType(r, &rstr_type));
CeedCheck(rstr_type != CEED_RESTRICTION_ORIENTED && rstr_type != CEED_RESTRICTION_CURL_ORIENTED, ceed, CEED_ERROR_BACKEND,
"Backend does not implement CeedElemRestrictionCreateOriented or CeedElemRestrictionCreateCurlOriented");
CeedCallBackend(CeedElemRestrictionIsStrided(r, &is_strided));
if (is_strided) {
bool has_backend_strides;
CeedCallBackend(CeedElemRestrictionHasBackendStrides(r, &has_backend_strides));
if (!has_backend_strides) {
CeedCallBackend(CeedElemRestrictionGetStrides(r, &strides));
}
} else {
CeedCallBackend(CeedElemRestrictionGetCompStride(r, &comp_stride));
}
impl->h_ind = NULL;
impl->h_ind_allocated = NULL;
impl->d_ind = NULL;
impl->d_ind_allocated = NULL;
impl->d_t_indices = NULL;
impl->d_t_offsets = NULL;
impl->num_nodes = size;
CeedCallBackend(CeedElemRestrictionSetData(r, impl));
CeedCallBackend(CeedElemRestrictionSetELayout(r, layout));
switch (mem_type) {
case CEED_MEM_HOST: {
switch (copy_mode) {
case CEED_OWN_POINTER:
impl->h_ind_allocated = (CeedInt *)indices;
impl->h_ind = (CeedInt *)indices;
break;
case CEED_USE_POINTER:
impl->h_ind = (CeedInt *)indices;
break;
case CEED_COPY_VALUES:
if (indices != NULL) {
CeedCallBackend(CeedMalloc(elem_size * num_elem, &impl->h_ind_allocated));
memcpy(impl->h_ind_allocated, indices, elem_size * num_elem * sizeof(CeedInt));
impl->h_ind = impl->h_ind_allocated;
}
break;
}
if (indices != NULL) {
CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
impl->d_ind_allocated = impl->d_ind; CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyHostToDevice));
if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(r, indices));
}
break;
}
case CEED_MEM_DEVICE: {
switch (copy_mode) {
case CEED_COPY_VALUES:
if (indices != NULL) {
CeedCallHip(ceed, hipMalloc((void **)&impl->d_ind, size * sizeof(CeedInt)));
impl->d_ind_allocated = impl->d_ind; CeedCallHip(ceed, hipMemcpy(impl->d_ind, indices, size * sizeof(CeedInt), hipMemcpyDeviceToDevice));
}
break;
case CEED_OWN_POINTER:
impl->d_ind = (CeedInt *)indices;
impl->d_ind_allocated = impl->d_ind;
break;
case CEED_USE_POINTER:
impl->d_ind = (CeedInt *)indices;
}
if (indices != NULL) {
CeedCallBackend(CeedMalloc(elem_size * num_elem, &impl->h_ind_allocated));
CeedCallHip(ceed, hipMemcpy(impl->h_ind_allocated, impl->d_ind, elem_size * num_elem * sizeof(CeedInt), hipMemcpyDeviceToHost));
impl->h_ind = impl->h_ind_allocated;
if (is_deterministic) CeedCallBackend(CeedElemRestrictionOffset_Hip(r, indices));
}
break;
}
default:
return CeedError(ceed, CEED_ERROR_BACKEND, "Only MemType = HOST or DEVICE supported");
}
CeedInt num_nodes = impl->num_nodes;
CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-restriction.h", &restriction_kernel_path));
CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source -----\n");
CeedCallBackend(CeedLoadSourceToBuffer(ceed, restriction_kernel_path, &restriction_kernel_source));
CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Restriction Kernel Source Complete! -----\n");
CeedCallBackend(CeedCompile_Hip(ceed, restriction_kernel_source, &impl->module, 8, "RESTR_ELEM_SIZE", elem_size, "RESTR_NUM_ELEM", num_elem,
"RESTR_NUM_COMP", num_comp, "RESTR_NUM_NODES", num_nodes, "RESTR_COMP_STRIDE", comp_stride, "RESTR_STRIDE_NODES",
strides[0], "RESTR_STRIDE_COMP", strides[1], "RESTR_STRIDE_ELEM", strides[2]));
CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedNoTranspose", &impl->StridedNoTranspose));
CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "StridedTranspose", &impl->StridedTranspose));
CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetNoTranspose", &impl->OffsetNoTranspose));
if (!is_deterministic) {
CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTranspose", &impl->OffsetTranspose));
} else {
CeedCallBackend(CeedGetKernel_Hip(ceed, impl->module, "OffsetTransposeDet", &impl->OffsetTransposeDet));
}
CeedCallBackend(CeedFree(&restriction_kernel_path));
CeedCallBackend(CeedFree(&restriction_kernel_source));
CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "Apply", CeedElemRestrictionApply_Hip));
CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "ApplyUnsigned", CeedElemRestrictionApply_Hip));
CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "ApplyUnoriented", CeedElemRestrictionApply_Hip));
CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "GetOffsets", CeedElemRestrictionGetOffsets_Hip));
CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", r, "Destroy", CeedElemRestrictionDestroy_Hip));
return CEED_ERROR_SUCCESS;
}