2016-03-30 23 views
2

MPI'de dinamik işlem oluşturmaya başladım. Yeni çalışan/çocuk süreçleri (worker.c) oluşturmaya ve bir intracommunicator'a birleştirmeye çalışan bir ana kodum (main.c) var. ebeveyn kodu (main.c) 'dirMPI spawn ile birleştirme ve birleştirme

#include<stdio.h> 
#include "mpi.h" 

MPI_Comm child_comm; 
int rank, size; 
MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
MPI_Comm_size(MPI_COMM_WORLD, &size); 

if(rank == 0) 
{ 
    int num_processes_to_spawn = 2; 
    MPI_Comm_spawn("worker", MPI_ARGV_NULL, num_processes_to_spawn, MPI_INFO_NULL, 0, MPI_COMM_SELF, &child_comm, MPI_ERRCODES_IGNORE); 

MPI_Comm intra_comm; 
MPI_Intercomm_merge(child_comm,0, &intra_comm); 
MPI_Barrier(child_comm); 


int tmp_size; 
MPI_Comm_size(intra_comm, &tmp_size); 
printf("size of intra comm world = %d\n", tmp_size); 

MPI_Comm_size(child_comm, &tmp_size); 
printf("size of child comm world = %d\n", tmp_size); 

MPI_Comm_size(MPI_COMM_WORLD, &tmp_size); 
printf("size of parent comm world = %d\n", tmp_size); 

} 

MPI_Finalize(); 

işçi (çocuk) kodudur:

#include<stdio.h> 
    #include "mpi.h" 
    int main(int argc, char *argv[]) 
    { 
    int numprocs, myrank; 
    MPI_Comm parentcomm; 
    MPI_Comm intra_comm; 

    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs); 
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 

    MPI_Comm_get_parent(&parentcomm); 

    MPI_Intercomm_merge(parentcomm, 1, &intra_comm); 
    MPI_Barrier(parentcomm); 

    if(myrank == 0) 
    { 
    int tmp_size; 
    MPI_Comm_size(parentcomm, &tmp_size); 
    printf("child size of parent comm world = %d\n", tmp_size); 

    MPI_Comm_size(MPI_COMM_WORLD, &tmp_size); 
    printf("child size of child comm world = %d\n", tmp_size); 

    MPI_Comm_size(intra_comm, &tmp_size); 
    printf("child size of intra comm world = %d\n", tmp_size); 

    MPI_Finalize(); 
    return 0; 
    } 
} 

Ben bekliyoruz, bölünmeden sonra kullanarak bu kodu

mpirun -np 12 main.c 

çalıştırın ve birleştirme

size of intra comm world = 14 
size of child comm world = 2 
size of parent comm world = 12 
child size of parent comm world = 12 
child size of child comm world = 2 
child size of intra comm world = 14 

Ancak çıktıyı yanlış çıkış

size of intra comm world = 3 
    size of child comm world = 1 
    size of parent comm world = 12 
    child size of parent comm world = 2 
    child size of child comm world = 2 
    child size of intra comm world = 3 

Hata nerede olduğunu anlamıyorum, birilerinin bana hatanın nerede olduğunu bildirmesine izin verebilir.

sayesinde Kris

cevap

1

Kodunuz burada sıralamak çalışacağım birkaç sorunlarıyla karşı karşıya kalır: Ana bölümde

  • , sadece süreç 0 çağrılar MPI_Comm_spawn(). Bu bir hata değildir (özellikle MPI_COMM_SELF'u ana iletişimci olarak kullandığınız için), ancak fiili olarak sonraki tüm birleşmelerden diğer tüm işlemleri hariç tutar.
  • Hem ana hem de çalışan parçalarda, MPI_Comm_remote_size() yerine uzak Communicator'ın boyutunu almak için MPI_Comm_size() kullanın. Bu nedenle, uzaktan iletişimcinin boyutu yerine, yalnızca Communicator'ın içindeki yerel iletişimcisinin boyutunu elde edersiniz. ana kod, yalnızca süreçte
  • 0 aramalar MPI_Finalise() İşte

senin kodlarının bazı sabit versiyonu (main() ve MPI_Init() eksik olduğunu saymıyorum):

master.c

#include <stdio.h> 
#include <mpi.h> 

int main(int argc, char *argv[]) { 

    MPI_Init(&argc, &argv); 
    int rank; 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 

    MPI_Comm child_comm; 
    int num_processes_to_spawn = 2; 
    MPI_Comm_spawn("./worker", MPI_ARGV_NULL, 
        num_processes_to_spawn, MPI_INFO_NULL, 
        0, MPI_COMM_WORLD, 
        &child_comm, MPI_ERRCODES_IGNORE); 

    MPI_Comm intra_comm; 
    MPI_Intercomm_merge(child_comm, 0, &intra_comm); 

    if (rank == 0) { 
     int tmp_size; 
     MPI_Comm_size(intra_comm, &tmp_size); 
     printf("size of intra comm world = %d\n", tmp_size); 

     MPI_Comm_remote_size(child_comm, &tmp_size); 
     printf("size of child comm world = %d\n", tmp_size); 

     MPI_Comm_size(MPI_COMM_WORLD, &tmp_size); 
     printf("size of parent comm world = %d\n", tmp_size); 
    } 

    MPI_Finalize(); 

    return 0; 
} 

worker.c

~> mpirun -n 12 ./master 
child size of parent comm world = 12 
child size of child comm world = 2 
child size of intra comm world = 14 
size of intra comm world = 14 
size of child comm world = 2 
size of parent comm world = 12 
+0

Teşekkür Gilles: benim laptop verir

#include <stdio.h> #include <mpi.h> int main(int argc, char *argv[]) { MPI_Init(&argc, &argv); int myrank; MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Comm parentcomm; MPI_Comm_get_parent(&parentcomm); MPI_Comm intra_comm; MPI_Intercomm_merge(parentcomm, 1, &intra_comm); if (myrank == 0) { int tmp_size; MPI_Comm_remote_size(parentcomm, &tmp_size); printf("child size of parent comm world = %d\n", tmp_size); MPI_Comm_size(MPI_COMM_WORLD, &tmp_size); printf("child size of child comm world = %d\n", tmp_size); MPI_Comm_size(intra_comm, &tmp_size); printf("child size of intra comm world = %d\n", tmp_size); } MPI_Finalize(); return 0; } 

. Uzak grup büyüklüğü sorunu olduğunu fark ettim; – marc

İlgili konular