Title: COSC330/530 - Lecture 9
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
There are two aspects to this program:
They form a typical server/client relationship:
When this happens the two processes obtain a two way connection channel, fd
, a file descriptor, and spawn two threads:
stdin
and echo out to the fd
.fd
and echo out to the stdout
.Note how I make arrays of function pointers.
void * (*pmain[2])(void *) = {reader, writer};
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);
}
In this unit we will do several examples involving matrix multiplication!
We do this for several reasons:
Ok so let:
A
be an m x p
matrix, andB
be an p x n
matrix:
C = A x B
.C = A x B
is the following m x n
matrix:where
typedef
:typedef int matrix_t[m][n];
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]) ;
}
}
#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);
#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);
}
[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
#include <pthread.h>
void * thread_entry(void *arg);
pthread_t my_thread;
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>
void * thread_entry(void *arg);
typedef struct {
int id;
int size;
int Arow;
int Bcol;
matrix_t *MA, *MB, *MC;
} package_t;
mult
routine.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); }
pthread_t my_thread;
pthread_t *threads =
(pthread_t *)calloc(size*size, sizeof(pthread_t));
package_t *thread_data =
(package_t *)calloc(size*size, sizeof(package_t));
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++; }}
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++; }}