forked from facebookresearch/faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWarpShuffles.cuh
More file actions
122 lines (103 loc) · 3.31 KB
/
Copy pathWarpShuffles.cuh
File metadata and controls
122 lines (103 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD+Patents license found in the
* LICENSE file in the root directory of this source tree.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <cuda.h>
#include "DeviceDefs.cuh"
#include "Float16.cuh"
namespace faiss { namespace gpu {
template <typename T>
inline __device__ T shfl(const T val,
int srcLane, int width = kWarpSize) {
#if CUDA_VERSION >= 9000
return __shfl_sync(0xffffffff, val, srcLane, width);
#else
return __shfl(val, srcLane, width);
#endif
}
// CUDA SDK does not provide specializations for T*
template <typename T>
inline __device__ T* shfl(T* const val,
int srcLane, int width = kWarpSize) {
static_assert(sizeof(T*) == sizeof(long long), "pointer size");
long long v = (long long) val;
return (T*) shfl(v, srcLane, width);
}
template <typename T>
inline __device__ T shfl_up(const T val,
unsigned int delta, int width = kWarpSize) {
#if CUDA_VERSION >= 9000
return __shfl_up_sync(0xffffffff, val, delta, width);
#else
return __shfl_up(val, delta, width);
#endif
}
// CUDA SDK does not provide specializations for T*
template <typename T>
inline __device__ T* shfl_up(T* const val,
unsigned int delta, int width = kWarpSize) {
static_assert(sizeof(T*) == sizeof(long long), "pointer size");
long long v = (long long) val;
return (T*) shfl_up(v, delta, width);
}
template <typename T>
inline __device__ T shfl_down(const T val,
unsigned int delta, int width = kWarpSize) {
#if CUDA_VERSION >= 9000
return __shfl_down_sync(0xffffffff, val, delta, width);
#else
return __shfl_down(val, delta, width);
#endif
}
// CUDA SDK does not provide specializations for T*
template <typename T>
inline __device__ T* shfl_down(T* const val,
unsigned int delta, int width = kWarpSize) {
static_assert(sizeof(T*) == sizeof(long long), "pointer size");
long long v = (long long) val;
return (T*) shfl_down(v, delta, width);
}
template <typename T>
inline __device__ T shfl_xor(const T val,
int laneMask, int width = kWarpSize) {
#if CUDA_VERSION >= 9000
return __shfl_xor_sync(0xffffffff, val, laneMask, width);
#else
return __shfl_xor(val, laneMask, width);
#endif
}
// CUDA SDK does not provide specializations for T*
template <typename T>
inline __device__ T* shfl_xor(T* const val,
int laneMask, int width = kWarpSize) {
static_assert(sizeof(T*) == sizeof(long long), "pointer size");
long long v = (long long) val;
return (T*) shfl_xor(v, laneMask, width);
}
#ifdef FAISS_USE_FLOAT16
// CUDA 9.0 has half shuffle
#if CUDA_VERSION < 9000
inline __device__ half shfl(half v,
int srcLane, int width = kWarpSize) {
unsigned int vu = v.x;
vu = __shfl(vu, srcLane, width);
half h;
h.x = (unsigned short) vu;
return h;
}
inline __device__ half shfl_xor(half v,
int laneMask, int width = kWarpSize) {
unsigned int vu = v.x;
vu = __shfl_xor(vu, laneMask, width);
half h;
h.x = (unsigned short) vu;
return h;
}
#endif // CUDA_VERSION
#endif // FAISS_USE_FLOAT16
} } // namespace