<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Youssef Lamine on Medium]]></title>
        <description><![CDATA[Stories by Youssef Lamine on Medium]]></description>
        <link>https://medium.com/@yousseflamine?source=rss-30f47311df8f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2PYysYST7reljPn2</url>
            <title>Stories by Youssef Lamine on Medium</title>
            <link>https://medium.com/@yousseflamine?source=rss-30f47311df8f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:02:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yousseflamine/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to Parallel Programming: A Comprehensive Summary]]></title>
            <link>https://medium.com/@yousseflamine/introduction-to-parallel-programming-a-comprehensive-summary-7f8786f1fdd7?source=rss-30f47311df8f------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f8786f1fdd7</guid>
            <category><![CDATA[parallel-hardware]]></category>
            <category><![CDATA[pthread]]></category>
            <category><![CDATA[openmp]]></category>
            <category><![CDATA[parallel-computing]]></category>
            <category><![CDATA[low-level-programming]]></category>
            <dc:creator><![CDATA[Youssef Lamine]]></dc:creator>
            <pubDate>Sun, 14 Dec 2025 13:40:34 GMT</pubDate>
            <atom:updated>2025-12-14T13:40:34.265Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Abstract</strong> This article provides a structured summary of the core concepts of parallel programming, covering shared-memory (Pthreads, OpenMP) and distributed-memory (MPI) paradigms. Key topics include parallel hardware taxonomies, software models, program design methodologies, performance evaluation metrics, and detailed algorithm parallelization strategies.</p><h3>1. Motivation and Fundamentals</h3><h3>Why Parallel Computing?</h3><p>From 1986 to 2002, processor performance grew by approximately 50% per year. However, since 2002, this rate has slowed to roughly 20% per year due to physical limitations — specifically heat dissipation and power consumption. To continue improving computational power, the industry shifted to <strong>multicore processors</strong>, integrating multiple processing cores (CPUs) onto a single chip.</p><p>Because serial programs designed for single-core systems cannot automatically exploit multiple cores, developers must explicitly write <strong>parallel programs</strong>.</p><h3>Approaches to Parallelism</h3><p>There are two primary methods for designing parallel programs:</p><ul><li><strong>Task-parallelism:</strong> Different tasks (instruction streams) are assigned to different cores.</li><li><strong>Data-parallelism:</strong> The data set is partitioned among cores, and each core performs similar operations on its specific subset.</li></ul><p><strong>Example: Grading 100 exams with 5 Teaching Assistants (TAs)</strong></p><ul><li><em>Task-parallel:</em> TA “A” grades question 1, TA “B” grades question 2, etc.</li><li><em>Data-parallel:</em> TA “A” grades all questions for exams 1–20, TA “B” grades exams 21–40, etc.</li></ul><h3>Parallel Program Requirements</h3><p>Parallel programs generally require three forms of coordination:</p><ol><li><strong>Communication:</strong> Exchanging data between processes or threads.</li><li><strong>Load balancing:</strong> Distributing computational work evenly to prevent idle cores.</li><li><strong>Synchronization:</strong> Ensuring processes/threads reach certain execution points in unison (e.g., to prevent data races).</li></ol><h3>System Models</h3><ul><li><strong>Shared-memory systems:</strong> All cores access a common global memory space. Communication occurs implicitly through reading/writing shared variables. (APIs: Pthreads, OpenMP).</li><li><strong>Distributed-memory systems:</strong> Each core (node) has its own private memory. Communication is explicit, typically via message passing over a network. (API: MPI).</li></ul><h3>2. Parallel Hardware and Software</h3><h3>Parallel Hardware Architectures (Flynn’s Taxonomy)</h3><p>Computer architectures are often classified using <strong>Flynn’s Taxonomy</strong>:</p><ul><li><strong>SISD (Single Instruction, Single Data):</strong> The traditional von Neumann machine.</li><li><strong>SIMD (Single Instruction, Multiple Data):</strong> One control unit broadcasts a single instruction to multiple ALUs, which operate on different data items simultaneously. This is ideal for vector operations (e.g., GPUs).</li><li><strong>MIMD (Multiple Instruction, Multiple Data):</strong> A collection of independent processors, each with its own control unit and ALU. This is the standard model for modern clusters and multicore systems.</li></ul><h3>Cache Coherence and False Sharing</h3><p>In shared-memory systems, cores have private caches. The <strong>cache coherence problem</strong> arises when one core updates a shared variable; other caches holding that variable must be invalidated or updated.</p><p>A specific performance pitfall is <strong>False Sharing</strong>. This occurs when:</p><ol><li>Two threads on different cores modify <em>different</em> variables.</li><li>These variables happen to reside on the <em>same</em> cache line.</li></ol><p>The cache coherency hardware invalidates the line repeatedly (“ping-ponging” the cache line), forcing expensive memory traffic despite no logical data sharing.</p><h3>Performance Metrics</h3><ul><li><strong>Speedup (S):</strong> S = T_serial / T_parallel</li><li><strong>Efficiency (E):</strong> E = S / p, where <em>p</em> is the number of cores.</li></ul><p><strong>Amdahl’s Law</strong> If a fraction <em>r</em> of a program is inherently serial (cannot be parallelized), the maximum theoretical speedup is bounded by 1/r, regardless of the number of processors.</p><p><em>Example:</em> If 25% of a program is serial (r=0.25), the max speedup is 1 / 0.25 = 4.0.</p><h3>3. Distributed-Memory Programming with MPI</h3><h3>Core Concepts</h3><p>The <strong>Message Passing Interface (MPI)</strong> is the standard for distributed-memory programming.</p><ul><li><strong>Processes:</strong> Independent execution units with private memory.</li><li><strong>Ranks:</strong> Unique integer identifiers (0 to p-1) assigned to processes.</li><li><strong>Communicator:</strong> A defined group of processes (e.g., MPI_COMM_WORLD).</li><li><strong>SPMD (Single Program, Multiple Data):</strong> The same executable runs on all nodes, using conditional logic based on <em>Rank</em> to perform different tasks.</li></ul><h3>Collective Communications</h3><p>MPI provides optimized routines involving all processes in a communicator:</p><ul><li>MPI_Reduce: Aggregates data from all processes to a root (e.g., Global Sum).</li><li>MPI_Allreduce: Same as Reduce, but the result is distributed to <em>all</em> processes.</li><li>MPI_Bcast: A root process broadcasts data to all others.</li><li>MPI_Scatter: Distributes chunks of an array from root to all processes.</li><li>MPI_Gather: Collects chunks from all processes to the root.</li><li>MPI_Allgather: Collects chunks from all processes and distributes the complete set to everyone. (Crucial for N-Body solvers).</li></ul><h3>Data Distributions</h3><ul><li><strong>Block Distribution:</strong> Array indices are assigned in contiguous chunks.</li><li><strong>Cyclic Distribution:</strong> Indices are assigned round-robin (Process 0 gets <em>i</em>, Process 1 gets <em>i+1</em>, etc.).</li></ul><h3>4. Shared-Memory Programming (Pthreads &amp; OpenMP)</h3><h3>Pthreads: Explicit Management</h3><p>POSIX Threads (Pthreads) is a low-level C library used for manual thread management.</p><ul><li><strong>Mutexes:</strong> Provide mutual exclusion for <strong>critical sections</strong> to prevent data races.</li><li><strong>Semaphores:</strong> Signaling mechanisms useful for producer-consumer patterns.</li><li><strong>Condition Variables:</strong> Allow threads to sleep until a specific state change occurs.</li></ul><h3>OpenMP: Directive-Based</h3><p>OpenMP simplifies threading using compiler pragmas.</p><ul><li>#pragma omp parallel: Forks a team of threads.</li><li>#pragma omp parallel for: Automatically partitions loop iterations.</li><li>#pragma omp critical: Protects a critical section.</li><li>#pragma omp atomic: Optimized protection for single memory updates (e.g., x++).</li></ul><p><strong>Example: Parallel Pi Calculation (OpenMP)</strong></p><pre>double sum = 0.0;<br>double factor;<br>int k, n = 10000;<br><br>// Note: factor must be private to prevent data races<br>#pragma omp parallel for reduction(+:sum) private(factor)<br>for (k = 0; k &lt; n; k++) {<br>    factor = (k % 2 == 0) ? 1.0 : -1.0;<br>    // Use 2.0*k to ensure floating point division<br>    sum += factor / (2.0*k + 1.0);<br>}<br>pi = 4.0 * sum;</pre><h3>Thread Safety</h3><p>A function is <strong>thread-safe</strong> if it can be simultaneously executed by multiple threads without error. The standard C function strtok is <strong>not</strong> thread-safe because it relies on internal static storage. The reentrant version, strtok_r, is thread-safe.</p><h3>5. Parallel Program Development</h3><h3>Foster’s Methodology</h3><p>A systematic design process:</p><ul><li><strong>Partitioning:</strong> Decomposition of computation into small tasks.</li><li><strong>Communication:</strong> Identifying data flow between tasks.</li><li><strong>Agglomeration:</strong> Grouping tasks to improve performance (reduce overhead).</li><li><strong>Mapping:</strong> Assigning groups to processors to balance load.</li></ul><h3>Case Study: The N-Body Problem</h3><p>This problem simulates gravitational forces between <em>n</em> particles.</p><ul><li><strong>Basic Algorithm:</strong> Every particle calculates force against every other particle (O(n²)). In MPI, this requires MPI_Allgather to share positions.</li><li><strong>Reduced Algorithm:</strong> Utilizes Newton’s 3rd Law to halve computation.</li><li><strong>Parallel Challenge:</strong> The Reduced algorithm creates write conflicts on the force array. In distributed memory, this is solved via a <strong>Ring-Pass</strong> communication pattern to circulate positions while accumulating forces, avoiding race conditions and minimizing bandwidth.</li></ul><h3>Case Study: Traveling Salesperson (TSP)</h3><p>Solved via Tree Search (Depth-First Search).</p><ul><li><strong>Static Partitioning:</strong> The root generates sub-trees and distributes them. This is prone to severe load imbalance (some sub-trees are pruned early).</li><li><strong>Dynamic Partitioning:</strong> Processes run out of work and request tasks from others.</li><li><strong>Termination Detection:</strong> In distributed dynamic TSP, processes must know when <em>everyone</em> is finished. A common solution involves passing a conserved “energy token”; when the root collects 100% of the tokens, the system is idle.</li></ul><figure><img alt="Visualizing the flow: Data synchronization in a parallel processing environment" src="https://cdn-images-1.medium.com/max/1024/1*c9bmWziCv2iMi-SEKKNbjA.png" /></figure><p>I hope this summary helps clarify the core concepts of Parallel Programming and serves as a useful reference for your studies or projects. If you found this guide helpful, feel free to leave a comment or connect with me. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f8786f1fdd7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>