Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Help Me out with this program in C++ write a matrix multiplication progra

ID: 3668326 • Letter: P

Question

Please Help Me out with this program in C++

write a matrix multiplication program in C++ where the work of calculating the product matrix is distributed among multiple threads created using the pthread library. To this end, your program must do the following:

Define two global matrices A[X][Y] & B[Y][Z] to store the two matrices to be multiplied. X and Y must be set using a #define directive.

Define a global matrix C[X][Z] to store the product of matrices A and B. Once again, Z must be set using a #define directive.

Define a pthread function called pthread_multiply to perform the actual matrix multiplication operation (Note: Recall that a pthread function must have a very specific prototype). As a starting point, a regular (non-threaded) function for matrix multiplication, called multiply, has been provided below.

void multiply()

{

          int row,col,inner;

          for (row = 0; row < X; row++) {

                      for (col = 0; col < Z; col++) {

                                  for (inner = 0; inner < Y; inner++) {

                                              C[row][col] += A[row][inner] * B[inner][col];

                                  }

                      }

          }

}

  

The pthread_multiply function you write will essentially be a modification of the above multiply function. Instead of computing the entire product matrix C, the pthread_multiply function will only calculate a subset of rows of the product matrix C. Parameters must be passed to pthread_multiply to tell it which subset of rows to compute (Note: recall that parameter passing to a pthread function is somewhat different because of its specific prototype).

Define a main function and in this function, do the following:

Initialize the matrices A, B and C as follows: each element in A & B must be initialized to the sum of its row and column index and all elements in matrix C must be initialized to zero.

Using a loop, create multiple pthreads to execute the pthread_multiply function (use a #define directive to define the number of threads - this number must be set to more than 2). To each thread, pass appropriate parameters to tell it which subset of rows in the resultant matrix C it is responsible for computing. Each thread must be given an equal share of the resultant matrix as far as possible. For e.g., if you have A[10][10], B[10][10] and if the number of threads is 2 (Note: this is just an example - you must use more than 2 threads), then, thread 1 should be assigned the task of calculating results for rows 0 – 4 of matrix C and thread 2, those for rows 5 – 9. However, if you have A[9][9], B[9][9] and two threads, one of the threads will be assigned a lower number of rows than the other. I.e., in general, the last thread may have less number of rows to compute.

Using a second loop, wait until all the created pthreads have completed, using the pthread_join function.

Once all threads have completed, display the matrix C in an appropriate manner.

Explanation / Answer

// PrimesThreads.c
2
3 // threads-based program to find the number of primes between 2 and n;
4 // uses the Sieve of Eratosthenes, deleting all multiples of 2, all
5 // multiples of 3, all multiples of 5, etc.
6
2Here and below, the term Unix includes Linux.
3There may be other processes running too. So the threads of our program must still take turns with other
processes running on the machine.
8 CHAPTER 1. INTRODUCTION TO PARALLEL PROCESSING
7 // for illustration purposes only; NOT claimed to be efficient
8
9 // Unix compilation: gcc -g -o primesthreads PrimesThreads.c -lpthread -lm
10
11 // usage: primesthreads n num_threads
12
13 #include <stdio.h>
14 #include <math.h>
15 #include <pthread.h> // required for threads usage
16
17 #define MAX_N 100000000
18 #define MAX_THREADS 25
19
20 // shared variables
21 int nthreads, // number of threads (not counting main())
22 n, // range to check for primeness
23 prime[MAX_N+1], // in the end, prime[i] = 1 if i prime, else 0
24 nextbase; // next sieve multiplier to be used
25 // lock for the shared variable nextbase
26 pthread_mutex_t nextbaselock = PTHREAD_MUTEX_INITIALIZER;
27 // ID structs for the threads
28 pthread_t id[MAX_THREADS];
29
30 // "crosses out" all odd multiples of k
31 void crossout(int k)
32 { int i;
33 for (i = 3; i*k <= n; i += 2) {
34 prime[i*k] = 0;
35 }
36 }
37
38 // each thread runs this routine
39 void *worker(int tn) // tn is the thread number (0,1,...)
40 { int lim,base,
41 work = 0; // amount of work done by this thread
42 // no need to check multipliers bigger than sqrt(n)
43 lim = sqrt(n);
44 do {
45 // get next sieve multiplier, avoiding duplication across threads
46 // lock the lock
47 pthread_mutex_lock(&nextbaselock);
48 base = nextbase;
49 nextbase += 2;
50 // unlock
51 pthread_mutex_unlock(&nextbaselock);
52 if (base <= lim) {
53 // don’t bother crossing out if base known composite
54 if (prime[base]) {
55 crossout(base);
56 work++; // log work done by this thread
57 }
58 }
59 else return work;
60 } while (1);
61 }
62
63 main(int argc, char **argv)
64 { int nprimes, // number of primes found
1.4. PROGRAMMER WORLD VIEWS 9
65 i,work;
66 n = atoi(argv[1]);
67 nthreads = atoi(argv[2]);
68 // mark all even numbers nonprime, and the rest "prime until
69 // shown otherwise"
70 for (i = 3; i <= n; i++) {
71 if (i%2 == 0) prime[i] = 0;
72 else prime[i] = 1;
73 }
74 nextbase = 3;
75 // get threads started
76 for (i = 0; i < nthreads; i++) {
77 // this call says create a thread, record its ID in the array
78 // id, and get the thread started executing the function worker(),
79 // passing the argument i to that function
80 pthread_create(&id[i],NULL,worker,i);
81 }
82
83 // wait for all done
84 for (i = 0; i < nthreads; i++) {
85 // this call says wait until thread number id[i] finishes
86 // execution, and to assign the return value of that thread to our
87 // local variable work here
88 pthread_join(id[i],&work);
89 printf("%d values of base done ",work);
90 }
91
92 // report results
93 nprimes = 1;
94 for (i = 3; i <= n; i++)
95 if (prime[i]) {
96 nprimes++;
97 }
98 printf("the number of primes found was %d ",nprimes);
99
100 }