1

I am new at parallel programming using MPI in C. So I am working on a matrix multiplication for example A*B. Here's my code in C but I have not yet any idea how I can implement the MPI in my code.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct sMatrix {
        int size;
        int *tab;
        double time;
} Matrix;

void init_mat(Matrix *m, int t){
        m->size=t;
        m->tab=malloc(t*t*sizeof(int));
        m->time=0;
}

void free_mat(Matrix *m){
        free(m->tab);
        m->size=0;
        m->time=0;
}

void generate_mat_random(Matrix *m){
        int i;
        for(i=0;i<(m->size*m->size); i++){
                m->tab[i]=rand()%10;                
        }
}

void generate_mat(Matrix *m){
        int i;
        int cmp=1;
        for(i=0;i<(m->size*m->size); i++){
                m->tab[i]=cmp;                
                cmp++;
        }
        cmp=1;
}

void display_mat(Matrix *m){
        int i,j;
        for(i=0;i<m->size;i++){
                for(j=0;j<m->size;j++){
                        printf("%3d   ",m->tab[i*m->size+j]);
                }
                printf("\n");
        }
}

void display_mat_file(Matrix *m, char outputFilename[]){
        FILE *ofp;
        ofp = fopen(outputFilename, "w");
        if (ofp == NULL) {
                    fprintf(stderr, "Can't open output file %s!\n", outputFilename);
                    exit(1);
          }

        int i,j;
        for(i=0;i<m->size;i++){
                for(j=0;j<m->size;j++){
                        fprintf(ofp, "%3d   ",m->tab[i*m->size+j]);
                }
                fprintf(ofp,"\n");
        }

        fprintf(ofp,"\n");
        fprintf(ofp,"Time :%f \n", m->time);

        fclose(ofp);
}

//m=m1*m2
 void multiply_MM(Matrix *m, Matrix m1, Matrix m2){
        clock_t begin, end;

        int i,j,k,s;
        int sum;
        begin=clock();

        for( i=0;i<m->size;i++){
                for( j=0;j<m->size;j++){
                        k=0;
                        sum=0;
                        for( s=0;s<m->size;s++){
                                sum = sum + m1.tab[i*m->size+s]*m2.tab[k*m->size+j];
                                k++;        
                        }        
                        m->tab[i*m->size+j]=sum;
                }
        }

        end=clock();
        m->time = (double)(end - begin) / CLOCKS_PER_SEC;
}

int main(int argc, char* argv[]){
        Matrix A;
        Matrix B;
        Matrix C;

        printf("Initialisation of matrix A and B\n");

        init_mat(&A, 2000);
        init_mat(&B, 2000);
        init_mat(&C, 2000);

        generate_mat_random(&A);
        generate_mat_random(&B);

        multiply_MM(&C, A, B);

        display_mat_file(&C,"2000x2000.txt");

        printf("End of multiplication\n");
        printf("Time for multiplication: %f\n",C.time);

        free_mat(&A);
        free_mat(&B);
        free_mat(&C);

        return 0;
}

Can anyone suggest what methods I should use from MPI and where to use them in the main()?

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
JPerk
  • 325
  • 1
  • 2
  • 12
  • 2
    What have you tried so far ? You have plenty examples on MPI matrix multiplication if you google with several different strategies. I suggest you to implement a simple one using Broadcast and Gather and then try the matrix block multiplication which is harder but much used ! – coincoin May 22 '15 at 00:02
  • @coincoin What do you think by a matrix block multiplication? What is the difference in my function multiply , it isn't about block multiplication? So I can only use Broadcast and Gather to implement the multiplication? – JPerk May 22 '15 at 09:48
  • You can use whatever MPI routines you want. Just that I find Broadcast and Gather easier to use. The most important is to understand what you are doing. You do not use block multiplication in your code yet. Look at this related question : http://stackoverflow.com/questions/7549316/mpi-partition-matrix-into-blocks . It might help you. – coincoin May 22 '15 at 10:00
  • @coincoin Ok I see, and yeah in my multiplication is not allowed that kind of multiply , sorry I got it wrong. – JPerk May 22 '15 at 10:17

0 Answers0