• VPP数据预取指令


    VPP使用GCC内置的函数__builtin_prefetch执行数据预取操作。函数原型如下:

    void __builtin_prefetch (const void *addr, ...)
    
    • 1

    如下为GCC文档对此函数的解释。

    This function is used to minimize cache-miss latency by moving data into a cache before it is accessed. You can insert calls to __builtin_prefetch into code for which you know addresses of data in memory that is likely to be accessed soon. If the target supports them, data prefetch instructions will be generated. If the prefetch is done early enough before the access then the data will be in the cache by the time it is accessed.

    此函数通过提前将要访问的数据移动到cache中,来减少cache缺失导致的延迟。在代码中,可针对即将访问的内存数据地址调用此函数。如果目标处理器支持,GCC将生成预取指令。如果预取足够提前的执行,那么在数据被访问的时候,其将位于cache中。

    The value of addr is the address of the memory to prefetch. There are two optional arguments, rw and locality. The value of rw is a compile-time constant one or zero; one means that the prefetch is preparing for a write to the memory address and zero, the default, means that the prefetch is preparing for a read. The value locality must be a compile-time constant integer between zero and three. A value of zero means that the data has no temporal locality, so it need not be left in the cache after the access. A value of three means that the data has a high degree of temporal locality and should be left in all levels of cache possible. Values of one and two mean, respectively, a low or moderate degree of temporal locality. The default is three.

    参数addr指定预取内存的地址。另外两个可选参数为rw和locality。参数rw是编译时常量,取值1或者0;前者意味着准备对预取的内存地址执行写操作;后者(默认值)意味着准备对预取的内存地址执行读操作。参数locality为编译时的整数常量,取值范围[0,3]。最小值0意味着数据没有时间局部性,所以在访问后不需要留在cache中。最大值3意味着数据有高度的时间局部性,应当尽可能的驻留在各级cache中。中间值1和2分别表示低度或者中度的时间局部性。默认值为3。

              for (i = 0; i < n; i++)
                {
                  a[i] = a[i] + b[i];
                  __builtin_prefetch (&a[i+j], 1, 1);
                  __builtin_prefetch (&b[i+j], 0, 1);
                  /* ... */
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Data prefetch does not generate faults if addr is invalid, but the address expression itself must be valid. For example, a prefetch of p->next will not fault if p->next is not a valid address, but evaluation will fault if p is not a valid address.

    如果参数addr无效,数据预取也不会产生错误,但是地址表达式自身必须是有效的。例如,在预取p->next地址时,如果p->next不是有效的地址,不会产生错误,但是如果p不是有效的地址,将报错。

    If the target does not support data prefetch, the address expression is evaluated if it includes side effects but no other code is generated and GCC does not issue a warning.

    如果目标处理器不支持数据预取,但是地址表达式自身包含边际效用,地址表达式将被执行,GCC不产生警告也不生成其它代码。

    VPP预取函数CLIB_PREFETCH如下,调用__builtin_prefetch函数时,固定参数locality的值为3,高度时间局部性。比如预取的报文IP头部,经常是反复使用。

    #define CLIB_PREFETCH_READ 0
    #define CLIB_PREFETCH_LOAD 0    /* alias for read */
    #define CLIB_PREFETCH_WRITE 1
    #define CLIB_PREFETCH_STORE 1   /* alias for write */
    
    #define _CLIB_PREFETCH(n,size,type)             \
      if ((size) > (n)*CLIB_CACHE_LINE_BYTES)           \
        __builtin_prefetch (_addr + (n)*CLIB_CACHE_LINE_BYTES,  \
                CLIB_PREFETCH_##type,           \
                /* locality */ 3);
          
    #define CLIB_PREFETCH(addr,size,type)       \
    do {                        \
      void * _addr = (addr);            \
                            \
      ASSERT ((size) <= 4*CLIB_CACHE_LINE_BYTES);   \
      _CLIB_PREFETCH (0, size, type);       \ 
      _CLIB_PREFETCH (1, size, type);       \
      _CLIB_PREFETCH (2, size, type);       \
      _CLIB_PREFETCH (3, size, type);       \
    } while (0)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    虚拟化、容器与Docker基本介绍以及安装部署(Docker 基本管理)
    Linux学习-HIS系统部署(1)
    飞行文本动画效果
    J2EE基础:通用前台分页02
    LQ0139 油漆面积【枚举】
    Spring的 webFlux 和 webMVC
    ClickHouse
    Transformer、BERT和GPT 自然语言处理领域的重要模型
    【Unity】渲染性能开挂GPU Animation, 动画渲染合批GPU Instance
    路由菜单路径匹配方法
  • 原文地址:https://blog.csdn.net/sinat_20184565/article/details/126563306