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

Update SetArray to keep old arrays for CEED_COPY_VALUES #1730

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions backends/cuda/ceed-cuda-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ 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;
Comment on lines -56 to -59
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is where we were always dumping the old borrowed array

if (!*(void **)target_array) {
if (*(void **)target_array_borrowed) {
*(void **)target_array = *(void **)target_array_borrowed;
} else {
if (!*(void **)target_array_owned) 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
13 changes: 9 additions & 4 deletions backends/hip/ceed-hip-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ 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) {
if (*(void **)target_array_borrowed) {
*(void **)target_array = *(void **)target_array_borrowed;
} else {
if (!*(void **)target_array_owned) 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
13 changes: 9 additions & 4 deletions interface/ceed.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,15 @@ 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) {
if (*(void **)target_array_borrowed) {
*(void **)target_array = *(void **)target_array_borrowed;
} else {
if (!*(void **)target_array_owned) 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
2 changes: 1 addition & 1 deletion python/tests/test-1-vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def test_126(ceed_resource, capsys):
a = np.arange(10, 10 + n, dtype=ceed.scalar_type())
x.set_array(a, cmode=libceed.USE_POINTER)

a2 = np.arange(10, n, dtype=ceed.scalar_type())
a2 = np.arange(0, n, dtype=ceed.scalar_type())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an embarrassing oops on my part from years ago

y.set_array(a2, cmode=libceed.USE_POINTER)

y.copy_from(x)
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;
}