Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

MPIprograms / main3

.cpp
Скачиваний:
7
Добавлен:
08.04.2015
Размер:
1.2 Кб
Скачать
#include <iostream>
#include "mpi.h"

int main( int argc, char **argv ) {
  using std::cin; using std::cout; using std::endl;
  int flag; MPI_Initialized(&flag);
  if ( !flag ) MPI_Init( &argc, &argv );
  int myrank; MPI_Comm_rank( MPI_COMM_WORLD, &myrank );
  int commsize; MPI_Comm_size( MPI_COMM_WORLD, &commsize );
  char source[256];
  char dest[256];
  char *bigsource;
  char *bigdest;
  if ( myrank == 0 ) {
    bigsource = new char[commsize*256]; 
    bigdest = new char[commsize*256];
    for ( int i = 0; i < commsize; i++ ) {
      cout << "Process 0: what to send to process " << i << "? "; 
      cin >> &bigsource[i*256];
    }
  }
  MPI_Scatter( bigsource, 256, MPI_CHAR, dest, 256, MPI_CHAR, 0, MPI_COMM_WORLD );
    cout << "Process " << myrank << " has received " << dest << endl;
    itoa( myrank, source, 10 ); strcat( dest, " " ); strcat( dest, source );
  MPI_Gather( dest, 256, MPI_CHAR, bigdest, 256, MPI_CHAR, 0, MPI_COMM_WORLD );
  if ( myrank == 0 ) {
    cout << "Process 0 has received:\n";
    for ( int i = 0; i < commsize; i++ )
      cout << &bigdest[i*256] << " from process " << i << endl;
  }
  MPI_Finalize();
  return 0;
}
Соседние файлы в папке MPIprograms