code: def test_sync_update():
      # This test if sync_update work. This can only be tested when
      # there is a GPU.  To test if we really sync, we compare a case we
      # can run in parallel GPU and CPU computation. Then we sync to
      # disable that parallel computation. Then we assert the time is
      # higher.
  
      # this import needs to go first because it generates the
      # local 'theano' variable.  You get an UnboundLocalError otherwise.
      import theano.gpuarray.tests.config
  
      if theano.config.mode == "DEBUG_MODE":
          raise SkipTest(
              "DEBUG_MODE forces synchronous behaviour "
              "which breaks this test"
          )
  
      if theano.gpuarray.pygpu_activated:
          sizes = [100, 500, 1000, 2000, 5000, 10000, 20000, 40000]
          size = sizes[0]
          w = theano.gpuarray.gpuarray_shared_constructor(
              np.random.rand(size, size).astype("float32"),
              "w",
              target=theano.gpuarray.tests.config.test_ctx_name,
          )
          x = theano.gpuarray.gpuarray_shared_constructor(
              np.random.rand(size, size).astype("float32"),
              "x",
              target=theano.gpuarray.tests.config.test_ctx_name,
          )
  
          updates = [
              (w, w + np.asarray(0.001, "float32") * T.dot(x, x))
          ]
  
          f = theano.function(
              [],
              updates=updates,
              mode=theano.gpuarray.tests.config.mode_with_gpu,
          )
          assert len(f.maker.fgraph.apply_nodes) == 1
          assert any(
              isinstance(n.op, theano.gpuarray.blas.GpuGemm)
              for n in f.maker.fgraph.apply_nodes
          )
          # Make sure libgpuarray have compile all kernels
          f()
          f.sync_shared()
  
          # Find a good size that will take about .5s.
          # This is to make the test more stable across different GPUs.
          size = sizes[-1]
          for i in sizes:
              data = np.random.rand(i, i).astype("float32")
              w.set_value(data)
              x.set_value(data)
              t0 = time.time()
              f()
              f.sync_shared()
              t1 = time.time()
              if (t1 - t0) < 0.5:
                  continue
              size = i
              break
          # sync to make sure all computation are done
          f.sync_shared()
  
          t_0 = time.time()
          for i in range(3):
              f()
              # Sync after each call to see the slowdown from sync.
              f.sync_shared()
              time.sleep(0.5)
          t_1 = time.time()
          for i in range(3):
              f()
              time.sleep(0.5)
          f.sync_shared()
          # Sync to make sure all computation are finished.
          t_2 = time.time()
          d1 = t_1 - t_0
          d2 = t_2 - t_1
          assert d1 > d2, (d1, d2)
      else:
          raise SkipTest(
              "Sync is only available when pygpu is activated."
          )
  
imports: from __future__ import absolute_import, print_function, division
  import copy
  import six.moves.cPickle
  import numpy
  import unittest
  import time
  from theano import config, gof
  from six import iteritems
  from theano.compile.io import In, Out
  from theano.compile import function
  from theano.compile import UnusedInputError
  from theano.gof import MissingInputError
  from theano.compat import exc_message
  from theano.tests.unittest_tools import SkipTest
  from theano import tensor
  from theano import tensor
  import theano
label_desc: Use image node_id 4 for calling a global function with example usage: test_sync_update() without return types