Hello Cuda(三)——VSCODE&LibTorch

CMAKELists

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example)

set(CMAKE_PREFIX_PATH /home/xuliangyu/libtorch)
set(Torch_DIR /home/shiyanshi/libtorch/share/cmake/) 
# 修改这里为自己的libtorch cmake路径,也可以用pytorch安装时编译的pkg里的cmake
find_package(Torch REQUIRED)
#find_package(CUDA REQUIRED)

INCLUDE_DIRECTORIES(
    "/home/shiyanshi/libtorch/include"
    "/home/shiyanshi/liborch/include/csrc/api/include"
)

add_executable(example example.cpp)
target_link_libraries(example "${TORCH_LIBRARIES}")
set_property(TARGET example PROPERTY CXX_STANDARD 14)

Tasks.json

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "cmake build",
			"command": "cmake", // 这里要写cmake编译指令 可以写在command 也可以拆开来写
			"args": [
				"-D",
				"CMAKE_PREFIX_PATH=/home/shiyanshi/libtorch/share/cmake",
				"..",
				"&&",
				"make",
				"-j"
			],
			"options": {
				"cwd": "/home/shiyanshi/Cuda_Lib/cuda_2/build"  // 工作路径,表示要编译的文件目录
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: /usr/bin/g++"
		}
	]
}

Launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/example", // 用于指定要进行调试的程序路径
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}", // 表示当前workspace文件夹路径
            "environment": [],
            "externalConsole": false,
            "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
                }
            ],
            "preLaunchTask": "cmake build",
            "miDebuggerArgs": "/usr/bin/gdb"
        }
    ]
}

测试C++文件

#include <torch/script.h>
#include <iostream>

int main() {
  torch::Tensor foo = torch::arange(25).reshape({5, 5});
  torch::Tensor bar  = torch::einsum("ii", foo);

  std::cout << "==> matrix is:\n " << foo << std::endl;
  std::cout << "==> trace of it is:\n " << bar << std::endl;
}

posted @ 2023-08-26 10:15  信海  阅读(25)  评论(0编辑  收藏  举报