Windows 中使用 VS Code 编译 MPI 和 OpenMP 程序

mpi

win 安装mpi

Win10下Microsoft MPI(MSMPI)的下载安装 - 知乎 (zhihu.com)

(34条消息) Dev配置MPI运行环境(msmpi)和OpenMP环境(运行通过)_devc++ mpi配置_一点年羊的博客-CSDN博客

#命令行运行启动多进程,需要进入hello.exe所在目录才能执行,否则会找不到程序
mpiexec -n 4 hello.exe

ubuntu 安装mpi

(39条消息) ubuntu下mpich的安装与使用_乌班图 可执行程序cpi_Wu_uuuu的博客-CSDN博客

sudo apt-get install mpich
#检查安装位置
which mpicc

测试

#编译
mpicc hello.c -o hello
#运行
mpirun -np 4 ./hello
#include "mpi.h"
#include <stdio.h>

int main (int argc, char **argv)
{
    int myid, numprocs;
    MPI_Init (&argc, &argv);
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
    printf ("Hello World! Process %d of %d\n", myid, numprocs);
    MPI_Finalize ();
    return 0;
}

openmp

Ubuntu 安装openmp

检查gcc版本:gcc --version (GCC从4.2.0版本开始支持OpenMP),如果没有gcc,先安装gcc

sudo apt install gcc

安装OpenMP

sudo apt install libomp-dev

测试

编译和运行程序:使用编译器编译 OpenMP 程序,并在命令行中使用“-fopenmp”选项启用 OpenMP 支持,例如:

#编译
gcc -fopenmp -o program program.c 
#运行
./program
使用export命令:可以使用export命令在当前会话中设置环境变量
其中,4代表您想要使用的线程数,可以根据需要进行修改。
export OMP_NUM_THREADS=4

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv) {
	int nthreads, tid;

	/* Fork a team of threads giving them their own copies of variables */
	#pragma omp parallel private(nthreads, tid)
	{

		/* Obtain thread number */
		tid = omp_get_thread_num();
		printf("Hello World from thread = %d\n", tid);

		/* Only master thread does this */
		if (tid == 0) {
			nthreads = omp_get_num_threads();
			printf("Number of threads = %d\n", nthreads);
		}

	} /* All threads join master thread and disband */
	return 0;
}

Snipaste_2023-04-01_15-48-23

win下输出
注:我的电脑默认是8个线程,不同的电脑运行结果不同,就算是同一部电脑每次运行的结果也可能不同(4个线程并行执行,没有确定的先后顺序)

Hello World from thread = 4
Hello World from thread = 1
Hello World from thread = 0
Number of threads = 8
Hello World from thread = 5
Hello World from thread = 6
Hello World from thread = 3
Hello World from thread = 7
Hello World from thread = 2

指定线程数

omp_set_num_threads(16);
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv) {
	int nthreads, tid;

	/* Fork a team of threads giving them their own copies of variables */
    omp_set_num_threads(16);
	#pragma omp parallel private(nthreads, tid)
	{

		/* Obtain thread number */
		tid = omp_get_thread_num();
		printf("Hello World from thread = %d\n", tid);

		/* Only master thread does this */
		if (tid == 0) {
			nthreads = omp_get_num_threads();
			printf("Number of threads = %d\n", nthreads);
		}

	} /* All threads join master thread and disband */
	return 0;
}

VScode 配置mpi和openmp运行环境

【中英字幕】Windows 中使用 VS Code 编译 MPI 和 OpenMP 程序_哔哩哔哩_bilibili

前提已经安装好msmpi

Snipaste_2023-04-03_19-06-25

.vscode目录下文件

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**", 
                "${MSMPI_INC}"],//添加路径
            "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
            "compilerPath": "D:\\MyApps\\MinGW64\\bin\\gcc.exe",
            "cStandard": "c17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

launch.json

{
    // 使用 IntelliSense 了解相关属性。
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\MyApps\\MinGW64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

tasks.json

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe 生成活动文件",
      "command": "D:\\MyApps\\MinGW64\\bin\\g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-fopenmp", //编译openmp
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "调试器生成的任务。"
    }
  ],
  "version": "2.0.0"
}


settings.json

{
  "files.associations": {
    "vector": "cpp",
    "iostream": "cpp"
  },
  "files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    // "**/.vscode": true,
    "**/*.exe": true
  }
}

  • 4
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
MPI(Message Passing Interface)和OpenMP(Open Multi-Processing)是两种常用的并行程序设计方法。 MPI是一种消息传递程序设计模型,主要用于分布式内存系统的并行计算。在MPI编程程序的各个进程通过发送和接收消息来进行通信和数据传递。每个进程都有自己的内存空间,并且可以直接访问和修改自己的内存,而不能直接访问其他进程的内存。MPI程序设计可以实现大规模的并行计算,适合于需要在多台计算机上进行并行计算的情况。 OpenMP是一种共享内存的并行程序设计模型,主要用于多核共享内存架构的并行计算。在OpenMP编程程序运行在多个线程,这些线程之间可以共享一部分或全部的内存。通过使用Pragmas和语句来指定并行区域和任务分配,开发者可以将串行程序转化为并行程序,以实现更高效的计算。OpenMP程序设计适用于多核心处理器上的并行计算,能够充分发挥多核处理器的计算能力。 MPIOpenMP有各自适用的场景和优势。MPI适用于需要在多个计算节点之间进行通信和数据传递的并行计算,可以实现集群或分布式计算;而OpenMP适用于在同一计算节点上的多核共享内存并行计算,可以利用多核处理器的并行特性。 在一些计算任务,可以结合使用MPIOpenMP来充分利用多节点和多核心的并行能力。通过MPI将多个计算节点连接起来,每个节点上运行多个OpenMP线程,可以实现更大规模和更高效率的并行计算。 总之,MPIOpenMP是两种常用的并行程序设计方法,它们分别适用于分布式内存和共享内存的并行计算。根据具体的应用场景和硬件环境,可以选择合适的并行程序设计方法来实现高效的并行计算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ROJS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值