Example 3

And one final example, to show that an optimization is disabled across an atomic operation.

Problem: Shared memory loads cannot be hoisted across atomic operations, verify that the optimization has been disabled.

And the code looks something like:

 int global = 0;

 int sum[3] = { 0, 0, 0 };
 atomic_int atomi;

 /* Bump the value of global every cycle.  */
 void other_threads()
 {
   global++;
 }

 /* Nothing to check every cycle.  */
 int step_verify()
 {
   return 0;
 }

 /* Final result should have a different value of 'global' stored in each
  * element, unless the load was hoisted.  */
 int final_verify()
 {
   if (sum[0] != sum[1] || sum[1] != sum[2] || sum[0] != sum[2])
     return 1;
   return 0;
 }

 /* 'global' is bumped by "the other thread" every insn.
  * test is that all elements of 'sum' are different, otherwise the load of 
  * 'global' has been illegally hoisted across the atomic store.  */
 void test()
 {
   int x;
   for (x=0; x< 3; x++)
     {
       atomic_store (&atomi, x);
       sum[x] = global;
     }
 }

 extern void done();

 int main()
 {
   test();
   done();
 }

None: Atomic/GCCMM/TestSamp3 (last edited 2010-05-06 15:16:17 by AndrewMacLeod)