Parallel Matrix-Matrix Multiplication of Square Matrices using POSIX threads

  1 #include <stdio.h> 
  2 #include <stdlib.h> 
  3 #include <math.h> 
  4 #include <pthread.h> 
  5  
  6 #define NUM_THREADS 100        // Can be = 4,16,25,64,100
  7  
  8 double **A,**B,**C; 
  9 int n; 
 10 int n_bar;  
 11  
 12 struct thread_data 
 13 { 
 14     int blocki;    // Row of Block
 15     int blockj; // Column of Block
 16 }; 
 17  
 18 // Function to print matrices 
 19 void print_matrices(){ 
 20     int i,j; 
 21     printf("A MATRIX\n"); 
 22     for( i=0; i<n; i++)
 23         for( j=0; j<n; j++){
 24             printf(" %.0f", A[i][j] ); 
 25             if( j>0 && (j+1) % n == 0 ) printf("\n");
 26         }     
 27     printf("\nB MATRIX\n"); 
 28     for( i=0; i<n; i++)
 29         for( j=0; j<n; j++){
 30             printf(" %.0f", B[i][j] ); 
 31             if( j>0 && (j+1) % n == 0 ) printf("\n");
 32         }     
 33     printf("\nC MATRIX\n"); 
 34     for( i=0; i<n; i++)
 35         for(j=0; j<n; j++){
 36             printf(" %.0f", C[i][j] ); 
 37             if( j>0 && (j+1) % n == 0 ) printf("\n");
 38         }     
 39         printf("\n"); 
 40 } 
 41  
 42 //The thread will begin control in this function
 43 void *multiply( void *param ) {
 44    struct thread_data *data = param; // the structure that holds our data
 45     int i=0,j=0,k=0;
 46     int i_start = n_bar * data->blocki;    // Starting point row of underlying matrix 
 47     int j_start = n_bar * data->blockj; // Starting point col of underlying matrix 
 48  
 49    for( i=i_start; i < (i_start + n_bar) ;i++)
 50        for ( j=j_start; j < (j_start + n_bar); j++)
 51            for (k=0; k<n; k++)
 52                C[i][j]+=A[i][k]*B[k][j];
 53  
 54    //Exit the thread 
 55    pthread_exit(0); 
 56 } 
 57  
 58  
 59 int main(int argc, char *argv[]) {
 60    int i,j,k,time; 
 61    int t; 
 62    pthread_t threads[NUM_THREADS]; 
 63    pthread_attr_t attr; 
 64    struct thread_data index[NUM_THREADS]; 
 65    void *status; 
 66  
 67   pthread_attr_init(&attr); 
 68   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 
 69  
 70   n = atoi(argv[1]);    // Order of matrices A, B and C (Perfect Square)
 71   n_bar = n/sqrt( NUM_THREADS ); 
 72   
 73   //Allocate and fill matrices 
 74   A = (double **)malloc(n*sizeof(double *));
 75   B = (double **)malloc(n*sizeof(double *));
 76   C = (double **)malloc(n*sizeof(double *));
 77  
 78   for(i=0;i<n;i++){
 79      A[i] = (double *)malloc(n*sizeof(double));
 80      B[i] = (double *)malloc(n*sizeof(double));
 81       C[i] = (double *)malloc(n*sizeof(double));
 82      } 
 83  
 84  
 85   for (i = 0; i<n; i++)
 86       for(j=0;j<n;j++){
 87           A[i][j] = rand() % 5 + 1;
 88           B[i][j] = rand() % 5 + 1;
 89           C[i][j] = 0.0;
 90       } 
 91    // Start timer  
 92    time=timer(); 
 93     t=0; 
 94     for(i = 0; i < n/n_bar; i++) {
 95           for(j = 0; j < n/n_bar; j++) {
 96              //Assign a row and column for each thread
 97              index[t].blocki = i; 
 98              index[t].blockj = j; 
 99              pthread_create(&threads[t], &attr, multiply, (void *)&index[t] );
100              pthread_join( threads[t], NULL);
101           } 
102        } 
103  
104     // Print out the matrices 
105 //    print_matrices();         
106            
107    time=timer()-time; 
108    printf("Elapsed time: %f \n",time/1000000.0);
109  
110 pthread_exit(NULL);  
111 } 
112  
113  
114  
115  
116  
117 
Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: