title: Measuring NUMA (Non-Uniform Memory Access). No observable asymmetry. Why?
body: <p>I've tried to measure the asymmetric memory access effects of NUMA, and failed.</p>
  
  <h2>The Experiment</h2>
  
  <p>Performed on an Intel Xeon X5570 @ 2.93GHz, 2 CPUs, 8 cores.</p>
  
  <p>On a thread pinned to core 0, I allocate an array <strong><em>x</em></strong> of size 10,000,000 bytes on core 0's NUMA node with numa_alloc_local. 
  Then I iterate over array <strong><em>x</em></strong> 50 times and read and write each byte in the array. Measure the elapsed time to do the 50 iterations. </p>
  
  <p>Then, on each of the other cores in my server, I pin a new thread and again measure the elapsed time to do 50 iterations of reading and writing
  to every byte in array <strong><em>x</em></strong>.</p>
  
  <p>Array <strong><em>x</em></strong> is large to minimize cache effects. We want to measure the speed when the CPU has to go all the way to RAM to load and store, not when caches are helping.</p>
  
  <p>There are two NUMA nodes in my server, so I would expect the cores that have affinity on the same node in which array <strong><em>x</em></strong> is allocated to have
  faster read/write speed. I'm not seeing that. </p>
  
  <p>Why?</p>
  
  <p>Perhaps NUMA is only relevant on systems with > 8-12 cores, as I've seen suggested elsewhere?</p>
  
  <p><a href="http://lse.sourceforge.net/numa/faq/" rel="noreferrer">http://lse.sourceforge.net/numa/faq/</a></p>
  
  <h2>numatest.cpp</h2>
  
  <pre><code>#include &lt;numa.h&gt;
  #include &lt;iostream&gt;
  #include &lt;boost/thread/thread.hpp&gt;
  #include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
  #include &lt;pthread.h&gt;
  
  void pin_to_core(size_t core)
  {
      cpu_set_t cpuset;
      CPU_ZERO(&amp;cpuset);
      CPU_SET(core, &amp;cpuset);
      pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &amp;cpuset);
  }
  
  std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const bitmask&amp; bm)
  {
      for(size_t i=0;i&lt;bm.size;++i)
      {
          os &lt;&lt; numa_bitmask_isbitset(&amp;bm, i);
      }
      return os;
  }
  
  void* thread1(void** x, size_t core, size_t N, size_t M)
  {
      pin_to_core(core);
  
      void* y = numa_alloc_local(N);
  
      boost::posix_time::ptime t1 = boost::posix_time::microsec_clock::universal_time();
  
      char c;
      for (size_t i(0);i&lt;M;++i)
          for(size_t j(0);j&lt;N;++j)
          {
              c = ((char*)y)[j];
              ((char*)y)[j] = c;
          }
  
      boost::posix_time::ptime t2 = boost::posix_time::microsec_clock::universal_time();
  
      std::cout &lt;&lt; "Elapsed read/write by same thread that allocated on core " &lt;&lt; core &lt;&lt; ": " &lt;&lt; (t2 - t1) &lt;&lt; std::endl;
  
      *x = y;
  }
  
  void thread2(void* x, size_t core, size_t N, size_t M)
  {
      pin_to_core(core);
  
      boost::posix_time::ptime t1 = boost::posix_time::microsec_clock::universal_time();
  
      char c;
      for (size_t i(0);i&lt;M;++i)
          for(size_t j(0);j&lt;N;++j)
          {
              c = ((char*)x)[j];
              ((char*)x)[j] = c;
          }
  
      boost::posix_time::ptime t2 = boost::posix_time::microsec_clock::universal_time();
  
      std::cout &lt;&lt; "Elapsed read/write by thread on core " &lt;&lt; core &lt;&lt; ": " &lt;&lt; (t2 - t1) &lt;&lt; std::endl;
  }
  
  int main(int argc, const char **argv)
  {
      int numcpus = numa_num_task_cpus();
      std::cout &lt;&lt; "numa_available() " &lt;&lt; numa_available() &lt;&lt; std::endl;
      numa_set_localalloc();
  
      bitmask* bm = numa_bitmask_alloc(numcpus);
      for (int i=0;i&lt;=numa_max_node();++i)
      {
          numa_node_to_cpus(i, bm);
          std::cout &lt;&lt; "numa node " &lt;&lt; i &lt;&lt; " " &lt;&lt; *bm &lt;&lt; " " &lt;&lt; numa_node_size(i, 0) &lt;&lt; std::endl;
      }
      numa_bitmask_free(bm);
  
      void* x;
      size_t N(10000000);
      size_t M(50);
  
      boost::thread t1(boost::bind(&amp;thread1, &amp;x, 0, N, M));
      t1.join();
  
      for (size_t i(0);i&lt;numcpus;++i)
      {
          boost::thread t2(boost::bind(&amp;thread2, x, i, N, M));
          t2.join();
      }
  
      numa_free(x, N);
  
      return 0;
  }
  </code></pre>
  
  <h2>The Output</h2>
  
  <pre><code>g++ -o numatest -pthread -lboost_thread -lnuma -O0 numatest.cpp
  
  ./numatest
  
  numa_available() 0                    &lt;-- NUMA is available on this system
  numa node 0 10101010 12884901888      &lt;-- cores 0,2,4,6 are on NUMA node 0, which is about 12 Gb
  numa node 1 01010101 12874584064      &lt;-- cores 1,3,5,7 are on NUMA node 1, which is slightly smaller than node 0
  
  Elapsed read/write by same thread that allocated on core 0: 00:00:01.767428
  Elapsed read/write by thread on core 0: 00:00:01.760554
  Elapsed read/write by thread on core 1: 00:00:01.719686
  Elapsed read/write by thread on core 2: 00:00:01.708830
  Elapsed read/write by thread on core 3: 00:00:01.691560
  Elapsed read/write by thread on core 4: 00:00:01.686912
  Elapsed read/write by thread on core 5: 00:00:01.691917
  Elapsed read/write by thread on core 6: 00:00:01.686509
  Elapsed read/write by thread on core 7: 00:00:01.689928
  </code></pre>
  
  <p>Doing 50 iterations reading and writing over array <strong><em>x</em></strong> takes about 1.7 seconds, no matter which core is doing the reading and writing.</p>
  
  <h2>Update:</h2>
  
  <p>The cache size on my CPUs is 8Mb, so maybe 10Mb array <strong><em>x</em></strong> is not big enough to eliminate cache effecs. I tried 100Mb array <strong><em>x</em></strong>, and
  I've tried issuing a full memory fence with __sync_synchronize() inside my innermost loops. It still doesn't reveal any asymmetry between NUMA nodes.</p>
  
  <h2>Update 2:</h2>
  
  <p>I've tried reading and writing to array <strong><em>x</em></strong> with __sync_fetch_and_add(). Still nothing.</p>