MPI广播矢量矢量

时间:2016-10-19 14:47:02

标签: c++ parallel-processing mpi broadcast

我正在从根进程广播const int num_rows1 = 4, num_rows2 = 4, num_cols1 = 4, num_cols2 = 4; const int root = 0; int i, j, k; vector<vector<int>> matrix1(num_rows1, vector <int>(num_cols1)); vector<vector<int>> matrix2(num_rows1, vector <int>(num_cols1)); vector<vector<int>> result(num_rows1, vector <int>(num_cols1)); int finale[num_rows1][num_cols2]; vector<vector<int>> transpose_mat(num_rows2, vector <int>(num_cols2)); vector<int> column1; vector<int> column2; double start_time, end_time; int * column3 = new int[]; //Function generating random matrices vector<vector<int>> generate_matrix(int nrow, int ncol) { vector<vector<int>> matrix(nrow, vector <int>(ncol)); for (int i = 0; i < nrow; ++i) { for (int j = 0; j < ncol; ++j) { matrix[i][j] = (15 *rand() / RAND_MAX - 3); } } return matrix; } //function taking the transpose vector<vector<int>>transpose(vector<vector<int>> matrix , int nrow, int ncol) { //Transpose of matrix 2 for (i = 0; i < nrow; ++i) for (j = 0; j < ncol; ++j) { transpose_mat[j][i] = matrix2[i][j]; } cout << "Transpose " << endl; for (int i = 0; i < num_rows2; ++i) { for (int j = 0; j < num_cols2; ++j) { cout << transpose_mat[i][j] << " "; } cout << endl; } return transpose_mat; } //main function int main(int argc, char *argv[]) { MPI_Status status; MPI_Request request; int tag = 1; int rank; int world_size; //Number of processes // Initialize the MPI environment MPI_Init(NULL, NULL); // Get the rank of the process MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get the name of the processor char processor_name[MPI_MAX_PROCESSOR_NAME]; int name_len; MPI_Get_processor_name(processor_name, &name_len); // Get the number of processes MPI_Comm_size(MPI_COMM_WORLD, &world_size); if (rank == root) { cout << "I am the root process" << endl; cout << "Filling and Displaying the first matrix" << endl; //Filling matrix1 = generate_matrix(num_rows1, num_cols1); //Display for (int i = 0; i < num_rows1; ++i) { for (int j = 0; j < num_cols1; ++j) { cout << matrix1[i][j] << " "; } cout << endl; } int x = matrix1.size(); //The first process(root process -> rank = 0) then broadcasts the first matrix to all the processes for (int i = 0; i < num_rows1; ++i) { for (int j = 0; j < num_cols1; ++j) { MPI_Bcast(&matrix1[i][0], x, MPI_INT, root, MPI_COMM_WORLD); } } } //The second process (-> rank = 1) fills the second matrix with random integers and prints it else if (rank == 1) { srand(time(NULL)); cout << "Filling and Displaying the second matrix" << endl; //Filling matrix2 = generate_matrix(num_rows2, num_cols2); //Display for (int i = 0; i < num_rows2; ++i) { for (int j = 0; j < num_cols2; ++j) { cout << matrix2[i][j] << " "; } cout << endl; } //Calling the function that determines transpose of the second matrix transpose_mat = transpose(matrix2, num_rows2, num_cols2); //Displaying the transpose } if (rank > root) { cout << "I am process " << rank << endl; int size = matrix1.size(); result.resize(size); for (int i = 0; i < num_rows1; ++i) { for (int j = 0; j < num_cols1; ++j) { //Receiving the first matrix broad casted by root process (-> rank = 0) MPI_Bcast(&result[j][0], size, MPI_INT, root, MPI_COMM_WORLD); //} } } cout << "The received matrix at process " << rank << " is: " << endl; for (int i = 0; i < num_rows1; ++i) { for (int i = 0; i < num_cols1; ++i) { cout << result[j][i] <<" "; } cout << endl; } MPI_Barrier(MPI_COMM_WORLD); //Terminating MPI MPI_Finalize(); return 0; } 一个2D矩阵实际上),然后在剩余的进程中接收。

但是当我尝试打印结果时,每次都会显示不规则的行为。 任何帮助,将不胜感激。

矢量大小为4x4(暂时使用固定大小)

这是完整的代码。

result[][]

我尝试在接收进程(即result)上调整向量的大小,但它会产生一些错误。 上面代码的结果是一个4x4矩阵(matrix1),它有时只包含第一行export default class Header extends Component{ render(){ let activeStyle = {"backgroundColor": "green"}; let inActiveStyle = {"backgroundColor": "red"}; return( <div className="profile-header" style={(this.props.active)? activeStyle:inActiveStyle}> <input type="checkbox" checked={this.props.active} readOnly/> </div> ); } } ,有时会产生内存问题。

0 个答案:

没有答案