add final exercise

This commit is contained in:
Tiziano Zito 2024-08-21 12:31:19 +02:00
parent a7c4f01a6c
commit f9faf88a72
2 changed files with 191 additions and 2 deletions

View file

@ -72,9 +72,17 @@ Setup:
- … unless of course you plan to mostly loop *across* time series :)
- watch out when migrating code from MATLAB® or to `pandas.DataFrame` ➔ they store data in memory using the opposite convention, the column-major order!!!
Notes on the [Python benchmark](benchmark_python/):
## A final exercise to put it all together
- fork this repo to your account and clone your fork on the laptop
- create a branch `ex` and switch to it
- work on the [exercise](exercise.ipynb)
- push your solution to yuor fork and create a Pull Request to this repo
- while running it attached to one core on my laptop, the core was running under a constant load of 100% (almost completely user-time) and at a fixed frequency of 3.8 GHz, where the theoretical max would be 5.2 GHz
## Notes on the benchmarks
- while running the benchmarks attached to one core on my laptop, the core was running under a constant load of 100% (almost completely user-time) and at a fixed frequency of 3.8 GHz, where the theoretical max would be 5.2 GHz
➔ the CPU does not "starve" because it scales its speed down to match the memory throughput? Or I am misinterpreting this? This problem which at first sight should be perfectly memory-bound, becomes CPU-bound, or actually, exactly balanced? From the [Intel documentation](https://lenovopress.lenovo.com/lp1836-tuning-uefi-settings-4th-gen-intel-xeon-scalable-processor):
> **Energy Efficient Turbo**

181
exercise.ipynb Normal file
View file

@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-04T09:40:28.904Z",
"iopub.status.busy": "2024-03-04T09:40:28.896Z",
"iopub.status.idle": "2024-03-04T09:40:28.978Z",
"shell.execute_reply": "2024-03-04T09:40:28.967Z"
}
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-04T10:02:39.062Z",
"iopub.status.busy": "2024-03-04T10:02:39.057Z",
"iopub.status.idle": "2024-03-04T10:02:39.068Z",
"shell.execute_reply": "2024-03-04T10:02:39.071Z"
}
},
"outputs": [],
"source": [
"n_series = 32\n",
"len_one_series = 5*2**20\n",
"time_series = np.random.rand(n_series, len_one_series)\n",
"gap = 16*2**10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-04T10:02:41.027Z",
"iopub.status.busy": "2024-03-04T10:02:41.020Z",
"iopub.status.idle": "2024-03-04T10:02:41.036Z",
"shell.execute_reply": "2024-03-04T10:02:41.040Z"
},
"scrolled": true
},
"outputs": [],
"source": [
"print(f'Size of one time series: {int(time_series[0].nbytes/2**20)} M')\n",
"print(f'Size of collection: {int(time_series.nbytes/2**20)} M')\n",
"print(f'Gap size: {int(gap*8/2**10)} K')\n",
"print(f'Gapped series size: {int(time_series[0, ::gap].nbytes/2**10)} K')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-04T10:06:08.461Z",
"iopub.status.busy": "2024-03-04T10:06:08.459Z",
"iopub.status.idle": "2024-03-04T10:06:08.466Z",
"shell.execute_reply": "2024-03-04T10:06:08.468Z"
}
},
"outputs": [],
"source": [
"# compute a Taylor-like series\n",
"def taylor(time_series, mean, gap):\n",
" for row, ts in enumerate(time_series):\n",
" for pwr in range(1,20):\n",
" mean[row] += (ts[::gap]**pwr).sum()\n",
" return mean\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Challenge\n",
"- Can you improve on the above implementation of the `taylor` function?\n",
"- Change the following `taylor_improved` function and see what you can do\n",
"- **Remember**: you can't change any other cell in this notebook!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-04T10:06:08.461Z",
"iopub.status.busy": "2024-03-04T10:06:08.459Z",
"iopub.status.idle": "2024-03-04T10:06:08.466Z",
"shell.execute_reply": "2024-03-04T10:06:08.468Z"
}
},
"outputs": [],
"source": [
"def taylor_improved(time_series, mean, gap):\n",
" for row, ts in enumerate(time_series):\n",
" for pwr in range(1,20):\n",
" mean[row] += (ts[::gap]**pwr).sum()\n",
" return mean"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# verify that they yield the same results\n",
"out1 = taylor(time_series, np.zeros(n_series), gap)\n",
"out2 = taylor_improved(time_series, np.zeros(n_series), gap)\n",
"np.testing.assert_allclose(out1, out2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-04T10:06:14.959Z",
"iopub.status.busy": "2024-03-04T10:06:14.956Z",
"iopub.status.idle": "2024-03-04T10:06:17.437Z",
"shell.execute_reply": "2024-03-04T10:06:17.443Z"
}
},
"outputs": [],
"source": [
"mean = np.zeros(n_series, dtype='float64')\n",
"%timeit taylor(time_series, mean, gap)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-03-04T10:06:20.056Z",
"iopub.status.busy": "2024-03-04T10:06:20.053Z",
"iopub.status.idle": "2024-03-04T10:06:21.695Z",
"shell.execute_reply": "2024-03-04T10:06:21.700Z"
}
},
"outputs": [],
"source": [
"mean = np.zeros(n_series, dtype='float64')\n",
"%timeit taylor_improved(time_series, mean, gap)"
]
}
],
"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.5"
},
"nteract": {
"version": "0.28.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}