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

resolve get on device edge case #82

Merged
merged 3 commits into from
Nov 14, 2023
Merged
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
12 changes: 11 additions & 1 deletion spiner/databox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
namespace Spiner {

enum class IndexType { Interpolated = 0, Named = 1, Indexed = 2 };
enum class DataStatus { Empty, Unmanaged, AllocatedHost, AllocatedDevice };
enum class DataStatus {
Empty = 0,
Unmanaged = 1,
AllocatedHost = 2,
AllocatedDevice = 3
};
enum class AllocationTarget { Host, Device };

template <typename T = Real, typename Grid_t = RegularGrid1D<T>,
Expand Down Expand Up @@ -302,6 +307,11 @@ class DataBox {

DataBox<T, Grid_t, Concept>
getOnDevice() const { // getOnDevice is always a deep copy
if (size() == 0 ||
status_ == DataStatus::Empty) { // edge case for unallocated
DataBox<T, Grid_t, Concept> a;
return a;
}
// create device memory (host memory if no device)
T *device_data = (T *)PORTABLE_MALLOC(sizeBytes());
// copy to device
Expand Down
8 changes: 8 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,14 @@ SCENARIO("Copying a DataBox to device", "[DataBox][GetOnDevice]") {
printf("free db_host\n");
free(db_host);
}
GIVEN("An empty databox");
DataBox db;
WHEN("We copy it to device") {
DataBox db2 = db.getOnDevice();
THEN("The new object is still empty") {
REQUIRE(db.dataStatus() == Spiner::DataStatus::Empty);
}
}
}

SCENARIO("Using unique pointers to garbage collect DataBox",
Expand Down
Loading