title: Do current x86 architectures support non-temporal loads (from "normal" memory)?
body: <p>I am aware of multiple questions on this topic, however, I haven't seen any clear answers nor any benchmark measurements. I thus created a simple program that works with two arrays of integers. The first array <code>a</code> is very large (64 MB) and the second array <code>b</code> is small to fit into L1 cache. The program iterates over <code>a</code> and adds its elements to corresponding elements of <code>b</code> in a modular sense (when the end of <code>b</code> is reached, the program starts from its beginning again). The measured numbers of L1 cache misses for different sizes of <code>b</code> is as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/LgpsB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LgpsB.png" alt="enter image description here"></a></p>
  
  <p>The measurements were made on a Xeon E5 2680v3 Haswell type CPU with 32 kiB L1 data cache. Therefore, in all the cases, <code>b</code> fitted into L1 cache. However, the number of misses grew considerably by around 16 kiB of <code>b</code> memory footprint. This might be expected since the loads of both <code>a</code> and <code>b</code> causes invalidation of cache lines from the beginning of <code>b</code> at this point.</p>
  
  <p>There is absolutely no reason to keep elements of <code>a</code> in cache, they are used only once. I therefore run a program variant with non-temporal loads of <code>a</code> data, but the number of misses did not change. I also run a variant with non-temporal prefetching of <code>a</code> data, but still with the very same results. </p>
  
  <p>My benchmark code is as follows (variant w/o non-temporal prefetching shown):</p>
  
  <pre><code>int main(int argc, char* argv[])
  {
     uint64_t* a;
     const uint64_t a_bytes = 64 * 1024 * 1024;
     const uint64_t a_count = a_bytes / sizeof(uint64_t);
     posix_memalign((void**)(&amp;a), 64, a_bytes);
  
     uint64_t* b;
     const uint64_t b_bytes = atol(argv[1]) * 1024;
     const uint64_t b_count = b_bytes / sizeof(uint64_t);
     posix_memalign((void**)(&amp;b), 64, b_bytes);
  
     __m256i ones = _mm256_set1_epi64x(1UL);
     for (long i = 0; i &lt; a_count; i += 4)
         _mm256_stream_si256((__m256i*)(a + i), ones);
  
     // load b into L1 cache
     for (long i = 0; i &lt; b_count; i++)
         b[i] = 0;
  
     int papi_events[1] = { PAPI_L1_DCM };
     long long papi_values[1];
     PAPI_start_counters(papi_events, 1);
  
     uint64_t* a_ptr = a;
     const uint64_t* a_ptr_end = a + a_count;
     uint64_t* b_ptr = b;
     const uint64_t* b_ptr_end = b + b_count;
  
     while (a_ptr &lt; a_ptr_end) {
  #ifndef NTLOAD
        __m256i aa = _mm256_load_si256((__m256i*)a_ptr);
  #else
        __m256i aa = _mm256_stream_load_si256((__m256i*)a_ptr);
  #endif
        __m256i bb = _mm256_load_si256((__m256i*)b_ptr);
        bb = _mm256_add_epi64(aa, bb);
        _mm256_store_si256((__m256i*)b_ptr, bb);
  
        a_ptr += 4;
        b_ptr += 4;
        if (b_ptr &gt;= b_ptr_end)
           b_ptr = b;
     }
  
     PAPI_stop_counters(papi_values, 1);
     std::cout &lt;&lt; "L1 cache misses: " &lt;&lt; papi_values[0] &lt;&lt; std::endl;
  
     free(a);
     free(b);
  }
  </code></pre>
  
  <p>What I wonder is whether CPU vendors support or are going to support non-temporal loads / prefetching or any other way how to label some data as not-being-hold in cache (e.g., to tag them as LRU). There are situations, e.g., in HPC, where similar scenarios are common in practice. For example, in sparse iterative linear solvers / eigensolvers, matrix data are usually very large (larger than cache capacities), but vectors are sometimes small enough to fit into L3 or even L2 cache. Then, we would like to keep them there at all costs. Unfortunately, loading of matrix data can cause invalidation of especially x-vector cache lines, even though in each solver iteration, matrix elements are used only once and there is no reason to keep them in cache after they have been processed.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I just did a similar experiment on an Intel Xeon Phi KNC, while measuring  runtime instead of L1 misses (I haven't find a way how to measure them reliably; PAPI and VTune gave weird metrics.) The results are here:</p>
  
  <p><a href="https://i.stack.imgur.com/CU2dY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CU2dY.png" alt="enter image description here"></a></p>
  
  <p>The orange curve represents ordinary loads and it has the expected shape. The blue curve represents loads with so-call eviction hint (EH) set in the instruction prefix and the gray curve represents a case where each cache line of <code>a</code> was manually evicted; both these tricks enabled by KNC obviously worked as we wanted to for <code>b</code> over 16 kiB. The code of the measured loop is as follows:</p>
  
  <pre><code>while (a_ptr &lt; a_ptr_end) {
  #ifdef NTLOAD
     __m512i aa = _mm512_extload_epi64((__m512i*)a_ptr,
        _MM_UPCONV_EPI64_NONE, _MM_BROADCAST64_NONE, _MM_HINT_NT);
  #else
     __m512i aa = _mm512_load_epi64((__m512i*)a_ptr);
  #endif
     __m512i bb = _mm512_load_epi64((__m512i*)b_ptr);
     bb = _mm512_or_epi64(aa, bb);
     _mm512_store_epi64((__m512i*)b_ptr, bb);
  
  #ifdef EVICT
     _mm_clevict(a_ptr, _MM_HINT_T0);
  #endif
  
     a_ptr += 8;
     b_ptr += 8;
     if (b_ptr &gt;= b_ptr_end)
         b_ptr = b;
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>On Xeon Phi, <code>icpc</code> generated for normal-load variant (orange curve) prefetching for <code>a_ptr</code>:</p>
  
  <pre><code>400e93:       62 d1 78 08 18 4c 24    vprefetch0 [r12+0x80]
  </code></pre>
  
  <p>When I manually (by hex-editing the executable) modified this to:</p>
  
  <pre><code>400e93:       62 d1 78 08 18 44 24    vprefetchnta [r12+0x80]
  </code></pre>
  
  <p>I got the desired resutls, even better than the blue/gray curves. However, I was not able to force the compiler to generate non-temporal prefetchnig for me, even by using <code>#pragma prefetch a_ptr:_MM_HINT_NTA</code> before the loop :(</p>