When do I need to use ndarray "wait_to_read"?

Looking at a lot of the tutorials and documentation, it seems like NDArray.wait_to_read() is not frequently used.

But looking at this page, it does get used.

My question is when do I need to call “wait_to_read” to evaluate results? Are there some rules of thumb?

Thanks

Hi,

The mxnet engine operates asynchronously which means that when ndarray operations are called in python, the functions return immediately before the computation is actually performed by the backend. wait_to_read is essentially a blocking call to synchronize the execution and ensures that the result of the computation to produce an ndarray is completed by the backend engine.

For example let’s say you have to ndarrays x and y declared. If you run z = x + y, the operation is queued to the mxnet engine but in python the function returns immediately. If you say z.wait_to_read() immediately after, this will ensure that z is actually computed and the values are assigned to the variable before moving on to the next line.

So wait_to_read is useful for benchmarking, when you want to actually measure how long it takes to compute the values in an ndarray. This is how it’s used in the tutorial you linked.

Also, wait_to_read can be use to introduce synchronization in your training loop to prevent excessively queuing up more operations than the engine can allocate memory for.