spacepaste

  1.  
  2. from theano import function, config, shared, sandbox, tensor, Out
  3. import numpy
  4. import time
  5. vlen = 10 * 30 * 768 # 10 x # cores x # threads per core
  6. iters = 1000
  7. rng = numpy.random.RandomState(22)
  8. x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
  9. f1 = function([], sandbox.cuda.basic_ops.gpu_from_host(tensor.exp(x)))
  10. f2 = function([],
  11. Out(sandbox.cuda.basic_ops.gpu_from_host(tensor.exp(x)),
  12. borrow=True))
  13. t0 = time.time()
  14. for i in range(iters):
  15. r = f1()
  16. t1 = time.time()
  17. no_borrow = t1 - t0
  18. t0 = time.time()
  19. for i in range(iters):
  20. r = f2()
  21. t1 = time.time()
  22. print(
  23. "Looping %s times took %s seconds without borrow "
  24. "and %s seconds with borrow" % (iters, no_borrow, (t1 - t0))
  25. )
  26. if numpy.any([isinstance(x.op, tensor.Elemwise) and
  27. ('Gpu' not in type(x.op).__name__)
  28. for x in f1.maker.fgraph.toposort()]):
  29. print('Used the cpu')
  30. else:
  31. print('Used the gpu')
  32.