<jupyter_start><jupyter_text>NDArray Contexts OverviewThis guide will introduce you to managing CPU versus GPU contexts for handling data.This content was extracted and simplified from the gluon tutorials in[Dive Into Deep Learning](http://gluon.io/). Prerequisites* [MXNet installed (with GPU support) in a Python environment](../../../install/index.html?language=Python).* Python 2.7.x or Python 3.x* **One or more GPUs** Managing ContextIn MXNet, every array has a context.One context could be the CPU. Other contexts might be various GPUs.Things can get even hairier when we deploy jobs across multiple servers.By assigning arrays to contexts intelligently, we can minimizethe time spent transferring data between devices.For example, when training neural networks on a server with a GPU,we typically prefer for the model's parameters to live on the GPU.If you have a GPU, let's try initializing an array on the first GPU.Otherwise, use `ctx=mx.cpu()` in place of `ctx=gpu(0)`.<jupyter_code>from mxnet import gpu
from mxnet import nd
z = nd.ones(shape=(3,3), ctx=gpu(0))
print(z)<jupyter_output><empty_output><jupyter_text>Given an NDArray on a given context, we can copy it to another context by usingthe copyto() method. Skip this if you don't have a GPU at the moment.<jupyter_code>x_gpu = x.copyto(gpu(0))
print(x_gpu)<jupyter_output><empty_output><jupyter_text>The result of an operator will have the same context as the inputs.<jupyter_code>x_gpu + z<jupyter_output><empty_output><jupyter_text>Watch out!Imagine that your variable z already lives on your second GPU(`gpu(0)`). What happens if we call `z.copyto(gpu(0))`? It will make a copy andallocate new memory, even though that variable already lives on the desireddevice!Often, we only want to makea copy if the variable currently lives in the wrong context. In these cases, wecan call `as_in_context()`. If the variable is already on `gpu(0)` then this isa no-op.<jupyter_code>print('id(z):', id(z))
z = z.copyto(gpu(0))
print('id(z):', id(z))
z = z.as_in_context(gpu(0))
print('id(z):', id(z))
print(z)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09909480705097666, 'fraction_numerical': 0.006193425440686041, 'mean_word_length': 5.752411575562701, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '9686', 'license': 'MIT', 'n_tokens_mistral': 658, 'n_tokens_neox': 630, 'n_words': 263, 'path': 'static_websites/python/docs/_sources/tutorials/packages/ndarray/03-ndarray-contexts.ipynb', 'repo_name': 'IvyBazan/mxnet.io-v2', 'size': '2099'}