{ "cells": [ { "cell_type": "markdown", "id": "e79cf444", "metadata": {}, "source": [ "# Setting memory limits" ] }, { "cell_type": "code", "execution_count": 1, "id": "7bd667b3", "metadata": { "ExecuteTime": { "end_time": "2023-05-10T21:54:08.946345Z", "start_time": "2023-05-10T21:54:06.442583Z" } }, "outputs": [], "source": [ "import aopy" ] }, { "cell_type": "markdown", "id": "6b56744b", "metadata": {}, "source": [ "## Available memory unused by anyone" ] }, { "cell_type": "code", "execution_count": 2, "id": "9689b87b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "914" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aopy.utils.get_memory_available_gb()" ] }, { "cell_type": "markdown", "id": "6bb95204", "metadata": {}, "source": [ "## Undoing the default memory limit" ] }, { "cell_type": "code", "execution_count": 3, "id": "072238d5", "metadata": { "ExecuteTime": { "end_time": "2023-05-10T21:54:35.673078Z", "start_time": "2023-05-10T21:54:35.659289Z" } }, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aopy.utils.get_memory_limit_gb()" ] }, { "cell_type": "code", "execution_count": 4, "id": "6fd2f9bf", "metadata": { "ExecuteTime": { "end_time": "2023-05-10T21:54:47.185877Z", "start_time": "2023-05-10T21:54:47.180089Z" } }, "outputs": [], "source": [ "aopy.utils.release_memory_limit()" ] }, { "cell_type": "code", "execution_count": 5, "id": "52196ef9", "metadata": { "ExecuteTime": { "end_time": "2023-05-10T21:54:50.466969Z", "start_time": "2023-05-10T21:54:50.462132Z" } }, "outputs": [], "source": [ "aopy.utils.get_memory_limit_gb()" ] }, { "cell_type": "markdown", "id": "4e91de84", "metadata": {}, "source": [ "## Using memory limits" ] }, { "cell_type": "code", "execution_count": 6, "id": "b2f5b350", "metadata": { "ExecuteTime": { "end_time": "2023-05-10T21:57:16.462968Z", "start_time": "2023-05-10T21:57:16.459431Z" } }, "outputs": [], "source": [ "aopy.utils.set_memory_limit_gb(128)" ] }, { "cell_type": "code", "execution_count": 7, "id": "02c2cf2d", "metadata": { "ExecuteTime": { "end_time": "2023-05-10T21:55:46.227122Z", "start_time": "2023-05-10T21:55:06.507888Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "80.0\n" ] } ], "source": [ "import numpy as np\n", "a = np.ones((1000000,10000))\n", "print(a.nbytes/1e9)" ] }, { "cell_type": "code", "execution_count": 8, "id": "a7c5a7b6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16.0\n" ] } ], "source": [ "b = np.ones((200000,10000))\n", "print(b.nbytes/1e9)" ] }, { "cell_type": "code", "execution_count": 9, "id": "5b15334c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "112.0\n" ] } ], "source": [ "c = np.ones((200000,10000))\n", "print((a.nbytes + b.nbytes + c.nbytes)/1e9)" ] }, { "cell_type": "code", "execution_count": 10, "id": "ee99fe34", "metadata": {}, "outputs": [], "source": [ "c -= b" ] }, { "cell_type": "code", "execution_count": 11, "id": "257d9161", "metadata": {}, "outputs": [ { "ename": "MemoryError", "evalue": "Unable to allocate 14.9 GiB for an array with shape (200000, 10000) and data type float64", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mMemoryError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [15]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m d \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mones\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m200000\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m10000\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniconda3/envs/leo-analysis/lib/python3.9/site-packages/numpy/core/numeric.py:204\u001b[0m, in \u001b[0;36mones\u001b[0;34m(shape, dtype, order, like)\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m like \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[1;32m 202\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _ones_with_like(shape, dtype\u001b[38;5;241m=\u001b[39mdtype, order\u001b[38;5;241m=\u001b[39morder, like\u001b[38;5;241m=\u001b[39mlike)\n\u001b[0;32m--> 204\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[43mempty\u001b[49m\u001b[43m(\u001b[49m\u001b[43mshape\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43morder\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 205\u001b[0m multiarray\u001b[38;5;241m.\u001b[39mcopyto(a, \u001b[38;5;241m1\u001b[39m, casting\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124munsafe\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 206\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a\n", "\u001b[0;31mMemoryError\u001b[0m: Unable to allocate 14.9 GiB for an array with shape (200000, 10000) and data type float64" ] } ], "source": [ "d = np.ones((200000,10000))" ] }, { "cell_type": "code", "execution_count": 12, "id": "b1d78845", "metadata": {}, "outputs": [], "source": [ "aopy.utils.release_memory_limit()" ] }, { "cell_type": "code", "execution_count": 13, "id": "f21c4b35", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "128.0\n" ] } ], "source": [ "d = np.ones((200000,10000))\n", "print((a.nbytes + b.nbytes + c.nbytes + d.nbytes)/1e9)" ] }, { "cell_type": "code", "execution_count": 14, "id": "93d1b2fc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "914" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aopy.utils.get_memory_available_gb()" ] }, { "cell_type": "code", "execution_count": 15, "id": "6dd16aef", "metadata": {}, "outputs": [], "source": [ "aopy.utils.release_memory_limit()" ] }, { "cell_type": "code", "execution_count": 16, "id": "a5586bba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "914" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aopy.utils.get_memory_available_gb()" ] }, { "cell_type": "markdown", "id": "885d4de8", "metadata": {}, "source": [ "## Seeing total memory" ] }, { "cell_type": "code", "execution_count": 17, "id": "48c74b30", "metadata": { "ExecuteTime": { "end_time": "2023-05-10T21:57:54.999593Z", "start_time": "2023-05-10T21:57:54.761658Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MemTotal: 1056466716 kB\r\n" ] } ], "source": [ "!grep MemTotal /proc/meminfo" ] }, { "cell_type": "code", "execution_count": null, "id": "e2db8068", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3fe4e1c5", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:leo-analysis]", "language": "python", "name": "conda-env-leo-analysis-py" }, "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.9.13" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }