Created
December 1, 2017 01:13
-
-
Save JiaweiZhuang/e3f229db8262ee0d0314fcb4515f08ee to your computer and use it in GitHub Desktop.
ESMPy_memory_layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import ESMF" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"Nlon, Nlat = 6001, 4001 # make a large grid" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"lon = np.linspace(-120, 120, Nlon)\n", | |
"lat = np.linspace(-60, 60, Nlat)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"lons, lats = np.meshgrid(lon, lat) # don't use indexing='ij'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(4001, 6001)" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lons.shape # (Nlat, Nlon), typical C-ordering" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
" C_CONTIGUOUS : True\n", | |
" F_CONTIGUOUS : False\n", | |
" OWNDATA : True\n", | |
" WRITEABLE : True\n", | |
" ALIGNED : True\n", | |
" UPDATEIFCOPY : False" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lons.flags # C-ordering" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Inefficient memory layout" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"grid = ESMF.Grid(np.array(lons.shape), \n", | |
" staggerloc = ESMF.StaggerLoc.CENTER,\n", | |
" coord_sys = ESMF.CoordSys.SPH_DEG)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"lon_pointer = grid.get_coords(coord_dim=0, \n", | |
" staggerloc=ESMF.StaggerLoc.CENTER)\n", | |
"lat_pointer = grid.get_coords(coord_dim=1, \n", | |
" staggerloc=ESMF.StaggerLoc.CENTER)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(4001, 6001)" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lon_pointer.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
" C_CONTIGUOUS : False\n", | |
" F_CONTIGUOUS : True\n", | |
" OWNDATA : False\n", | |
" WRITEABLE : True\n", | |
" ALIGNED : True\n", | |
" UPDATEIFCOPY : False" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lon_pointer.flags # Fortran-ordering" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 480 ms, sys: 0 ns, total: 480 ms\n", | |
"Wall time: 475 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"# passing C-ordered array to F-ordered array is slow\n", | |
"%time lon_pointer[:] = lons " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Efficient memory layout" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"grid_T = ESMF.Grid(np.array(lons.T.shape), \n", | |
" staggerloc = ESMF.StaggerLoc.CENTER,\n", | |
" coord_sys = ESMF.CoordSys.SPH_DEG)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"lon_T_pointer = grid_T.get_coords(coord_dim=0, \n", | |
" staggerloc=ESMF.StaggerLoc.CENTER)\n", | |
"lat_T_pointer = grid_T.get_coords(coord_dim=1, \n", | |
" staggerloc=ESMF.StaggerLoc.CENTER)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(6001, 4001)" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lon_T_pointer.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
" C_CONTIGUOUS : False\n", | |
" F_CONTIGUOUS : True\n", | |
" OWNDATA : False\n", | |
" WRITEABLE : True\n", | |
" ALIGNED : True\n", | |
" UPDATEIFCOPY : False" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lons.T.flags # Fortran-ordering" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 40 ms, sys: 0 ns, total: 40 ms\n", | |
"Wall time: 40.9 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"# passing F-ordered array to F-ordered array is fast\n", | |
"%time lon_T_pointer[...] = lons.T " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Mathematically, this is equivalent to using `indexing='ij'` at the beginning. For memory efficiency we should first create a normal, C-ordered numpy array and pass its transpose (which is F-ordered) to ESMPy." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment