pmpi简单实例

来源:互联网 发布:mysql数据库5.5.20安装 编辑:程序博客网 时间:2024/05/19 20:18

1. Compile OpenMPI with --enable-mpi-profile. This option will enable building of PMPI interface.

./configure --prefix=/home/dycz0fx/opt/ompi/debug --enable-mpirun-prefix-by-default --enable-debug --enable-mpi-profile

2. Build OpenMPI

make all install

3. Write PMPI profiler, name it as test_pmpi.c.

/* PMPI profiler example */#include <stdio.h>#include "mpi.h"int MPI_Init(int *argc, char ***argv){    int rank, err;    err = PMPI_Init(argc, argv);    PMPI_Comm_rank(MPI_COMM_WORLD, &rank) ;    printf("[%d]: Profile MPI_Init.\n", rank) ;    return err;}int MPI_Finalize(){int rank;PMPI_Comm_rank(MPI_COMM_WORLD, &rank) ;printf("[%d]: Profile MPI_Finalize.\n", rank) ;return PMPI_Finalize( );}


4. Compile test_pmpi.c to a object file.

mpicc -c test_pmpi.c -o test_pmpi.o

5. Produce a static library with the object file test_pmpi.o.

ar rcs libtestpmpi.a test_pmpi.o

6. For any MPI program, for example bcast.c below.

/* MPI_Bcast example */
#include <mpi.h>#include <stdio.h>#define SIZE 10#define TIMES 1int main(int argc, char** argv) {    int rank;    int buf[SIZE];    const int root=1;    int i, j;    MPI_Init(&argc, &argv);    MPI_Comm_rank(MPI_COMM_WORLD, &rank);    for (j = 0; j<TIMES; j++) {        if(rank == root) {            for (i=0; i<SIZE; i++) {                buf[i] = 10 * i;            }        }        else{            for (i=0; i<SIZE; i++) {                buf[i] = 0;            }        }        printf("[%d]: Before Bcast, buf is", rank);        for (i=0; i<SIZE; i++) {            printf(" %d", buf[i]);        }        printf("\n");        /* everyone calls bcast, data is taken from root and ends up in everyone's buf */        MPI_Bcast(&buf, SIZE, MPI_INT, root, MPI_COMM_WORLD);        printf("[%d]: After Bcast, buf is", rank);        for (i=0; i<SIZE; i++) {            printf(" %d", buf[i]);        }        printf("\n");    }    MPI_Finalize();    return 0;}~            
Compile bcast.c with the link to the static library created before.

mpicc bcast.c -L. -ltestpmpi -g -o bcast

0 0
原创粉丝点击