{ "cells": [ { "cell_type": "markdown", "id": "c8cc6690", "metadata": {}, "source": [ "# Multiproc example\n", "\n", "In this example we demonstrate how to use the multiproc module to parallelize the computation of\n", "the function f(x) = x^2 + c." ] }, { "cell_type": "code", "execution_count": null, "id": "f3c0099d", "metadata": {}, "outputs": [], "source": [ "# Imports and helper functions for examples.\n", "\n", "import numpy as np\n", "\n", "from skyllh.core.multiproc import parallelize\n", "\n", "\n", "def f(x, c=0.0):\n", " return x**2 + c" ] }, { "cell_type": "markdown", "id": "9f6e0cb6", "metadata": {}, "source": [ "## Example" ] }, { "cell_type": "code", "execution_count": 2, "id": "1d2084d5", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 9/9 [00:00<00:00, 340.90it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "((np.int64(1),), {'c': np.int64(1)}) -> 2\n", "((np.int64(2),), {'c': np.int64(2)}) -> 6\n", "((np.int64(3),), {'c': np.int64(3)}) -> 12\n", "((np.int64(4),), {'c': np.int64(4)}) -> 20\n", "((np.int64(5),), {'c': np.int64(5)}) -> 30\n", "((np.int64(6),), {'c': np.int64(6)}) -> 42\n", "((np.int64(7),), {'c': np.int64(7)}) -> 56\n", "((np.int64(8),), {'c': np.int64(8)}) -> 72\n", "((np.int64(9),), {'c': np.int64(9)}) -> 90\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "args_list = [((x,), {'c': x}) for x in np.arange(1, 10, 1)]\n", "\n", "res = parallelize(func=f, args_list=args_list, ncpu=3)\n", "\n", "for argument, result in zip(args_list, res, strict=True):\n", " print(f'{argument} -> {result}')" ] } ], "metadata": { "kernelspec": { "display_name": "skyllh", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.3" } }, "nbformat": 4, "nbformat_minor": 5 }