Prerequisites (Conceptual)
Before understanding this program, students should know the following OpenMP concepts:
Static Scheduling
In static scheduling, loop iterations are divided into fixed chunks before execution begins.
These chunks are assigned to threads in a round-robin manner.
The assignment remains fixed throughout the execution, resulting in low scheduling overhead.
Chunk Size
A chunk is a group of consecutive loop iterations assigned to a thread.
In
schedule(static, 2), each chunk contains 2 iterations.Example (8 iterations):
Chunk 1 → Iterations 0, 1
Chunk 2 → Iterations 2, 3
Chunk 3 → Iterations 4, 5
Chunk 4 → Iterations 6, 7
Thread Assignment
Each thread executes the chunk assigned to it.
If there are more chunks than threads, chunks are assigned cyclically (round-robin).
Parallel for Loop
The
#pragma omp parallel fordirective divides the loop iterations among multiple threads so they execute simultaneously.
Example: schedule(static,2)
For 8 iterations and 2 threads:
| Chunk | Iterations | Assigned Thread |
|---|---|---|
| 1 | 0, 1 | Thread 0 |
| 2 | 2, 3 | Thread 1 |
| 3 | 4, 5 | Thread 0 |
| 4 | 6, 7 | Thread 1 |
For 9 iterations and 2 threads:
| Chunk | Iterations | Assigned Thread |
|---|---|---|
| 1 | 0, 1 | Thread 0 |
| 2 | 2, 3 | Thread 1 |
| 3 | 4, 5 | Thread 0 |
| 4 | 6, 7 | Thread 1 |
| 5 | 8 | Thread 0 |
Key Point: In static scheduling, the chunks are assigned before execution starts, and the assignment does not change during program execution. The output order may vary due to concurrent execution, but the thread-to-iteration assignment remains fixed.
Program 2: OpenMP Static Scheduling with Chunk Size = 2
Objective
To demonstrate static scheduling in OpenMP with a chunk size of 2, where loop iterations are divided into fixed chunks of two consecutive iterations and assigned to threads before execution.
Algorithm
Start.
Read the number of iterations n from the user.
Create a parallel region using OpenMP.
Apply
#pragma omp parallel for schedule(static,2)to the loop.Divide the loop iterations into chunks of 2.
Assign each chunk statically to available threads in a round-robin manner.
Each thread prints its thread ID and the iteration it executes.
Stop.
Program
#include <stdio.h>
#include <omp.h>
int main() {
int n;
printf("Enter number of iterations: ");
scanf("%d", &n);
#pragma omp parallel for schedule(static, 2)
for (int i = 0; i < n; i++) {
printf("Thread %d executes iteration %d\n",
omp_get_thread_num(), i);
}
return 0;
}
Sample Input
Enter number of iterations: 4
Sample Output (2 Threads)
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Note: The exact order of printed lines may vary because threads execute concurrently. However, with
schedule(static,2), iterations are assigned in fixed chunks of two. For example, with two threads:
Thread 0: Iterations 0–1
Thread 1: Iterations 2–3
If more iterations exist, the next chunks are assigned in a round-robin manner (e.g., Thread 0 gets 4–5, Thread 1 gets 6–7, and so on).
Result
The program successfully demonstrates OpenMP static scheduling with chunk size = 2, where consecutive groups of two iterations are statically distributed among the available threads before execution.
Since OpenMP executes threads concurrently, the assignment of iterations to threads remains fixed with schedule(static,2), but the order in which the printf() statements appear can vary.
Assume:
Number of iterations = 8
Number of threads = 2
Possible Output 1
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7
Possible Output 2
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7
Possible Output 3
Thread 0 executes iteration 0
Thread 1 executes iteration 2
Thread 0 executes iteration 1
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 1 executes iteration 6
Thread 0 executes iteration 5
Thread 1 executes iteration 7
If 4 Threads are Used (n = 8)
Each thread initially gets one chunk of 2 iterations.
Possible Output 1
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 2 executes iteration 4
Thread 2 executes iteration 5
Thread 3 executes iteration 6
Thread 3 executes iteration 7
Possible Output 2
Thread 2 executes iteration 4
Thread 2 executes iteration 5
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 3 executes iteration 6
Thread 3 executes iteration 7
Thread 1 executes iteration 2
Thread 1 executes iteration 3
If n = 10 and 2 Threads
The chunks are:
Chunk 1 → Iterations 0–1 → Thread 0
Chunk 2 → Iterations 2–3 → Thread 1
Chunk 3 → Iterations 4–5 → Thread 0
Chunk 4 → Iterations 6–7 → Thread 1
Chunk 5 → Iterations 8–9 → Thread 0
One possible output is:
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7
Thread 0 executes iteration 8
Thread 0 executes iteration 9
Important Note
With schedule(static,2):
The iteration-to-thread assignment is deterministic (fixed before execution).
Only the order of the printed output may vary because threads execute simultaneously. The thread assigned to a particular iteration will not change for a given number of threads and scheduling policy.
Yes. schedule(static,2) works for both even and odd numbers of iterations. The iterations are divided into chunks of 2, and if the total number of iterations is odd, the last chunk contains only one iteration.
Case 1: Even Number of Iterations (n = 8)
Chunks:
Chunk 1 → 0, 1
Chunk 2 → 2, 3
Chunk 3 → 4, 5
Chunk 4 → 6, 7
With 2 Threads
| Thread | Iterations |
|---|---|
| Thread 0 | 0, 1, 4, 5 |
| Thread 1 | 2, 3, 6, 7 |
Possible Output
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7
Case 2: Odd Number of Iterations (n = 9)
Chunks:
Chunk 1 → 0, 1
Chunk 2 → 2, 3
Chunk 3 → 4, 5
Chunk 4 → 6, 7
Chunk 5 → 8 (only one iteration)
With 2 Threads
| Thread | Iterations |
|---|---|
| Thread 0 | 0, 1, 4, 5, 8 |
| Thread 1 | 2, 3, 6, 7 |
Possible Output
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7
Thread 0 executes iteration 8
Another Odd Example (n = 7)
Chunks:
Chunk 1 → 0, 1
Chunk 2 → 2, 3
Chunk 3 → 4, 5
Chunk 4 → 6
With 2 Threads
| Thread | Iterations |
|---|---|
| Thread 0 | 0, 1, 4, 5 |
| Thread 1 | 2, 3, 6 |
Possible Output
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Conclusion
Even number of iterations: Every chunk contains exactly 2 iterations.
Odd number of iterations: The last chunk contains only 1 iteration.
The chunk-to-thread assignment is fixed with
schedule(static,2), while the order of the printed lines may vary because the threads execute concurrently.

No comments:
Post a Comment