{ "cells": [ { "cell_type": "markdown", "id": "a57bee67-d094-4ec8-9175-33465f46e1d0", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "## Matrix memory cache" ] }, { "cell_type": "raw", "id": "ac1d768f-b386-4229-90c9-a5cc7c550643", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "This notebook demonstrates the usage of the in-memory precomputed interpolation weights cache. Please note this is only avilable for the :ref:`precomputed ` backend (for both the high-level and and array-level :func:`regrid` methods). By default this cache is enabled, for details see :ref:`mem_cache`. " ] }, { "cell_type": "code", "execution_count": 1, "id": "3f3c0324-708c-4ea9-8eac-406cabc0a1e8", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "import earthkit.regrid as ekr\n", "from earthkit.regrid.array import regrid\n", "import numpy as np\n", "\n", "# create input data array for an O1280 grid\n", "in_data = np.ones(6599680)\n", "\n", "# helper method for regrid\n", "def _run(n=10):\n", " for _ in range(n):\n", " regrid(in_data, \n", " {\"grid\": \"O1280\"}, \n", " {\"grid\": [0.1,0.1]}, backend=\"precomputed\")" ] }, { "cell_type": "markdown", "id": "a3b773e5-4378-43ba-a706-428ffb8b3c5f", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "In the examples below we will change the configuration multiple times. First we ensure all the changes are temporary and no options are saved into the configuration file. We also reset the configuration to the defaults." ] }, { "cell_type": "code", "execution_count": 2, "id": "e0377623-20b8-4935-9614-eb937ae34b3e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "ekr.config.autosave = False\n", "ekr.config.reset()" ] }, { "cell_type": "markdown", "id": "2aeaea49-cb22-42b0-8e34-65f2abf90ce3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### The \"off\" cache policy" ] }, { "cell_type": "raw", "id": "4c2772b6-de75-4ec4-ae7d-5ff78a49a51c", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can control the in-memory weights cache via the :ref:`config`. In this example we turn off the in-memory cache." ] }, { "cell_type": "code", "execution_count": 3, "id": "5b0734b7-272e-47a5-859f-2af38da13cc3", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "ekr.config.set(weights_memory_cache_policy=\"off\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "22333b87-6005-43ec-9e86-d39833e7de35", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 9.18 s, sys: 1.54 s, total: 10.7 s\n", "Wall time: 7.29 s\n" ] } ], "source": [ "%time _run()" ] }, { "cell_type": "code", "execution_count": 5, "id": "44c806ab-88f0-4364-a1c9-8d8a217a7295", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "CacheInfo(hits=0, misses=0, maxsize=0, currsize=0, count=0, policy='off')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ekr.memory_cache_info()" ] }, { "cell_type": "markdown", "id": "3485ddee-56fa-4346-8946-669286ef954d", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### The \"largest\" (default) cache policy" ] }, { "cell_type": "raw", "id": "e06d0739-ef6f-4a4e-b2de-7b141fcdb0c9", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "Now we will use the default memory cache policy, which is \"largest\". For details see: :ref:`largest_mem_cache_policy`. Notice the roughly x9 speed-up we achieved with the caching." ] }, { "cell_type": "code", "execution_count": 6, "id": "7c8d2977-befc-47aa-a894-aab195aaf4db", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "ekr.config.set(weights_memory_cache_policy=\"largest\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "74152589-4085-4c73-baef-d0c109f5e158", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 728 ms, sys: 77.5 ms, total: 805 ms\n", "Wall time: 806 ms\n" ] } ], "source": [ "%time _run()" ] }, { "cell_type": "code", "execution_count": 8, "id": "59137910-dfb2-4334-beb0-f5590b56d47e", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "CacheInfo(hits=9, misses=1, maxsize=524288000, currsize=259170724, count=1, policy='largest')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ekr.memory_cache_info()" ] }, { "cell_type": "markdown", "id": "a857e482-d056-4b21-b66d-0b08ff29720a", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "#### Clearing the cache" ] }, { "cell_type": "raw", "id": "4abb400b-f5a7-4dad-a802-a0a1ccc05e4b", "metadata": { "editable": true, "raw_mimetype": "text/restructuredtext", "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "We can clear the cache with :func:`clear_memory_cache`." ] }, { "cell_type": "code", "execution_count": 9, "id": "767f7420-0a80-4c3e-ac33-d8dfcd5c4cf7", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "CacheInfo(hits=0, misses=0, maxsize=524288000, currsize=0, count=0, policy='largest')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ekr.clear_memory_cache()\n", "ekr.memory_cache_info()" ] } ], "metadata": { "kernelspec": { "display_name": "mir", "language": "python", "name": "mir" }, "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.13.1" } }, "nbformat": 4, "nbformat_minor": 5 }