Skip to content

Commit

Permalink
Imported project file
Browse files Browse the repository at this point in the history
  • Loading branch information
kraihn committed Jul 19, 2012
1 parent 19f513f commit bab9557
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <mpi.h>
#include <sys/time.h>

#define MASTER 0
#define TAG 0
#define MSGSIZE 1000
#define MAX 25

using namespace std;

int main(int argc, char* argv[])
{
int my_rank, source, num_nodes, workers;
int buffer[MSGSIZE];

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_nodes);
workers = num_nodes - 1;

// Build matrix to fit the number of workers, but won't work if workers are greater tand 1000
int **matrix;
matrix = new int *[workers];
for(int i = 0; i < workers; i++){
matrix[i] = new int[workers];
}
for(int i = 0, x = 0; i < workers; i++)
{
for(int j = 0; j < workers; j++, x++)
{
matrix[i][j] = x;
}
}

if (my_rank != MASTER) {
int send[1];

for(int i = workers - my_rank, x = 0; x < workers; i++, x++){
// Can't wander off the array
if(i >= workers)
i = 0;

// Can't talk to yourself
if((i + 1) == my_rank)
continue;

send[0] = matrix[my_rank-1][i];

// Send first
if(my_rank < (i + 1)){
MPI_Send(send, 1, MPI_INT, (i + 1), TAG, MPI_COMM_WORLD);
MPI_Recv(buffer, 1, MPI_INT, (i + 1), TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
matrix[my_rank-1][i]=buffer[0];
}

// Receive first
else{
MPI_Recv(buffer, 1, MPI_INT, (i + 1), TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
matrix[my_rank-1][i]=buffer[0];
MPI_Send(send, 1, MPI_INT, (i + 1), TAG, MPI_COMM_WORLD);
}
}

MPI_Send(matrix[my_rank-1], MSGSIZE, MPI_INT, MASTER, TAG, MPI_COMM_WORLD);
}
else {
//sleep(20);
printf ("Num_nodes: %d\n", num_nodes);
printf ("Hello from Master (process %d)!\n", my_rank);
cout << "Initial array:" << endl;
for(int i = 0, x = 0; i < workers; i++)
{
for(int j = 0; j < workers; j++, x++)
{
if(matrix[i][j] < 10)
cout << " ";
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;

// Get results
for (source = 1; source < num_nodes; source++) {
MPI_Recv(buffer, MSGSIZE, MPI_INT, source, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for(int i = 0; i < workers; i++)
matrix[source-1][i] = buffer[i];
}

cout << "Transposed array:" << endl;
for(int i = 0, x = 0; i < workers; i++)
{
for(int j = 0; j < workers; j++, x++)
{
if(matrix[i][j] < 10)
cout << " ";
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

MPI_Finalize();

return 0;
}

0 comments on commit bab9557

Please sign in to comment.