Skip to content

Commit

Permalink
Tue Jan 9 10:22:53 CET 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
dgerosa committed Jan 9, 2024
1 parent 1ef6027 commit 1b7503b
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions lectures/L07_numba_multiprocessing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"- Write some core functions in compiled language like C or Fortran and interface it with python. This is also useful to recycle some legacy code written by the supervisor of your supervisor who doesn't know python.\n",
"- Use a library that converts python into compiled code (with some restrictions)\n",
"\n",
"Some libraries:"
"Some examples:"
]
},
{
Expand Down Expand Up @@ -82,7 +82,7 @@
" compiled C code. The advantage here is that the code looks like python, with some\n",
" declarations of the variable types with `cdef`. Performance can be\n",
" really great when you need to explicitly write out loops over\n",
" numpy array indices.\n",
" numpy array indices. Advantage: you can identify the bottleneck of your code and cytonize only that part. \n",
" * [Numba](https://numba.pydata.org/) : this is a just-in-time\n",
" compiler. It just requires a simple decorator and then it will\n",
" compile a python function the first time it is encountered.\n",
Expand Down Expand Up @@ -110,12 +110,38 @@
"\n",
"A python decorator is a function that takes another function as input, modifies it, and returns the modified function to the user. I realize that this sentence sounds tricky, but it's not. \n",
"\n",
"```\n",
"def decorator(function):\n",
" [do something]\n",
" output=function(input)\n",
" [do something else]\n",
" return [result]\n",
"```\n",
"\n",
"Then if you have any function\n",
"\n",
"```\n",
"def myprecious(ring):\n",
" return gollum\n",
"``` \n",
" \n",
"You can redefine it to e.g.\n",
"\n",
"myprecious=decorator\n",
"\n",
"\n",
" \n",
" \n",
"\n",
"\n",
"\n",
"\n",
"Remember that in python everything is an object. Functions are objects, and classes are objects too. For instance, let's take this simple function:"
]
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 1,
"id": "e38aba41",
"metadata": {},
"outputs": [],
Expand All @@ -134,7 +160,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 2,
"id": "efe340f1",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -163,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 3,
"id": "0806aae1",
"metadata": {
"colab": {
Expand All @@ -174,14 +200,11 @@
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'hello' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[29], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m whello \u001b[38;5;241m=\u001b[39m make_sure(\u001b[43mhello\u001b[49m)\n\u001b[1;32m 2\u001b[0m whello()\n",
"\u001b[0;31mNameError\u001b[0m: name 'hello' is not defined"
"name": "stdout",
"output_type": "stream",
"text": [
"are you sure you want to greet the world? [y/n]y\n",
"Hello world\n"
]
}
],
Expand Down

0 comments on commit 1b7503b

Please sign in to comment.