Question: I will post the codes to work with at the end of the questions. With C programinmg to work on the questions thanks! sumMutex1.c /*

I will post the codes to work with at the end of the questions. With C programinmg to work on the questions thanks!

I will post the codes to work with at the end of

the questions. With C programinmg to work on the questions thanks! sumMutex1.c

/* Programmer: File: sumMutex1.c Compile As: gcc -o sumArray -O3 sumMutex1.c -lpthread

Run as: ./sumArray Description: A parallel C solution to sum an array

sumMutex1.c

/* Programmer:

File: sumMutex1.c

Compile As: gcc -o sumArray -O3 sumMutex1.c -lpthread

Run as: ./sumArray

Description: A parallel C solution to sum an array of floats

using pthreads. CORRECT solution that uses a mutex

to update the globalSum.

*/

#include

#include

#include

#include

#include

// Prototypes

void * threadPartialSum(void * args);

// GLOBAL variables

double globalSum;

int numberOfThreads;

long length;

float * myArray;

pthread_mutex_t updateSumLock;

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

long i;

pthread_t * threadHandles;

int errorCode;

double seqSum;

long startTime, endTime, seqTime, parallelTime;

if (argc != 3) {

printf("Usage: %s ", argv[0]);

return(0);

};

sscanf(argv[1],"%d",&length);

sscanf(argv[2],"%d",&numberOfThreads);

// Generate arrays for threads handles

threadHandles = (pthread_t *) malloc(numberOfThreads*sizeof(pthread_t));

// Generate data array

myArray=(float *) malloc(length*sizeof(float));

srand(5);

for (i=0; i

myArray[i] = rand() / (float) RAND_MAX;

} // end for i

time(&startTime);

seqSum = 0.0;

for (i=0; i

seqSum += myArray[i];

} // end for i

time(&endTime);

seqTime = endTime - startTime;

time(&startTime);

pthread_mutex_init(&updateSumLock, NULL);

globalSum = 0.0;

for (i=0; i

if (errorCode = pthread_create(&threadHandles[i], NULL, threadPartialSum, (void *) i) != 0) {

printf("pthread %d failed to be created with error code %d ", i, errorCode);

} // end if

} // end for

for (i=0; i

if (errorCode = pthread_join(threadHandles[i], (void **) NULL) != 0) {

printf("pthread %d failed to be joined with error code %d ", i, errorCode);

} // end if

} // end for

time(&endTime);

parallelTime = endTime - startTime;

printf( "Time to sum %ld floats using %d threads %ld seconds (seq. %ld seconds) ", length, numberOfThreads,

parallelTime, seqTime);

printf("Thread's Sum is %lf and seq. sum %lf ", globalSum, seqSum);

free(myArray);

return 0;

} /* end main */

void * threadPartialSum(void * rank) {

long myRank = (long) rank;

long i, blockSize;

long firstIndex, lastIndex;

blockSize = length / numberOfThreads;

firstIndex = blockSize * myRank;

if (myRank == numberOfThreads-1) { // last thread gets the rest

lastIndex = length-1;

} else {

lastIndex = blockSize * (myRank+1) - 1;

} // end if

for (i=firstIndex; i

pthread_mutex_lock(&updateSumLock);

globalSum += myArray[i];

pthread_mutex_unlock(&updateSumLock);

} /* end for (i */

return NULL;

} // end threadPartialSum

sumMutex2.c

/* Programmer:

File: sumMutex2.c

Compile As: gcc -o sumArray -O3 sumMutex2.c -lpthread

Run as: ./sumArray

Description: A parallel C solution to sum an array of floats

using pthreads. CORRECT solution that uses a mutex

to update the globalSum once using a localSum.

*/

#include

#include

#include

#include

#include

// Prototypes

void * threadPartialSum(void * args);

// GLOBAL variables

double globalSum;

int numberOfThreads;

long length;

float * myArray;

pthread_mutex_t updateSumLock;

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

long i;

pthread_t * threadHandles;

int errorCode;

double seqSum;

long startTime, endTime, seqTime, parallelTime;

if (argc != 3) {

printf("Usage: %s ", argv[0]);

return(0);

};

sscanf(argv[1],"%d",&length);

sscanf(argv[2],"%d",&numberOfThreads);

// Generate arrays for threads handles

threadHandles = (pthread_t *) malloc(numberOfThreads*sizeof(pthread_t));

// Generate data array

myArray=(float *) malloc(length*sizeof(float));

srand(5);

for (i=0; i

myArray[i] = rand() / (float) RAND_MAX;

} // end for i

time(&startTime);

seqSum = 0.0;

for (i=0; i

seqSum += myArray[i];

} // end for i

time(&endTime);

seqTime = endTime - startTime;

time(&startTime);

pthread_mutex_init(&updateSumLock, NULL);

globalSum = 0.0;

for (i=0; i

if (errorCode = pthread_create(&threadHandles[i], NULL, threadPartialSum, (void *) i) != 0) {

printf("pthread %d failed to be created with error code %d ", i, errorCode);

} // end if

} // end for

for (i=0; i

if (errorCode = pthread_join(threadHandles[i], (void **) NULL) != 0) {

printf("pthread %d failed to be joined with error code %d ", i, errorCode);

} // end if

} // end for

time(&endTime);

parallelTime = endTime - startTime;

printf( "Time to sum %ld floats using %d threads %ld seconds (seq. %ld seconds) ", length, numberOfThreads,

parallelTime, seqTime);

printf("Thread's Sum is %lf and seq. sum %lf ",globalSum, seqSum);

free(myArray);

return 0;

} /* end main */

void * threadPartialSum(void * rank) {

long myRank = (long) rank;

long i, blockSize;

long firstIndex, lastIndex;

double localSum;

blockSize = length / numberOfThreads;

firstIndex = blockSize * myRank;

if (myRank == numberOfThreads-1) { // last thread gets the rest

lastIndex = length-1;

} else {

lastIndex = blockSize * (myRank+1) - 1;

} // end if

localSum = 0.0;

for (i=firstIndex; i

localSum += myArray[i];

} /* end for (i */

pthread_mutex_lock(&updateSumLock);

globalSum += localSum;

pthread_mutex_unlock(&updateSumLock);

return NULL;

} // end threadPartialSum

sumMutex2cyclic.c

/* Programmer:

File: sumMutex3.c

Compile As: gcc -o sumArray -O3 sumMutex3.c -lpthread

Run as: ./sumArray

Description: A parallel C solution to sum an array of floats

using pthreads. CORRECT solution that uses a mutex

to update the globalSum once using a localSum.

*/

#include

#include

#include

#include

#include

// Prototypes

void * threadPartialSum(void * args);

// GLOBAL variables

double globalSum;

int numberOfThreads;

long length;

float * myArray;

pthread_mutex_t updateSumLock;

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

long i;

pthread_t * threadHandles;

int errorCode;

double seqSum;

long startTime, endTime, seqTime, parallelTime;

if (argc != 3) {

printf("Usage: %s ", argv[0]);

return(0);

};

sscanf(argv[1],"%d",&length);

sscanf(argv[2],"%d",&numberOfThreads);

// Generate arrays for threads handles

threadHandles = (pthread_t *) malloc(numberOfThreads*sizeof(pthread_t));

// Generate data array

myArray=(float *) malloc(length*sizeof(float));

srand(5);

for (i=0; i

myArray[i] = rand() / (float) RAND_MAX;

} // end for i

time(&startTime);

seqSum = 0.0;

for (i=0; i

seqSum += myArray[i];

} // end for i

time(&endTime);

seqTime = endTime - startTime;

time(&startTime);

pthread_mutex_init(&updateSumLock, NULL);

globalSum = 0.0;

for (i=0; i

if (errorCode = pthread_create(&threadHandles[i], NULL, threadPartialSum, (void *) i) != 0) {

printf("pthread %d failed to be created with error code %d ", i, errorCode);

} // end if

} // end for

for (i=0; i

if (errorCode = pthread_join(threadHandles[i], (void **) NULL) != 0) {

printf("pthread %d failed to be joined with error code %d ", i, errorCode);

} // end if

} // end for

time(&endTime);

parallelTime = endTime - startTime;

printf( "Time to sum %ld floats using %d threads %ld seconds (seq. %ld seconds) ", length, numberOfThreads,

parallelTime, seqTime);

printf("Thread's Sum is %lf and seq. sum %lf ",globalSum, seqSum);

free(myArray);

return 0;

} /* end main */

void * threadPartialSum(void * rank) {

long myRank = (long) rank;

long i, stride;

long firstIndex;

double localSum;

stride = length / numberOfThreads;

firstIndex = myRank;

localSum = 0.0;

for (i=firstIndex; i

localSum += myArray[i];

} /* end for (i */

pthread_mutex_lock(&updateSumLock);

globalSum += localSum;

pthread_mutex_unlock(&updateSumLock);

return NULL;

} // end threadPartialSum

sumMutex3.c

/* Programmer:

File: sumMutex3.c

Compile As: gcc -o sumArray -O3 sumMutex3.c -lpthread

Run as: ./sumArray

Description: A parallel C solution to sum an array of floats

using pthreads. CORRECT solution that uses a mutex

to update the globalSum once using a localSum.

*/

#include

#include

#include

#include

#include

// Prototypes

void * threadPartialSum(void * args);

// GLOBAL variables

double globalSum;

int numberOfThreads;

long length;

float * myArray;

pthread_mutex_t updateSumLock;

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

long i;

pthread_t * threadHandles;

int errorCode;

double seqSum;

long startTime, endTime, seqTime, parallelTime;

if (argc != 3) {

printf("Usage: %s ", argv[0]);

return(0);

};

sscanf(argv[1],"%d",&length);

sscanf(argv[2],"%d",&numberOfThreads);

// Generate arrays for threads handles

threadHandles = (pthread_t *) malloc(numberOfThreads*sizeof(pthread_t));

// Generate data array

myArray=(float *) malloc(length*sizeof(float));

srand(5);

for (i=0; i

myArray[i] = rand() / (float) RAND_MAX;

} // end for i

time(&startTime);

seqSum = 0.0;

for (i=0; i

seqSum += myArray[i];

} // end for i

time(&endTime);

seqTime = endTime - startTime;

time(&startTime);

pthread_mutex_init(&updateSumLock, NULL);

globalSum = 0.0;

for (i=0; i

if (errorCode = pthread_create(&threadHandles[i], NULL, threadPartialSum, (void *) i) != 0) {

printf("pthread %d failed to be created with error code %d ", i, errorCode);

} // end if

} // end for

for (i=0; i

if (errorCode = pthread_join(threadHandles[i], (void **) NULL) != 0) {

printf("pthread %d failed to be joined with error code %d ", i, errorCode);

} // end if

} // end for

time(&endTime);

parallelTime = endTime - startTime;

printf( "Time to sum %ld floats using %d threads %ld seconds (seq. %ld seconds) ", length, numberOfThreads,

parallelTime, seqTime);

printf("Thread's Sum is %lf and seq. sum %lf ",globalSum, seqSum);

free(myArray);

return 0;

} /* end main */

void * threadPartialSum(void * rank) {

long myRank = (long) rank;

long i, stride, firstIndex;

double localSum;

stride = numberOfThreads;

firstIndex = myRank;

localSum = 0.0;

for (i=firstIndex; i

localSum += myArray[i];

} /* end for (i */

pthread_mutex_lock(&updateSumLock);

globalSum += localSum;

pthread_mutex_unlock(&updateSumLock);

return NULL;

} // end threadPartialSum

sumNoSync.c

/* Programmer:

File: sum1DArray_pthreads_NoSync.c

Compile As: gcc -o sumArray -O0 sumNoSync.c -lpthread

Compile As: gcc -o sumArray -O3 sumNoSync.c -lpthread

Run as: ./sumArray

Description: A parallel C solution to sum an array of floats

using pthreads, BUT IT IS INCORRECT.

*/

#include

#include

#include

#include

#include

// Prototypes

void * threadPartialSum(void * args);

// GLOBAL variables

double globalSum;

int numberOfThreads;

long length;

float * myArray;

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

long i;

clock_t clockStart, clockEnd;

pthread_t * threadHandles;

pthread_attr_t * threadAttrs;

int errorCode;

double seqSum;

if (argc != 3) {

printf("Usage: %s ", argv[0]);

return(0);

};

sscanf(argv[1],"%d",&length);

sscanf(argv[2],"%d",&numberOfThreads);

// Generate arrays for threads handles

threadHandles = (pthread_t *) malloc(numberOfThreads*sizeof(pthread_t));

threadAttrs = (pthread_attr_t *) malloc(numberOfThreads*sizeof(pthread_attr_t));

// Generate data array

myArray=(float *) malloc(length*sizeof(float));

srand(5);

seqSum = 0.0;

for (i=0; i

myArray[i] = rand() / (float) RAND_MAX;

seqSum += myArray[i];

} // end for i

clockStart = clock();

for (i=0; i

pthread_attr_init(&threadAttrs[i]);

pthread_attr_setscope(&threadAttrs[i], PTHREAD_SCOPE_SYSTEM);

if (errorCode = pthread_create(&threadHandles[i], &threadAttrs[i],

threadPartialSum, (void *) i) != 0) {

printf("pthread %d failed to be created with error code %d ", i, errorCode);

} // end if

} // end for

for (i=0; i

if (errorCode = pthread_join(threadHandles[i], (void **) NULL) != 0) {

printf("pthread %d failed to be joined with error code %d ", i, errorCode);

} // end if

} // end for

clockEnd = clock();

printf( "Time to sum %ld floats using %d threads %3.5f seconds ", length,

numberOfThreads, (clockEnd - clockStart) / (float) CLOCKS_PER_SEC);

/* printf("clockStart = %d clockEnd = %d CLOCKS_PER_SEC = %d ", */

/* clockStart, clockEnd, CLOCKS_PER_SEC); */

printf("Thread's Sum is %lf and seq. sum %lf ",globalSum, seqSum);

free(myArray);

return 0;

} /* end main */

void * threadPartialSum(void * rank) {

long myRank = (long) rank;

long i, blockSize;

long firstIndex, lastIndex;

double localSum;

blockSize = length / numberOfThreads;

firstIndex = blockSize * myRank;

if (myRank == numberOfThreads-1) { // last thread gets the rest

lastIndex = length-1;

} else {

lastIndex = blockSize * (myRank+1) - 1;

} // end if

localSum = 0.0;

for (i=firstIndex; i

localSum += myArray[i];

globalSum += myArray[i];

} /* end for (i */

printf("ThreadId %ld generated partial sum %f ", myRank, localSum);

return NULL;

} // end threadPartialSum

sumNoSyncNeeded.c

/* Programmer:

File: sumNoSyncNeeded.c

Compile As: gcc -o sumArray -O3 sumNoSyncNeeded.c -lpthread

Run as: ./sumArray

Description: A parallel C solution to sum an array of floats

using pthreads. CORRECT solution that needs no

thread synchronization because each updates their

own index in the threadSums array.

*/

#include

#include

#include

#include

#include

// Prototypes

void * threadPartialSum(void * args);

// GLOBAL variables

double globalSum;

double * threadSums;

int numberOfThreads;

long length;

float * myArray;

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

long i;

pthread_t * threadHandles;

int errorCode;

double seqSum;

long startTime, endTime, seqTime, parallelTime;

if (argc != 3) {

printf("Usage: %s ", argv[0]);

return(0);

};

sscanf(argv[1],"%d",&length);

sscanf(argv[2],"%d",&numberOfThreads);

// Generate arrays for threads handles

threadHandles = (pthread_t *) malloc(numberOfThreads*sizeof(pthread_t));

threadSums = (double *) malloc(numberOfThreads*sizeof(double));

// Generate data array

myArray=(float *) malloc(length*sizeof(float));

srand(5);

for (i=0; i

myArray[i] = rand() / (float) RAND_MAX;

} // end for i

time(&startTime);

seqSum = 0.0;

for (i=0; i

seqSum += myArray[i];

} // end for i

time(&endTime);

seqTime = endTime - startTime;

time(&startTime);

for (i=0; i

if (errorCode = pthread_create(&threadHandles[i], NULL, threadPartialSum, (void *) i) != 0) {

printf("pthread %d failed to be created with error code %d ", i, errorCode);

} // end if

} // end for

for (i=0; i

if (errorCode = pthread_join(threadHandles[i], (void **) NULL) != 0) {

printf("pthread %d failed to be joined with error code %d ", i, errorCode);

} // end if

} // end for

// Sum up all the threadSums sequentially

globalSum = 0.0;

for (i=0; i

globalSum += threadSums[i];

} // end for

time(&endTime);

parallelTime = endTime - startTime;

printf( "Time to sum %ld floats using %d threads %ld seconds (seq. %ld seconds) ",

length, numberOfThreads, parallelTime, seqTime);

printf("Thread's Sum is %lf and seq. sum %lf ", globalSum, seqSum);

free(myArray);

return 0;

} /* end main */

void * threadPartialSum(void * rank) {

long myRank = (long) rank;

long i, blockSize;

long firstIndex, lastIndex;

blockSize = length / numberOfThreads;

firstIndex = blockSize * myRank;

if (myRank == numberOfThreads-1) { // last thread gets the rest

lastIndex = length-1;

} else {

lastIndex = blockSize * (myRank+1) - 1;

} // end if

threadSums[myRank] = 0.0;

for (i=firstIndex; i

threadSums[myRank] += myArray[i];

} /* end for (i */

return NULL;

} // end threadPartialSum

open the file sumNoSynss which contains a C program Part A: Using an editor on that allows the user to enter two integer command-line arguments: the number of values and the number of threads. It creates a 1-dimensional array of that size, fills the array with random floating-point numbers, and uses the specified number of pthreads to sum the array values. WARNING: the pthreads can generate an incorTect sum, ut the correct sum is also calculated sequentially Compile the program using NO optimization (i.e., compiler option -00) and run the program repeatedly until the thread's sum differs from the sequential sum. (REMEMBER: you can use the up-arrow, , key to recall the last command-line command. Use the command-line of a) What different values did you get for both: gcso sumArra 00 SUyns.1pthread sumArray 10000 4 Thread's sum Seq. sum: b) What do notice that's strange about the difference between the two sums? (Explain the difference.) c) Examine at the threadPartialsum function. What's wrong about each thread incrementing the globalSum (ie.. global sum-mxarca [d] without any synchronization with the other threads? Part B: Using an editor open the file sumNoSyncNeeded.c which contains a similar program, but correctly calculates the array sum using pthreads. In this program thread synchronization is not needed because each thread updates their own index in the threadSums array, i.e., thread 0 updates threadSum[0], etc a) After all the threads sum their chuck of the array into their slot of the threadSum array, what does the main function do to compute the overall globalSum? b) How does the main function "know" when all threads have finished computing their partial sums? open the file sumNoSynss which contains a C program Part A: Using an editor on that allows the user to enter two integer command-line arguments: the number of values and the number of threads. It creates a 1-dimensional array of that size, fills the array with random floating-point numbers, and uses the specified number of pthreads to sum the array values. WARNING: the pthreads can generate an incorTect sum, ut the correct sum is also calculated sequentially Compile the program using NO optimization (i.e., compiler option -00) and run the program repeatedly until the thread's sum differs from the sequential sum. (REMEMBER: you can use the up-arrow, , key to recall the last command-line command. Use the command-line of a) What different values did you get for both: gcso sumArra 00 SUyns.1pthread sumArray 10000 4 Thread's sum Seq. sum: b) What do notice that's strange about the difference between the two sums? (Explain the difference.) c) Examine at the threadPartialsum function. What's wrong about each thread incrementing the globalSum (ie.. global sum-mxarca [d] without any synchronization with the other threads? Part B: Using an editor open the file sumNoSyncNeeded.c which contains a similar program, but correctly calculates the array sum using pthreads. In this program thread synchronization is not needed because each thread updates their own index in the threadSums array, i.e., thread 0 updates threadSum[0], etc a) After all the threads sum their chuck of the array into their slot of the threadSum array, what does the main function do to compute the overall globalSum? b) How does the main function "know" when all threads have finished computing their partial sums

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 Databases Questions!