.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/parallel_memmap.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_parallel_memmap.py: =============================== NumPy memmap in joblib.Parallel =============================== This example illustrates some features enabled by using a memory map (:class:`numpy.memmap`) within :class:`joblib.Parallel`. First, we show that dumping a huge data array ahead of passing it to :class:`joblib.Parallel` speeds up computation. Then, we show the possibility to provide write access to original data. .. GENERATED FROM PYTHON SOURCE LINES 15-20 Speed up processing of a large data array ############################################################################# We create a large data array for which the average is computed for several slices. .. GENERATED FROM PYTHON SOURCE LINES 20-30 .. code-block:: Python import numpy as np data = np.random.random((int(1e7),)) window_size = int(5e5) slices = [ slice(start, start + window_size) for start in range(0, data.size - window_size, int(1e5)) ] .. GENERATED FROM PYTHON SOURCE LINES 31-35 The ``slow_mean`` function introduces a :func:`time.sleep` call to simulate a more expensive computation cost for which parallel computing is beneficial. Parallel may not be beneficial for very fast operation, due to extra overhead (workers creations, communication, etc.). .. GENERATED FROM PYTHON SOURCE LINES 35-45 .. code-block:: Python import time def slow_mean(data, sl): """Simulate a time consuming processing.""" time.sleep(0.01) return data[sl].mean() .. GENERATED FROM PYTHON SOURCE LINES 46-47 First, we will evaluate the sequential computing on our problem. .. GENERATED FROM PYTHON SOURCE LINES 47-57 .. code-block:: Python tic = time.time() results = [slow_mean(data, sl) for sl in slices] toc = time.time() print( "\nElapsed time computing the average of couple of slices {:.2f} s".format( toc - tic ) ) .. rst-class:: sphx-glr-script-out .. code-block:: none Elapsed time computing the average of couple of slices 0.98 s .. GENERATED FROM PYTHON SOURCE LINES 58-60 :class:`joblib.Parallel` is used to compute in parallel the average of all slices using 2 workers. .. GENERATED FROM PYTHON SOURCE LINES 60-72 .. code-block:: Python from joblib import Parallel, delayed tic = time.time() results = Parallel(n_jobs=2)(delayed(slow_mean)(data, sl) for sl in slices) toc = time.time() print( "\nElapsed time computing the average of couple of slices {:.2f} s".format( toc - tic ) ) .. rst-class:: sphx-glr-script-out .. code-block:: none Elapsed time computing the average of couple of slices 0.67 s .. GENERATED FROM PYTHON SOURCE LINES 73-76 Parallel processing is already faster than the sequential processing. It is also possible to remove a bit of overhead by dumping the ``data`` array to a memmap and pass the memmap to :class:`joblib.Parallel`. .. GENERATED FROM PYTHON SOURCE LINES 76-100 .. code-block:: Python import os from joblib import dump, load folder = "./joblib_memmap" try: os.mkdir(folder) except FileExistsError: pass data_filename_memmap = os.path.join(folder, "data_memmap") dump(data, data_filename_memmap) data = load(data_filename_memmap, mmap_mode="r") tic = time.time() results = Parallel(n_jobs=2)(delayed(slow_mean)(data, sl) for sl in slices) toc = time.time() print( "\nElapsed time computing the average of couple of slices {:.2f} s\n".format( toc - tic ) ) .. rst-class:: sphx-glr-script-out .. code-block:: none Elapsed time computing the average of couple of slices 0.51 s .. GENERATED FROM PYTHON SOURCE LINES 101-104 Therefore, dumping large ``data`` array ahead of calling :class:`joblib.Parallel` can speed up the processing by removing some overhead. .. GENERATED FROM PYTHON SOURCE LINES 106-112 Writable memmap for shared memory :class:`joblib.Parallel` ############################################################################## ``slow_mean_write_output`` will compute the mean for some given slices as in the previous example. However, the resulting mean will be directly written on the output array. .. GENERATED FROM PYTHON SOURCE LINES 112-122 .. code-block:: Python def slow_mean_write_output(data, sl, output, idx): """Simulate a time consuming processing.""" time.sleep(0.005) res_ = data[sl].mean() print("[Worker %d] Mean for slice %d is %f" % (os.getpid(), idx, res_)) output[idx] = res_ .. GENERATED FROM PYTHON SOURCE LINES 123-124 Prepare the folder where the memmap will be dumped. .. GENERATED FROM PYTHON SOURCE LINES 124-127 .. code-block:: Python output_filename_memmap = os.path.join(folder, "output_memmap") .. GENERATED FROM PYTHON SOURCE LINES 128-130 Pre-allocate a writable shared memory map as a container for the results of the parallel computation. .. GENERATED FROM PYTHON SOURCE LINES 130-135 .. code-block:: Python output = np.memmap( output_filename_memmap, dtype=data.dtype, shape=len(slices), mode="w+" ) .. GENERATED FROM PYTHON SOURCE LINES 136-138 ``data`` is replaced by its memory mapped version. Note that the buffer has already been dumped in the previous section. .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: Python data = load(data_filename_memmap, mmap_mode="r") .. GENERATED FROM PYTHON SOURCE LINES 142-143 Fork the worker processes to perform computation concurrently .. GENERATED FROM PYTHON SOURCE LINES 143-149 .. code-block:: Python Parallel(n_jobs=2)( delayed(slow_mean_write_output)(data, sl, output, idx) for idx, sl in enumerate(slices) ) .. rst-class:: sphx-glr-script-out .. code-block:: none [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] .. GENERATED FROM PYTHON SOURCE LINES 150-151 Compare the results from the output buffer with the expected results .. GENERATED FROM PYTHON SOURCE LINES 151-155 .. code-block:: Python print("\nExpected means computed in the parent process:\n {}".format(np.array(results))) print("\nActual means computed by the worker processes:\n {}".format(output)) .. rst-class:: sphx-glr-script-out .. code-block:: none Expected means computed in the parent process: [0.50002103 0.50036029 0.50008031 0.49999635 0.50028443 0.50025333 0.49995089 0.5001809 0.49991628 0.49996058 0.50033139 0.50020282 0.49988549 0.50001114 0.50001947 0.49989474 0.50018009 0.50041618 0.50060965 0.50058802 0.50025647 0.50007361 0.49984857 0.49954089 0.49957099 0.49960444 0.49936499 0.4995634 0.49988962 0.50005242 0.50017598 0.500169 0.5000079 0.49982724 0.4993404 0.49941355 0.49932725 0.49932196 0.49931019 0.49986596 0.49985221 0.50015143 0.50002687 0.50023714 0.49990782 0.50018854 0.49989053 0.50018442 0.50036258 0.49991866 0.49988412 0.50015057 0.49993613 0.49943957 0.49959668 0.49923599 0.49906369 0.49921858 0.49919622 0.49950759 0.49948359 0.49938519 0.49956413 0.4998388 0.49992041 0.50031202 0.50071311 0.50066894 0.50068989 0.50045297 0.50007644 0.49983788 0.49988535 0.49996623 0.50003819 0.50034879 0.5002201 0.50011355 0.50004696 0.50031225 0.5002595 0.50040981 0.50013147 0.49985945 0.49982015 0.49992346 0.49966136 0.50015187 0.50010573 0.49983015 0.49944963 0.49994343 0.49939871 0.49945812 0.49901023] Actual means computed by the worker processes: [0.50002103 0.50036029 0.50008031 0.49999635 0.50028443 0.50025333 0.49995089 0.5001809 0.49991628 0.49996058 0.50033139 0.50020282 0.49988549 0.50001114 0.50001947 0.49989474 0.50018009 0.50041618 0.50060965 0.50058802 0.50025647 0.50007361 0.49984857 0.49954089 0.49957099 0.49960444 0.49936499 0.4995634 0.49988962 0.50005242 0.50017598 0.500169 0.5000079 0.49982724 0.4993404 0.49941355 0.49932725 0.49932196 0.49931019 0.49986596 0.49985221 0.50015143 0.50002687 0.50023714 0.49990782 0.50018854 0.49989053 0.50018442 0.50036258 0.49991866 0.49988412 0.50015057 0.49993613 0.49943957 0.49959668 0.49923599 0.49906369 0.49921858 0.49919622 0.49950759 0.49948359 0.49938519 0.49956413 0.4998388 0.49992041 0.50031202 0.50071311 0.50066894 0.50068989 0.50045297 0.50007644 0.49983788 0.49988535 0.49996623 0.50003819 0.50034879 0.5002201 0.50011355 0.50004696 0.50031225 0.5002595 0.50040981 0.50013147 0.49985945 0.49982015 0.49992346 0.49966136 0.50015187 0.50010573 0.49983015 0.49944963 0.49994343 0.49939871 0.49945812 0.49901023] .. GENERATED FROM PYTHON SOURCE LINES 156-161 Clean-up the memmap ############################################################################## Remove the different memmap that we created. It might fail in Windows due to file permissions. .. GENERATED FROM PYTHON SOURCE LINES 161-168 .. code-block:: Python import shutil try: shutil.rmtree(folder) except: # noqa print("Could not clean-up automatically.") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.513 seconds) .. _sphx_glr_download_auto_examples_parallel_memmap.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: parallel_memmap.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: parallel_memmap.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: parallel_memmap.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_