[SOLVED] COEN177 - Lab4

30.00 $

Category:

Description

5/5 - (1 vote)

 Developing multi-threaded applications

C Program with threads (problems 1, 2, 7, and 8 of the class textbook).

 

Explain what happens when you run the threadHello.c program? Do you get the same result if you run it multiple times? What if you are also running some other demanding processes (e.g., compiling a big program, playing a Flash game on a website, or watching streaming video) when you run this program?

 

The function go() has the parameter arg passed a local variable. Are these variables per-thread or

shared state? Where does the compiler store these variables’ states?

 

The main() has local variable i. Is this variable per-thread or shared state? Where does the compiler store this variable?

 

 

  • Delete the second for loop in threadHello.c program so that the main routine simply creates NTHREADS threads and then prints “Main thread done.” What are the possible outputs of the program now. Explain.

 

 

Matrix multiplication with threads (problem 5 of the class text book)

  • Write a program that uses threads to perform a parallel matrix multiply. To multiply two matrices, C = A*B, the result entry C(i,j) is computed by taking the dot product of the ith row of A and the jth column of B: Ci,j = SUM A(i,k)*B(k,j) for k = 0 to N-1, where N is the matrix size. We can divide the work by creating one thread to compute each value (or each row) in C, and then executing those threads on different processors in parallel on multi-processor systems. As shown in the following figure, each is cell in the resulting matrix is the sum of the multiplication of row elements of the first matrix by the column elements of the second matrix.

 

 

You may fill in the entries of A and B matrices (double matrixA[N][M], matrixB[M][L] ) using a random number generator as below:

srand(time(NULL));

for (int i = 0; i < N; i++)

for (int j = 0; j < M; j++)

matrixA[i][j] = rand();

 

srand(time(NULL));

for (int i = 0; i < M; i++)

for (int j = 0; j < L; j++)

matrixB[i][j] = rand();

 

 

The following are important notes:

  • The number of columns of the first matrix must be equal to the number of rows in the second matrix.
  • The values of N, M, and L must be large to exploit parallelism (e.g. N, M, L = 1024).
  • The output matrix would be defined double matrixC[N][L];
  • The Matrix multiplication is a loosely coupled problem and so decomposable, i.e. multiplication of each row of matrixA with all columns of matrix can be performed independently and in parallel
  • The number of threads to be created in the program equals to N and each thread i would be performing the following task:

for (int j = 0; j < L; j++){

double temp = 0;

for (int k = 0; k < M; k++){

temp += matrixA[i][k] * matrixB[k][j];

}

matrixC[i][j] = temp;

}

 

  • The main thread needs to wait for all other threads before it displays the resulting matrixC
  • You may use the code skeleton “lab4Skeleton.c” available on Camino

 

  • [Bonus] Modify your program in Step 3 to creat N*L threads, each computing ith row * jth