common.hip.hpp 2.49 KB
Newer Older
1
#pragma once
Chao Liu's avatar
Chao Liu committed
2
#include "vector_type.hip.hpp"
Chao Liu's avatar
Chao Liu committed
3
#include "integral_constant.hip.hpp"
4
5
6
#include "Sequence.hip.hpp"
#include "Array.hip.hpp"
#include "functional.hip.hpp"
7
#include "functional2.hip.hpp"
Chao Liu's avatar
Chao Liu committed
8
#include "functional3.hip.hpp"
9

Chao Liu's avatar
Chao Liu committed
10
#if USE_AMD_INLINE_ASM
Chao Liu's avatar
Chao Liu committed
11
#include "amd_inline_asm.hip.hpp"
Chao Liu's avatar
Chao Liu committed
12
13
#endif

Chao Liu's avatar
Chao Liu committed
14
__device__ index_t get_thread_local_1d_id() { return threadIdx.x; }
Chao Liu's avatar
Chao Liu committed
15

Chao Liu's avatar
Chao Liu committed
16
__device__ index_t get_block_1d_id() { return blockIdx.x; }
17

18
19
20
template <class T1, class T2>
struct is_same
{
Chao Liu's avatar
Chao Liu committed
21
    static constexpr bool value = false;
22
23
24
25
26
};

template <class T>
struct is_same<T, T>
{
Chao Liu's avatar
Chao Liu committed
27
    static constexpr bool value = true;
28
};
Chao Liu's avatar
Chao Liu committed
29

Chao Liu's avatar
Chao Liu committed
30
31
32
33
34
35
template <class X, class Y>
__host__ __device__ constexpr bool is_same_type(X, Y)
{
    return is_same<X, Y>::value;
}

36
namespace mod_conv { // namespace mod_conv
Chao Liu's avatar
Chao Liu committed
37
38
template <class T, T s>
struct scales
39
{
Chao Liu's avatar
Chao Liu committed
40
    __host__ __device__ constexpr T operator()(T a) const { return s * a; }
41
42
};

43
44
45
46
47
48
49
50
51
52
53
54
template <class T>
struct plus
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a + b; }
};

template <class T>
struct multiplies
{
    __host__ __device__ constexpr T operator()(T a, T b) const { return a * b; }
};

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
template <class T>
struct integer_divide_ceiler
{
    __host__ __device__ constexpr T operator()(T a, T b) const
    {
        static_assert(is_same<T, index_t>::value || is_same<T, int>::value, "wrong type");

        return (a + b - 1) / b;
    }
};

template <class T>
__host__ __device__ constexpr T integer_divide_ceil(T a, T b)
{
    static_assert(is_same<T, index_t>::value || is_same<T, int>::value, "wrong type");

Chao Liu's avatar
Chao Liu committed
71
    return (a + b - 1) / b;
Chao Liu's avatar
Chao Liu committed
72
73
}

Chao Liu's avatar
Chao Liu committed
74
75
template <class T>
__host__ __device__ constexpr T max(T x, T y)
Chao Liu's avatar
Chao Liu committed
76
{
Chao Liu's avatar
Chao Liu committed
77
    return x > y ? x : y;
Chao Liu's avatar
Chao Liu committed
78
79
}

Chao Liu's avatar
Chao Liu committed
80
81
template <class T, class... Ts>
__host__ __device__ constexpr T max(T x, Ts... xs)
Chao Liu's avatar
Chao Liu committed
82
{
Chao Liu's avatar
Chao Liu committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    static_assert(sizeof...(xs) > 0, "not enough argument");

    auto y = max(xs...);

    static_assert(is_same<decltype(y), T>::value, "not the same type");

    return x > y ? x : y;
}

template <class T>
__host__ __device__ constexpr T min(T x, T y)
{
    return x < y ? x : y;
}

template <class T, class... Ts>
__host__ __device__ constexpr T min(T x, Ts... xs)
{
    static_assert(sizeof...(xs) > 0, "not enough argument");

    auto y = min(xs...);

    static_assert(is_same<decltype(y), T>::value, "not the same type");

    return x < y ? x : y;
}
109
110
111
112

// this is wrong
// TODO: implement correct least common multiple, instead of calling max()
template <class T, class... Ts>
Chao Liu's avatar
Chao Liu committed
113
__host__ __device__ constexpr T lcm(T x, Ts... xs)
114
115
116
117
{
    return max(x, xs...);
}

Chao Liu's avatar
tidy up  
Chao Liu committed
118
} // namespace mod_conv