MPI 01 MPI基础

01 基本信息

Message Passing Interface (MPI) :消息传递接口是一个并行计算的应用程序接口(API)标准。目前主要有两种实现。OpenMPI和MPICH。MPI标准目前已经发展到4.0。
下面是一些主要资料:
MPI论坛(MPI Forum): https://www.mpi-forum.org/
OpenMPI: https://www.open-mpi.org/
MPICH: http://www.mpich.org/
https://mpitutorial.com/tutorials/mpi-hello-world/zh_cn/
微软的资料:
https://docs.microsoft.com/en-us/message-passing-interface/microsoft-mpi
下面介绍windows10上微软的microsoft-mpi的单机使用。

02 microsoft-mpi单机环境

02.01 windows上面的MS-MPI安装包

从http://www.mpich.org/的download页面选择最新的MS-MPI v10.0版本(https://www.microsoft.com/en-us/download/details.aspx?id=57467)。
下载页面中有开发用的sdk安装包(sdk:msmpisdk.msi)运行使用环境安装包(msmpisetup.exe)
1 SDK安装
sdk:msmpisdk.msi默认安装在C:\Program Files (x86)\Microsoft SDKs\MPI目录。里面有开发用的头文件和lib库。
C:\Program Files (x86)\Microsoft SDKs\MPI\Include
C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x64
C:\Program Files (x86)\Microsoft SDKs\MPI\Lib\x86
2 运行环境安装
msmpisetup.exe默认会被安装在C:\Program Files\Microsoft MPI
C:\Program Files\Microsoft MPI\Bin\mpiexec.exe
C:\Program Files\Microsoft MPI\Bin\msmpilaunchsvc.exe
C:\Program Files\Microsoft MPI\Bin\smpd.exe

02.02 使用cmake3.14.5和vs2019构建开发环境

1 创建源码管理目录
D:\git\cpp\cpp_demo\MPI
2 构建练习的工程配置文件
D:\git\cpp\cpp_demo\MPI\CMakeLists.txt

cmake_minimum_required(VERSION 3.2)
if(CMAKE_VERSION VERSION_LESS 3.2)
  return()
endif()

set(proname "MPI")

project("MPI")

add_executable(${proname} 
  src/MPI01.01.h
  src/MPI01.01.cpp
  src/MPI_main.cpp)
  
if (WIN32)
  if(MSVC)
    # 增加一些编译属性的方法
    target_compile_options(${proname} PRIVATE
	  #/openmp
      #/wd4996
      #/wd4503
	  #/std:c++17
    )
	if(CMAKE_SIZEOF_VOID_P EQUAL 8)
	  set(_lib_suffix x64)
	else()
	  set(_lib_suffix x86)
	endif()
	# MPI SDK安装路径
	set(mpi_path "C:/Program Files (x86)/Microsoft SDKs/MPI" CACHE STRING "mpi include path" FORCE)
	include_directories("${mpi_path}/include")
	# 设置一些链接属性
	set_target_properties(${proname}
        PROPERTIES
		# MPI静态库路径
        LINK_FLAGS /LIBPATH:"${mpi_path}/Lib/${_lib_suffix}"
    )
	# MPI的静态库引入
	target_link_libraries(${proname} msmpi)
  endif()
  # g++ 
  # SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-elide-constructors")
endif()

c++文件:
D:\git\cpp\cpp_demo\MPI\src\MPI01.01.h

#pragma once

void test01_01();

D:\git\cpp\cpp_demo\MPI\src\MPI01.01.cpp

#include "MPI01.01.h"
#include "mpi.h"

#include <iostream>

void test01_01() {
    // 初始化 MPI 环境
    MPI_Init(NULL, NULL);

    // 通过调用以下方法来得到所有可以工作的进程数量
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    // 得到当前进程的秩
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    // 得到当前进程的名字
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);

    // 打印一条带有当前进程名字,秩以及
    // 整个 communicator 的大小的 hello world 消息。
    std::cout << "Hello world from processor " << processor_name << ", rank " 
        << world_rank << " out of " << world_size << " processors" << std::endl;

    // 释放 MPI 的一些资源
    MPI_Finalize();
}

D:\git\cpp\cpp_demo\MPI\src\MPI_main.cpp

#include "MPI01.01.h"
int main(int argc, char** argv) {
    test01_01();
	return 0;
}

3 生成工程文件
在这里插入图片描述
4 运行效果
mpiexec.exe -n 8 mpi.exe
在这里插入图片描述

03 入门介绍

https://mpitutorial.com/tutorials/
MPI 教程介绍(https://mpitutorial.com/tutorials/mpi-introduction/zh_cn/)
MPI Hello World(https://mpitutorial.com/tutorials/mpi-hello-world/zh_cn/)
MPI Send and Receive(https://mpitutorial.com/tutorials/mpi-send-and-receive/zh_cn/)
MPI Scatter, Gather, and Allgather(https://mpitutorial.com/tutorials/mpi-scatter-gather-and-allgather/zh_cn/)

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值