Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable multiple precisions in CeedVector #948

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
85 changes: 49 additions & 36 deletions backends/cuda-ref/ceed-cuda-vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ static inline int CeedVectorSyncD2H_Cuda(const CeedVector vec) {
//------------------------------------------------------------------------------
// Sync arrays
//------------------------------------------------------------------------------
static int CeedVectorSyncArray_Cuda(const CeedVector vec,
CeedMemType mem_type) {
static int CeedVectorSyncArrayGeneric_Cuda(const CeedVector vec,
CeedMemType mem_type,
CeedScalarType prec) {
int ierr;
// Check whether device/host sync is needed
bool need_sync = false;
Expand All @@ -123,8 +124,8 @@ static int CeedVectorSyncArray_Cuda(const CeedVector vec,
return CEED_ERROR_SUCCESS;

switch (mem_type) {
case CEED_MEM_HOST: return CeedVectorSyncD2H_Cuda(vec);
case CEED_MEM_DEVICE: return CeedVectorSyncH2D_Cuda(vec);
case CEED_MEM_HOST: return CeedVectorSyncD2H_Cuda(vec, prec);
case CEED_MEM_DEVICE: return CeedVectorSyncH2D_Cuda(vec, prec);
}
return CEED_ERROR_UNSUPPORTED;
}
Expand Down Expand Up @@ -182,7 +183,8 @@ static inline int CeedVectorHasArrayOfType_Cuda(const CeedVector vec,
// Check if has borrowed array of given type
//------------------------------------------------------------------------------
static inline int CeedVectorHasBorrowedArrayOfType_Cuda(const CeedVector vec,
CeedMemType mem_type, bool *has_borrowed_array_of_type) {
CeedMemType mem_type, CeedScalarType prec,
bool *has_borrowed_array_of_type) {
int ierr;
CeedVector_Cuda *impl;
ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr);
Expand All @@ -203,7 +205,7 @@ static inline int CeedVectorHasBorrowedArrayOfType_Cuda(const CeedVector vec,
// Set array from host
//------------------------------------------------------------------------------
static int CeedVectorSetArrayHost_Cuda(const CeedVector vec,
const CeedCopyMode copy_mode, CeedScalar *array) {
const CeedCopyMode copy_mode, void *array) {
int ierr;
CeedVector_Cuda *impl;
ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr);
Expand Down Expand Up @@ -245,7 +247,7 @@ static int CeedVectorSetArrayHost_Cuda(const CeedVector vec,
// Set array from device
//------------------------------------------------------------------------------
static int CeedVectorSetArrayDevice_Cuda(const CeedVector vec,
const CeedCopyMode copy_mode, CeedScalar *array) {
const CeedCopyMode copy_mode, void *array) {
int ierr;
Ceed ceed;
ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr);
Expand Down Expand Up @@ -289,9 +291,11 @@ static int CeedVectorSetArrayDevice_Cuda(const CeedVector vec,
// Set the array used by a vector,
// freeing any previously allocated array if applicable
//------------------------------------------------------------------------------
static int CeedVectorSetArray_Cuda(const CeedVector vec,
const CeedMemType mem_type,
const CeedCopyMode copy_mode, CeedScalar *array) {
static int CeedVectorSetArrayGeneric_Cuda(const CeedVector vec,
const CeedMemType mem_type,
const CeedScalarType prec,
const CeedCopyMode copy_mode,
void *array) {
int ierr;
Ceed ceed;
ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr);
Expand Down Expand Up @@ -368,8 +372,8 @@ static int CeedVectorSetValue_Cuda(CeedVector vec, CeedScalar val) {
//------------------------------------------------------------------------------
// Vector Take Array
//------------------------------------------------------------------------------
static int CeedVectorTakeArray_Cuda(CeedVector vec, CeedMemType mem_type,
CeedScalar **array) {
static int CeedVectorTakeArrayGeneric_Cuda(CeedVector vec, CeedMemType mem_type,
CeedScalarType prec, void **array) {
int ierr;
Ceed ceed;
ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr);
Expand Down Expand Up @@ -401,7 +405,9 @@ static int CeedVectorTakeArray_Cuda(CeedVector vec, CeedMemType mem_type,
// If a different memory type is most up to date, this will perform a copy
//------------------------------------------------------------------------------
static int CeedVectorGetArrayCore_Cuda(const CeedVector vec,
const CeedMemType mem_type, CeedScalar **array) {
const CeedMemType mem_type,
const CeedScalarType prec,
void **array) {
int ierr;
Ceed ceed;
ierr = CeedVectorGetCeed(vec, &ceed); CeedChkBackend(ierr);
Expand All @@ -426,21 +432,26 @@ static int CeedVectorGetArrayCore_Cuda(const CeedVector vec,
//------------------------------------------------------------------------------
// Get read-only access to a vector via the specified mem_type
//------------------------------------------------------------------------------
static int CeedVectorGetArrayRead_Cuda(const CeedVector vec,
const CeedMemType mem_type, const CeedScalar **array) {
return CeedVectorGetArrayCore_Cuda(vec, mem_type, (CeedScalar **)array);
static int CeedVectorGetArrayReadGeneric_Cuda(const CeedVector vec,
const CeedMemType mem_type,
const CeedScalarType prec,
const void **array) {
return CeedVectorGetArrayCore_Cuda(vec, mem_type, prec, (void **)array);
}

//------------------------------------------------------------------------------
// Get read/write access to a vector via the specified mem_type
// Get read/write access to a vector via the specified mem_type and precision
//------------------------------------------------------------------------------
static int CeedVectorGetArray_Cuda(const CeedVector vec,
const CeedMemType mem_type, CeedScalar **array) {
const CeedMemType mem_type,
const CeedScalarType prec,
void **array) {
int ierr;
CeedVector_Cuda *impl;
ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr);

ierr = CeedVectorGetArrayCore_Cuda(vec, mem_type, array); CeedChkBackend(ierr);
ierr = CeedVectorGetArrayCore_Cuda(vec, mem_type, prec, array);
CeedChkBackend(ierr);

ierr = CeedVectorSetAllInvalid_Cuda(vec); CeedChkBackend(ierr);
switch (mem_type) {
Expand All @@ -456,10 +467,12 @@ static int CeedVectorGetArray_Cuda(const CeedVector vec,
}

//------------------------------------------------------------------------------
// Get write access to a vector via the specified mem_type
// Get write access to a vector via the specified mem_type and precision
//------------------------------------------------------------------------------
static int CeedVectorGetArrayWrite_Cuda(const CeedVector vec,
const CeedMemType mem_type, CeedScalar **array) {
static int CeedVectorGetArrayWriteGeneric_Cuda(const CeedVector vec,
const CeedMemType mem_type,
const CeedScalarType prec,
void **array) {
int ierr;
CeedVector_Cuda *impl;
ierr = CeedVectorGetData(vec, &impl); CeedChkBackend(ierr);
Expand All @@ -469,7 +482,7 @@ static int CeedVectorGetArrayWrite_Cuda(const CeedVector vec,
CeedChkBackend(ierr);
if (!has_array_of_type) {
// Allocate if array is not yet allocated
ierr = CeedVectorSetArray(vec, mem_type, CEED_COPY_VALUES, NULL);
ierr = CeedVectorSetArrayGeneric(vec, mem_type, prec, CEED_COPY_VALUES, NULL);
CeedChkBackend(ierr);
} else {
// Select dirty array
Expand All @@ -488,7 +501,7 @@ static int CeedVectorGetArrayWrite_Cuda(const CeedVector vec,
}
}

return CeedVectorGetArray_Cuda(vec, mem_type, array);
return CeedVectorGetArrayGeneric_Cuda(vec, mem_type, prec, array);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -755,21 +768,21 @@ int CeedVectorCreate_Cuda(CeedSize n, CeedVector vec) {
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType",
CeedVectorHasBorrowedArrayOfType_Cuda);
CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SetArray",
CeedVectorSetArray_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "TakeArray",
CeedVectorTakeArray_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SetArrayGeneric",
CeedVectorSetArrayGeneric_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "TakeArrayGeneric",
CeedVectorTakeArrayGeneric_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SetValue",
(int (*)())(CeedVectorSetValue_Cuda));
CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SyncArray",
CeedVectorSyncArray_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArray",
CeedVectorGetArray_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayRead",
CeedVectorGetArrayRead_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWrite",
CeedVectorGetArrayWrite_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "SyncArrayGeneric",
CeedVectorSyncArrayGeneric_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayGeneric",
CeedVectorGetArrayGeneric_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayReadGeneric",
CeedVectorGetArrayReadGeneric_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "GetArrayWriteGeneric",
CeedVectorGetArrayWriteGeneric_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "Norm",
CeedVectorNorm_Cuda); CeedChkBackend(ierr);
ierr = CeedSetBackendFunction(ceed, "Vector", vec, "Reciprocal",
Expand Down
32 changes: 20 additions & 12 deletions backends/hip-ref/ceed-hip-ref-basis.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ int CeedBasisApply_Hip(CeedBasis basis, const CeedInt num_elem,
const int max_block_size = 64;

// Read vectors
const CeedScalar *d_u;
CeedScalar *d_v;
const void *d_u;
void *d_v;
if (eval_mode != CEED_EVAL_WEIGHT) {
ierr = CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u); CeedChkBackend(ierr);
ierr = CeedVectorGetArrayReadGeneric(u, CEED_MEM_DEVICE, CEED_SCALAR_TYPE,
&d_u);
CeedChkBackend(ierr);
}
ierr = CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v); CeedChkBackend(ierr);
ierr = CeedVectorGetArrayWriteGeneric(v, CEED_MEM_DEVICE, CEED_SCALAR_TYPE,
&d_v);
CeedChkBackend(ierr);

// Clear v for transpose operation
if (t_mode == CEED_TRANSPOSE) {
Expand Down Expand Up @@ -93,9 +97,9 @@ int CeedBasisApply_Hip(CeedBasis basis, const CeedInt num_elem,

// Restore vectors
if (eval_mode != CEED_EVAL_WEIGHT) {
ierr = CeedVectorRestoreArrayRead(u, &d_u); CeedChkBackend(ierr);
ierr = CeedVectorRestoreArrayReadGeneric(u, &d_u); CeedChkBackend(ierr);
}
ierr = CeedVectorRestoreArray(v, &d_v); CeedChkBackend(ierr);
ierr = CeedVectorRestoreArrayGeneric(v, &d_v); CeedChkBackend(ierr);
return CEED_ERROR_SUCCESS;
}

Expand All @@ -121,12 +125,16 @@ int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem,
num_elem/elemsPerBlock*elemsPerBlock<num_elem)?1:0);

// Read vectors
const CeedScalar *d_u;
CeedScalar *d_v;
const void *d_u;
void *d_v;
if (eval_mode != CEED_EVAL_WEIGHT) {
ierr = CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u); CeedChkBackend(ierr);
ierr = CeedVectorGetArrayReadGeneric(u, CEED_MEM_DEVICE, CEED_SCALAR_TYPE,
&d_u);
CeedChkBackend(ierr);
}
ierr = CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v); CeedChkBackend(ierr);
ierr = CeedVectorGetArrayWriteGeneric(v, CEED_MEM_DEVICE, CEED_SCALAR_TYPE,
&d_v);
CeedChkBackend(ierr);

// Clear v for transpose operation
if (t_mode == CEED_TRANSPOSE) {
Expand Down Expand Up @@ -183,9 +191,9 @@ int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem,

// Restore vectors
if (eval_mode != CEED_EVAL_WEIGHT) {
ierr = CeedVectorRestoreArrayRead(u, &d_u); CeedChkBackend(ierr);
ierr = CeedVectorRestoreArrayReadGeneric(u, &d_u); CeedChkBackend(ierr);
}
ierr = CeedVectorRestoreArray(v, &d_v); CeedChkBackend(ierr);
ierr = CeedVectorRestoreArrayGeneric(v, &d_v); CeedChkBackend(ierr);
return CEED_ERROR_SUCCESS;
}

Expand Down
Loading