10

I am working on data prefetch in CUDA (Fermi GPU) through C code. Cuda reference manual talks about the prefetching at ptx level code not at C level code.

Can anyone connect me with some documents or something regarding prefetching through cuda code (cu file). Any help would be appreciated.

user1805482
  • 101
  • 1
  • 3

2 Answers2

9

According to PTX manual here is how prefetch works in PTX:

enter image description here

You can embed the PTX instructions into the CUDA kernel. Here is a tiny sample from NVIDIA's documentation:

__device__ int cube (int x)
{
  int y;
  asm("{\n\t"                       // use braces for local scope
      " .reg .u32 t1;\n\t"           // temp reg t1,
      " mul.lo.u32 t1, %1, %1;\n\t" // t1 = x * x
      " mul.lo.u32 %0, t1, %1;\n\t" // y = t1 * x
      "}"
      : "=r"(y) : "r" (x));
  return y;
}

You may come to conclude with the following prefetch function in C:

__device__ void prefetch_l1 (unsigned int addr)
{

  asm(" prefetch.global.L1 [ %1 ];": "=r"(addr) : "r"(addr));
}

NOTICE: You need the GPU of Compute Capability 2.0 or higher for prefetch. Pass proper compile flags accordingly -arch=sm_20

lashgar
  • 5,184
  • 3
  • 37
  • 45
  • can you provide me on some more documentation on how prefetching works, like the explanation of the concept itself. – Fady Kamal Jan 09 '14 at 17:54
  • 2
    sure! check this GPGPU prefetching study and go through references to figure out more about the concept: http://www.cc.gatech.edu/~hyesoon/lee_taco12.pdf – lashgar Jan 10 '14 at 03:54
3

According to this thread, below is the code for different cache prefetching techniques:

#define DEVICE_STATIC_INTRINSIC_QUALIFIERS  static __device__ __forceinline__

#if (defined(_MSC_VER) && defined(_WIN64)) || defined(__LP64__)
#define PXL_GLOBAL_PTR   "l"
#else
#define PXL_GLOBAL_PTR   "r"
#endif

DEVICE_STATIC_INTRINSIC_QUALIFIERS void __prefetch_global_l1(const void* const ptr)
{
  asm("prefetch.global.L1 [%0];" : : PXL_GLOBAL_PTR(ptr));
}

DEVICE_STATIC_INTRINSIC_QUALIFIERS void __prefetch_global_uniform(const void* const ptr)
{
  asm("prefetchu.L1 [%0];" : : PXL_GLOBAL_PTR(ptr));
}

DEVICE_STATIC_INTRINSIC_QUALIFIERS void __prefetch_global_l2(const void* const ptr)
{
  asm("prefetch.global.L2 [%0];" : : PXL_GLOBAL_PTR(ptr));
}
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • Good lifting. Now we just need an example where these actually provide a benefit. – tera Jul 28 '19 at 15:12
  • @tera I have a general rule of thumb: if Nisght Compute lists long scoreboard stalls as the top stall contributer, you're mostly going to benefit from prefetches. This rule of thumb has worked in 9/10 cases for me. It's very important for low occupancy kernels (like when you are limited to a warp or a block). – Yashas Jun 02 '20 at 14:20