attempt 2
git-svn-id: svn://anubis/gvsu@249 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
ccf6d9f0fe
commit
520420f548
@ -1,8 +1,6 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h> /* gethostname() */
|
||||
#include <stdlib.h> /* rand() */
|
||||
#include <mpi.h>
|
||||
using namespace std;
|
||||
|
||||
@ -56,11 +54,13 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Initialize the matrix */
|
||||
/* Initialize the matrices */
|
||||
int matrix[n][n];
|
||||
for (int i = 0; i < n; i++)
|
||||
for (int j = 0; j < n; j++)
|
||||
for (int i = 0; i < n; i++) /* zero the matrices for prettier */
|
||||
for (int j = 0; j < n; j++) /* printing */
|
||||
matrix[i][j] = 0;
|
||||
|
||||
/* Determine which rows I am responsible for */
|
||||
int my_first_row;
|
||||
int my_num_rows;
|
||||
taskAllocate(n, p, my_rank, &my_first_row, &my_num_rows);
|
||||
@ -85,25 +85,21 @@ int main(int argc, char * argv[])
|
||||
total += count;
|
||||
}
|
||||
|
||||
/* Transpose the matrix with p gather operations */
|
||||
int recvbuf[p];
|
||||
for (int i = 0; i < p; i++)
|
||||
/* Transpose the matrix with n gather operations */
|
||||
for (int i = 0, toproc = 0, proccount = counts[0]; i < n; i++)
|
||||
{
|
||||
int col_i_vals_for_proc_p[my_num_rows];
|
||||
int my_col_i_vals[my_num_rows];
|
||||
for (int row_offset = 0; row_offset < my_num_rows; row_offset++)
|
||||
my_col_i_vals[row_offset] = matrix[my_first_row + row_offset][i];
|
||||
MPI_Gatherv(&my_col_i_vals[0], my_num_rows, MPI_INT,
|
||||
&matrix[i][0], &counts[0], &displs[0],
|
||||
MPI_INT, toproc, MPI_COMM_WORLD);
|
||||
proccount--;
|
||||
if (proccount <= 0)
|
||||
{
|
||||
col_i_vals_for_proc_p[row_offset] =
|
||||
matrix[my_first_row + row_offset][i];
|
||||
toproc++;
|
||||
proccount = ccounts[toproc];
|
||||
}
|
||||
MPI_Gatherv(&col_i_vals_for_proc_p[0], my_num_rows, MPI_INT,
|
||||
&recvbuf[0], &counts[0], &displs[0],
|
||||
MPI_INT, i, MPI_COMM_WORLD);
|
||||
}
|
||||
|
||||
/* Put my received entries into my columns of the matrix */
|
||||
for (int i = 0; i < p; i++)
|
||||
{
|
||||
matrix[i][my_rank] = recvbuf[i];
|
||||
}
|
||||
|
||||
/* Print out the final matrix */
|
||||
@ -114,7 +110,7 @@ int main(int argc, char * argv[])
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cout << matrix[i][j] << " ";
|
||||
cout << recvmatrix[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user