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

编程入门 行业动态 更新时间:2024-10-08 10:58:51

Windows 中使用 VS Code 编译 MPI 和 OpenMP <a href=https://www.elefans.com/category/jswz/34/1771429.html style=程序"/>

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

mpi

win 安装mpi

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

(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;
}

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

.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 了解相关属性。// 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: /?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}
}

更多推荐

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

本文发布于:2024-02-06 08:37:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1747558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   Code   Windows   OpenMP   MPI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!