Question

In: Computer Science

refund quesetion /**************************************************************** * FILE: dotprod_mutex.c * DESCRIPTION: * This example program illustrates the use of...

refund quesetion

/****************************************************************
* FILE: dotprod_mutex.c
* DESCRIPTION:
*   This example program illustrates the use of mutex variables 
*   in a threads program. This version was obtained by modifying the
*   serial version of the program (dotprod_serial.c) which performs a 
*   dot product. The main data is made available to all threads through 
*   a globally accessible  structure. Each thread works on a different 
*   part of the data. The main thread waits for all the threads to complete 
*   their computations, and then it prints the resulting sum.
* SOURCE: Vijay Sonnad, IBM
* LAST REVISED: 01/29/09 Blaise Barney
******************************************************************************/
#include 
#include 
#include 

/*   
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

Solutions

Expert Solution

dotprod_mutex.c

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

typedef struct 
{
   double *a;
   double *b;
   double sum; 
   int veclen; 
}DOTDATA;

//Define globally accessible variables and a mutex

#define NUMTHRDS 4
#define VECLEN 100000
   DOTDATA dotstr; 
   pthread_t callThd[NUMTHRDS];
   pthread_mutex_t mutexsum;

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 the 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\n",offset,start,end,mysum,dotstr.sum);
   pthread_mutex_unlock (&mutexsum);

   pthread_exit((void*) 0);
}


int main (int argc, char *argv[])
{
    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 \n", dotstr.sum);
    free (a);
    free (b);
    pthread_mutex_destroy(&mutexsum);
    pthread_exit(NULL);
} 

dotprod_serial.c

#include <stdio.h>
#include <stdlib.h>

typedef struct 
{
  double *a;
  double *b;
  double sum; 
  int veclen; 
}DOTDATA;

#define VECLEN 100000
  DOTDATA dotstr; 

void dotprod()
{

// Define and use local variables for convenience

   int start, end, i; 
   double mysum, *x, *y;
   start=0;
   end = dotstr.veclen;
   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]);
    }
   dotstr.sum = mysum;

}

//The main program initializes data and calls the dotprd() function. Finally, it prints the result.
int main (int argc, char *argv[])
{
    int i,len;
    double *a, *b;
    // Assign storage and initialize values 
    len = VECLEN;
    a = (double*) malloc (len*sizeof(double));
    b = (double*) malloc (len*sizeof(double));
    for (i=0; i<len; i++) 
    {
      a[i]=1;
      b[i]=a[i];
    }
    dotstr.veclen = len; 
    dotstr.a = a; 
    dotstr.b = b; 
    dotstr.sum=0;
    //Perform the  dotproduct
    dotprod ();
    //Print result and release storage
    printf ("Sum =  %f \n", dotstr.sum);
    free (a);
    free (b);
}

NOTE: The above-provided source code will help you with your question. If in case you face any error then please do let me know through comments.

UPVOTE!!!Please... Thank You!!!!


Related Solutions

Working with Strings The following program illustrates the use of some of the methods in the...
Working with Strings The following program illustrates the use of some of the methods in the String class. Study the program to see what it is doing. // *************************************************************** // StringManips.java // Test several methods for manipulating String objects // *************************************************************** import java.util.Scanner; public class StringManips { public static void main (String[] args) { String phrase = new String ("This is a String test."); int phraseLength; // number of characters in the phrase String int middleIndex; // index of the...
Use an example from the real world that illustrates the effective, or ineffective, use of decision...
Use an example from the real world that illustrates the effective, or ineffective, use of decision making using the steps listed. Discuss why they were successful or not successful. Be specific about which steps were missed, not used, ineffective, etc.
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Use an example form the text or one from the real world that illustrates the effective,...
Use an example form the text or one from the real world that illustrates the effective, or ineffective, use of decision making using the steps listed. Discuss why they were successful or not successful. Be specific about which steps were missed, not used, ineffective, etc. *Please use a different answer from the Chegg since my friends already use it. Thankyou.
Which description best illustrates gender dysphoria (transsexualism)?
Which description best illustrates gender dysphoria (transsexualism)?
write a program that replace each line of a file with its reverse. for example, if...
write a program that replace each line of a file with its reverse. for example, if you run: java Reverse HelloPrinter.java then the contents of HelloPrinter.java are changed to retnirPolleH ssalc clibup { )sgra ][gnirtS(niam diov citats clibup { wodniw elosnoc eht ni gniteerg a yalpsiD // ;) "!dlroW ,olleH " (nltnirp.tuo.mestyS } }
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use...
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use of interfaces. The program should not take up more than half a page of size A4.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT