"vscode:/vscode.git/clone" did not exist on "1b40311f660360ee0ce9006217cae4ad5508b8a2"
common.hip.hpp 2.81 KB
Newer Older
1
2
#pragma once

Chao Liu's avatar
Chao Liu committed
3
4
5
__device__ unsigned get_thread_local_1d_id() { return threadIdx.x; }

__device__ unsigned get_block_1d_id() { return blockIdx.x; }
6

7
8
9
10
11
12
13
14
15
16
17
template <class T1, class T2>
struct is_same
{
    static const bool value = false;
};

template <class T>
struct is_same<T, T>
{
    static const bool value = true;
};
Chao Liu's avatar
Chao Liu committed
18

Chao Liu's avatar
Chao Liu committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
template <class T, unsigned N>
struct vector_type
{
};

template <>
struct vector_type<float, 1>
{
    using type = float;
};

template <>
struct vector_type<float, 2>
{
    using type = float2;
};

template <>
struct vector_type<float, 4>
{
    using type = float4;
};

#if 0
template <>
struct vector_type<half_float::half, 1>
{
    using type = half_float::half;
};

template <>
struct vector_type<half_float::half, 2>
{
    using type = float;
};

template <>
struct vector_type<half_float::half, 4>
{
    using type = float2;
};

template <>
struct vector_type<half_float::half, 8>
{
    using type = float4;
};
#endif

#if 1
template <>
struct vector_type<half, 1>
{
    using type = half;
};

template <>
struct vector_type<half, 2>
{
    using type = half2;
};

template <>
struct vector_type<half, 4>
{
    using type = float2;
};

template <>
struct vector_type<half, 8>
{
    using type = float4;
};
#endif

Chao Liu's avatar
Chao Liu committed
94
template <class T, T N>
Chao Liu's avatar
Chao Liu committed
95
struct integral_constant
Chao Liu's avatar
Chao Liu committed
96
{
Chao Liu's avatar
Chao Liu committed
97
    static const T value = N;
Chao Liu's avatar
Chao Liu committed
98

Chao Liu's avatar
Chao Liu committed
99
    __host__ __device__ constexpr T Get() const { return value; }
Chao Liu's avatar
Chao Liu committed
100
101
102
};

template <unsigned N>
Chao Liu's avatar
Chao Liu committed
103
using Number = integral_constant<unsigned, N>;
Chao Liu's avatar
Chao Liu committed
104
105
106
107

template <unsigned... Is>
struct Sequence
{
Chao Liu's avatar
Chao Liu committed
108
109
    using Type = Sequence<Is...>;

Chao Liu's avatar
Chao Liu committed
110
111
112
113
114
115
116
117
118
119
120
    static constexpr unsigned nDim = sizeof...(Is);

    const unsigned mData[nDim] = {Is...};

    template <unsigned I>
    __host__ __device__ constexpr unsigned Get(Number<I>) const
    {
        return mData[I];
    }

    template <unsigned I0, unsigned I1, unsigned I2, unsigned I3>
Chao Liu's avatar
Chao Liu committed
121
    __host__ __device__ constexpr auto ReorderByGetNewFromOld(Sequence<I0, I1, I2, I3>) const
Chao Liu's avatar
Chao Liu committed
122
    {
Chao Liu's avatar
Chao Liu committed
123
        constexpr auto old_sequence = Type{};
Chao Liu's avatar
Chao Liu committed
124

Chao Liu's avatar
Chao Liu committed
125
126
127
128
129
130
        constexpr unsigned NR0 = old_sequence.mData[I0];
        constexpr unsigned NR1 = old_sequence.mData[I1];
        constexpr unsigned NR2 = old_sequence.mData[I2];
        constexpr unsigned NR3 = old_sequence.mData[I3];

        return Sequence<NR0, NR1, NR2, NR3>{};
Chao Liu's avatar
Chao Liu committed
131
132
133
    }

    template <unsigned I0, unsigned I1, unsigned I2, unsigned I3>
Chao Liu's avatar
Chao Liu committed
134
    __host__ __device__ constexpr auto ReorderByPutOldToNew(Sequence<I0, I1, I2, I3>) const
Chao Liu's avatar
Chao Liu committed
135
    {
Chao Liu's avatar
Chao Liu committed
136
137
138
        // don't know how to implement this
        printf("Sequence::ReorderByPutOldToNew not implemented");
        assert(false);
Chao Liu's avatar
Chao Liu committed
139
    }
Chao Liu's avatar
Chao Liu committed
140
};
Chao Liu's avatar
Chao Liu committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

template <typename T>
__host__ __device__ constexpr T max(T a, T b)
{
    return a > b ? a : b;
}

template <typename T>
__host__ __device__ constexpr T min(T a, T b)
{
    return a < b ? a : b;
}

__host__ __device__ constexpr unsigned integer_divide_ceil(unsigned a, unsigned b)
{
    return (a + b - 1) / b;
}