Question: Consider the linked.c program. This program traverses a linked list computing a sequence of Fibonacci numbers at each node. 3.1 Parallelize this program using OpenMP

Consider the linked.c program. This program traverses a linked list computing a sequence of Fibonacci numbers at each node.

3.1Parallelize this program using OpenMP tasks.

3.2Parallelize this program using loop worksharing constructs.

Change the list size (i.e., N) and the number of threads, and report the execution times.

CODE BELOW:

#include

#include

#include "omp.h"

#ifndef N

#define N 5

#endif

#ifndef FS

#define FS 38

#endif

struct node {

int data;

int fibdata;

struct node* next;

};

int fib(int n) {

int x, y;

if (n

return (n);

} else {

x = fib(n - 1);

y = fib(n - 2);

return (x + y);

}

}

void processwork(struct node* p)

{

int n;

n = p->data;

p->fibdata = fib(n);

}

struct node* init_list(struct node* p) {

int i;

struct node* head = NULL;

struct node* temp = NULL;

head = malloc(sizeof(struct node));

p = head;

p->data = FS;

p->fibdata = 0;

for (i=0; i

temp = malloc(sizeof(struct node));

p->next = temp;

p = temp;

p->data = FS + i + 1;

p->fibdata = i+1;

}

p->next = NULL;

return head;

}

int main(int argc, char *argv[]) {

double start, end;

struct node *p=NULL;

struct node *temp=NULL;

struct node *head=NULL;

printf("Process linked list ");

printf(" Each linked list node will be processed by function 'processwork()' ");

printf(" Each ll node will compute %d fibonacci numbers beginning with %d ",N,FS);

p = init_list(p);

head = p;

start = omp_get_wtime();

{

while (p != NULL) {

processwork(p);

p = p->next;

}

}

end = omp_get_wtime();

p = head;

while (p != NULL) {

printf("%d : %d ",p->data, p->fibdata);

temp = p->next;

free (p);

p = temp;

}

free (p);

printf("Compute Time: %f seconds ", end - start);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!