This commit is contained in:
2020-04-12 18:55:29 -06:00
parent 26480bcc76
commit 4081c9783d
56 changed files with 551 additions and 835 deletions

View File

@@ -1,31 +1,76 @@
#include <fstream>
#include <iostream>
#include <mpi.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#define MCW MPI_COMM_WORLD
using namespace std;
struct Matrix{
int M, N;
double** matrix;
};
//Problem size
const int N = 10;
vector<Matrix> matricies;
//global variables
double A[N][N];
double B[N][N];
double AB[N][N];
double AB_serial[N][N];
void fill_matrices();
void print_matrix(double mat[][N]);
void serial_version();
void compute_interval(int start, int interval);
void multiplyMatrix(int rank, int size);
void read_in_matrices();
int main(int argc, char** argv){
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MCW, &rank);
MPI_Comm_size(MCW, &size);
multiplyMatrix(rank, size);
if(!rank)
read_in_matrices();
MPI_Finalize();
}
void read_in_matrices(){
for(int i = 0; i < 2; i++) {
Matrix* matrix = new Matrix;
ifstream f("Matrix.txt");
f >> matrix->M >> matrix->N;
// Allocate memory
matrix->matrix = new double *[matrix->M];
for (int i = 0; i < matrix->M; ++i)
matrix->matrix[i] = new double[N];
for (int i = 0; i < matrix->M; i++)
for (int j = 0; j < matrix->N; j++)
f >> matrix->matrix[i][j];
matricies.push_back(*matrix);
}
}
//Function to fill matrices at random
void fill_matrices(){
srand(time(NULL));
for(int i = 0; i <N; i++){
for (int j = 0; j < N; j++){
A[i][j] = rand() %4;
B[i][j] = rand() %4;
}
for(int i = 0; i <N; i++){
for (int j = 0; j < N; j++){
A[i][j] = rand() %4;
B[i][j] = rand() %4;
}
}
}
//Function to print matrix
void print_matrix(double mat[][N]){
for (int i = 0; i < N; i++){
@@ -36,31 +81,28 @@ void print_matrix(double mat[][N]){
}
cout << endl;
}
//Serial version of solution
void serial_version(){
for (int i = 0; i <N; i++){
for (int j = 0; j < N;j++){
AB_serial[i][j] = 0;
for (int k = 0; k <N; k++){
AB_serial[i][j] += A[i][k]*B[k][j];
AB_serial[i][j] += A[i][k]*B[k][j];
}
}
}
}
//Compute interval multiplication
void compute_interval(int start,int interval){
for(int i = start; i <start+interval;i++){
for (int j = 0; j <N; j++){
AB[i][j] = 0;
for (int k = 0; k <N; k++){
AB[i][j] += A[i][k]*B[k][j];
AB[i][j] += A[i][k]*B[k][j];
}
}
}
}
void multiplyMatrix(int rank, int size){
//timing variables
double time1,time2,time3;
@@ -119,17 +161,3 @@ void multiplyMatrix(int rank, int size){
print_matrix(AB_serial);
}
}
int main(int argc, char** argv){
int interval, remainder, rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MCW, &rank);
MPI_Comm_size(MCW, &size);
multiplyMatrix(rank, size);
MPI_Finalize();
}