Question

In: Computer Science

Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal...

Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal fractions given the number of threads and perform Parallel MergeSort pThreads. Your algorithm should work for 2 threads.

ParallelMergeSort.c

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define SIZE 100

int array[SIZE];

void fillArrayWithRandomNumbers(int arr[SIZE]) {
for(int i = 0; i<SIZE; i++) array[i] = rand()%100;
}

void printArray(int arr[SIZE]) {
for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]);
printf("\n");
}

typedef struct StartEndIndexes {
int start;
int end;
} StartEndIndexes;

// Runs mergesort on the array segment described in the
// argument. Spawns two threads to mergesort each half
// of the array segment, and then merges the results.
void* mergeSort(void* args) {
return NULL;
}

int main() {
srand(time(0));
StartEndIndexes sei;
sei.start = 0;
sei.end = SIZE - 1;
  
// 1. Fill array with random numbers.
fillArrayWithRandomNumbers(array);
  
// 2. Print the array.
printf("Unsorted array: ");
printArray(array);
  
// 3. Create a 2 threads for merge sort.
  
// 4. Wait for mergesort to finish.
  
// 5. Print the sorted array.
printf("Sorted array: ");
printArray(array);
}

Makefile

mergeSort: ParallelMergeSort.c
   gcc -std=c99 -pthread -o ParallelMergeSort ParallelMergeSort.c -I.

Solutions

Expert Solution

Answer: Note: // & /* */ - indicates comment lines. To explain in detail, comment lines included.

ParallelMergeSort.c

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define SIZE 100

int array[SIZE];

void fillArrayWithRandomNumbers(int arr[SIZE]) {
for(int i = 0; i<SIZE; i++) array[i] = rand()%100;
}

void printArray(int arr[SIZE]) {
for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]);
printf("\n");
}

typedef struct StartEndIndexes {
int start;
int end;
} StartEndIndexes;

// Runs mergesort on the array segment described in the
// argument. Spawns two threads to mergesort each half
// of the array segment, and then merges the results.
void* mergeSort(void* args) {

return NULL;
}

int main() {
srand(time(0));
StartEndIndexes sei;
sei.start = 0;
sei.end = SIZE - 1;
  
// 1. Fill array with random numbers.
fillArrayWithRandomNumbers(array);
  
// 2. Print the array.
printf("Unsorted array: ");
printArray(array);
  
// 3. Create a 2 threads for merge sort.

// p1 & p2 are thread ids

pthread_t p1, p2;

int retval;

/* create thread 1 - in the following function call - 4th argument is one (1) to indicate that the first thread should sort elements from 0 to 50. */

retval = pthread_create(&p1, NULL, &mergeSort, 1);

if (retval != 0)

{

printf("Error in thread1 creation\n");

return -1;

}

/* create thread 2 - in the following function call - 4th argument is two (2) to indicate that the first thread should sort elements from 50 to 100. */

retval = pthread_create(&p2, NULL, &mergeSort, 2);

if (retval != 0)

{

printf("Error in thread2 creation\n");

return -1;

}

// 4. Wait for mergesort to finish.

// Wait for first thread to finish mergesort
retval = pthread_join(p1, NULL);

if (retval != 0)

{

printf("Error in thread1 join\n");

return -1;

}

// Wait for second thread to finish mergesor

int retval1 = pthread_join(p2, NULL);

if (retval1 != 0)

{

printf("Error in thread2 join\n");

return -1;

}

// thread exit

pthread_exit(&retval);

// thread exit

pthread_exit(&retval1);
// 5. Print the sorted array.
printf("Sorted array: ");
printArray(array);
}


Related Solutions

Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal fractions given the number of threads and perform Parallel MergeSort pThreads. Your algorithm should work for 2 threads. ParallelMergeSort.c #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #define SIZE 100 int array[SIZE]; void fillArrayWithRandomNumbers(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) array[i] = rand()%100; } void printArray(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]); printf("\n"); } typedef struct...
Given an array ? of ? integers. Divide ? into three subsets such that the total...
Given an array ? of ? integers. Divide ? into three subsets such that the total absolute difference of subset sums between them is minimum. Provide python source code, time complexity, and pseudocode. thank you
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Write recursive method to return true if a given array has element equal to employee emp,...
Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns...
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
Write a method called splitArray which will receive four parameters as follows: Original array Array of...
Write a method called splitArray which will receive four parameters as follows: Original array Array of multiples Array of not multiples Number to compare if the values within the original array are multiples of Assuming that always the original array will contain half values as multiples and half as not multiples. Your method should return the numbers within the original array that are multiples of the last parameter in the array of multiples (second parameter). Furthermore, your method should return...
Write a program using multiple functions. Make use of an array to store data Make use...
Write a program using multiple functions. Make use of an array to store data Make use of searching techniques Read data from a file Write data to a file Instructions: In this lab, you will be examining a set of stock collected over a twenty four day period. Be sure to make use of an array to store these stocks. You will be required to read in the data points from a file. Write a function to read in the...
Given an array, write code to scan the array for a particular purpose. Given a class,...
Given an array, write code to scan the array for a particular purpose. Given a class, write a getter and/or a setter. Given a class with a list, write code that manages the list.
Given an array of integers, and given a specific value k (not equal to 0), produce...
Given an array of integers, and given a specific value k (not equal to 0), produce all unique pairs of values in the array which differ by k. For example, if the array has [1,4,9,12, 6, 15, 5, 13,17] and k=3, the answer would be (1,4 ) ( 9,12), ( 9,6), (12,15). If k=4, the answer would be (1,5), (9,5), (13,17), (9.13). Notice that you do not print the same answer twice, for instance (9,13), and (13,9).
Sorry now I want this question to be answered Write a group of template functions for...
Sorry now I want this question to be answered Write a group of template functions for examining and manipulating a collection of items via the collection’s forward iterator. For example, one of the functions might have this prototype: template <class Iterator, class T> Iterator find( Iterator begin, Iterator end, const T& target ); The find function searches the range starting at *begin and going up to (but not including) *end. If one of these elements is equal to the target,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT