ó ùµÈ[c@@sädZddlmZddlZddlZddlZddlmZmZm Z ddlm Z ddlm Z dee e ƒfd „ƒYZ e d dƒe j_dd „Zdd „Zdd „Zd„Zd„ZdS(s Context management API of mxnet.i(tabsolute_importNi(t classpropertytwith_metaclasst_MXClassPropertyMetaClass(t_LIB(t check_calltContextcB@sÐeZdZejƒZidd6dd6dd6dd6Zidd6dd6dd6dd6Zd d „Ze d „ƒZ d „Z d „Z d„Z d„Zd„Zd„Zed„ƒZejd„ƒZRS(s#Constructs a context. MXNet can run operations on CPU and different GPUs. A context describes the device type and ID on which computation should be carried on. One can use mx.cpu and mx.gpu for short. See also ---------- `How to run MXNet on multiple CPU/GPUs ` for more details. Parameters ---------- device_type : {'cpu', 'gpu'} or Context. String representing the device type. device_id : int (default=0) The device id of the device, needed for GPU. Note ---- Context can also be used as a way to change the default context. Examples -------- >>> # array on cpu >>> cpu_array = mx.nd.ones((2, 3)) >>> # switch default context to GPU(2) >>> with mx.Context(mx.gpu(2)): ... gpu_array = mx.nd.ones((2, 3)) >>> gpu_array.context gpu(2) One can also explicitly specify the context when creating an array. >>> gpu_array = mx.nd.ones((2, 3), mx.gpu(1)) >>> gpu_array.context gpu(1) tcpuitgpuit cpu_pinnedit cpu_sharediicC@sPt|tƒr*|j|_|j|_ntj||_||_d|_dS(N(t isinstanceRt device_typeidt device_idt devstr2typetNonet_old_ctx(tselft device_typeR ((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyt__init__Js   cC@stj|jS(sReturns the device type of current context. Examples ------- >>> mx.context.current_context().device_type 'cpu' >>> mx.current_context().device_type 'cpu' Returns ------- device_type : str (Rt devtype2strR (R((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyRSscC@st|j|jfƒS(s3Compute hash value of context for dictionary lookup(thashR R (R((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyt__hash__dscC@s1t|tƒo0|j|jko0|j|jkS(soCompares two contexts. Two contexts are equal if they have the same device type and device id. (R RR R (Rtother((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyt__eq__hscC@sd|j|jfS(Ns%s(%d)(RR (R((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyt__str__pscC@s |jƒS(N(R(R((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyt__repr__sscC@sIttjdƒs*tddƒtj_ntjj|_|tj_|S(NtvalueRi(thasattrRt _default_ctxRR(R((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyt __enter__vs  cC@s|jtj_dS(N(RRRR(RtptypeRttrace((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyt__exit__}scC@sDtjdtƒttjdƒs:tddƒ|j_n|jjS(Ns™Context.default_ctx has been deprecated. Please use Context.current_context() instead. Please use test_utils.set_default_context to set a default contextRRi(twarningstwarntDeprecationWarningRRRR(tcls((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyt default_ctxs  cC@s tjdtƒ||j_dS(Ns™Context.default_ctx has been deprecated. Please use Context.current_context() instead. Please use test_utils.set_default_context to set a default context(R"R#R$RR(R%tval((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyR&‹s (t__name__t __module__t__doc__t threadingtlocalRRRRtpropertyRRRRRRR!RR&tsetter(((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyRs( ""        RcC@s td|ƒS(s°Returns a CPU context. This function is a short cut for ``Context('cpu', device_id)``. For most operations, when no context is specified, the default context is `cpu()`. Examples ---------- >>> with mx.cpu(): ... cpu_array = mx.nd.ones((2, 3)) >>> cpu_array.context cpu(0) >>> cpu_array = mx.nd.ones((2, 3), ctx=mx.cpu()) >>> cpu_array.context cpu(0) Parameters ---------- device_id : int, optional The device id of the device. `device_id` is not needed for CPU. This is included to make interface compatible with GPU. Returns ------- context : Context The corresponding CPU context. R(R(R ((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyR˜scC@s td|ƒS(séReturns a CPU pinned memory context. Copying from CPU pinned memory to GPU is faster than from normal CPU memory. This function is a short cut for ``Context('cpu_pinned', device_id)``. Examples ---------- >>> with mx.cpu_pinned(): ... cpu_array = mx.nd.ones((2, 3)) >>> cpu_array.context cpu_pinned(0) >>> cpu_array = mx.nd.ones((2, 3), ctx=mx.cpu_pinned()) >>> cpu_array.context cpu_pinned(0) Parameters ---------- device_id : int, optional The device id of the device. `device_id` is not needed for CPU. This is included to make interface compatible with GPU. Returns ------- context : Context The corresponding CPU pinned memory context. R (R(R ((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyR ¶scC@s td|ƒS(sŽReturns a GPU context. This function is a short cut for Context('gpu', device_id). The K GPUs on a node are typically numbered as 0,...,K-1. Examples ---------- >>> cpu_array = mx.nd.ones((2, 3)) >>> cpu_array.context cpu(0) >>> with mx.gpu(1): ... gpu_array = mx.nd.ones((2, 3)) >>> gpu_array.context gpu(1) >>> gpu_array = mx.nd.ones((2, 3), ctx=mx.gpu(1)) >>> gpu_array.context gpu(1) Parameters ---------- device_id : int, optional The device id of the device, needed for GPU. Returns ------- context : Context The corresponding GPU context. R(R(R ((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyRÔscC@s/tjƒ}ttjtj|ƒƒƒ|jS(s»Query CUDA for the number of GPUs present. Raises ------ Will raise an exception on any CUDA error. Returns ------- count : int The number of GPUs. (tctypestc_intRRt MXGetGPUCounttbyrefR(tcount((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pytnum_gpusôs cC@s4ttjdƒs*tddƒtj_ntjjS(s6Returns the current context. By default, `mx.cpu()` is used for all the computations and it can be overridden by using `with mx.Context(x)` statement where x can be cpu(device_id) or gpu(device_id). Examples ------- >>> mx.current_context() cpu(0) >>> with mx.Context('gpu', 1): # Context changed in `with` block. ... mx.current_context() # Computation done here will be on gpu(1). ... gpu(1) >>> mx.current_context() # Back to default context. cpu(0) Returns ------- default_ctx : Context RRi(RRRR(((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pytcurrent_contexts(R*t __future__RR+R"R/tbaseRRRRRtobjectRRRRR RR4R5(((sM/usr/local/lib/python2.7/site-packages/mxnet-1.3.1-py2.7.egg/mxnet/context.pyts   x