0% found this document useful (0 votes)
9 views

20bce2126 PDC Lab Da 3

The document describes three OpenMP programs: 1. Linear and binary search algorithms parallelized with sections clauses. 2. Matrix-vector addition and multiplication algorithms parallelized with sections clauses. 3. A producer-consumer problem solved with critical sections to synchronize access to a shared buffer.

Uploaded by

vedantmodi202013
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)
9 views

20bce2126 PDC Lab Da 3

The document describes three OpenMP programs: 1. Linear and binary search algorithms parallelized with sections clauses. 2. Matrix-vector addition and multiplication algorithms parallelized with sections clauses. 3. A producer-consumer problem solved with critical sections to synchronize access to a shared buffer.

Uploaded by

vedantmodi202013
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/ 11

Vedant Modi

20BCE2126
PDC lab DA-3

1. Develop an OpenMp program to perform Linear Search and Binary


search using sections clauses

Algorithm:

Linear Search Algorithm:

1. Initialize result to -1 to indicate that the target value has not


been found yet.
2. Iterate through each element in the array sequentially.
For each element, compare it with the target value.
3. If the element matches the target value and result is still -1,
update result with the current index.
4. Return result as the final result.

Binary Search Algorithm:

1. Initialize result to -1 to indicate that the target value has not


been found yet.
2. Set left to the index of the first element in the array and
right to the index of the last element in the array.
3. Repeat the following until left is less than or equal to right
and result is still -1:
a. Calculate the middle index as mid using (left + right) / 2.
b. Compare the element at index mid with the target value.
c. If they match, update result with mid.
d. If the element at index mid is less than the target value,
set left to mid + 1.
e. If the element at index mid is greater than the target
value, set right to mid - 1.
4. Return result as the final result.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define ARRAY_SIZE 1000000


#define SEARCH_VALUE 500000

int linear_search(int *array, int size, int target)


{
int result = -1;

#pragma omp parallel for


for (int i = 0; i < size; i++)
{
if (array[i] == target)
{
#pragma omp critical
{
if (result == -1)
{
result = i;
}
}
}
}

return result;
}

int binary_search(int *array, int size, int target)


{
int result = -1;

#pragma omp parallel


{
int left = 0, right = size - 1;
int local_result = -1;

while (left <= right && local_result == -1)


{
int mid = left + (right - left) / 2;
if (array[mid] == target)
{
local_result = mid;
}

if (array[mid] < target)


{
left = mid + 1;
}
else
{
right = mid - 1;
}
}

#pragma omp critical


{
if (local_result != -1 && result == -1)
{
result = local_result;
}
}
}

return result;
}

int main()
{
int array[ARRAY_SIZE];

for (int i = 0; i < ARRAY_SIZE; i++)


{
array[i] = i + 1;
}

double linear_start, linear_end, binary_start, binary_end;

linear_start = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single nowait
{
int linear_result = linear_search(array, ARRAY_SIZE, SEARCH_VALUE);
if (linear_result != -1)
{
printf("Linear search found %d at index %d\n", SEARCH_VALUE, linear_result);
}
else
{
printf("Linear search did not find %d\n", SEARCH_VALUE);
}
}
}
linear_end = omp_get_wtime();

binary_start = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single nowait
{
int binary_result = binary_search(array, ARRAY_SIZE, SEARCH_VALUE);
if (binary_result != -1)
{
printf("Binary search found %d at index %d\n", SEARCH_VALUE, binary_result);
}
else
{
printf("Binary search did not find %d\n", SEARCH_VALUE);
}
}
}
binary_end = omp_get_wtime();

printf("Time taken by linear search: %f seconds\n", linear_end -


linear_start);
printf("Time taken by binary search: %f seconds\n", binary_end -
binary_start);

return 0;
}

Output:
2. Develop an OpenMP program to perform two-dimensional matrix
vector addition and multiplication using sections clauses

Algorithm:
Matrix-Vector Addition Algorithm:

1. Initialize a result vector, result, of size N to all zeros.


2. Iterate over each row (i) of the matrix.
3. For each row, iterate over each column (j) of the matrix.
4. Update result[i] by adding matrix[i][j] * vector[j].
5. After the loops, the result vector contains the result of the
matrix-vector addition.

Matrix-Vector Multiplication Algorithm:

1. Initialize a result vector, result, of size N to all zeros.


2. Iterate over each row (i) of the matrix.
3. For each row, iterate over each column (j) of the matrix.
4. Update result[i] by adding matrix[i][j] * vector[j].
5. After the loops, the result vector contains the result of the
matrix-vector multiplication.

Code:
#include <stdio.h>
#include <omp.h>

#define N 4
void matrixVectorAddition(double matrix[][N], double vector[], double
result[]) {
int i, j;

#pragma omp parallel for shared(matrix, vector, result) private(i, j)


for (i = 0; i < N; i++) {
result[i] = 0.0;
for (j = 0; j < N; j++) {
result[i] += matrix[i][j] * vector[j];
}
}
}

void matrixVectorMultiplication(double matrix[][N], double vector[], double


result[]) {
int i, j;

#pragma omp parallel for shared(matrix, vector, result) private(i, j)


for (i = 0; i < N; i++) {
result[i] = 0.0;
for (j = 0; j < N; j++) {
result[i] += matrix[i][j] * vector[j];
}
}
}

int main() {
double matrix[N][N];
double vector[N];
double result[N];

for (int i = 0; i < N; i++) {


vector[i] = 1.0;
for (int j = 0; j < N; j++) {
matrix[i][j] = 1.0;
}
}

#pragma omp parallel sections


{
#pragma omp section
{
printf("Matrix-Vector Addition:\n");
matrixVectorAddition(matrix, vector, result);
for (int i = 0; i < N; i++) {
printf("%f ", result[i]);
}
printf("\n");
}

#pragma omp section


{
printf("Matrix-Vector Multiplication:\n");
matrixVectorMultiplication(matrix, vector, result);
for (int i = 0; i < N; i++) {
printf("%f ", result[i]);
}
printf("\n");
}
}

return 0;
}

Output:

3. Develop an OpenMp program to solve Producer and Consumer


problem.

Algorithm:
Producer Algorithm:

1. Initialize an infinite loop to continuously produce items.


2. Generate a random item to be added to the buffer.
3. Enter a critical section to check if there is space in the
buffer (itemCount < BUFFER_SIZE).
4. If there is space in the buffer, add the item to the buffer,
update the buffer index (in), and increment itemCount.
5. Print a message indicating that an item has been produced.
6. Exit the critical section.
7. Sleep for a while to simulate work or a delay.

Consumer Algorithm:

1. Initialize an infinite loop to continuously consume items.


2. Enter a critical section to check if there are items in the
buffer (itemCount > 0).
3. If there are items in the buffer, remove an item from the
buffer, update the buffer index (out), and decrement
itemCount.
4. Print a message indicating that an item has been
consumed.
5. Exit the critical section.
6. Sleep for a while to simulate work or a delay.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <unistd.h>

#define BUFFER_SIZE 5
#define NUM_PRODUCERS 2
#define NUM_CONSUMERS 2
#define NUM_ITEMS 10

int buffer[BUFFER_SIZE];
int itemCount = 0;
int in = 0;
int out = 0;

void producer() {
int item;
while (1) {
item = rand() % 100;

#pragma omp critical


{
if (itemCount < BUFFER_SIZE) {
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
itemCount++;
printf("Produced: %d (Items in buffer: %d)\n", item, itemCount);
}
}

usleep(100000);
}
}

void consumer() {
int item;
while (1) {
#pragma omp critical
{
if (itemCount > 0) {
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
itemCount--;
printf("Consumed: %d (Items in buffer: %d)\n", item, itemCount);
}
}

usleep(150000);
}
}

int main() {
omp_set_num_threads(NUM_PRODUCERS + NUM_CONSUMERS);

#pragma omp parallel


{
int id = omp_get_thread_num();

if (id < NUM_PRODUCERS) {


producer();
} else {
consumer();
}
}

return 0;
}
Output:

You might also like