>

Title: COSC330/530 - Lecture 9

COSC330/530 - Parallel and Distributed Computing

Lecture 9 - Multithreaded Applications

Dr. Mitchell Welch


Reading


Summary


Multithreaded Chat

For example:

Window 1:
[cosc330@turing Chat]$ ./server 8000
Server listening on port 8000
Connection has been made to turing
Window 2:
[cosc330@turing Chat]$ ./client turing 8000
Connection has been made to turing


Multithreaded Chat


Multithreaded Chat


void * (*pmain[2])(void *) = {reader, writer};

Multithreaded Chat


for(i = 0; i < 2; i++) if(pthread_create(&thread[i],NULL,pmain[i],&outfd) != 0){ fprintf(stderr, "Thread creation failed in client\n"); exit(1); }

Matrix Multiplication


Multithreaded Matrix Multiplication


center-aligned image


Multithreaded Matrix Multiplication


center-aligned image


Multithreaded Matrix Multiplication


center-aligned image
center-aligned image


Multithreaded Matrix Multiplication


center-aligned image

where


center-aligned image


Multithreaded Matrix Multiplication


center-aligned image


Multithreaded Matrix Multiplication


center-aligned image


Multithreaded Matrix Multiplication

typedef int matrix_t[m][n];

Multithreaded Matrix Multiplication

void mult(int size, int row, int column,
          matrix_t MA, matrix_t MB, matrix_t MC){
  int position;

  MC[row][column] = 0;
  for(position = 0; position < size; position++) {
    MC[row][column] += (MA[row][position] * MB[position][column]) ;
  }
}

Multithreaded Matrix Multiplication


Multithreaded Matrix Multiplication

#define ARRAY_SIZE 5
typedef int matrix_t[ARRAY_SIZE][ARRAY_SIZE];
void mult(int,int,int,matrix_t,matrix_t,matrix_t);
int one(int row, int column);
int identity(int row, int column);
int sum(int row, int column);
void matrix_init(int sz, matrix_t M, int (*init_fun)(int,int));
void matrix_printf(char* name, int sz, matrix_t M);


Multithreaded Matrix Multiplication


center-aligned image


Multithreaded Matrix Multiplication


Multithreaded Matrix Multiplication

#include <stdlib.h>
#include "matrix_lib.h"

static matrix_t MA,MB,MC;

int main(int argc, char **argv){
  int  row, column, size = ARRAY_SIZE;
  /* initialize MA         */
  matrix_init(size, MA, identity);
  /* initialize MB         */
  matrix_init(size, MB, sum);
  /* display MA            */
  matrix_printf("A", size, MA);
  /* display MB            */
  matrix_printf("B", size, MB);
  /* multiply the suckers  */
  for(row = 0; row < size; row++) {
    for (column = 0; column < size; column++) {
      mult(size, row, column, MA, MB, MC); } }
  /* display the  result   */
  matrix_printf("C", size, MC);
  exit(EXIT_SUCCESS);
}

Multithreaded Matrix Multiplication

[turing Matrices]$ ./matrix_serial
Matrix: The A array is;
    1     0     0     0     0
    0     1     0     0     0
    0     0     1     0     0
    0     0     0     1     0
    0     0     0     0     1
Matrix: The B array is;
    1     2     3     4     5
    2     3     4     5     6
    3     4     5     6     7
    4     5     6     7     8
    5     6     7     8     9
Matrix: The C array is;
    1     2     3     4     5
    2     3     4     5     6
    3     4     5     6     7
    4     5     6     7     8
    5     6     7     8     9



Multithreaded Matrix Multiplication


Multithreaded Matrix Multiplication

#include <pthread.h>
void * thread_entry(void *arg);
pthread_t my_thread;

Multithreaded Matrix Multiplication

void *thread_arg = (void *)<some arbitrary expression>;
 pthread_create(&my_thread, NULL, thread_entry, thread_arg);
 ```

* Compile with gcc Use the flag `-pthread`
*when both linking and compiling.*

---

## Multithreaded Matrix Multiplication

* Include the appropriate library:

```cpp
#include <pthread.h>

Multithreaded Matrix Multiplication

void * thread_entry(void *arg);
typedef struct {
  int       id;
  int       size;
  int       Arow;
  int       Bcol;
  matrix_t  *MA, *MB, *MC;
} package_t;

Multithreaded Matrix Multiplication

void *mult_worker(void *arg){
  package_t p = *((package_t *)arg);
  mult(p.size, p.Arow, p.Bcol, *(p.MA), *(p.MB), *(p.MC));
  return(NULL);  }


Multithreaded Matrix Multiplication

pthread_t my_thread;
pthread_t *threads =
    (pthread_t *)calloc(size*size, sizeof(pthread_t));

Multithreaded Matrix Multiplication

package_t *thread_data =  
   (package_t *)calloc(size*size, sizeof(package_t));

Multithreaded Matrix Multiplication

  num_threads = 0;
  for(row = 0; row < size; row++) {
    for (column = 0; column < size; column++) {
      thread_data[num_threads].id = num_threads;
      thread_data[num_threads].size = size;
      thread_data[num_threads].Arow = row;
      thread_data[num_threads].Bcol = column;
      (thread_data[num_threads]).MA = &MA;
      (thread_data[num_threads]).MB = &MB;
      (thread_data[num_threads]).MC = &MC;
      num_threads++; }}

Multithreaded Matrix Multiplication

 pthread_create(&my_thread, NULL, thread_entry, thread_arg);

num_threads = 0; for(row = 0; row < size; row++) { for (column = 0; column < size; column++) { pthread_create(&threads[num_threads], NULL, mult_worker, (void *)&thread_data[num_threads]); num_threads++; }}

Multithreaded Matrix Multiplication


Multithreaded Matrix Multiplication


Summary

* Multithreaded Matrix Multiplication

Questions?


Reading