{"id":2955,"date":"2024-06-24T12:58:50","date_gmt":"2024-06-24T07:58:50","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=2955"},"modified":"2024-06-24T12:58:56","modified_gmt":"2024-06-24T07:58:56","slug":"introduction-to-scatter-operation-in-mpi","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/06\/24\/introduction-to-scatter-operation-in-mpi\/","title":{"rendered":"Introduction to Scatter Operation in MPI"},"content":{"rendered":"\n<p>In MPI (Message Passing Interface) programming, the scatter operation is a collective communication pattern used to distribute data from one process to multiple processes. It takes an array or list of data on the root process and divides it into smaller chunks, then scatters these chunks to all other processes in the communicator. Each process receives one chunk of the data, allowing for parallel processing on different subsets of the data.<\/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 (MPI4PY) on Jupyter Tutorial\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/videoseries?list=PLGiqyN7d0mypGRrq2Bv9mXLGoeZPXTcZA\" 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<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from mpi4py import MPI  # Import MPI module from mpi4py library\n\ncomm = MPI.COMM_WORLD  # Initialize MPI communicator\nsize = comm.Get_size()  # Get the size of the communicator (total number of processes)\nrank = comm.Get_rank()  # Get the rank of the current process\n\nprint(\"Total number of processes:\", size)  # Print the total number of processes\nprint(\"Rank of current process:\", rank)  # Print the rank of the current process\n\nif rank == 0:  # Check if the current process is the root process (rank 0)\n    data = &#91;(i+1)**2 for i in range(size)]  # Generate data to be scattered by root process\n    print(\"Data generated by root process:\", data)  # Print the generated data by root process\nelse:  # For non-root processes\n    data = None  # Set data to None\n\ndata = comm.scatter(data, root=0)  # Scatter data from root process to all other processes\nprint(\"Process\", rank, \"received data:\", data)  # Print the data received by the current process\n\nassert data == (rank+1)**2  # Verify correctness of received data\nprint(\"Data verification successful for process\", rank)  # Print success message if data verification passes<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from mpi4py import MPI  # Import MPI module from mpi4py library<\/code><\/pre>\n\n\n\n<p>Import the MPI module from the mpi4py library, which provides MPI functionality for Python programs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>comm = MPI.COMM_WORLD  # Initialize MPI communicator<\/code><\/pre>\n\n\n\n<p>Initialize the MPI communicator <code>comm<\/code> using <code>MPI.COMM_WORLD<\/code>, representing the group of processes involved in communication.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>size = comm.Get_size()  # Get the size of the communicator (total number of processes)<\/code><\/pre>\n\n\n\n<p>Retrieve the total number of processes in the communicator and store it in the variable <code>size<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rank = comm.Get_rank()  # Get the rank of the current process<\/code><\/pre>\n\n\n\n<p>Retrieve the rank of the current process within the communicator and store it in the variable <code>rank<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Total number of processes:\", size)  # Print the total number of processes\nprint(\"Rank of current process:\", rank)  # Print the rank of the current process<\/code><\/pre>\n\n\n\n<p>Print the total number of processes and the rank of the current process.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if rank == 0:  # Check if the current process is the root process (rank 0)\n    data = &#91;(i+1)**2 for i in range(size)]  # Generate data to be scattered by root process\n    print(\"Data generated by root process:\", data)  # Print the generated data by root process\nelse:  # For non-root processes\n    data = None  # Set data to None<\/code><\/pre>\n\n\n\n<p>If the current process is the root process (rank 0), generate data to be scattered. Otherwise, set data to None.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = comm.scatter(data, root=0)  # Scatter data from root process to all other processes<\/code><\/pre>\n\n\n\n<p>Scatter the data from the root process (rank 0) to all other processes in the communicator. Each process receives one chunk of the data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Process\", rank, \"received data:\", data)  # Print the data received by the current process<\/code><\/pre>\n\n\n\n<p>Print the data received by the current process after the scatter operation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>assert data == (rank+1)**2  # Verify correctness of received data\nprint(\"Data verification successful for process\", rank)  # Print success message if data verification passes<\/code><\/pre>\n\n\n\n<p>Verify the correctness of the received data by checking if each element of the received data is equal to the square of the rank of the respective process. If the assertion passes, print a success message.<\/p>\n\n\n\n<p><a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/mpi-with-python\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Visit the detailed tutorial here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MPI (Message Passing Interface) programming, the scatter operation is a collective communication pattern used to distribute data from one process to multiple processes. It takes an array or list of data on the root process and divides it into smaller chunks, then scatters these chunks to all other processes in the communicator. Each process receives one chunk of the data, allowing for parallel processing on different subsets of the data. Code: Explanation: Import the MPI module from the mpi4py&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/06\/24\/introduction-to-scatter-operation-in-mpi\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3597,"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":[],"class_list":["post-2955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mpi-with-python"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/06\/MPI-Python-2.png?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-LF","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2955","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=2955"}],"version-history":[{"count":3,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2955\/revisions"}],"predecessor-version":[{"id":3598,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2955\/revisions\/3598"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/3597"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=2955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=2955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=2955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}