Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include <stdlib.h>
- #include <stdio.h>
- int main(int argc, char *argv[])
- {
- int rank, nproc;
- int row1,col1;
- int row2,col2;
- FILE *fp1, *fp2, *fp3;
- unsigned int i,j,k,sum;
- int *matrix1, *matrix2, *matrixtemp, *temp1, *temp2, *matrix1r, *matrix2r, *result;
- double StartTime, EndTime;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &nproc);
- if(rank==0){
- fp1 = fopen("/Users/win/Desktop/MatrixMultiplication_large/matAlarge.txt", "r");
- fp2 = fopen("/Users/win/Desktop/MatrixMultiplication_large/matBlarge.txt", "r");
- //fp1 = fopen("/Users/win/Desktop/MatrixMul/matrix2.txt", "r");
- //fp2 = fopen("/Users/win/Desktop/MatrixMul/matrix2.txt", "r");
- fp3 = fopen("/Users/win/Desktop/matSol.txt", "w");
- fscanf(fp1, "%d", &row1);
- fscanf(fp1, "%d", &col1);
- fscanf(fp2, "%d", &row2);
- fscanf(fp2, "%d", &col2);
- fprintf(fp3, "%d %d\n", row1, col2);
- MPI_Bcast(&row1, 1, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_Bcast(&col1, 1, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_Bcast(&row2, 1, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_Bcast(&col2, 1, MPI_INT, 0, MPI_COMM_WORLD);
- matrix1 = (int*)malloc(row1 * col1 * sizeof(int));
- matrix2 = (int*)malloc(row2 * col2 * sizeof(int));
- temp1 = (int*)malloc(row1 * col1 * sizeof(int));
- temp2 = (int*)malloc(row2 * col2 * sizeof(int));
- result = (int*)malloc(row1 * col2 * sizeof(int));
- for(i=0; i<row1; i++){
- for(j=0; j<col1; j++){
- fscanf(fp1, "%d", &matrix1[(i*col1)+j]);
- }
- }
- //transpose matrix
- for(i=0; i<row2; i++){
- for(j=0; j<col2; j++){
- fscanf(fp2, "%d", &matrix2[(j*row2)+i]);
- }
- }
- StartTime = MPI_Wtime();
- for(i=1;i<nproc;i++){
- if(i==nproc-1){
- MPI_Send(&matrix1[i*(row1/nproc)*col1], (row1/nproc)*col1 + (row1%(row1/nproc))*col1, MPI_INT, i, 1, MPI_COMM_WORLD);
- } else {
- MPI_Send(&matrix1[i*(row1/nproc)*col1], (row1/nproc)*col1, MPI_INT, i, 1, MPI_COMM_WORLD);
- }
- MPI_Send(&matrix2[0], row2*col2, MPI_INT, i, 2, MPI_COMM_WORLD);
- }
- for(i=0;i<row1/nproc;i++){ // loop row matrix1
- for(j=0;j<col2;j++){ // loop row matrix2
- sum = 0;
- for(k=0;k<row2;k++){ // loop col matrix2 (sum)
- sum += matrix1[(i*col1)+k] * matrix2[(j*row2)+k];
- }
- result[(i*col2)+j] = sum;
- }
- }
- for(i=1;i<nproc;i++){
- if(i==nproc-1){
- MPI_Recv(&result[i*(row1/nproc)*col2], (row1/nproc)*col2 + (row1%(row1/nproc))*col2, MPI_INT, i, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- } else {
- MPI_Recv(&result[(i*(row1/nproc))*col2], (row1/nproc)*col2, MPI_INT, i, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- }
- }
- EndTime = MPI_Wtime();
- for(i=0; i<row1; i++){
- for(j=0; j<col2; j++){
- fprintf(fp3, "%d ", result[(i*col2)+j]);
- }
- fprintf(fp3, "\n");
- }
- fclose(fp1);
- fclose(fp2);
- fclose(fp3);
- printf("Timings : %f Sec\n", EndTime - StartTime);
- } else {
- MPI_Bcast(&row1, 1, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_Bcast(&col1, 1, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_Bcast(&row2, 1, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_Bcast(&col2, 1, MPI_INT, 0, MPI_COMM_WORLD);
- matrix1r = malloc(row1 * col1 * sizeof(int));
- matrix2r = malloc(row2 * col2 * sizeof(int));
- matrixtemp = malloc(row1 * col2 * sizeof(int));
- if(rank==nproc-1){
- MPI_Recv(&matrix1r[0], (row1/nproc)*col1 + (row1%(row1/nproc))*col1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- } else {
- MPI_Recv(&matrix1r[0], (row1/nproc)*col1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- }
- MPI_Recv(&matrix2r[0], row2*col2, MPI_INT, 0, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- if(rank==nproc-1){
- for(i=0;i<(row1/nproc) + (row1%(row1/nproc));i++){ // loop row matrix1
- for(j=0;j<col2;j++){ // loop row matrix2
- sum = 0;
- for(k=0;k<row2;k++){ // loop col matrix2 (sum)
- sum += matrix1r[(i*col1)+k] * matrix2r[(j*row2)+k];
- }
- matrixtemp[(i*col2)+j] = sum;
- }
- }
- } else {
- for(i=0;i<row1/nproc;i++){ // loop row matrix1
- for(j=0;j<col2;j++){ // loop row matrix2
- sum = 0;
- for(k=0;k<row2;k++){ // loop col matrix2 (sum)
- sum += matrix1r[(i*col1)+k] * matrix2r[(j*row2)+k];
- }
- matrixtemp[(i*col2)+j] = sum;
- }
- }
- }
- printf("Process %d done!\n", rank) ;
- if(rank==nproc-1){
- MPI_Send(&matrixtemp[0], (row1/nproc)*col2 + (row1%(row1/nproc))*col2, MPI_INT, 0, 1, MPI_COMM_WORLD);
- } else {
- MPI_Send(&matrixtemp[0], (row1/nproc)*col2, MPI_INT, 0, 1, MPI_COMM_WORLD);
- }
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement