{"id":3102,"date":"2024-04-06T10:15:27","date_gmt":"2024-04-06T05:15:27","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=3102"},"modified":"2024-05-08T22:21:32","modified_gmt":"2024-05-08T17:21:32","slug":"mpi-concurrent-file-i-o-for-by-multiple-processes","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/06\/mpi-concurrent-file-i-o-for-by-multiple-processes\/","title":{"rendered":"MPI: Concurrent File I\/O for by Multiple Processes"},"content":{"rendered":"\n<p>In this tutorial, we&#8217;ll explore an MPI (Message Passing Interface) program using <code>mpi4py<\/code> to demonstrate how multiple processors can collectively write to and read from a shared file.  <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<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from mpi4py import MPI\n\n    comm = MPI.COMM_WORLD\n    rank = comm.Get_rank()\n    size = comm.Get_size()\n\n    # Define the file name\n    filename = \"output.txt\"\n\n    # Open the file in append mode and write a message from each processor\n    with open(filename, \"a\") as file:\n        file.write(f\"Hello from processor {rank} of {size}\\n\")\n\n    # Synchronize all processors before reading the file\n    comm.Barrier()\n\n    # Now let only one processor (e.g., rank 0) read and display the file content\n    if rank == 0:\n        # Open the file in read mode\n        with open(filename, \"r\") as file:\n            # Read and display the file content\n            print(\"File contents:\")\n            print(file.read())\n<\/code><\/pre>\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 Python Tutorial: Parallel File I\/O with mpi4py (Step-by-Step Guide)\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/xxq8f1BHfVI?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 Explanation<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from mpi4py import MPI<\/code><\/pre>\n\n\n\n<p>Imports the necessary <code>MPI<\/code> module from <code>mpi4py<\/code> which provides bindings for MPI functionality in Python.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    comm = MPI.COMM_WORLD\n    rank = comm.Get_rank()\n    size = comm.Get_size()<\/code><\/pre>\n\n\n\n<p>Initializes MPI communication (<code>comm<\/code>) for all processes (<code>MPI.COMM_WORLD<\/code>). <code>rank<\/code> is assigned the unique identifier (rank) of the current process, and <code>size<\/code> represents the total number of processes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    # Define the file name\n    filename = \"output.txt\"<\/code><\/pre>\n\n\n\n<p>Sets the name of the file (<code>output.txt<\/code>) that will be used for writing and reading.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    # Open the file in append mode and write a message from each processor\n    with open(filename, \"a\") as file:\n        file.write(f\"Hello from processor {rank} of {size}\\n\")<\/code><\/pre>\n\n\n\n<p>Opens <code>output.txt<\/code> file in append mode (<code>\"a\"<\/code>). Each MPI process writes a message to the file containing its rank (<code>rank<\/code>) and the total number of processes (<code>size<\/code>).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"331\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/WhatsApp-Image-2024-04-06-at-09.34.13-1024x529.webp?resize=640%2C331&#038;ssl=1\" alt=\"\" class=\"wp-image-3107\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/WhatsApp-Image-2024-04-06-at-09.34.13-jpeg.webp?resize=1024%2C529&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/WhatsApp-Image-2024-04-06-at-09.34.13-jpeg.webp?resize=300%2C155&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/WhatsApp-Image-2024-04-06-at-09.34.13-jpeg.webp?resize=768%2C397&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/WhatsApp-Image-2024-04-06-at-09.34.13-jpeg.webp?resize=522%2C270&amp;ssl=1 522w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/WhatsApp-Image-2024-04-06-at-09.34.13-jpeg.webp?w=1395&amp;ssl=1 1395w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/WhatsApp-Image-2024-04-06-at-09.34.13-jpeg.webp?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Output of the above program<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>    # Synchronize all processors before reading the file\n    comm.Barrier()<\/code><\/pre>\n\n\n\n<p>Ensures all MPI processes reach this point before proceeding, creating a synchronization barrier. This ensures all writes to <code>output.txt<\/code> are complete before any process attempts to read from it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    # Now let only one processor (e.g., rank 0) read and display the file content\n    if rank == 0:\n        # Open the file in read mode\n        with open(filename, \"r\") as file:\n            # Read and display the file content\n            print(\"File contents:\")\n            print(file.read())<\/code><\/pre>\n\n\n\n<p>Only the MPI process with <code>rank == 0<\/code> executes the following block:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Opens <code>output.txt<\/code> in read mode (<code>\"r\"<\/code>).<\/li>\n\n\n\n<li>Prints a header indicating file contents.<\/li>\n\n\n\n<li>Reads and displays the entire content of <code>output.txt<\/code>.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Steps to Execute the Program<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Save the Script<\/strong>: Save the above code to a file named <code>mpi_file_io.py<\/code> on your system.<\/li>\n\n\n\n<li><strong>Run the Program<\/strong>:<br>Open a terminal or command prompt. Run the MPI program using multiple processes:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   mpiexec -n 4 python mpi_file_io.py<\/code><\/pre>\n\n\n\n<p>Replace <code>4<\/code> with the number of MPI processes you want to run (e.g., <code>4<\/code> processes in this case).<\/p>\n\n\n\n<p>You have learned how to use MPI in Python (<code>mpi4py<\/code>) to coordinate file I\/O operations among multiple processors. This technique is essential for parallel computing tasks where data needs to be shared or coordinated among distributed processes. Experiment with different numbers of MPI processes to observe how the program behaves with varying levels of parallelism.<\/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>In this tutorial, we&#8217;ll explore an MPI (Message Passing Interface) program using mpi4py to demonstrate how multiple processors can collectively write to and read from a shared file. The detailed tutorial of MPI with a python can be visited here. Code Code Explanation Imports the necessary MPI module from mpi4py which provides bindings for MPI functionality in Python. Initializes MPI communication (comm) for all processes (MPI.COMM_WORLD). rank is assigned the unique identifier (rank) of the current process, and size represents&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/06\/mpi-concurrent-file-i-o-for-by-multiple-processes\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3105,"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":[568,540,543,567],"class_list":["post-3102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mpi-with-python","tag-file-i-o","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-3-jpg.webp?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-O2","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3102","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=3102"}],"version-history":[{"count":6,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3102\/revisions"}],"predecessor-version":[{"id":3210,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3102\/revisions\/3210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/3105"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=3102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=3102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=3102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}