{"id":3088,"date":"2024-04-03T22:17:30","date_gmt":"2024-04-03T17:17:30","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=3088"},"modified":"2024-05-08T22:21:26","modified_gmt":"2024-05-08T17:21:26","slug":"mpi-gather-function-in-python","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/mpi-gather-function-in-python\/","title":{"rendered":"MPI Gather Function in Python"},"content":{"rendered":"\n<p>The gather function is used to gather data from multiple processes into a single process. We&#8217;ll go through the provided code, line by line, and understand how the gather function works.  <a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/mpi-with-python\/\" target=\"_blank\" rel=\"noopener\" title=\"\">The detailed tutorial of MPI with a python can be visited here.<\/a> <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"MPI Gather Function | MPI4PY | Parallel Processing | Parallel Programing\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/mlyHZZ2IsNc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from mpi4py import MPI\n\ncomm = MPI.COMM_WORLD\nsize = comm.Get_size()\nrank = comm.Get_rank()\ndata = (rank + 1)\ndata = comm.gather(data, root=0)\nprint(f\"Process {rank}: Calculated data = {data}\")\n\nif rank == 0:\n    for i in range(size):\n        assert data&#91;i] == (i + 1)\n        print(f\"Process 0: Checked data received from process {i + 1}\")\nelse:\n    assert data is None<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from mpi4py import MPI<\/code><\/pre>\n\n\n\n<p>This line imports the MPI functionality from the mpi4py library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comm = MPI.COMM_WORLD\nsize = comm.Get_size()\nrank = comm.Get_rank()<\/code><\/pre>\n\n\n\n<p>These lines initialize the MPI communicator (<code>comm<\/code>) and obtain the total number of processes (<code>size<\/code>) and the rank of the current process (<code>rank<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = (rank + 1)\ndata = comm.gather(data, root=0)<\/code><\/pre>\n\n\n\n<p>Each process calculates its own <code>data<\/code> value based on its rank (<code>rank + 1<\/code>). Then, the <code>gather<\/code> function is called on the communicator <code>comm<\/code>. This function gathers data from all processes and returns it to the root process (rank 0) as a list. In this case, the data from each process is gathered at process 0 (<code>root=0<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(f\"Process {rank}: Calculated data = {data}\")<\/code><\/pre>\n\n\n\n<p>Each process prints its own calculated data value. Since <code>data<\/code> is different for each process, the output will vary accordingly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if rank == 0:\n    for i in range(size):\n        assert data&#91;i] == (i + 1)\n        print(f\"Process 0: Checked data received from process {i + 1}\")<\/code><\/pre>\n\n\n\n<p>In the root process (rank 0), a loop iterates over all processes (<code>size<\/code>). It checks whether the received data from each process matches the expected value (<code>(i + 1)<\/code>). If the data matches, it prints a confirmation message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>else:\n    assert data is None<\/code><\/pre>\n\n\n\n<p>In non-root processes (ranks other than 0), it&#8217;s ensured that the <code>data<\/code> variable is <code>None<\/code> since they do not receive any data through the <code>gather<\/code> operation.<\/p>\n\n\n\n<p>In this tutorial, we&#8217;ve learned how to use the <code>gather<\/code> function in MPI to collect data from multiple processes into a single process. This is a fundamental operation in parallel programming, often used to gather results from worker processes to a master process for further processing or analysis. Understanding MPI collective operations  <code>gather<\/code> is essential for developing efficient parallel programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Material<\/h2>\n\n\n\n<p><a href=\"https:\/\/drive.google.com\/drive\/folders\/1vKaZIsBGLhzew2DKmmYfMCSrPAo4jXe-?usp=sharing\" target=\"_blank\" rel=\"noopener\" title=\"\">Download the programs (code), covering the MPI4Py.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The gather function is used to gather data from multiple processes into a single process. We&#8217;ll go through the provided code, line by line, and understand how the gather function works. The detailed tutorial of MPI with a python can be visited here. Code Explanation This line imports the MPI functionality from the mpi4py library. These lines initialize the MPI communicator (comm) and obtain the total number of processes (size) and the rank of the current process (rank). Each process&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/mpi-gather-function-in-python\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3090,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[506],"tags":[565,540,543,567],"class_list":["post-3088","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mpi-with-python","tag-gather","tag-mpi","tag-mpi4py","tag-parallel-processing"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MPI-Python-1-jpg.webp?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-NO","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=3088"}],"version-history":[{"count":6,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3088\/revisions"}],"predecessor-version":[{"id":3209,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3088\/revisions\/3209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/3090"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=3088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=3088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=3088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}