diff --git a/notebooks/examples.ipynb b/notebooks/examples.ipynb index 183b2658..c40eab1c 100644 --- a/notebooks/examples.ipynb +++ b/notebooks/examples.ipynb @@ -70,10 +70,9 @@ "import flux.job\n", "from executorlib import Executor\n", "\n", - "with flux.job.FluxExecutor() as flux_exe:\n", - " with Executor(max_cores=1, flux_executor=flux_exe) as exe:\n", - " future = exe.submit(sum, [1, 1])\n", - " print(future.result())" + "with Executor(max_cores=1, flux_executor=flux.job.FluxExecutor()) as exe:\n", + " future = exe.submit(sum, [1, 1])\n", + " print(future.result())" ] }, { @@ -101,25 +100,22 @@ "import flux.job\n", "from executorlib import Executor\n", "\n", - "\n", "def calc(*args):\n", " return sum(*args)\n", "\n", - "\n", - "with flux.job.FluxExecutor() as flux_exe:\n", - " with Executor(max_cores=2, flux_executor=flux_exe) as exe:\n", - " fs_1 = exe.submit(calc, [2, 1])\n", - " fs_2 = exe.submit(calc, [2, 2])\n", - " fs_3 = exe.submit(calc, [2, 3])\n", - " fs_4 = exe.submit(calc, [2, 4])\n", - " print(\n", - " [\n", - " fs_1.result(),\n", - " fs_2.result(),\n", - " fs_3.result(),\n", - " fs_4.result(),\n", - " ]\n", - " )" + "with Executor(max_cores=2, flux_executor=flux.job.FluxExecutor()) as exe:\n", + " fs_1 = exe.submit(calc, [2, 1])\n", + " fs_2 = exe.submit(calc, [2, 2])\n", + " fs_3 = exe.submit(calc, [2, 3])\n", + " fs_4 = exe.submit(calc, [2, 4])\n", + " print(\n", + " [\n", + " fs_1.result(),\n", + " fs_2.result(),\n", + " fs_3.result(),\n", + " fs_4.result(),\n", + " ]\n", + " )" ] }, { @@ -159,14 +155,11 @@ "import flux.job\n", "from executorlib import Executor\n", "\n", - "\n", "def calc(*args):\n", " return sum(*args)\n", "\n", - "\n", - "with flux.job.FluxExecutor() as flux_exe:\n", - " with Executor(max_cores=2, flux_executor=flux_exe) as exe:\n", - " print(list(exe.map(calc, [[2, 1], [2, 2], [2, 3], [2, 4]])))" + "with Executor(max_cores=2, flux_executor=flux.job.FluxExecutor()) as exe:\n", + " print(list(exe.map(calc, [[2, 1], [2, 2], [2, 3], [2, 4]])))" ] }, { @@ -275,24 +268,21 @@ "import flux.job\n", "from executorlib import Executor\n", "\n", - "\n", "def calc_function(parameter_a, parameter_b):\n", " return parameter_a + parameter_b\n", "\n", - "\n", - "with flux.job.FluxExecutor() as flux_exe:\n", - " with Executor(\n", - " # Resource definition on the executor level\n", - " max_cores=2, # total number of cores available to the Executor\n", - " block_allocation=True, # reuse python processes\n", - " flux_executor=flux_exe,\n", - " ) as exe:\n", - " future_obj = exe.submit(\n", - " calc_function,\n", - " 1, # parameter_a\n", - " parameter_b=2,\n", - " )\n", - " print(future_obj.result())" + "with Executor(\n", + " # Resource definition on the executor level\n", + " max_cores=2, # total number of cores available to the Executor\n", + " block_allocation=True, # reuse python processes\n", + " flux_executor=flux.job.FluxExecutor(),\n", + ") as exe:\n", + " future_obj = exe.submit(\n", + " calc_function,\n", + " 1, # parameter_a\n", + " parameter_b=2,\n", + " )\n", + " print(future_obj.result())" ] }, { @@ -329,24 +319,20 @@ "import flux.job\n", "from executorlib import Executor\n", "\n", - "\n", "def calc(i, j, k):\n", " return i + j + k\n", "\n", - "\n", "def init_function():\n", " return {\"j\": 4, \"k\": 3, \"l\": 2}\n", "\n", - "\n", - "with flux.job.FluxExecutor() as flux_exe:\n", - " with Executor(\n", - " max_cores=1,\n", - " init_function=init_function,\n", - " flux_executor=flux_exe,\n", - " block_allocation=True,\n", - " ) as exe:\n", - " fs = exe.submit(calc, 2, j=5)\n", - " print(fs.result())" + "with Executor(\n", + " max_cores=1,\n", + " init_function=init_function,\n", + " flux_executor=flux.job.FluxExecutor(),\n", + " block_allocation=True,\n", + ") as exe:\n", + " fs = exe.submit(calc, 2, j=5)\n", + " print(fs.result())" ] }, { @@ -461,7 +447,6 @@ "import flux.job\n", "from executorlib import Executor\n", "\n", - "\n", "def calc(i):\n", " from mpi4py import MPI\n", "\n", @@ -469,16 +454,14 @@ " rank = MPI.COMM_WORLD.Get_rank()\n", " return i, size, rank\n", "\n", - "\n", - "with flux.job.FluxExecutor() as flux_exe:\n", - " with Executor(\n", - " max_cores=2,\n", - " resource_dict={\"cores\": 2},\n", - " flux_executor=flux_exe,\n", - " flux_executor_pmi_mode=\"pmix\",\n", - " ) as exe:\n", - " fs = exe.submit(calc, 3)\n", - " print(fs.result())" + "with Executor(\n", + " max_cores=2,\n", + " resource_dict={\"cores\": 2},\n", + " flux_executor=flux.job.FluxExecutor(),\n", + " flux_executor_pmi_mode=\"pmix\",\n", + ") as exe:\n", + " fs = exe.submit(calc, 3)\n", + " print(fs.result())" ] }, { @@ -512,9 +495,8 @@ ] }, { - "cell_type": "markdown", - "id": "db3727c5da7072cd", "metadata": {}, + "cell_type": "markdown", "source": [ "```\n", "import socket\n", @@ -529,17 +511,17 @@ " for x in local_device_protos if x.device_type == 'GPU'\n", " ]\n", "\n", - "with flux.job.FluxExecutor() as flux_exe:\n", - " with Executor(\n", - " max_workers=2, \n", - " gpus_per_worker=1,\n", - " executor=flux_exe,\n", - " ) as exe:\n", - " fs_1 = exe.submit(get_available_gpus)\n", - " fs_2 = exe.submit(get_available_gpus)\n", - " print(fs_1.result(), fs_2.result())\n", + "with Executor(\n", + " max_workers=2, \n", + " gpus_per_worker=1,\n", + " executor=flux.job.FluxExecutor(),\n", + ") as exe:\n", + " fs_1 = exe.submit(get_available_gpus)\n", + " fs_2 = exe.submit(get_available_gpus)\n", + " print(fs_1.result(), fs_2.result())\n", "```" - ] + ], + "id": "db3727c5da7072cd" }, { "cell_type": "markdown", @@ -637,26 +619,23 @@ "import flux.job\n", "from executorlib import Executor\n", "\n", - "\n", "def calc_function(parameter_a, parameter_b):\n", " return parameter_a + parameter_b\n", "\n", - "\n", - "with flux.job.FluxExecutor() as flux_exe:\n", - " with Executor(max_cores=2, flux_executor=flux_exe) as exe:\n", - " future_1 = exe.submit(\n", - " calc_function,\n", - " 1,\n", - " parameter_b=2,\n", - " resource_dict={\"cores\": 1},\n", - " )\n", - " future_2 = exe.submit(\n", - " calc_function,\n", - " 1,\n", - " parameter_b=future_1,\n", - " resource_dict={\"cores\": 1},\n", - " )\n", - " print(future_2.result())" + "with Executor(max_cores=2, flux_executor=flux.job.FluxExecutor()) as exe:\n", + " future_1 = exe.submit(\n", + " calc_function,\n", + " 1,\n", + " parameter_b=2,\n", + " resource_dict={\"cores\": 1},\n", + " )\n", + " future_2 = exe.submit(\n", + " calc_function,\n", + " 1,\n", + " parameter_b=future_1,\n", + " resource_dict={\"cores\": 1},\n", + " )\n", + " print(future_2.result())" ] }, {