import tensorflow as tf


class AssignOpTest(tf.test.TestCase):

  # NOTE(mrry): We exclude thess tests from the TSAN TAP target, because they
  #   contain benign and deliberate data races when multiple threads update
  #   the same parameters without a lock.
  def testParallelUpdateWithoutLocking(self):
    with self.test_session() as sess:
      ones_t = tf.fill([1024, 1024], 1.0)
      p = tf.Variable(tf.zeros([1024, 1024]))
      adds = [tf.assign_add(p, ones_t, use_locking=False)
              for _ in range(20)]
      tf.initialize_all_variables().run()

      def run_add(add_op):
        sess.run(add_op)
      threads = [self.checkedThread(target=run_add, args=(add_op,))
                 for add_op in adds]
      for t in threads:
        t.start()
