Skip to content

Commit

Permalink
vec - update SetArray to keep old arrays for CEED_COPY_VALUES
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylt committed Jan 16, 2025
1 parent 20e9c75 commit f0c2a8a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 18 deletions.
9 changes: 5 additions & 4 deletions backends/cuda/ceed-cuda-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ static inline int CeedSetDeviceGenericArray_Cuda(Ceed ceed, const void *source_a
void *target_array_owned, void *target_array_borrowed, void *target_array) {
switch (copy_mode) {
case CEED_COPY_VALUES:
if (!*(void **)target_array_owned) CeedCallCuda(ceed, cudaMalloc(target_array_owned, size_unit * num_values));
if (source_array) CeedCallCuda(ceed, cudaMemcpy(*(void **)target_array_owned, source_array, size_unit * num_values, cudaMemcpyDeviceToDevice));
*(void **)target_array_borrowed = NULL;
*(void **)target_array = *(void **)target_array_owned;
if (!*(void **)target_array) {
CeedCallCuda(ceed, cudaMalloc(target_array_owned, size_unit * num_values));
*(void **)target_array = *(void **)target_array_owned;
}
if (source_array) CeedCallCuda(ceed, cudaMemcpy(*(void **)target_array, source_array, size_unit * num_values, cudaMemcpyDeviceToDevice));
break;
case CEED_OWN_POINTER:
CeedCallCuda(ceed, cudaFree(*(void **)target_array_owned));
Expand Down
9 changes: 5 additions & 4 deletions backends/hip/ceed-hip-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ static inline int CeedSetDeviceGenericArray_Hip(Ceed ceed, const void *source_ar
void *target_array_owned, void *target_array_borrowed, void *target_array) {
switch (copy_mode) {
case CEED_COPY_VALUES:
if (!*(void **)target_array_owned) CeedCallHip(ceed, hipMalloc(target_array_owned, size_unit * num_values));
if (source_array) CeedCallHip(ceed, hipMemcpy(*(void **)target_array_owned, source_array, size_unit * num_values, hipMemcpyDeviceToDevice));
*(void **)target_array_borrowed = NULL;
*(void **)target_array = *(void **)target_array_owned;
if (!*(void **)target_array) {
CeedCallHip(ceed, hipMalloc(target_array_owned, size_unit * num_values));
*(void **)target_array = *(void **)target_array_owned;
}
if (source_array) CeedCallHip(ceed, hipMemcpy(*(void **)target_array, source_array, size_unit * num_values, hipMemcpyDeviceToDevice));
break;
case CEED_OWN_POINTER:
CeedCallHip(ceed, hipFree(*(void **)target_array_owned));
Expand Down
13 changes: 7 additions & 6 deletions backends/memcheck/ceed-memcheck-vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,21 @@ static int CeedVectorSetArray_Memcheck(CeedVector vec, CeedMemType mem_type, Cee
VALGRIND_DISCARD(impl->allocated_block_id);
}
CeedCallBackend(CeedFree(&impl->array_allocated));
if (impl->array_owned) {
for (CeedSize i = 0; i < length; i++) impl->array_owned[i] = NAN;
VALGRIND_DISCARD(impl->owned_block_id);
if (copy_mode != CEED_COPY_VALUES) {
if (impl->array_owned) {
for (CeedSize i = 0; i < length; i++) impl->array_owned[i] = NAN;
VALGRIND_DISCARD(impl->owned_block_id);
}
CeedCallBackend(CeedFree(&impl->array_owned));
}
CeedCallBackend(CeedFree(&impl->array_owned));

// Clear borrowed block id, if present
if (impl->array_borrowed) VALGRIND_DISCARD(impl->borrowed_block_id);

// Set internal pointers to external arrays
switch (copy_mode) {
case CEED_COPY_VALUES:
impl->array_owned = NULL;
impl->array_borrowed = NULL;
// Nothing to update
break;
case CEED_OWN_POINTER:
impl->array_owned = array;
Expand Down
9 changes: 5 additions & 4 deletions interface/ceed.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,11 @@ static inline int CeedSetHostGenericArray(const void *source_array, CeedCopyMode
void *target_array_owned, void *target_array_borrowed, void *target_array) {
switch (copy_mode) {
case CEED_COPY_VALUES:
if (!*(void **)target_array_owned) CeedCall(CeedCallocArray(num_values, size_unit, target_array_owned));
if (source_array) memcpy(*(void **)target_array_owned, source_array, size_unit * num_values);
*(void **)target_array_borrowed = NULL;
*(void **)target_array = *(void **)target_array_owned;
if (!*(void **)target_array) {
CeedCall(CeedCallocArray(num_values, size_unit, target_array_owned));
*(void **)target_array = *(void **)target_array_owned;
}
if (source_array) memcpy(*(void **)target_array, source_array, size_unit * num_values);
break;
case CEED_OWN_POINTER:
CeedCall(CeedFree(target_array_owned));
Expand Down
51 changes: 51 additions & 0 deletions tests/t128-vector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/// @file
/// Test copying into vector with borrowed pointer
/// \test Test copying into vector with borrowed pointer
#include <ceed.h>
#include <stdio.h>

int main(int argc, char **argv) {
Ceed ceed;
CeedVector x, x_copy;
CeedInt len = 10;
CeedScalar array_borrowed[len];

CeedInit(argv[1], &ceed);

CeedVectorCreate(ceed, len, &x);
CeedVectorCreate(ceed, len, &x_copy);

{
CeedScalar array[len];

for (CeedInt i = 0; i < len; i++) {
array[i] = i;
array_borrowed[i] = 10 + i;
}

CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
CeedVectorSetArray(x_copy, CEED_MEM_HOST, CEED_USE_POINTER, array_borrowed);
}

// Copy to device if preferred
{
CeedMemType mem_type = CEED_MEM_HOST;

CeedGetPreferredMemType(ceed, &mem_type);
if (mem_type == CEED_MEM_DEVICE) CeedVectorSyncArray(x, CEED_MEM_DEVICE);
}

// Copy and sync borrowed array
CeedVectorCopy(x, x_copy);
CeedVectorSyncArray(x_copy, CEED_MEM_HOST);

// Check that borrowed array is the same as the original input array a
for (CeedInt i = 0; i < len; i++) {
if (array_borrowed[i] != i) printf("Error in copying values of CeedVector\n");
}

CeedVectorDestroy(&x);
CeedVectorDestroy(&x_copy);
CeedDestroy(&ceed);
return 0;
}

0 comments on commit f0c2a8a

Please sign in to comment.