Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views5 pages

Parallel and Distributed Computing

The document presents a linear searching program implemented in C using OpenMP for parallel processing. It includes both serial and parallel linear search functions, allowing users to input an array and search for a target element while comparing execution times. The program also calculates and displays the speedup achieved through parallel execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Parallel and Distributed Computing

The document presents a linear searching program implemented in C using OpenMP for parallel processing. It includes both serial and parallel linear search functions, allowing users to input an array and search for a target element while comparing execution times. The program also calculates and displays the speedup achieved through parallel execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PARALLEL AND DISTRIBUTED

COMPUTING

ASSIGNMENT

Batch_05:

221FA04625-M.Shanmukha Priya

Submitted to :
SD.Shareefunnisa Mam
Linear searching program in openmp
Source Code:-
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <omp.h>

// Serial Linear Search

int linearSearchSerial(int arr[], int size, int target) {

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

if (arr[i] == target) {

return i;

return -1;

// Parallel Linear Search using OpenMP

int linearSearchParallel(int arr[], int size, int target, int num_threads) {

int index = -1;

#pragma omp parallel num_threads(num_threads)

int local_index = -1;

#pragma omp for

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

if (arr[i] == target && local_index == -1) {

local_index = i;

#pragma omp critical

{
if (index == -1 || (local_index != -1 && local_index < index)) {

index = local_index;

return index;

int main() {

int size;

printf("Enter the number of elements: \n");

scanf("%d", &size);

int arr[size];

printf("Enter %d elements:\n", size);

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

scanf("%d", &arr[i]);

int target, num_threads;

printf("Enter target element: \n");

scanf("%d", &target);

printf("Enter number of threads to use: \n");

scanf("%d", &num_threads);

// Serial Execution Time

clock_t start = clock();

int serialIndex = linearSearchSerial(arr, size, target);

clock_t stop = clock();

double serialDuration = ((double)(stop - start)) / CLOCKS_PER_SEC;

// Parallel Execution Time


start = clock();

int parallelIndex = linearSearchParallel(arr, size, target, num_threads);

stop = clock();

double parallelDuration = ((double)(stop - start)) / CLOCKS_PER_SEC;

// Output Results

if (serialIndex != -1) {

printf("Element found at index %d in serial search.\n", serialIndex);

} else {

printf("Element not found in serial search.\n");

if (parallelIndex != -1) {

printf("Element found at index %d in parallel search.\n", parallelIndex);

} else {

printf("Element not found in parallel search.\n");

printf("Serial Execution Time: %f seconds\n", serialDuration);

printf("Parallel Execution Time: %f seconds\n", parallelDuration);

printf("Speedup: %f\n", serialDuration / parallelDuration);

return 0;

}
OUTPUT:-

You might also like