The norm is found as the maximum modulo sum of the elements of a string. It seems that something is parallelized, the processes calculate the sums, but only one process enters the cycle of finding the maximum. Yes, and how to make the program work under the condition that the number of processes is less than the matrix elements, that is, one process will have to work with several iterations?

#include <iostream> #include <cmath> #include<stdio.h> #include "conio.h" #include <mpi.h> const int N=10; int max,sum,t; int a[N][N],b[N];//задаем массив int proc_count, proc_this, quant, ibeg; int main(int argc,char** argv) { sum=0; max=0; for ( int i = 0; i < N; i++ ) { for ( int j = 0; j < N; j++) { a[i][j]=rand()%20; }//заполняем массив } MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&proc_count); MPI_Comm_rank(MPI_COMM_WORLD,&proc_this); t = MPI_Wtime(); for ( int i = 0; i < N; i++) { if (proc_this == i) //каждый процесс делает одну итерацию { for ( int j = 0; j < N; j++) { sum += abs( a[ i ][ j ] ); } b[i] = sum; } sum = 0; } if (proc_this == 0) { max=b[0]; for ( int k=1; k < N; k++ ) { if ( b[ k ] > max ) { max = b[ k ]; } } printf("norma=%d",max); printf("time=%f", MPI_Wtime()-t); } MPI_Finalize(); } 
  • @noidea, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

So that a process can perform more than one iteration, a comparison can be made modulo proc_count , assuming that 0 <= proc_this < proc_count .

The cycle with the maximum should also use if (proc_this == k % proc_count) . And then call MPI_Reduce() to find the global maximum. Processes work with local copies of data, unless explicitly transferred.