All programs
PC - Lab Manual (Simplified) · BCS7O2 C

Problem Statement Program 2

Write an OpenMP program that divides the iterations into chunks containing 2 iterations respectively (OMP_SCHEDULE=static,2). Its input should be the number of iterations, and its output should display which iterations of a parallelized for loop are executed by which thread.

For example, if there are two threads and four iterations, the output might be:

Thread 0 : Iterations 0 – 1
Thread 1 : Iterations 2 – 3

C Code Source

program2.c
#include <stdio.h>
#include <omp.h>

int main() {
    int n;

    printf("Enter number of iterations: ");
    scanf("%d", &n);

    #pragma omp parallel
    {
        int tid = omp_get_thread_num();

        #pragma omp for schedule(static,2)
        for (int i = 0; i < n; i++) {
            printf("Thread %d executes iteration %d\n", tid, i);
        }
    }

    return 0;
}

Output Console

Output
Enter number of iterations: 4

Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Protected content · © VTU Developer · Unauthorised distribution is prohibited