0% found this document useful (0 votes)
18 views4 pages

Abd-ur-Rehman - PND Assignment 2

The document outlines an assignment involving multiple tasks that utilize OpenMP for parallel programming in C. It includes tasks for thread management, array summation with and without reduction, matrix addition and multiplication, and calculating the Fibonacci sequence. Each task is accompanied by code snippets, execution results, and performance measurements.

Uploaded by

AbdurRehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Abd-ur-Rehman - PND Assignment 2

The document outlines an assignment involving multiple tasks that utilize OpenMP for parallel programming in C. It includes tasks for thread management, array summation with and without reduction, matrix addition and multiplication, and calculating the Fibonacci sequence. Each task is accompanied by code snippets, execution results, and performance measurements.

Uploaded by

AbdurRehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

12/29/24, 3:29 PM 210312 Abdur Rehman Assignment 02 - Colab

Task 1:

%%writefile task_1.c
#include <stdio.h>
#include <omp.h>

int main() {
// Set the number of threads to 4
omp_set_num_threads(4);

#pragma omp parallel


{
int thread_id = omp_get_thread_num();
int total_threads = omp_get_num_threads();

printf("Thread ID: %d, Total Threads: %d\n", thread_id, total_threads);


}

return 0;
}

Writing task_1.c

!gcc -fopenmp -o task_1 task_1.c


!./task_1

Thread ID: 1, Total Threads: 4


Thread ID: 0, Total Threads: 4
Thread ID: 3, Total Threads: 4
Thread ID: 2, Total Threads: 4

Task 2:

%%writefile task_2.c
#include <stdio.h>
#include <omp.h>

#define ARRAY_SIZE 10000

int main() {
int array[ARRAY_SIZE];
long sum_without_reduction = 0;
long sum_with_reduction = 0;

// Initialize the array with values from 1 to ARRAY_SIZE


for (int i = 0; i < ARRAY_SIZE; i++) {
array[i] = i + 1;
}

// Summation without reduction


#pragma omp parallel for
for (int i = 0; i < ARRAY_SIZE; i++) {
// This may lead to a race condition
sum_without_reduction += array[i];
}

// Summation with reduction


#pragma omp parallel for reduction(+:sum_with_reduction)
for (int i = 0; i < ARRAY_SIZE; i++) {
sum_with_reduction += array[i];
}

// Display the results


printf("Expected sum: %ld\n", (long)ARRAY_SIZE * (ARRAY_SIZE + 1) / 2);
printf("Sum without reduction (may be incorrect due to race conditions): %ld\n", sum_without_reduction);
printf("Sum with reduction: %ld\n", sum_with_reduction);

return 0;
}

Overwriting task_2.c

https://colab.research.google.com/drive/1tlcKemgly13iFAJxT-CuC0cm6g2rCLMY#scrollTo=ZX_tnvtn9A6p&printMode=true 1/4
12/29/24, 3:29 PM 210312 Abdur Rehman Assignment 02 - Colab

!gcc -fopenmp -o task_2 task_2.c


!./task_2

Expected sum: 50005000


Sum without reduction (may be incorrect due to race conditions): 36934234
Sum with reduction: 50005000

Task 3:

%%writefile task_3.c
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define SIZE 500

int main() {
int matrixA[SIZE][SIZE], matrixB[SIZE][SIZE], result[SIZE][SIZE];

// Seed the random number generator


srand(42);

// Initialize matrices A and B with random integers


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
matrixA[i][j] = rand() % 100; // Random integers between 0 and 99
matrixB[i][j] = rand() % 100;
}
}

// Perform matrix addition using OpenMP


#pragma omp parallel for collapse(2)
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
result[i][j] = matrixA[i][j] + matrixB[i][j];
}
}

// Print the first row of the resulting matrix


printf("First row of the resulting matrix:\n");
for (int j = 0; j < SIZE; j++) {
printf("%d ", result[0][j]);
}
printf("\n");

return 0;
}

Writing task_3.c

!gcc -fopenmp -o task_3 task_3.c


!./task_3

First row of the resulting matrix:


106 122 70 61 78 117 21 158 140 157 119 42 106 73 117 40 30 80 45 81 112 89 102 140 47 73 63 83 120 171 70 165 82 110 43 105 49 145 99 9

Task 4:

%%writefile task_4.c
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define SIZE 500

int main() {
int matrixA[SIZE][SIZE], matrixB[SIZE][SIZE], result[SIZE][SIZE];
double start_time, end_time;

https://colab.research.google.com/drive/1tlcKemgly13iFAJxT-CuC0cm6g2rCLMY#scrollTo=ZX_tnvtn9A6p&printMode=true 2/4
12/29/24, 3:29 PM 210312 Abdur Rehman Assignment 02 - Colab
// Seed the random number generator
srand(42);

// Initialize matrices A and B with random integers


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
matrixA[i][j] = rand() % 100; // Random integers between 0 and 99
matrixB[i][j] = rand() % 100;
}
}

// Measure performance for thread counts 1, 2, 4, and 8


for (int threads = 1; threads <= 8; threads *= 2) {
// Set the number of threads
omp_set_num_threads(threads);

// Start timing
start_time = omp_get_wtime();

// Perform matrix multiplication


#pragma omp parallel for
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
result[i][j] = 0;
for (int k = 0; k < SIZE; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}

// End timing
end_time = omp_get_wtime();

// Print the execution time


printf("Threads: %d, Execution Time: %f seconds\n", threads, end_time - start_time);
}

// Verify correctness by printing the first element of the result matrix


printf("First element of result matrix: %d\n", result[0][0]);

return 0;
}

Writing task_4.c

!gcc -fopenmp -o task_4 task_4.c


!./task_4

Threads: 1, Execution Time: 0.822809 seconds


Threads: 2, Execution Time: 0.704161 seconds
Threads: 4, Execution Time: 0.727977 seconds
Threads: 8, Execution Time: 0.726445 seconds
First element of result matrix: 1198954

Task 5:

%%writefile task_5.c
#include <stdio.h>
#include <omp.h>

#define N 30

int main() {
long fibonacci[N + 1];

// Initialize the first two Fibonacci numbers


fibonacci[0] = 0;
fibonacci[1] = 1;

// Compute the Fibonacci sequence using OpenMP


#pragma omp parallel for ordered
for (int i = 2; i <= N; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];

https://colab.research.google.com/drive/1tlcKemgly13iFAJxT-CuC0cm6g2rCLMY#scrollTo=ZX_tnvtn9A6p&printMode=true 3/4
12/29/24, 3:29 PM 210312 Abdur Rehman Assignment 02 - Colab
// Use ordered to ensure computations are handled sequentially
#pragma omp ordered
printf("Fibonacci[%d] = %ld\n", i, fibonacci[i]);
}

// Print the complete Fibonacci sequence


printf("\nComplete Fibonacci sequence up to N=%d:\n", N);
for (int i = 0; i <= N; i++) {
printf("%ld ", fibonacci[i]);
}
printf("\n");

return 0;
}

Writing task_5.c

!gcc -fopenmp -o task_5 task_5.c


!./task_5

Fibonacci[2] = 1
Fibonacci[3] = 2
Fibonacci[4] = 3
Fibonacci[5] = 5
Fibonacci[6] = 8
Fibonacci[7] = 13
Fibonacci[8] = 21
Fibonacci[9] = 34
Fibonacci[10] = 55
Fibonacci[11] = 89
Fibonacci[12] = 144
Fibonacci[13] = 233
Fibonacci[14] = 377
Fibonacci[15] = 610
Fibonacci[16] = 987
Fibonacci[17] = 0
Fibonacci[18] = 987
Fibonacci[19] = 987
Fibonacci[20] = 1974
Fibonacci[21] = 2961
Fibonacci[22] = 4935
Fibonacci[23] = 7896
Fibonacci[24] = 12831
Fibonacci[25] = 20727
Fibonacci[26] = 33558
Fibonacci[27] = 54285
Fibonacci[28] = 87843
Fibonacci[29] = 142128
Fibonacci[30] = 229971

Complete Fibonacci sequence up to N=30:


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 0 987 987 1974 2961 4935 7896 12831 20727 33558 54285 87843 142128 229971

Start coding or generate with AI.

https://colab.research.google.com/drive/1tlcKemgly13iFAJxT-CuC0cm6g2rCLMY#scrollTo=ZX_tnvtn9A6p&printMode=true 4/4

You might also like