{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial FFT 3D parallel (MPI): Domain decomposition\n", "We have seen that FluidFFT provides a unified framework for different parallelized FFT 3D libraries, viz. FFTW (with MPI), P3DFFT, and PFFT.\n", "\n", "In this tutorial, we will look into how these libraries perform domain decomposition, and thereby try to balance the load evenly. Understanding how this is done is important to plan the discretization (i.e. shape of the arrays).\n", "\n", "Always remember:\n", "\n", "> \"FFTW is best at handling sizes of the form $2^a \\times 3^b \\times 5^c \\times 7^d \\times 11^e \\times 13^f$, where $e+f$ is either 0 or 1, and the other exponents are arbitrary. Other sizes are computed by means of a slow, general-purpose routine (which nevertheless retains $O(n \\log n)$ performance, even for prime sizes). (It is possible to customize FFTW for different array sizes. See Section [Installation and Customization](http://www.fftw.org/fftw2_doc/fftw_6.html#SEC66), for more information.) Transforms whose sizes are powers of 2 are especially fast.\"\n", ">\n", "> Source: http://www.fftw.org/fftw2_doc/fftw_3.html\n", "\n", "Just like, before we start an parallelized IPython/Jupyter session with `ipcluster start -n 8 --engines=MPIEngineSetLauncher`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ipyparallel as ipp\n", "rc = ipp.Client()\n", "dview = rc[:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by importing all the functions that we need" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%%px\n", "from fluiddyn.util.info import _print_dict\n", "from fluidfft.fft3d import get_classes_mpi\n", "from fluiddyn.util.mpi import rank, print_sorted, printby0" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%%px\n", "dict_classes = get_classes_mpi()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function `get_classes_mpi` creates a dictionary of all available FFT classes." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " - fft3d.mpi_with_fftw1d : \n", " - fft3d.mpi_with_fftwmpi3d : \n", " - fft3d.mpi_with_p3dfft : \n", " - fft3d.mpi_with_pfft : \n" ] } ], "source": [ "%%px --targets 1\n", "_print_dict(dict_classes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now chose a small shape for the purpose of this tutorial, compatible with FFTW requirements: say $20 \\times 12 \\times 16$, and instantiate FFT operators (or objects) of the above classes. Let us compose a nifty function which takes an FFT class as the argument, instantiates it with the shape and prints the information we seek." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "%%px\n", "def fft_info(cls):\n", " \"\"\"Instanitate and display array shapes\"\"\"\n", " opfft = cls(20, 12, 16)\n", " printby0('get_dimX_K :'.rjust(35), opfft.get_dimX_K())\n", " print_sorted(\n", " 'Global physical shape:'.rjust(35), opfft.get_shapeX_seq(),\n", " '\\n' + 'Local physical shape :'.rjust(35), opfft.get_shapeX_loc(),\n", " '\\n' + 'Global FFT shape :'.rjust(35), opfft.get_shapeK_seq(),\n", " '\\n' + 'Local FFT shape :'.rjust(35), opfft.get_shapeK_loc()\n", " )\n", " del opfft" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## fft3d.mpi_with_fftw1d" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[stdout:0] \n", " get_dimX_K : (2, 1, 0)\n", "rank 0:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (8, 12, 20) \n", " Local FFT shape : (1, 12, 20)\n", "[stdout:1] \n", "rank 1:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (8, 12, 20) \n", " Local FFT shape : (1, 12, 20)\n", "[stdout:2] \n", "rank 2:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (8, 12, 20) \n", " Local FFT shape : (1, 12, 20)\n", "[stdout:3] \n", "rank 3:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (8, 12, 20) \n", " Local FFT shape : (1, 12, 20)\n", "[stdout:4] \n", "rank 4:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (8, 12, 20) \n", " Local FFT shape : (1, 12, 20)\n", "[stdout:5] \n", "rank 5:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (8, 12, 20) \n", " Local FFT shape : (1, 12, 20)\n", "[stdout:6] \n", "rank 6:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (8, 12, 20) \n", " Local FFT shape : (1, 12, 20)\n", "[stdout:7] \n", "rank 7:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (8, 12, 20) \n", " Local FFT shape : (1, 12, 20)\n" ] } ], "source": [ "%%px\n", "fft_info(dict_classes['fft3d.mpi_with_fftw1d'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## fft3d.mpi_with_fftwmpi3d" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[stdout:0] \n", " get_dimX_K : (1, 0, 2)\n", "rank 0:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (3, 12, 16) \n", " Global FFT shape : (12, 20, 9) \n", " Local FFT shape : (2, 20, 9)\n", "[stdout:1] \n", "rank 1:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (3, 12, 16) \n", " Global FFT shape : (12, 20, 9) \n", " Local FFT shape : (2, 20, 9)\n", "[stdout:2] \n", "rank 2:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (3, 12, 16) \n", " Global FFT shape : (12, 20, 9) \n", " Local FFT shape : (2, 20, 9)\n", "[stdout:3] \n", "rank 3:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (3, 12, 16) \n", " Global FFT shape : (12, 20, 9) \n", " Local FFT shape : (2, 20, 9)\n", "[stdout:4] \n", "rank 4:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (3, 12, 16) \n", " Global FFT shape : (12, 20, 9) \n", " Local FFT shape : (2, 20, 9)\n", "[stdout:5] \n", "rank 5:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (3, 12, 16) \n", " Global FFT shape : (12, 20, 9) \n", " Local FFT shape : (2, 20, 9)\n", "[stdout:6] \n", "rank 6:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (2, 12, 16) \n", " Global FFT shape : (12, 20, 9) \n", " Local FFT shape : (0, 20, 9)\n", "[stdout:7] \n", "rank 7:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (0, 12, 16) \n", " Global FFT shape : (12, 20, 9) \n", " Local FFT shape : (0, 20, 9)\n" ] } ], "source": [ "%%px\n", "fft_info(dict_classes['fft3d.mpi_with_fftwmpi3d'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## fft3d.mpi_with_pfft" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[stdout:0] \n", " get_dimX_K : (1, 2, 0)\n", "rank 0:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (10, 3, 16) \n", " Global FFT shape : (12, 9, 20) \n", " Local FFT shape : (6, 3, 20)\n", "[stdout:1] \n", "rank 1:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (10, 3, 16) \n", " Global FFT shape : (12, 9, 20) \n", " Local FFT shape : (6, 3, 20)\n", "[stdout:2] \n", "rank 2:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (10, 3, 16) \n", " Global FFT shape : (12, 9, 20) \n", " Local FFT shape : (6, 3, 20)\n", "[stdout:3] \n", "rank 3:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (10, 3, 16) \n", " Global FFT shape : (12, 9, 20) \n", " Local FFT shape : (6, 0, 20)\n", "[stdout:4] \n", "rank 4:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (10, 3, 16) \n", " Global FFT shape : (12, 9, 20) \n", " Local FFT shape : (6, 3, 20)\n", "[stdout:5] \n", "rank 5:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (10, 3, 16) \n", " Global FFT shape : (12, 9, 20) \n", " Local FFT shape : (6, 3, 20)\n", "[stdout:6] \n", "rank 6:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (10, 3, 16) \n", " Global FFT shape : (12, 9, 20) \n", " Local FFT shape : (6, 3, 20)\n", "[stdout:7] \n", "rank 7:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (10, 3, 16) \n", " Global FFT shape : (12, 9, 20) \n", " Local FFT shape : (6, 0, 20)\n" ] } ], "source": [ "%%px\n", "fft_info(dict_classes['fft3d.mpi_with_pfft'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## fft3d.mpi_with_p3dfft" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[stdout:0] \n", " get_dimX_K : (0, 1, 2)\n", "rank 0:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (5, 6, 16) \n", " Global FFT shape : (20, 12, 9) \n", " Local FFT shape : (20, 3, 4)\n", "[stdout:1] \n", "rank 1:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (5, 6, 16) \n", " Global FFT shape : (20, 12, 9) \n", " Local FFT shape : (20, 3, 5)\n", "[stdout:2] \n", "rank 2:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (5, 6, 16) \n", " Global FFT shape : (20, 12, 9) \n", " Local FFT shape : (20, 3, 4)\n", "[stdout:3] \n", "rank 3:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (5, 6, 16) \n", " Global FFT shape : (20, 12, 9) \n", " Local FFT shape : (20, 3, 5)\n", "[stdout:4] \n", "rank 4:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (5, 6, 16) \n", " Global FFT shape : (20, 12, 9) \n", " Local FFT shape : (20, 3, 4)\n", "[stdout:5] \n", "rank 5:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (5, 6, 16) \n", " Global FFT shape : (20, 12, 9) \n", " Local FFT shape : (20, 3, 5)\n", "[stdout:6] \n", "rank 6:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (5, 6, 16) \n", " Global FFT shape : (20, 12, 9) \n", " Local FFT shape : (20, 3, 4)\n", "[stdout:7] \n", "rank 7:\n", " Global physical shape: (20, 12, 16) \n", " Local physical shape : (5, 6, 16) \n", " Global FFT shape : (20, 12, 9) \n", " Local FFT shape : (20, 3, 5)\n" ] } ], "source": [ "%%px\n", "fft_info(dict_classes['fft3d.mpi_with_p3dfft'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "We have only looked at the default options of the FFT classes. It is possible to transpose and customize array ordering. Different approaches are adopted by different FFT libraries both in terms of array ordering and distributing processes.\n", "\n", "Note that FFTW methods distributes processes only over one index, i.e. splits the global array into **slabs**. On the other hand P3DFFT and PFFT distributes processes over 2 indices, i.e. splitting the global array in 2 dimensions (also known as **pencil decomposition**). With this method, there is a 2d process grid with shape $(p_0, p_1)$ such as $p = p_0 p_1$ is the total number of MPI processes. In our example, $p = 8$, $p_0 = 2$ and $p_1 = 4$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "| Method | FFT array order | Physical array process grid | FFT array process grid |\n", "|---------------------------|-----------------|-----------------------------|------------------------|\n", "| `fft3d.mpi_with_fftw1d` | $(2, 1, 0)$ | $(p, 1, 1)$ | $(p, 1, 1)$ |\n", "| `fft3d.mpi_with_fftwmpi3d`| $(1, 0, 2)$ | $(p, 1, 1)$ | $(p, 1, 1)$ |\n", "| `fft3d.mpi_with_pfft` | $(1, 2, 0)$ | $(p_0, p_1, 1)$ | $(p_0, p_1, 1)$ |\n", "| `fft3d.mpi_with_p3dfft` | $(0, 1, 2)$ | $(p_0, p_1, 1)$ | $(1, p_1, p_0)$ |" ] } ], "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.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }