diff --git a/codes/kwant_helper/utils.py b/codes/kwant_helper/utils.py index 6a67b48eab8dab51493bc333a6efe30000811ad2..6e74a4e9f237cb564c93edd97d54a8dd3dbb3f66 100644 --- a/codes/kwant_helper/utils.py +++ b/codes/kwant_helper/utils.py @@ -6,7 +6,7 @@ import inspect from copy import copy -def builder2h_0(builder, params={}, return_data=False): +def builder2tb(builder, params={}, return_data=False): """ Constructs a tight-binding model dictionary from a `kwant.Builder`. diff --git a/codes/test_graphene.py b/codes/test_graphene.py index 9dbf414adcbf4087e6a0857563b492e26f30a266..59769cc10a4159c28fe9ec9663eb44be0aabcb6d 100644 --- a/codes/test_graphene.py +++ b/codes/test_graphene.py @@ -11,7 +11,7 @@ import pytest repeatNumber = 10 # %% graphene_builder, int_builder = kwant_examples.graphene_extended_hubbard() -h_0 = utils.builder2h_0(graphene_builder) +h_0 = utils.builder2tb(graphene_builder) # %% @@ -45,7 +45,7 @@ def gap_prediction(U, V): filling = 2 nK = 20 - h_int = utils.builder2h_0(int_builder, params) + h_int = utils.builder2tb(int_builder, params) guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0])) model = Model(h_0, h_int, filling) diff --git a/examples/graphene_extended_hubbard.ipynb b/examples/graphene_extended_hubbard.ipynb index d394d1004f18b0a102935280d4ba7268db0d8405..20b9770e677681f124134c24d7f0ba744d4a19bf 100644 --- a/examples/graphene_extended_hubbard.ipynb +++ b/examples/graphene_extended_hubbard.ipynb @@ -65,7 +65,7 @@ "source": [ "# Create translationally-invariant `kwant.Builder`\n", "graphene_builder, int_builder = kwant_examples.graphene_extended_hubbard()\n", - "h_0 = utils.builder2h_0(graphene_builder)" + "h_0 = utils.builder2tb(graphene_builder)" ] }, { @@ -75,6 +75,7 @@ "metadata": {}, "outputs": [], "source": [ + "np.random.seed(5)\n", "def compute_phase_diagram(Us, Vs, int_builder, h_0): \n", " gap = []\n", " ks = np.linspace(-np.pi, np.pi, 300)\n", @@ -83,12 +84,12 @@ " guess=None\n", " for V in Vs: \n", " params = dict(U=U, V=V)\n", - " h_int = utils.builder2h_0(int_builder, params)\n", + " h_int = utils.builder2tb(int_builder, params)\n", " if guess==None:\n", " guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0]))\n", " model = Model(h_0, h_int, filling=2)\n", "\n", - " mf_sol = solverkvector(model, guess, nK=18)\n", + " mf_sol = solver(model, guess, nK=18)\n", " hkfunc = tb2kfunc(addTb(h_0, mf_sol))\n", " hkarray = np.array([hkfunc((kx, -kx)) for kx in ks])\n", " vals = np.linalg.eigvalsh(hkarray)\n", @@ -100,22 +101,33 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "14f332f2", "metadata": {}, "outputs": [ { - "name": "stderr", - "output_type": "stream", - "text": [ - "100%|██████████| 10/10 [02:25<00:00, 14.56s/it]\n" + "ename": "RuntimeError", + "evalue": "There is already a profiler running. You cannot run multiple profilers in the same thread or async context, unless you disable async support.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[9], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m Us \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mlinspace(\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m10\u001b[39m, endpoint\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 4\u001b[0m Vs \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mlinspace(\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1.5\u001b[39m, \u001b[38;5;241m10\u001b[39m, endpoint\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m----> 5\u001b[0m \u001b[43mprofiler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstart\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6\u001b[0m compute_phase_diagram(Us, Vs, int_builder, h_0)\n\u001b[1;32m 7\u001b[0m profiler\u001b[38;5;241m.\u001b[39mstop()\n", + "File \u001b[0;32m~/opt/anaconda3/envs/python3/lib/python3.11/site-packages/pyinstrument/profiler.py:131\u001b[0m, in \u001b[0;36mProfiler.start\u001b[0;34m(self, caller_frame)\u001b[0m\n\u001b[1;32m 124\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_active_session \u001b[38;5;241m=\u001b[39m ActiveProfilerSession(\n\u001b[1;32m 125\u001b[0m start_time\u001b[38;5;241m=\u001b[39mtime\u001b[38;5;241m.\u001b[39mtime(),\n\u001b[1;32m 126\u001b[0m start_process_time\u001b[38;5;241m=\u001b[39mprocess_time(),\n\u001b[1;32m 127\u001b[0m start_call_stack\u001b[38;5;241m=\u001b[39mbuild_call_stack(caller_frame, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minitial\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m),\n\u001b[1;32m 128\u001b[0m )\n\u001b[1;32m 130\u001b[0m use_async_context \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39masync_mode \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdisabled\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m--> 131\u001b[0m \u001b[43mget_stack_sampler\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msubscribe\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 132\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_sampler_saw_call_stack\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minterval\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43muse_async_context\u001b[49m\n\u001b[1;32m 133\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 134\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m:\n\u001b[1;32m 135\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_active_session \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n", + "File \u001b[0;32m~/opt/anaconda3/envs/python3/lib/python3.11/site-packages/pyinstrument/stack_sampler.py:62\u001b[0m, in \u001b[0;36mStackSampler.subscribe\u001b[0;34m(self, target, desired_interval, use_async_context)\u001b[0m\n\u001b[1;32m 60\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m use_async_context:\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m active_profiler_context_var\u001b[38;5;241m.\u001b[39mget() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m---> 62\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[1;32m 63\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThere is already a profiler running. You cannot run multiple profilers in the same thread or async context, unless you disable async support.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 64\u001b[0m )\n\u001b[1;32m 65\u001b[0m active_profiler_context_var\u001b[38;5;241m.\u001b[39mset(target)\n\u001b[1;32m 67\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msubscribers\u001b[38;5;241m.\u001b[39mappend(\n\u001b[1;32m 68\u001b[0m StackSamplerSubscriber(\n\u001b[1;32m 69\u001b[0m target\u001b[38;5;241m=\u001b[39mtarget,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 73\u001b[0m )\n\u001b[1;32m 74\u001b[0m )\n", + "\u001b[0;31mRuntimeError\u001b[0m: There is already a profiler running. You cannot run multiple profilers in the same thread or async context, unless you disable async support." ] } ], "source": [ + "from pyinstrument import Profiler\n", + "profiler = Profiler()\n", "Us = np.linspace(0, 3, 10, endpoint=True)\n", "Vs = np.linspace(0, 1.5, 10, endpoint=True)\n", - "gap = compute_phase_diagram(Us, Vs, int_builder, h_0)" + "profiler.start()\n", + "compute_phase_diagram(Us, Vs, int_builder, h_0)\n", + "profiler.stop()\n", + "profiler.write_html(path=\"timeProfileGrapheneExtendedOld.html\")" ] }, { @@ -213,7 +225,7 @@ "params = dict(U=0, V=1)\n", "filling = 2 \n", "\n", - "h_int = utils.builder2h_0(int_builder, params)\n", + "h_int = utils.builder2tb(int_builder, params)\n", "model = Model(h_0, h_int, filling)\n", "mf_guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0]))\n", "mf_sol = solver(model, mf_guess, nK=30)\n", @@ -228,10 +240,131 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "e183c3cb", "metadata": {}, "outputs": [], + "source": [ + "\n", + "import numpy as np\n", + "from codes.model import Model\n", + "from codes import kwant_examples\n", + "from codes.kwant_helper import utils\n", + "import timeit\n", + "import memray\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "078dd782", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n", + "Intel MKL WARNING: Support of Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) enabled only processors has been deprecated. Intel oneAPI Math Kernel Library 2025.0 will require Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.\n" + ] + } + ], + "source": [ + "graphene_builder, int_builder = kwant_examples.graphene_extended_hubbard()\n", + "\n", + "params = {\"U\": 0.5, \"V\": 1.1}\n", + "filling = 2\n", + "nK = 300\n", + "\n", + "h_int = utils.builder2tb(int_builder, params)\n", + "h_0 = utils.builder2tb(graphene_builder)\n", + "guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0]))\n", + "\n", + "model = Model(h_0, h_int, filling)\n", + "\n", + "\n", + "def scf_loop():\n", + " model.mfield(guess, nK=nK)\n", + "\n", + "\n", + "# %% Memory profile\n", + "with memray.Tracker(\"memoryProfile.bin\"):\n", + " scf_loop()\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3455735e", + "metadata": {}, + "outputs": [], + "source": [ + "# %% Time profiler\n", + "profiler = Profiler()\n", + "\n", + "profiler.start()\n", + "scf_loop()\n", + "profiler.stop()\n", + "profiler.write_html(path=\"timeProfile.html\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "75fe9023", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Single SCF loop takes 7.120591291008168 whereas a single diagonalization of a corresponding system takes 0.024635875000967644\n" + ] + } + ], + "source": [ + "# %%\n", + "number = 1\n", + "\n", + "timeSCF = timeit.timeit(scf_loop, number=number) / number\n", + "\n", + "H = np.random.rand(nK, nK)\n", + "H += H.T.conj()\n", + "timeDiag = timeit.timeit(lambda: np.linalg.eigh(H), number=number) / number\n", + "\n", + "print(\n", + " f\"Single SCF loop takes {timeSCF} whereas a single diagonalization of a corresponding system takes {timeDiag}\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f650872f", + "metadata": {}, + "outputs": [], "source": [] } ], diff --git a/profiling/graphene.py b/profiling/graphene.py index 82681de9e152e7ee41fe4390987e6ba401b8062a..a82e382721a2321ff6ba02bc51903c4adf1ccc09 100644 --- a/profiling/graphene.py +++ b/profiling/graphene.py @@ -14,8 +14,8 @@ params = {"U": 0.5, "V": 1.1} filling = 2 nK = 600 -h_int = utils.builder2h_0(int_builder, params) -h_0 = utils.builder2h_0(graphene_builder) +h_int = utils.builder2tb(int_builder, params) +h_0 = utils.builder2tb(graphene_builder) guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0])) model = Model(h_0, h_int, filling)