Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming: Please help me edit the function given in the code below so that it creates only one new thread instead of two. We

C Programming: Please help me edit the function given in the code below so that it creates only one new thread instead of two. We are using Pthreads and a merge sort algorithm. We are to only modify the function pmerge_sort that is inside the file par_merge.c. We are not to modify any other file. Thank You!

These are the instructions:

image text in transcribed

par_merge.c :

/* * A parallel merge sort using pthreads */ #include  #include  #include  #include  #include  typedef struct sort_args_t { float *x; // array to be sorted int len; // length of array } Sort_args; // sort and return float array x, of length n // using gnome sort float *gsort(float *x, int n) { int i = 0; while (i = x[i-1]) { i++; } else { // swap x[i] and x[i-1] float temp = x[i]; x[i] = x[i-1]; x[i-1] = temp; i--; } } return x; } // return a new array obtained by merging arrays // x and y, where m is the length of x and n // is the length of y. The input arrays are // assumed to be sorted. If they are, the output // will be sorted. float *merge(float *x, int m, float *y, int n) { // x and y are merged into new array z float *z = (float *)malloc((m + n)*(sizeof(float))); int i = 0; // index into x int j = 0; // index into y int k = 0; // index into z while (i x; int n = sargs->len; // if n  

--------------------------------------------------------------

test_sort.c :

/* * Test a sort algorithm for correctness */ #include  #include  #include  #include  #include "merge_sort.h" // test merge sort int main() { // size of test data array int n = 10000; // initialize input float *x = (float *)malloc(n * sizeof(float)); int i; for (i = 0; i  

--------------------------------------------------------------

merge_sort.h (needed for test_sort.c file):

void merge_sort(float *, int);

--------------------------------------------------------------

Makefile :

test: pmsort ./pmsort pmsort: par_merge.c test_sort.c merge_sort.h gcc -o pmsort test_sort.c par_merge.c -pthread -Wall clean: rm -f pmsort *~
The files given below are a simple implementation of a parallel merge sort using pthreads. The implementation of the merge sort is in par_merge.c, and the code to test it is in test_sort.c. Look at the Makefile. If you type 'make', the code will be compiled and tested. Your job is to rewrite function 'pmerge_sort' in file par_merge.c. Please do the following Read the code to understand how it works. If you do not know how merge sort works, look online or in an algorithms book. It's an elegant recursive sorting algorithm and suitable for a concurrent implementations. 1. 2. Look at function pmerge_sort carefully. It creates two threads, each of which sorts half the data. Then the results from these two sorts are merged 3. Function pmerge_sort creates two threads, but this is wasteful, because pmerge_sort could sort half the data itself, and create only one new thread to sort the other half I want you to modify function pmerge_sort so that it creates only one new thread, not two. Do not modify any code except for pmerge_sort! Also, do not modify the arguments or return type of pmerge_sort. This code is simplistic in that it will create many threads if the input array is large. Don't try running your code on very large inputs. A more realistic algorithm would use the number of available cores as a parameter, and not create more threads than the number of cores. Your code must use threads as directed, and be correct. I may change test_sort.c, but I do not expect your code to work on arrays larger than the one used in the current version of test_sort.c. The files given below are a simple implementation of a parallel merge sort using pthreads. The implementation of the merge sort is in par_merge.c, and the code to test it is in test_sort.c. Look at the Makefile. If you type 'make', the code will be compiled and tested. Your job is to rewrite function 'pmerge_sort' in file par_merge.c. Please do the following Read the code to understand how it works. If you do not know how merge sort works, look online or in an algorithms book. It's an elegant recursive sorting algorithm and suitable for a concurrent implementations. 1. 2. Look at function pmerge_sort carefully. It creates two threads, each of which sorts half the data. Then the results from these two sorts are merged 3. Function pmerge_sort creates two threads, but this is wasteful, because pmerge_sort could sort half the data itself, and create only one new thread to sort the other half I want you to modify function pmerge_sort so that it creates only one new thread, not two. Do not modify any code except for pmerge_sort! Also, do not modify the arguments or return type of pmerge_sort. This code is simplistic in that it will create many threads if the input array is large. Don't try running your code on very large inputs. A more realistic algorithm would use the number of available cores as a parameter, and not create more threads than the number of cores. Your code must use threads as directed, and be correct. I may change test_sort.c, but I do not expect your code to work on arrays larger than the one used in the current version of test_sort.c

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions

Question

Design a health and safety policy.

Answered: 1 week ago