zoukankan      html  css  js  c++  java
  • 初识并发编程 MPI

    MPI是一个跨语言的通讯协议,用于并发编程。MPI标准定义了一组具有可移植性的编程接口。

    安装环境

    MPICH 是开源的消息传递接口(MPI)标准的实现。
    下载地址

    # 解压文件
    tar -xzvf mpich-3.2.1.tar.gz
    cd mpich-3.2.1
    # /usr/local/Cellar/mpich 改为你要安装 MPICH 的路径
    ./configure –-prefix=/usr/local/Cellar/mpich |& tee c.log
    make |& tee m.log
    make install |& tee mi.log
    # 将你安装 MPICH 的路径添加到 PATH
    export PATH=/usr/local/Cellar/mpich:$PATH;
    

    编程例子

    1. 简单 MPI 编程之进程识别

    #include <mpi.h> // mpi 头文件
    #include <stdio.h>
    
    int main(int argc, char **argv) {
        int numprocs, myid;
        MPI_Init (&argc, &argv); // 初始化 MPI 执行环境
        MPI_Comm_size (MPI_COMM_WORLD, &numprocs); // 获取有多少个进程属于 MPI_COMM_WORLD 通信域
        MPI_Comm_rank (MPI_COMM_WORLD, &myid); // 获取当前进程的 id
    
        if (myid == 0) {
            // 进程号为0的进程执行的操作...
        } else {
            // 其它进程执行的操作...
        }
        // 所有进程都执行的操作...
    
        MPI_Finalize(); // 结束 MPI 执行环境
        return 0;
    }
    

    2. MPI 简单通信

    #include <mpi.h> // mpi 头文件
    #include <stdio.h>
    
    int main(int argc, char **argv) {
        int data[100], myid;
        MPI_Init (&argc, &argv);
        MPI_Comm_rank (MPI_COMM_WORLD, &myid); 
    
        if (myid == 0) 
            MPI_Send(data, 100, MPI_INT, 1, 0, MPI_COMM_WORLD);
        else if (myid == 1)
            MPI_Recv(data, 100, MPI_INT, 0, 0, MPI_COMM_WORLD,
                                        MPI_STATUS_IGNORE);
    
        MPI_Finalize(); // 结束 MPI 执行环境
        return 0;
    }
    

    其中,MPI_SEND(buf, count, datatype, dest, tag, comm) 是发送消息的 API,
    buf 是消息缓存区。
    count是消息大小。
    datatype是数据类型。
    dest是目的进程在指定的进程域 comm 的进程号。
    tag是用户定义的消息的类型。

    MPI_RECV(buf, count, datatype, source, tag, comm, status) 是(阻塞)接收消息的 API。
    source 是来源进程在指定的进程域 commMPI_ANY_SOURCE 的进程号。
    tag 可以是 MPI_ANY_TAG
    status 用来接收更多信息,可以用 MPI_STATUS_IGNORE 如果我们不需要更多信息。

    编译运行程序

    封装的编译器:

    • 对于 C 程序:mpicc test.c -o test
    • 对于 C++ 程序:mpicxx test.cpp -o test
    • 对于 Fortran 77 程序:mpif77 test.f -o test
    • 对于 Fortran 90 程序:mpif90 test.f90 -o test

    你也可以链接其它库:mpicc test.c -o test -lm

    运行:
    启动 16 个进程:mpiexec -n 16 ./test
    如果-n 指定的进程数超过了系统的CPU(核)数,就会报错如下:

    There are not enough slots available in the system to satisfy the 16 slots
    that were requested by the application:
    ./test

    Either request fewer slots for your application, or make more slots available
    for use.

    进阶一些:

    // 非阻塞发送。非阻塞接收为 MPI_Irecv,具体说明可以用 man 命令查询.
    int MPI_Isend(const void *buf, int count, MPI_Datatype datatype, int dest, int tag,
                  MPI_Comm comm, MPI_Request *request)
    
    // 检测非阻塞通信是否完成。阻塞等待为 MPI_Wait,可以等待全部(MPI_Waitall)、等待一些(MPI_Waitsome,MPI_Waitany)
    nt MPI_Test( MPI_Request *request, int *flag, MPI_Status *status );
    
    // 数据归约:通过计算收集到的多个数据得到一个数据。
    int MPI_Reduce (void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
    
  • 相关阅读:
    Mybatis配置文件中Insert 元素标签添加配置有哪些呢?
    Mybatis配置文件中Select元素标签输入参数有多少种输入方式呢?
    Mybatis配置文件如何进行配置呢?
    Centos安装 Apache Benchmark
    本地连接阿里云上的mysql centos
    python 导出项目需要的库
    Nginx报错:nginx: [error] OpenEvent("Global gx_reload_14944") failed (2: The system cannot find the file specified)
    windows安装uwsgi报错 AttributeError: module 'os' has no attribute 'uname'
    P3346 [ZJOI2015]诸神眷顾的幻想乡(广义后缀自动机)
    P6139 【模板】广义后缀自动机(广义 SAM)
  • 原文地址:https://www.cnblogs.com/flipped/p/9139219.html
Copyright © 2011-2022 走看看