2013-10-26 2 views
2

첫 번째 MPICH2 프로그램을 두 대의 PC에서 LAN으로 실행했습니다. 나는 클라이언트에서 입력하고 명령은 다음과 같습니다MPICH2 : 프로세스의 컴퓨터 이름을 가져 오는 API

[email protected]:/home# mpiexec -f hosts.cfg -n 4 ./hello 
Hello world from process 3 of 4 
Hello world from process 2 of 4 
Hello world from process 1 of 4 
Hello world from process 0 of 4 

내 프로그램은 이것이다 : 나는 로컬로 각 시스템에서 실행 파일을 얻을 수 MPI_hello.c를 컴파일

/* C Example */ 
#include <mpi.h> 
#include <stdio.h> 

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

    MPI_Init (&argc, &argv);  /* starts MPI */ 
    MPI_Comm_rank (MPI_COMM_WORLD, &rank);  /* get current process id */ 
    MPI_Comm_size (MPI_COMM_WORLD, &size);  /* get number of processes */ 
    printf("Hello world from process %d of %d\n", rank, size); 
    MPI_Finalize(); 
    return 0; 
} 

.

Hello world from process 3 running on PC2 of 4 
Hello world from process 2 running on PC2 of 4 
Hello world from process 1 running on PC1 of 4 
Hello world from process 0 running on PC1 of 4 

PC1과 PC2 내 MPI 프로그램을 실행하도록되어 두의 PC의 이름입니다 : 그것은 이런 식으로 뭔가를 출력해야한다 있도록

나는 코드를 수정합니다. 기본적으로 각 프로세스와 함께 컴퓨터의 이름을 가져올 API를 찾고 있습니다.

어떻게하면됩니까?

업데이트

damienfrancois 년대 모두 대답은 완벽하게 잘 작동했다.

[email protected]:/home# mpiexec -f hosts.cfg -n 4 ./hello 
Hello world from process 1 running on PC1 of 4 
Hello world from process 3 running on PC1 of 4 
Hello world from process 2 running on PC2 of 4 
Hello world from process 0 running on PC2 of 4 

프로세스 ID의 할당은

답변

2

하나의 옵션이 gethostname(2) 시스템 호출을 사용하는 것입니다 hosts.cfg 파일에 언급되어야 친화의 문제입니다 : 여기 내 출력 :

/* C Example */ 
#include <mpi.h> 
#include <stdio.h> 
#include <stddef.h> 
#include <stdlib.h> 

int main (int argc, char* argv[]) 
{ 
    int rank, size; 
    int buffer_length = 512; 
    char hostname[buffer_length]; 


    MPI_Init (&argc, &argv);  /* starts MPI */ 
    MPI_Comm_rank (MPI_COMM_WORLD, &rank);  /* get current process id */ 
    MPI_Comm_size (MPI_COMM_WORLD, &size);  /* get number of processes */ 

    gethostname(hostname, buffer_length); /* get hostname */ 

    printf("Hello world from process %d running on %s of %d\n", rank, hostname, size); 
    MPI_Finalize(); 
    return 0; 
} 

또 다른

은 사용하는 것입니다 MPI_Get_processor_name :

/* C Example */ 
#include <mpi.h> 
#include <stdio.h> 
#include <stddef.h> 
#include <stdlib.h> 

int main (int argc, char* argv[]) 
{ 
    int rank, size; 
    int buffer_length = MPI_MAX_PROCESSOR_NAME; 
    char hostname[buffer_length]; 

    MPI_Init (&argc, &argv);  /* starts MPI */ 
    MPI_Comm_rank (MPI_COMM_WORLD, &rank);  /* get current process id */ 
    MPI_Comm_size (MPI_COMM_WORLD, &size);  /* get number of processes */ 

    MPI_Get_processor_name(hostname, &buffer_length); /* get hostname */ 

    printf("Hello world from process %d running on %s of %d\n", rank, hostname, size); 
    MPI_Finalize(); 
    return 0; 
} 
관련 문제