Created
March 1, 2011 03:11
-
-
Save Osmose/848538 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 "mpi.h" | |
#include <stdio.h> | |
int main(int argc, char *argv[]) { | |
int numprocs, myid; | |
int rows; | |
int pagesPerProcessor; | |
int begin; | |
int pageCount; | |
int k, m; | |
double dampingFactor; | |
double* xLocal; | |
double* xGlobal; | |
double* aMatrix; | |
double* totalMatrix; | |
// Init MPI | |
MPI_Init(&argc, &argv); | |
MPI_Comm_size(MPI_COMM_WORLD, &numprocs); | |
MPI_Comm_rank(MPI_COMM_WORLD, &myid); | |
// Proc 0 init stuff | |
if (myid == 0) { | |
rows = 100; | |
dampingFactor = 0.15; | |
pagesPerProcessor = rows / numprocs; | |
} | |
// Broadcast initial data | |
MPI_Bcast(&dampingFactor, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
MPI_Bcast(&pagesPerProcessor, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
pageCount = pagesPerProcessor * numprocs; | |
xLocal = new double[pagesPerProcessor]; | |
xGlobal = new double[pageCount]; | |
aMatrix = new double[pagesPerProcessor * pageCount]; | |
if (myid == 0) { | |
totalMatrix = new double[pageCount * pageCount]; | |
} | |
// Populate xLocal | |
for (k = 0; k < pagesPerProcessor; k++) { | |
xLocal[k] = 1.0f / (pageCount); | |
} | |
// Populate aMatrix | |
// aMatrix[NodeFrom][NodeTo] | |
begin = pagesPerProcessor * myid; | |
// Add links to next pages | |
for (k = 0; k < pagesPerProcessor - 1; k++) { | |
aMatrix[(k * pageCount) + (begin + k + 1)] = 0.5f; | |
} | |
// Add links to previous pages | |
for (k = 1; k < pagesPerProcessor - 1; k++) { | |
aMatrix[(k * pageCount) + (begin + (k - 1))] = 0.5f; | |
} | |
// Set last node correctly | |
aMatrix[((pagesPerProcessor - 1) * pageCount) + (begin + pagesPerProcessor - 2)] = 1.0f; | |
// Set first node correctly | |
if (myid == 0) { | |
aMatrix[1] = 1.0f; | |
} else { | |
aMatrix[0] = 0.5f; | |
} | |
MPI_Gather(aMatrix, pagesPerProcessor * pageCount, MPI_DOUBLE, | |
totalMatrix, pageCount * pageCount, MPI_DOUBLE, | |
0, MPI_COMM_WORLD); | |
if (myid == 0) { | |
for (k = 0; k < pageCount; k++) { | |
for (m = 0; m < pageCount; m++) { | |
printf("%1.1f ", totalMatrix[(m * pageCount) + k]); | |
} | |
printf("\n"); | |
} | |
// Clean up memory | |
delete [] totalMatrix; | |
} | |
// Clean up memory | |
delete [] aMatrix; | |
delete [] xLocal; | |
delete [] xGlobal; | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment