CUB的TexRefInputIterator是如何工作的?

How does CUB's TexRefInputIterator work?

本文关键字:工作 何工作 TexRefInputIterator CUB      更新时间:2023-10-16

CUB为纹理引用提供了一个迭代器,其实现很容易访问。

因为我不能弄清楚如何实现可模板的纹理引用自己-他们"只能声明为一个静态全局变量" -我现在试图理解它是如何在CUB中完成的。但有些问题超出了我的c++知识范围,而且我还没能在其他地方找到答案(再说一次,我真的不知道该搜索什么)。

专:

围绕IteratorTexRef的未命名的namespace有意义吗?我只能认为这是将IteratorTexRef::TexId::ref限制在文件/翻译单元范围内。

IteratorTexRef的目的是什么?它只包装了TexId,但是删除它会导致难以理解的(对我来说)编译时错误。

这段代码是链接实现的精简版本,编译并运行:

#include <thrust/device_vector.h>
namespace {
template <typename T>
struct IteratorTexRef
{
    template <int UNIQUE_ID>
    struct TexId
    {
        // Assume T is a valid texture word size.
        typedef texture<T> TexRef;
        static TexRef ref;
        static __device__ T fetch(ptrdiff_t offset)
        {
            return tex1Dfetch(ref, offset);
        }
    };
};
template <typename  T>
template <int UNIQUE_ID>
typename IteratorTexRef<T>:: template TexId<UNIQUE_ID>::TexRef IteratorTexRef<T>:: template TexId<UNIQUE_ID>::ref;
} // Anomymous namespace
template <typename T, int UNIQUE_ID = 0>
class TextureRefIterator
{
private:
    typedef typename IteratorTexRef<T>:: template TexId<UNIQUE_ID> TexId;
    ptrdiff_t tex_offset;
public:
    __device__ T operator[](int i) const
    {
        return TexId::fetch(this->tex_offset + i);
    }
    cudaError_t bind(
        const T* const ptr,
        size_t bytes = size_t(-1))
    {
        size_t offset;
        cudaError_t state = cudaBindTexture(&offset, TexId::ref, ptr, bytes);
        this->tex_offset = (ptrdiff_t) (offset / sizeof(T));
        return state;
    }
};
template <typename TexIter>
__global__ void kernel(TexIter iter)
{
    int a = iter[threadIdx.x];
    printf("tid %d, a %dn", threadIdx.x, a);
}
template <typename T>
void launch_kernel(T* d_in)
{
    TextureRefIterator<T> tex_iter;
    tex_iter.bind(d_in);
    kernel<<<1, 32>>>(tex_iter);
}
int main()
{
    thrust::device_vector<float> d_in(32, 1);
    launch_kernel(thrust::raw_pointer_cast(d_in.data()));
}

我得到的最接近于下面的东西,基于通常如何访问静态模板成员。为了清楚起见,下面简单地从上面删除了IteratorTexRef:

#include <thrust/device_vector.h>
namespace {
template <typename T, int UNIQUE_ID>
struct TexId
{
    // Assume T is a valid texture word size.
    typedef texture<T> TexRef;
    static TexRef ref;
    static __device__ T fetch(ptrdiff_t offset)
    {
        return tex1Dfetch(ref, offset);
    }
};
template <typename  T, int UNIQUE_ID>
typename TexId<T, UNIQUE_ID>::TexRef TexId<T, UNIQUE_ID>::ref;

} // Anonymous namespace
template <typename T, int UNIQUE_ID = 0>
class TextureRefIterator
{
private:
    typedef TexId<T, UNIQUE_ID> TexId;
    ptrdiff_t tex_offset;
public:
    __device__ T operator[](int i) const
    {
        return TexId::fetch(this->tex_offset + i);
    }
    cudaError_t bind(
        const T* const ptr,
        size_t bytes = size_t(-1))
    {
        size_t offset;
        cudaError_t state = cudaBindTexture(&offset, TexId::ref, ptr, bytes);
        this->tex_offset = (ptrdiff_t) (offset / sizeof(T));
        return state;
    }
};
template <typename TexIter>
__global__ void kernel(TexIter iter)
{
    int a = iter[0];
    printf("tid %d, a %dn", threadIdx.x, a);
}
template <typename T>
void launch_kernel(T* d_in)
{
    TextureRefIterator<T> tex_iter;
    tex_iter.bind(d_in);
    kernel<<<1, 32>>>(tex_iter);
}
int main()
{
    thrust::device_vector<float> d_in(32, 1);
    launch_kernel(thrust::raw_pointer_cast(d_in.data()));
}

它给出了这些有点深奥的编译时错误。(使用nvcc iter.cu和CUDA 7.0编译):

In file included from tmpxft_000057d4_00000000-4_test2.cudafe1.stub.c:1:0:
/tmp/tmpxft_000057d4_00000000-4_test2.cudafe1.stub.c:30:3737: error: macro "__text_var" passed 3 arguments, but takes just 2
 dIfLi0EE3refE,::_NV_ANON_NAMESPACE::TexId<float, (int)0> ::ref), 1, 0, 0);__cudaReg
                                                                         ^
/tmp/tmpxft_000057d4_00000000-4_test2.cudafe1.stub.c:30:1: error: macro "__device__text_var" passed 3 arguments, but takes just 2
 static void __nv_cudaEntityRegisterCallback(void **__T2202){__nv_dummy_param_ref(__
 ^
/tmp/tmpxft_000057d4_00000000-4_test2.cudafe1.stub.c:30:1: error: macro "__name__text_var" passed 3 arguments, but takes just 2

该编译错误是由于使用包含模板类型的宏生成的代码,因此模板中的逗号使预处理器认为它们是更多的参数。我通过修补crt/host_runtime头并使这些宏(__text_var, __device__text_var和__name__text_var)的cpp参数变进来解决这个问题。换句话说,将cpp替换为cpp....