網頁

2017年1月17日 星期二

MPI_Comm_split以及worker_id的關係

第九章利用MPI_Comm_split產生出新的communicator叫做worker_comm

MPI_UNDEFINED: CPU=0的color沒有分類到worker_comm的communicator

//CPU=0
if (!id) {  
  MPI_Comm_split (MPI_COMM_WORLD, MPI_UNDEFINED, id, &worker_comm);
  manager (&argc, &argv, p);
}
//非CPU=0的processors
else {  
  MPI_Comm_split (MPI_COMM_WORLD, 0, id, &worker_comm);
  worker (&argc, &argv, worker_comm);
}

$ mpicc check_worker.c
$ mpirun -np 4 a.out

/* check_worker.c */
#include "stdio.h"
#include <mpi.h>

void manager (int argc, char *argv[], int p) {
  int id;
  MPI_Comm_rank (MPI_COMM_WORLD, &id);
  printf("manager id=%d\n", id);
  fflush (stdout);
}

void worker (int argc, char *argv[], MPI_Comm worker_comm) {
  int id;
  int worker_id;

  MPI_Comm_rank (MPI_COMM_WORLD, &id);
  printf("manager id=%d\n", id);
  fflush (stdout);

  MPI_Comm_rank (worker_comm, &worker_id);
  if (!worker_id) printf("worker id=%d\n", worker_id);
  if (worker_id) printf("worker id=%d\n", worker_id);
  fflush (stdout);
  //printf("%d", worker_id);
}

int main (int argc, char *argv[])
{
  int id, p;  
  MPI_Comm worker_comm; 

  MPI_Init (&argc, &argv);
  MPI_Comm_rank (MPI_COMM_WORLD, &id);
  MPI_Comm_size (MPI_COMM_WORLD, &p);

  if (!id) {  
    MPI_Comm_split (MPI_COMM_WORLD, MPI_UNDEFINED, id, &worker_comm);
    manager (&argc, &argv, p);
  }
  else {  
    MPI_Comm_split (MPI_COMM_WORLD, 0, id, &worker_comm);
    worker (&argc, &argv, worker_comm);
  }

  printf ("%d\n",prime[7]);
  return 0;

}

out:
manager id=0

manager id=1
worker id=0

manager id=2
worker id=1

manager id=3
worker id=2

從螢幕輸出可以發現
CPU1在worker_comm communicator的id變成0
CPU2在worker_comm communicator的id變成1
以此類推




沒有留言:

張貼留言