-
Notifications
You must be signed in to change notification settings - Fork 0
Update Array2D accessing pattern & use CUDA_CALLABLE macro #17
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
Conversation
|
@horizon-blue I leave this to you, but I did prompt cursor for what kind of interesting tests we could do with // Test that modifications through view affect underlying data
TYPED_TEST(Array2DTestFixture, ViewModifiesUnderlyingData) {
if constexpr (std::is_same_v<TypeParam, std::vector<float>>) {
uint32_t rows = 3;
uint32_t cols = 4;
auto data = TypeParam(rows * cols, 0.0f);
auto array2d = Array2D(thrust::raw_pointer_cast(data.data()), rows, cols);
// Modify through view
array2d[1][2] = 42.5f;
// Verify underlying data changed
EXPECT_FLOAT_EQ(data[1 * cols + 2], 42.5f);
// Modify underlying data directly
data[0 * cols + 1] = 99.9f;
// Verify view reflects change
EXPECT_FLOAT_EQ(array2d[0][1], 99.9f);
}
}
// Test multiple views of the same data
TYPED_TEST(Array2DTestFixture, MultipleViewsOfSameData) {
if constexpr (std::is_same_v<TypeParam, std::vector<float>>) {
uint32_t rows = 2;
uint32_t cols = 3;
auto data = TypeParam(rows * cols, 0.0f);
auto view1 = Array2D(thrust::raw_pointer_cast(data.data()), rows, cols);
auto view2 = Array2D(thrust::raw_pointer_cast(data.data()), rows, cols);
// Modify through view1
view1[0][0] = 100.0f;
// Verify view2 sees the change
EXPECT_FLOAT_EQ(view2[0][0], 100.0f);
// Modify through view2
view2[1][2] = 200.0f;
// Verify view1 sees the change
EXPECT_FLOAT_EQ(view1[1][2], 200.0f);
}
}Considering that this is a view on an underlying data, these tests made sense to me. For I leave it to you to decide if you want to add them in, they pass on my side locally so either ways this works for sure! Otherwise nice change with the operator! Will be super convenient. I'll approve this, and you can merge it when you decide what to do! |
genmetaballs/src/cuda/core/utils.cuh
Outdated
| } | ||
|
|
||
| __host__ __device__ constexpr auto rank() const noexcept { | ||
| CUDA_CALLABLE constexpr auto rank() const noexcept { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| CUDA_CALLABLE constexpr auto rank() const noexcept { | |
| CUDA_CALLABLE constexpr auto ndim() const noexcept { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extremely minor thing, I know this rank here refers to the dynamic rank of a matrix, which is 2, but I tend to think of the linear algebra rank instead. suggestion is to rename it to ndim (copying pythonic ndarrays), but feel free to ignore haha.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me haha. I named the method rank() only because the mdspan is calling it so, but I agree that this can be confusing for folks with a math brain 😆. I will update this shortly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you accept my ndim suggestion, then you would need to edit line 147 from rank to ndim for test to pass. It works on my end locally.
c611bbb to
96ce9ba
Compare
|
Ok, I renamed |
(Closes MET-47)
Summary of Changes
This PR addresses some of the suggestions that @mugamma brought up in #14. In particular, it defines the
operator[]onArray2Dto return a 1D view of a row, so we can use patterns likearray2d[i][j]instead ofarray2d(i, j)to access the element.Another nice thing about returning the 1D span is that we can use range-based for loop to go over the elements in a row as well, e.g.
You can find some example usages in the included test file.
Another minor change in this PR is the refactoring of
Array2Dmethods to use the newCUDA_CALLABLEmacro that Arijit introduced recently.Test Plan
To run the included unit tests:
pixi run test