c++ MPI参数的VScode配置

new9mtju  于 2023-08-09  发布在  Vscode
关注(0)|答案(1)|浏览(141)

我正在尝试使用mpi在Linux上为C++程序设置我的VScode调试器。虽然编译和运行工作正常,但我很难让调试器在mpi代码的情况下添加命令行参数。argv数组只保存垃圾。此外,调试器不会启动在launch.json文件中使用-np选项指定的进程数。如果我只是使用mpirun -np 2 code.exe arg1 arg2...手动运行代码,一切都很正常
任何帮助都是感激的。
以下是我的.vscode文件:

tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "mpicxx build active file",
      "command": "/usr/bin/mpicxx",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

字符串

launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "MPI Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
      "args": [
          "-np",  // should govern number of processes launched
          "1",    // by mpi
          "${fileDirname}/${fileBasenameNoExtension}.exe",
          "0",    // these arguments are additional arguments
          "0.01", // used by the program
          "5",    
          "10"
      ],
      "stopAtEntry": true,
      "cwd": "${fileDirname}",
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "/usr/bin/gdb",
      "preLaunchTask": "mpicxx build active file",
      "setupCommands": [
        {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        },
        {
            "description": "Set Disassembly Flavor to Intel",
            "text": "-gdb-set disassembly-flavor intel",
            "ignoreFailures": true
        }
    ],
    }
  ]
}


要找到mpi头,还需要一个c_cpp_properties.json

{
  "configurations": [
    {
      "name": "linux-gcc-x64",
      "includePath": [
        "${workspaceFolder}/**",
        "/usr/lib/x86_64-linux-gnu/openmpi/include/"
      ],
      "compilerPath": "/usr/bin/gcc",
      "cStandard": "${default}",
      "cppStandard": "${default}",
      "intelliSenseMode": "linux-gcc-x64",
      "compilerArgs": [
        ""
      ]
    }
  ],
  "version": 4
}

fjnneemd

fjnneemd1#

我是这样做的:
首先,OpenMPI的网站上有一段描述了如何在所有MPI进程中构建断点并将GDB附加到每个进程。
使用VSCode,我将以下配置添加到launch.json

,
    {
        "name": "(gdb) Attach",
        "type": "cppdbg",
        "request": "attach",
        "processId": "${command:pickProcess}",
        "program": "path/to/your/exe",
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            },
            {
                "description": "Set Disassembly Flavor to Intel",
                "text": "-gdb-set disassembly-flavor intel",
                "ignoreFailures": true
            }
        ]
    }

字符串
例如,如果我运行mpirun -np 4 ./ComputeInverseSquare_x64,其中ComputeInverseSquare看起来像这样:

#include <iostream>

int main(){
   MPI_Init();

   int idProc, nbProc;
   MPI_Comm_rank(MPI_COMM_WORLD, &idProc);
   MPI_Comm_size(MPI_COMM_WORLD, &nbProc);

   int volatile j = 1;
   while(j)
      sleep(5);
  
   // compute some inverse square of something
   // ...

   MPI_Finalize();
   return 0;
}


然后,在VSCode中,当点击f5或启动“(gdb)Attach”配置时,command:pickProcess将被触发,我可以搜索名称为ComputeInverseSquare的可执行文件,我会看到其中4个具有不同PID的文件,我可以附加和调试。
希望能帮上忙。
其他信息:
1.如何在Windows上使用GDB

  1. how to attach running processes with VSCode

相关问题