From 35d2d1f0bfe9b9c028db64581206331d7b209392 Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Mon, 2 Sep 2024 12:56:18 +0200 Subject: [PATCH] Add Fibonacci example --- examples/fibonacci.ipynb | 248 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 examples/fibonacci.ipynb diff --git a/examples/fibonacci.ipynb b/examples/fibonacci.ipynb new file mode 100644 index 00000000..9a8c7cbd --- /dev/null +++ b/examples/fibonacci.ipynb @@ -0,0 +1,248 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "id": "f2f139dd-f393-472c-9bf5-57e8e44062a0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34m\u001b[1mReport\u001b[0m: [1145|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1145|WorkGraphEngine|continue_workgraph]: tasks ready to run: fibonacci1\n", + "------------------------------------------------------------\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1145|WorkGraphEngine|run_tasks]: Run task: fibonacci1, type: graph_builder\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1145|WorkGraphEngine|on_wait]: Process status: Waiting for child processes: 1146\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|continue_workgraph]: tasks ready to run: f_n1\n", + "------------------------------------------------------------\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|run_tasks]: Run task: f_n1, type: CALCFUNCTION\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|update_task_state]: Task: f_n1 finished.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|continue_workgraph]: tasks ready to run: fibonacci_last\n", + "------------------------------------------------------------\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|run_tasks]: Run task: fibonacci_last, type: CALCFUNCTION\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|update_task_state]: Task: fibonacci_last finished.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|continue_workgraph]: tasks ready to run: \n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1146|WorkGraphEngine|finalize]: Finalize workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1145|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1145|WorkGraphEngine|continue_workgraph]: tasks ready to run: \n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1145|WorkGraphEngine|finalize]: Finalize workgraph.\n", + "13.773984909057617\n" + ] + } + ], + "source": [ + "from aiida_workgraph import task, WorkGraph\n", + "from aiida import load_profile\n", + "import time\n", + "\n", + "load_profile()\n", + "\n", + "@task.calcfunction()\n", + "def f_n(f_n1, f_n2):\n", + " import time\n", + " time.sleep(5)\n", + " return f_n1 + f_n2\n", + "\n", + "@task.graph_builder(outputs = [{\"name\": \"f_max_it\", \"from\": \"fibonacci_last.result\"}])\n", + "def fibonacci(max_it, f_0=0, f_1=1):\n", + " if max_it <= 0:\n", + " raise ValueError(\"At least one iteration is required.\")\n", + " wg = WorkGraph()\n", + " f_n1 = f_0\n", + " f_n2 = f_1\n", + " for _ in range(max_it):\n", + " task = wg.add_task(f_n, f_n1=f_n1, f_n2=f_n2)\n", + " f_n2 = f_n1\n", + " f_n1 = task.outputs[\"result\"]\n", + " task = wg.add_task(f_n, name=\"fibonacci_last\", f_n1=f_n1, f_n2=f_n2)\n", + " return wg\n", + " \n", + "wg = WorkGraph()\n", + "task = wg.add_task(fibonacci, max_it=1)\n", + "start = time.time()\n", + "wg.run()\n", + "end = time.time()\n", + "print(end-start)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f453d6ee-78a5-4f07-bd22-b2fda64c2033", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "task.outputs[\"f_max_it\"].value" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "076277b4-5b6b-47b7-8b12-267f6f2a01ef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The aiida extension is already loaded. To reload it, use:\n", + " %reload_ext aiida\n", + "\u001b[22mschema:\n", + " default: false\n", + " description: Enable calculation caching by default.\n", + " title: Caching Default Enabled\n", + " type: boolean\n", + "values:\n", + " default: false\n", + " global: false\n", + " profile: \n", + "\u001b[0m\n" + ] + } + ], + "source": [ + "%load_ext aiida\n", + "%verdi config show caching.default_enabled" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f58f0d5e-4eae-42e3-b189-373e59348d5c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32m\u001b[1mSuccess: \u001b[0m\u001b[22m'caching.default_enabled' set to True globally\u001b[0m\n" + ] + } + ], + "source": [ + "%verdi config set -g caching.default_enabled true" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "15cb72df-1fd6-489d-83cc-5d737718b814", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[22mschema:\n", + " default: false\n", + " description: Enable calculation caching by default.\n", + " title: Caching Default Enabled\n", + " type: boolean\n", + "values:\n", + " default: false\n", + " global: true\n", + " profile: \n", + "\u001b[0m\n" + ] + } + ], + "source": [ + "%verdi config show caching.default_enabled" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "a776bcc0-f3c7-40d5-a830-1ddfd71da926", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34m\u001b[1mReport\u001b[0m: [1169|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1169|WorkGraphEngine|continue_workgraph]: tasks ready to run: fibonacci1\n", + "------------------------------------------------------------\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1169|WorkGraphEngine|run_tasks]: Run task: fibonacci1, type: graph_builder\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1169|WorkGraphEngine|on_wait]: Process status: Waiting for child processes: 1170\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|continue_workgraph]: tasks ready to run: f_n1\n", + "------------------------------------------------------------\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|run_tasks]: Run task: f_n1, type: CALCFUNCTION\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|update_task_state]: Task: f_n1 finished.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|continue_workgraph]: tasks ready to run: f_n2\n", + "------------------------------------------------------------\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|run_tasks]: Run task: f_n2, type: CALCFUNCTION\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|update_task_state]: Task: f_n2 finished.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|continue_workgraph]: tasks ready to run: fibonacci_last\n", + "------------------------------------------------------------\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|run_tasks]: Run task: fibonacci_last, type: CALCFUNCTION\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|update_task_state]: Task: fibonacci_last finished.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|continue_workgraph]: tasks ready to run: \n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1170|WorkGraphEngine|finalize]: Finalize workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1169|WorkGraphEngine|continue_workgraph]: Continue workgraph.\n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1169|WorkGraphEngine|continue_workgraph]: tasks ready to run: \n", + "\u001b[34m\u001b[1mReport\u001b[0m: [1169|WorkGraphEngine|finalize]: Finalize workgraph.\n", + "4.6344099044799805\n" + ] + } + ], + "source": [ + "wg = WorkGraph()\n", + "task = wg.add_task(fibonacci, max_it=2)\n", + "start = time.time()\n", + "wg.run()\n", + "end = time.time()\n", + "print(end-start)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "176a0193-50d3-4b46-b95c-b350f57997ab", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}