alpaka-group / alpaka

Abstraction Library for Parallel Kernel Acceleration :llama:
https://alpaka.readthedocs.io
Mozilla Public License 2.0
356 stars 74 forks source link

add static function dim() to alpaka::Vec #2415

Closed SimeonEhrig closed 2 weeks ago

SimeonEhrig commented 4 weeks ago

Easy way to get the dim of a alpaka::Vec.

mehmetyusufoglu commented 4 weeks ago

I opened another PR (#2416) without noticing this is already opened, I closed the other one but I think the name should be size insead of dim.

SimeonEhrig commented 4 weeks ago

My explanation from the other PR, why we should not name the function size().

We can compare alpaka::Vec with std::extents. std::extents has no explicit size but rank, what is the same like your Dim. If we use std::extents with std::mdspan, we have the function size() which returns the production of the values of each rank in the std::extents. See this example:

std::vector data(100, 0);
std::mdspan md(data.data(), std::extents{10, 10});

// output: 100
std::cout << md.size() << "\n";

// output:
// rank 0: 10
// rank 1: 10
for(auto i = 0; i < md.rank(); ++i){
    std::cout << "rank " << i << ": " << md.extent(i) << "\n"; 
}

Also the special case alpaka::Vec<int, alpaka::DimInt<1>> is the same like std::vector<int>. And in the case of std::vector size() will return the value of the one dimension.

Therefore is clear what is size() is doing. We can discuss, if we want to name the function rank() or dim() something else but size() is already used for a different function. For alpaka::Vec it is:


alpaka::Vec<int, alpaka::DimInt<1>> vec = {10};
std::cout << vec[0] << "\n"; // outputs the size 10
std::cout << vec.size() << "\n"; // outputs the size 10

alpaka::Vec<int, alpaka::DimInt<1>> vec2 = {10, 2};
std::cout << vec.size() << "\n"; // outputs the size 20
``