Question: 5 - ( 6 marks ) Write a program that uses 8 threads to compute the sum of an array of 5 0 0 0

5-(6 marks) Write a program that uses 8 threads to compute the sum of an array of 5000 integers. The program should divide the set to 8 parts and give the 1st thread the first 625 integers , the 2nd thread the next 625 integers and so on. The 8 sums from these parts should then be added to give a total sum. The program should display this total sum.
800/8=100 array elements->1 to 8 threads (1st thread11 to 100 summed)
(2nd thread2101 to 200 sum)
3rd ,4th....8th)
The program should read the dataset of a 5000 integers from a file. This file name should be given in the command line (argc, argv).
In order to aid you in this question some parts of the program have already been provided.
The data set of a 5000 integers is already provided in a file called input.txt. In this file every integer is written on a single separate line.
Skeleton code to start has already been provided in the file project1q5s.c. In this file a function to get the filename from the command line and read the file has already been written. The function readfile() gets the filename, opens the file in read mode and copies its contents into an array of integers of size 5000. This array has been defined and set as a global variable.
A Debugging and testing has also been provided called testSum(). This function sequentially computes the sum of the 5000 integers in the array and displays the total sum. This function is present to test against the answer provided by using the 8 threads. Note : This function is only for testing , please do not use this function in the final output of the program.
Your job is now to add to this existing code the implementation using 6 pthreads.
Output of the program : The program should display the 8 sums computed by the threads and the total sum when these 8 sums are added together.
e.g of the run of the program and how the output should look like:
Before Parallelization,running the original sequential program, output is:
Gcc o project1q5s project1q5s.c
cse480@cse480-desktop:~$ ./project1q5s input.txt Reading file: input.txt
Reading file Complete, integers stored in array.
Testing without threads, Sum is : 170281
After Parallelization with Pthreads , output should be :
Thread 8 Sum:
22347
(1-100)Thread 6 Sum:
37524
(101-200)Thread 5 Sum:
31700
Thread 3 Sum:
14235
Thread 2 Sum:
13891
Thread 7 Sum:
14176
Thread 4 Sum:
21677
Thread 1 Sum:
14731
Total Sum is:
170281
6-(4 marks) Write a program that does as follows : Create an array of 500 integers (You are free to create this array however you like, hard code it in the code, read from a file , take it from the command line ). Create two threads that read this array. The 1st Thread should compute the Average of the 500 integers. The 2nd Thread should compute the Minimum value of the 500 integers. Display the Average and Minimum in the end.
Notes for all Programs :
-Provide the Source code .c files of all your programs
-Provide the output of the program in a text format.
/* CSE 480 : Project 1 : Question 5 This is the starting skeleton code. The function to read a file has already been given. Another Debug function called testSum has been given for testing, to check the value of the sum against the values the threads calculate. (This function is purely for testing and debugging only) Split the "array" of 1000 elements into 4 portions and give each portion to one thread. So Thread 1 will get 250 elements , Thread 2 the next 250 and so on. In the end when all four threads are done , add up their sums to get the final total sum of all 1000 elements. Print the sum. */ #include #include // The array that holds the data int array[1000]; void testSum(){ int sum=0; int j; for(j=0 ; j<1000 ; j++){ sum+=array[j]; } printf("Testing without threads, Sum is : %d
",sum); }// This function reads a file with 1000 integers, an integer is stored in // each line.The function stores the integers in the array void readfile(char* file_name){ char ch; FILE *fp; fp = fopen(file_name,"r"); // read mode if( fp == NULL ){ perror("Error while opening the file.
"); exit(EXIT_FAILURE); } char line [5]; /* line size */ int i=0; printf("Reading file: "); fputs(file_name,stdout); printf("
"); while ( fgets ( line, sizeof line, fp)!= NULL )/* read a line */{ if (i <1000){ array[i]=atoi(line); }//debug code //fputs ( line, stdout ); /* write the line */ i++; }// debug code //printf("i is : %d
",i); fclose(fp); printf("Reading file Complete, integers stored in array.
"); } int main(int argc, char* argv[]){ if (argc !=2){ fprintf(stderr,"usage: a.out
"); /*exit(1);*/ return -1; } readfile(argv[1]); //Debug code for testing only testSum(); 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!