Alpaka: MSVC and g++ host compiler fail to deduce return type of min/max

Created on 26 May 2020  路  11Comments  路  Source: alpaka-group/alpaka

When I use the alpaka::math::min or alpaka::math::max function, nvcc compiles fine, but MSVC or g++ as host compiler fails to compile the remaining code after nvcc.

Given, from mallocMC,
`` const uint32 fullsegments = alpaka::math::min(acc, 32u, pagesize / segmentsize); ```` g++ complains with:error: void value not ignored as it ought to beat the assignment tofullsegments. The error message indicates, that the host compiler sees a return type ofvoid` for the alpaka min/max functions, probably in the code it is given my nvcc.

The problem disappears, if the return type for alpaka::math::traits::Min<MinUniformCudaHipBuiltIn, ...>::min(...) is explicitely specified as e.g. Tx or decltype(::min(x, y)).

Most helpful comment

As a sidenote: the alpaka unit tests seem to only test the math functions on float and double, but not on integral arguments (which does not make sense for all functions).

All 11 comments

Can you reproduce this in an alpaka unit test so that it can be checked in CI?

@sbastrakov said that it could be an issue when min is a function which can not be inlined. ::min() is in the CUDA headers defined as extern __device__ function so it could be the reason.

Another problem could be that a,b in min(acc, a,b) has a different type.

So while reproducing the issue in a small unit test, I ran into a different issue. But maybe this is caused by the same problem.

The following code:

#include <alpaka/alpaka.hpp>
void test() {
    using Idx = std::size_t;
    using Dim = alpaka::dim::DimInt<1>;
    using Acc = alpaka::acc::AccGpuCudaRt<Dim, Idx>;
    auto f = [] ALPAKA_FN_ACC(const Acc & acc) {
        const auto l = 4;
        const auto u = 5;
        const auto r = alpaka::math::min(acc, l, u);
    };
}

fails to compile with the host hompiler (MSVC):

1>C:/dev/mallocMC/alpaka/include\alpaka/math/min/Traits.hpp(57): error #2970: calling a __device__ function("min") from a __host__ __device__ function("min") is not allowed
1>          detected during instantiation of "auto alpaka::math::min(const T &, const Tx &, const Ty &) [with T=alpaka::acc::AccGpuCudaRt<alpaka::dim::DimInt<1Ui64>, size_t>, Tx=int, Ty=int]".

nvcc compiles fine.

The problem is that the CUDA ::min() function is marked as __device__, whereas the alpaka::math::min() is marked as __host__ __device__. Since the return type of alpaka::math::min() is deduced, the host compiler seems to fail to deduce this type from the __device__ function and assumes it's void.

As a sidenote: the alpaka unit tests seem to only test the math functions on float and double, but not on integral arguments (which does not make sense for all functions).

I think it is only solvable by adding the return type. I got the same issue in cupla during I implemented math functions there https://github.com/alpaka-group/cupla/blob/7e7509463b48d393edc7596d36b20de2b0fef2f4/include/cupla/device/math/Common.hpp#L89-L103

See my comment in the cupla code

return type is required for the compiler to detect host, device  function qualifier correctly                                             

@bernhardmgruber could you please test if you workaround decltype(::min(x, y)) helps in you unit test.

@psychocoderHPC yes, adding decltype(::min(x, y)) solves it. This is what i added a few days ago on top of the alpaka subtree in mallocMC.

Is this issue already solved?

solved with #1085

Was this page helpful?
0 / 5 - 0 ratings

Related issues

theZiz picture theZiz  路  5Comments

ax3l picture ax3l  路  4Comments

SimeonEhrig picture SimeonEhrig  路  5Comments

shefmarkh picture shefmarkh  路  4Comments

mxmlnkn picture mxmlnkn  路  5Comments