This is a UNIX activity that I have to modify and run this program. show me your
ID: 3859357 • Letter: T
Question
This is a UNIX activity that I have to modify and run this program.
show me your work and please modify dot1m.c and dot1s.c so that I can the result of this program.
1. Compile and run the programs (and name the executable dots1 and dotm1 respectively).
Use time to get the 3 times of the program run. For example,
time ./dots1
2. Modify dots1.c so that it will take the value for VECLEN from an argument. That is, to run the program (the executable is dots).
time ./dots1 1000000
3. Modify dotm1.c so that it will take and set the values for VECLEN and NUMTHRDS from two arguments. That is, to run the program where the value of VECLEN*NUMTHRDS for dotm1.c should be equal to the value of VECLEN of dots1.c
time ./dotm1 10 100000
4. Run dots1 and dotm1 with the various size. For example,
time ./dots1 32000000
time ./dotm1 2 16000000
time ./dotm1 4 8000000
time ./dotm1 8 4000000
time ./dotm1 16 2000000
time ./dotm1 32 1000000
** Summarize the results of each run (serial versus thread program run, and the number of threads for each result).
5. Submit a word document to show your work done (with the source code and runs in the document). Provide a proper headings for each code listing and the run of the programs.
Explanation / Answer
Please find the modified program below.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
/*
The following structure contains the necessary information
to allow the function "dotprod" to access its input data and
place its output into the structure. This structure is
unchanged from the sequential version.
*/
typedef struct
{
double *a;
double *b;
double sum;
int veclen;
} DOTDATA;
/* Define globally accessible variables and a mutex */
/* now moved to main */
// #define NUMTHRDS 10
// #define VECLEN 100000
// pthread_t callThd[NUMTHRDS];
pthread_mutex_t mutexsum;
DOTDATA dotstr;
/*
The function dotprod is activated when the thread is created.
As before, all input to this routine is obtained from a structure
of type DOTDATA and all output from this function is written into
this structure. The benefit of this approach is apparent for the
multi-threaded program: when a thread is created we pass a single
argument to the activated function - typically this argument
is a thread number. All the other information required by the
function is accessed from the globally accessible structure.
*/
void *dotprod(void *arg)
{
/* Define and use local variables for convenience */
int i, start, end, len ;
long offset;
double mysum, *x, *y;
offset = (long)arg;
len = dotstr.veclen;
start = offset*len;
end = start + len;
x = dotstr.a;
y = dotstr.b;
/*
Perform the dot product and assign result
to the appropriate variable in the structure.
*/
mysum = 0;
for (i=start; i<end ; i++)
{
mysum += (x[i] * y[i]);
}
/*
Lock a mutex prior to updating the value in the shared
structure, and unlock it upon updating.
*/
pthread_mutex_lock (&mutexsum);
dotstr.sum += mysum;
printf("Thread %ld did %d to %d: mysum=%f global sum=%f ",
offset,start,end,mysum,dotstr.sum);
pthread_mutex_unlock (&mutexsum);
pthread_exit((void*) 0);
}
/*
The main program creates threads which do all the work and then
print out result upon completion. Before creating the threads,
The input data is created. Since all threads update a shared structure, we
need a mutex for mutual exclusion. The main thread needs to wait for
all threads to complete, it waits for each one of the threads. We specify
a thread attribute value that allow the main thread to join with the
threads it creates. Note also that we free up handles when they are
no longer needed.
*/
int main (int argc, char *argv[])
{
// modify the program to get MUNTHRDS as the argument1
// and VECLEN as argument2
// that is, run the program: ./dotm 10 100000
//
if(argc<=1)
{
printf("You did not feed me arguments, I will die now :( ...");
exit(1);
}
int NUMTHRDS = atoi(argv[1]);;
int VECLEN = atoi(argv[2]);
pthread_t callThd[NUMTHRDS];
long i;
double *a, *b;
void *status;
pthread_attr_t attr;
/* Assign storage and initialize values */
a = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double));
b = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double));
for (i=0; i<VECLEN*NUMTHRDS; i++) {
a[i]=1;
b[i]=a[i];
}
dotstr.veclen = VECLEN;
dotstr.a = a;
dotstr.b = b;
dotstr.sum=0;
pthread_mutex_init(&mutexsum, NULL);
/* Create threads to perform the dotproduct */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(i=0;i<NUMTHRDS;i++)
{
/* Each thread works on a different set of data.
* The offset is specified by 'i'. The size of
* the data for each thread is indicated by VECLEN.
*/
pthread_create(&callThd[i], &attr, dotprod, (void *)i);
}
pthread_attr_destroy(&attr);
/* Wait on the other threads */
for(i=0;i<NUMTHRDS;i++) {
pthread_join(callThd[i], &status);
}
/* After joining, print out the results and cleanup */
printf ("Sum = %f ", dotstr.sum);
free (a);
free (b);
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.