CUDA PTX f32.f32纹理读取
是否可以使用浮点索引直接从CUDA纹理读取,例如,是否可以使用CUDA PTX f32.f32纹理读取,cuda,textures,ptx,Cuda,Textures,Ptx,是否可以使用浮点索引直接从CUDA纹理读取,例如,是否可以使用tex.1d.v4.f32.f32执行纹理提取 查看.ptx文件时,这似乎保存了两条指令,这反映在基准测试时性能的提高上。然而,一个相当关键的缺点是,尽管这看起来没有问题,但它并没有产生预期的结果 下面的代码演示了该问题: #include "cuda.h" #include <thrust/device_vector.h> //create a global 1D texture of type float textu
tex.1d.v4.f32.f32
执行纹理提取
查看.ptx
文件时,这似乎保存了两条指令,这反映在基准测试时性能的提高上。然而,一个相当关键的缺点是,尽管这看起来没有问题,但它并没有产生预期的结果
下面的代码演示了该问题:
#include "cuda.h"
#include <thrust/device_vector.h>
//create a global 1D texture of type float
texture<float, cudaTextureType1D, cudaReadModeElementType> tex;
//below is a hand rolled ptx texture lookup using tex.1d.v4.f32.f32
__device__
float tex_load(float idx)
{
float4 temp;
asm("tex.1d.v4.f32.f32 {%0, %1, %2, %3}, [tex, {%4}];" :
"=f"(temp.x), "=f"(temp.y), "=f"(temp.z), "=f"(temp.w) : "f"(idx));
return temp.x;
}
//Try to read from the texture using tex1Dfetch and the custom tex_load
__global__ void read(){
float x = tex1Dfetch(tex,0.0f);
float y = tex_load(0.0f);
printf("tex1Dfetch: %f tex_load: %f\n",x,y);
}
int main()
{
//create a vector of size 1 with the x[0]=3.14
thrust::device_vector<float> x(1,3.14);
float* x_ptr = thrust::raw_pointer_cast(&x[0]);
//bind the texture
cudaBindTexture(0, tex, x_ptr, sizeof(float));
//launch a single thread single block kernel
read<<<1,1>>>();
cudaUnbindTexture(tex);
return 0;
}
这是可能的,还是我选错了树?您的问题是,您使用的纹理指令不受支持,该纹理绑定到线性内存,默认为
cudaReadModeElementType
读取模式。如果您像这样重写函数:
__device__
float tex_load(int idx)
{
float4 temp;
asm("tex.1d.v4.f32.s32 {%0, %1, %2, %3}, [tex, {%4}];" :
"=f"(temp.x), "=f"(temp.y), "=f"(temp.z), "=f"(temp.w) : "r"(idx));
return temp.x;
}
将整数索引传递给纹理单元,而不是浮点,我想你会发现它工作正常。要使用
tex.1d.v4.f32.f32
为什么要使用v4
变量?这是偶然的还是故意的?别无选择。对于不使用FP16的1d纹理查找,纹理加载始终返回32位值的4元素向量。看这里:的确如此。你每天都会学到新东西。我只为surface access编写过PTX,其中有.none
、.v2
和.v4
修改器,我只是假设标准纹理指令与CUDAREADMODELEMENTTYPE
的读取模式相同,不执行过滤,唯一有效的坐标类型是整数索引。在这种情况下,您会发现tex1DFetch
也将发出tex.1d.v4.f32.s32
。
__device__
float tex_load(int idx)
{
float4 temp;
asm("tex.1d.v4.f32.s32 {%0, %1, %2, %3}, [tex, {%4}];" :
"=f"(temp.x), "=f"(temp.y), "=f"(temp.z), "=f"(temp.w) : "r"(idx));
return temp.x;
}