Created
April 23, 2013 00:01
-
-
Save bfb/5439667 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <mpi.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main(int argc, char* argv[]){ | |
int rank, size, i, j, k, tag = 0; | |
MPI_Status status; | |
MPI_Init(&argc, &argv ); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
MPI_Comm_size(MPI_COMM_WORLD, &size); | |
double start_time,final_time; | |
// Set matrices lines and columns | |
int lines = 1000; | |
int columns = 1000; | |
start_time = MPI_Wtime(); | |
int *matrix_line = malloc(sizeof(int) * lines); | |
// initialize the matrix_b | |
int** matrix_b = (int**) malloc(sizeof(int*) * lines); | |
matrix_b[0] = (int*) malloc(lines * columns * sizeof(int)); | |
for(i=1; i<lines; i++) | |
matrix_b[i] = &(matrix_b[0][i*columns]); | |
for(i = 0; i < lines; i++){ | |
for(j = 0; j < columns; j++){ | |
matrix_b[i][j] = (int) j; | |
} | |
} | |
// final matrix | |
int** final_matrix = (int**) malloc(sizeof(int*) * lines); | |
final_matrix[0] = (int*) malloc(lines * columns * sizeof(int)); | |
for(i=1; i<lines; i++) | |
final_matrix[i] = &(final_matrix[0][i*columns]); | |
if(size > 1){ | |
int partial = lines / (size -1); | |
printf("PARTIAL: %d", partial); | |
int** receive = (int**) malloc(sizeof(int*) * partial); | |
receive[0] = (int*) malloc(partial * columns * sizeof(int)); | |
for(i=1; i<lines; i++) | |
receive[i] = &(receive[0][i*columns]); | |
int** partial_matrix = (int**) malloc(sizeof(int*) * partial); | |
partial_matrix[0] = (int*) malloc(partial * columns * sizeof(int)); | |
for(i=1; i<lines; i++) | |
partial_matrix[i] = &(partial_matrix[0][i*columns]); | |
int *line = malloc(sizeof(int) * lines); | |
if (rank == 0){ | |
int** matrix_a = (int**) malloc(sizeof(int*) * lines); | |
matrix_a[0] = (int*) malloc(lines * columns * sizeof(int)); | |
for(i=1; i<lines; i++) | |
matrix_a[i] = &(matrix_a[0][i*columns]); | |
for(i = 0; i < lines; i++){ | |
for(j = 0; j < columns; j++){ | |
matrix_a[i][j] = (int) j; | |
} | |
} | |
int count = 0; | |
for(j = 1; j < size; j++){ | |
printf("COUNT: %d\n", count); | |
MPI_Send(&matrix_a[count][0], partial * columns, MPI_INT, j, tag, MPI_COMM_WORLD); | |
MPI_Recv(&final_matrix[count][0], partial * columns, MPI_INT, j, tag, MPI_COMM_WORLD, &status); | |
count += partial; | |
} | |
final_time = MPI_Wtime(); | |
printf("Time: %f\n", final_time - start_time); | |
} else { | |
printf("PARTIAL => %d", partial); | |
MPI_Recv(&receive[0][0], partial * columns, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); | |
int partial_counter = 0.0; | |
for(k = 0; k < partial; k++){ | |
for(i = 0; i < partial; i++){ | |
for(j = 0; j < columns; j++){ | |
// printf("%d * %d + ", receive[j], matrix_b[j][i]); | |
partial_counter += receive[k][j] * matrix_b[j][i]; | |
} | |
// printf("PARTIAL: %d\n", partial); | |
line[i] = partial_counter; | |
partial_counter = 0.0; | |
} | |
partial_matrix[k] = matrix_line; | |
} | |
MPI_Send(&partial_matrix[0][0], partial * columns, MPI_INT, 0, tag, MPI_COMM_WORLD); | |
} | |
} else { | |
int** matrix_a = (int**) malloc(sizeof(int*) * lines); | |
matrix_a[0] = (int*) malloc(lines * columns * sizeof(int)); | |
for(i=1; i<lines; i++) | |
matrix_a[i] = &(matrix_a[0][i*columns]); | |
for(i = 0; i < lines; i++){ | |
for(j = 0; j < columns; j++){ | |
matrix_a[i][j] = (int) j; | |
} | |
} | |
int partial = 0.0; | |
for(k = 0; k < lines; k++){ | |
for(i = 0; i < lines; i++){ | |
for(j = 0; j < columns; j++){ | |
// printf("%d * %d + ", receive[j], matrix_b[j][i]); | |
partial += matrix_a[k][j] * matrix_b[j][i]; | |
} | |
// printf("PARTIAL: %d\n", partial); | |
matrix_line[i] = partial; | |
partial = 0; | |
} | |
final_matrix[k] = matrix_line; | |
} | |
final_time = MPI_Wtime(); | |
printf("Time: %f\n", final_time - start_time); | |
} | |
MPI_Finalize(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment