Write Prefix sum using MPI and PThreads You can use your cod

Write Prefix sum using MPI and PThreads You can use your code from Assignment 1 Modify it to work in serial at the thread level Write the MPI code to launch and communicate between these PThread running processes Analyze Test to find the fastest combination of processors and threads Use atleast 4 processes and atleast 8 threads Rum for different sized inputs Deliver Source code for parallel version Graph Running time vs input size

Solution

#include <stdio.h>
#include <pthread.h>
#define array_size 1000
#define no_threads 10

int a[array_size];
int global_index = 0;
int sum = 0;
pthread mutex_t mutex1;

void *slave(void *ignored)
{
int local_index, partial_sum = 0;
do {
pthread mutex lock(&mutex1);
local_index = global_index;
global index++;
pthread mutex unlock(&mutex1);

if (local_index < array_size)
partial_sum += *(a + local_index);
} while (local_index < array_size);

pthread mutex lock(&mutex1);
sum += partial_sum;
pthread mutex unlock(&mutex1);

return();
}

main()
{
int i;
pthread_t thread[10];
pthread_mutex_init(&mutex1, NULL);

for (i = 0; i < array_size; i++)
a[i] = i+1;

for (i = 0; i < no_threads; i++)
if (pthread create(&thread[i], NULL, slave, NULL) != 0)
perror(\"Pthread create fails\");

for (i = 0; i < no_threads; i++)
if (pthread join(thread[i], NULL) != 0)
perror(\"Pthread join fails\");

printf(\"The sum of 1 to %i is %d\ \", array_size, sum);
}

 Write Prefix sum using MPI and PThreads You can use your code from Assignment 1 Modify it to work in serial at the thread level Write the MPI code to launch an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site