n = 10a = mx.nd.ones((1000,1000))b = mx.nd.ones((6000,6000), gpu_device)tic = time.time()c = do(a, n)wait(c)print(‘Time to finish the CPU workload: %f sec‘ % (time.time() - tic))d = do(b, n)wait(d)print(‘Time to finish both CPU/GPU workloads: %f sec‘ % (time.time() - tic))
# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import mxnet as mximport numpy as npimport pickle as pklimport timedef do(x, n): ???"""push computation into the backend engine""" ???return [mx.nd.dot(x,x) for i in range(n)]def wait(x): ???"""wait until all results are available""" ???for y in x: ???????y.wait_to_read()tic = time.time()a = mx.nd.ones((1000,1000))b = do(a, 50)print(‘time for all computations are pushed into the backend engine:\n %f sec‘ % (time.time() - tic))wait(b)print(‘time for all computations are finished:\n %f sec‘ % (time.time() - tic))
mxnet-等待后台任务完成,后台完成自动并行化
原文地址:http://blog.51cto.com/13959448/2316997