its working
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
3 3
|
3
|
||||||
1 2 3
|
1 2 3
|
||||||
4 5 6
|
4 5 6
|
||||||
7 8 9
|
7 8 9
|
||||||
3 3
|
|
||||||
9 8 7
|
9 8 7
|
||||||
6 5 4
|
6 5 4
|
||||||
3 2 1
|
3 2 1
|
||||||
|
|||||||
Binary file not shown.
@@ -0,0 +1,36 @@
|
|||||||
|
#IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">])
|
||||||
|
|
||||||
|
#IncludeRegexScan: ^.*$
|
||||||
|
|
||||||
|
#IncludeRegexComplain: ^$
|
||||||
|
|
||||||
|
#IncludeRegexTransform:
|
||||||
|
|
||||||
|
/Users/bradybodily/Repositories/CS5500_Parallel_Programming/FinalProject/main.cpp
|
||||||
|
fstream
|
||||||
|
-
|
||||||
|
iostream
|
||||||
|
-
|
||||||
|
mpi.h
|
||||||
|
-
|
||||||
|
stdlib.h
|
||||||
|
-
|
||||||
|
time.h
|
||||||
|
-
|
||||||
|
vector
|
||||||
|
-
|
||||||
|
|
||||||
|
/usr/local/Cellar/open-mpi/4.0.2/include/mpi.h
|
||||||
|
stddef.h
|
||||||
|
-
|
||||||
|
mpi_portable_platform.h
|
||||||
|
/usr/local/Cellar/open-mpi/4.0.2/include/mpi_portable_platform.h
|
||||||
|
openmpi/ompi/mpi/cxx/mpicxx.h
|
||||||
|
/usr/local/Cellar/open-mpi/4.0.2/include/openmpi/ompi/mpi/cxx/mpicxx.h
|
||||||
|
|
||||||
|
/usr/local/Cellar/open-mpi/4.0.2/include/mpi_portable_platform.h
|
||||||
|
omp.h
|
||||||
|
/usr/local/Cellar/open-mpi/4.0.2/include/omp.h
|
||||||
|
omp.h
|
||||||
|
/usr/local/Cellar/open-mpi/4.0.2/include/omp.h
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# CMAKE generated file: DO NOT EDIT!
|
||||||
|
# Generated by "Unix Makefiles" Generator, CMake Version 3.15
|
||||||
|
|
||||||
|
CMakeFiles/HW10.dir/main.cpp.o
|
||||||
|
/Users/bradybodily/Repositories/CS5500_Parallel_Programming/FinalProject/main.cpp
|
||||||
|
/usr/local/Cellar/open-mpi/4.0.2/include/mpi.h
|
||||||
|
/usr/local/Cellar/open-mpi/4.0.2/include/mpi_portable_platform.h
|
||||||
BIN
FinalProject/cmake-build-debug/CMakeFiles/HW10.dir/main.cpp.o
Normal file
BIN
FinalProject/cmake-build-debug/CMakeFiles/HW10.dir/main.cpp.o
Normal file
Binary file not shown.
BIN
FinalProject/cmake-build-debug/HW10
Executable file
BIN
FinalProject/cmake-build-debug/HW10
Executable file
Binary file not shown.
@@ -9,22 +9,12 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Matrix{
|
|
||||||
int M, N;
|
|
||||||
double** matrix;
|
|
||||||
};
|
|
||||||
|
|
||||||
//Problem size
|
//Problem size
|
||||||
const int N = 10;
|
int N;
|
||||||
vector<Matrix> matricies;
|
|
||||||
//global variables
|
//global variables
|
||||||
double A[N][N];
|
double **A, **B, **AB, **AB_serial;
|
||||||
double B[N][N];
|
|
||||||
double AB[N][N];
|
|
||||||
double AB_serial[N][N];
|
|
||||||
|
|
||||||
void fill_matrices();
|
void print_matrix(double **mat);
|
||||||
void print_matrix(double mat[][N]);
|
|
||||||
void serial_version();
|
void serial_version();
|
||||||
void compute_interval(int start, int interval);
|
void compute_interval(int start, int interval);
|
||||||
void multiplyMatrix(int rank, int size);
|
void multiplyMatrix(int rank, int size);
|
||||||
@@ -38,41 +28,40 @@ int main(int argc, char** argv){
|
|||||||
MPI_Comm_rank(MCW, &rank);
|
MPI_Comm_rank(MCW, &rank);
|
||||||
MPI_Comm_size(MCW, &size);
|
MPI_Comm_size(MCW, &size);
|
||||||
multiplyMatrix(rank, size);
|
multiplyMatrix(rank, size);
|
||||||
if(!rank)
|
|
||||||
read_in_matrices();
|
|
||||||
MPI_Finalize();
|
MPI_Finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void read_in_matrices() {
|
||||||
|
ifstream f("Matrix.txt");
|
||||||
|
f >> N;
|
||||||
|
// Allocate memory
|
||||||
|
A = new double *[N];
|
||||||
|
for (int i = 0; i < N; ++i)
|
||||||
|
A[i] = new double[N];
|
||||||
|
B = new double *[N];
|
||||||
|
for (int i = 0; i < N; ++i)
|
||||||
|
B[i] = new double[N];
|
||||||
|
AB = new double *[N];
|
||||||
|
for (int i = 0; i < N; ++i)
|
||||||
|
AB[i] = new double[N];
|
||||||
|
AB_serial = new double *[N];
|
||||||
|
for (int i = 0; i < N; ++i)
|
||||||
|
AB_serial[i] = new double[N];
|
||||||
|
|
||||||
void read_in_matrices(){
|
// Fill Matricies
|
||||||
for(int i = 0; i < 2; i++) {
|
for (int i = 0; i < N; i++)
|
||||||
Matrix* matrix = new Matrix;
|
for (int j = 0; j < N; j++)
|
||||||
ifstream f("Matrix.txt");
|
f >> A[i][j];
|
||||||
f >> matrix->M >> matrix->N;
|
for (int i = 0; i < N; i++)
|
||||||
// Allocate memory
|
for (int j = 0; j < N; j++)
|
||||||
matrix->matrix = new double *[matrix->M];
|
f >> B[i][j];
|
||||||
for (int i = 0; i < matrix->M; ++i)
|
for (int i = 0; i < N; i++)
|
||||||
matrix->matrix[i] = new double[N];
|
for (int j = 0; j < N; j++)
|
||||||
|
AB[i][j] = 0;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//Function to print matrix
|
//Function to print matrix
|
||||||
void print_matrix(double mat[][N]){
|
void print_matrix(double **mat) {
|
||||||
for (int i = 0; i < N; i++){
|
for (int i = 0; i < N; i++){
|
||||||
for (int j = 0; j<N; j++){
|
for (int j = 0; j<N; j++){
|
||||||
cout << mat[i][j] << " ";
|
cout << mat[i][j] << " ";
|
||||||
@@ -81,7 +70,7 @@ void print_matrix(double mat[][N]){
|
|||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
//Serial version of solution
|
// Serial multiplication works.
|
||||||
void serial_version(){
|
void serial_version(){
|
||||||
for (int i = 0; i <N; i++){
|
for (int i = 0; i <N; i++){
|
||||||
for (int j = 0; j < N;j++){
|
for (int j = 0; j < N;j++){
|
||||||
@@ -108,27 +97,37 @@ void multiplyMatrix(int rank, int size){
|
|||||||
double time1,time2,time3;
|
double time1,time2,time3;
|
||||||
//compute interval size
|
//compute interval size
|
||||||
//rank 0 responsible for remainder
|
//rank 0 responsible for remainder
|
||||||
int interval = N/size;
|
|
||||||
int remainder = N%size;
|
|
||||||
//Record start time
|
|
||||||
MPI_Barrier(MCW);
|
|
||||||
time1 = MPI_Wtime();
|
|
||||||
|
|
||||||
//Rank 0 fills the matrices and computes the remainder
|
//Rank 0 fills the matrices and computes the remainder
|
||||||
if(!rank){
|
if(!rank){
|
||||||
fill_matrices();
|
read_in_matrices();
|
||||||
compute_interval(size*interval,remainder);
|
}
|
||||||
|
MPI_Bcast(&N, 1, MPI_INT, 0, MCW);
|
||||||
|
// Record start time
|
||||||
|
MPI_Barrier(MCW);
|
||||||
|
time1 = MPI_Wtime();
|
||||||
|
|
||||||
|
int interval = N / size;
|
||||||
|
int remainder = N % size;
|
||||||
|
MPI_Bcast(&interval, 1, MPI_INT, 0, MCW);
|
||||||
|
MPI_Bcast(&remainder, 1, MPI_INT, 0, MCW);
|
||||||
|
|
||||||
|
if (!rank) {
|
||||||
|
compute_interval(size * interval, remainder);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Broadcast Matrix B and scatter relevant portions of Matrix A
|
//Broadcast Matrix B and scatter relevant portions of Matrix A
|
||||||
|
print_matrix(A);
|
||||||
MPI_Bcast(B,N*N,MPI_DOUBLE,0,MCW);
|
MPI_Bcast(B,N*N,MPI_DOUBLE,0,MCW);
|
||||||
MPI_Scatter(A,interval*N,MPI_DOUBLE,A[rank*interval],interval*N,MPI_DOUBLE,0,MCW);
|
MPI_Scatter(A, interval * N, MPI_DOUBLE, &A[rank * interval], interval * N,
|
||||||
|
MPI_DOUBLE, 0, MCW);
|
||||||
|
|
||||||
//Each processor cumputes the interval they are responsible for
|
//Each processor cumputes the interval they are responsible for
|
||||||
compute_interval(rank*interval,interval);
|
compute_interval(rank*interval,interval);
|
||||||
|
|
||||||
//Gather results
|
//Gather results
|
||||||
MPI_Gather(AB[rank*interval],interval*N,MPI_DOUBLE,AB,interval*N,MPI_DOUBLE,0,MCW);
|
MPI_Gather(AB[rank * interval], interval * N, MPI_DOUBLE, &AB[0][0],
|
||||||
|
interval * N, MPI_DOUBLE, 0, MCW);
|
||||||
|
|
||||||
//Record parallel finish time
|
//Record parallel finish time
|
||||||
MPI_Barrier(MCW);
|
MPI_Barrier(MCW);
|
||||||
@@ -155,9 +154,9 @@ void multiplyMatrix(int rank, int size){
|
|||||||
print_matrix(A);
|
print_matrix(A);
|
||||||
cout << "multiplied Matrix B:" << endl;
|
cout << "multiplied Matrix B:" << endl;
|
||||||
print_matrix(B);
|
print_matrix(B);
|
||||||
cout << "gives matrix AB:" << endl;
|
|
||||||
print_matrix(AB);
|
|
||||||
cout << "serial version gives:" << endl;
|
cout << "serial version gives:" << endl;
|
||||||
print_matrix(AB_serial);
|
print_matrix(AB_serial);
|
||||||
|
cout << "gives matrix AB:" << endl;
|
||||||
|
print_matrix(AB);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user