-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathLookupTable1D.hpp
More file actions
68 lines (53 loc) · 2.15 KB
/
LookupTable1D.hpp
File metadata and controls
68 lines (53 loc) · 2.15 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
/*******************************************************
* Copyright (c) 2020, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#pragma once
#include <Array.hpp>
#include <err_cuda.hpp>
#include <type_traits>
namespace arrayfire {
namespace cuda {
template<typename T>
class LookupTable1D {
public:
LookupTable1D() = delete;
LookupTable1D(const LookupTable1D& arg) = delete;
LookupTable1D(const LookupTable1D&& arg) = delete;
LookupTable1D& operator=(const LookupTable1D& arg) = delete;
LookupTable1D& operator=(const LookupTable1D&& arg) = delete;
LookupTable1D(const Array<T>& lutArray) : mTexture(0), mData(lutArray) {
cudaResourceDesc resDesc;
memset(&resDesc, 0, sizeof(resDesc));
cudaTextureDesc texDesc;
memset(&texDesc, 0, sizeof(texDesc));
resDesc.resType = cudaResourceTypeLinear;
resDesc.res.linear.devPtr = mData.get();
resDesc.res.linear.desc.x = sizeof(T) * 8;
resDesc.res.linear.sizeInBytes = mData.elements() * sizeof(T);
if (std::is_signed<T>::value)
resDesc.res.linear.desc.f = cudaChannelFormatKindSigned;
else if (std::is_unsigned<T>::value)
resDesc.res.linear.desc.f = cudaChannelFormatKindUnsigned;
else
resDesc.res.linear.desc.f = cudaChannelFormatKindFloat;
texDesc.readMode = cudaReadModeElementType;
CUDA_CHECK(
cudaCreateTextureObject(&mTexture, &resDesc, &texDesc, NULL));
}
~LookupTable1D() {
if (mTexture) { cudaDestroyTextureObject(mTexture); }
}
cudaTextureObject_t get() const noexcept { return mTexture; }
private:
// Keep a copy so that ref count doesn't go down to zero when
// original Array<T> goes out of scope before LookupTable1D object does.
Array<T> mData;
cudaTextureObject_t mTexture;
};
} // namespace cuda
} // namespace arrayfire