繁体   English   中英

如何在 C++ 中连接使用多线程进程 std::thread 创建的 LibTorch 张量?

[英]How to concatenate LibTorch tensors created with a multi-thread process std::thread in C++?

C++ 中的多线程进程返回张量,我想将它们按顺序连接成一个张量。

在 C++ 我有一个 function 返回一个 1x8 张量。
我用std::thread同时多次调用这个 function 并且我想将它返回的张量连接成一个大张量。 例如,我称它为 12 次,我希望它完成后会有一个 12x8 的张量。

我需要将它们按顺序连接起来,也就是说,用 0 调用的张量应该总是第 0 个 position 中的 go ,然后是第 1 个 Z4757FE07FD492A8BE0EA6A760 中的第一个 Z4757FE07FD492A8BE0EAZ,以此类推。

我知道我可以让 function 返回一个 12x8 张量,但我需要解决如何获取多线程过程中产生的张量的问题。

在下面的尝试中,我尝试将张量连接到all_episode_steps张量中,但这会返回错误。
如果您注释掉all_episode_steps行并将std::cout << one; 在返回语句上方的get_tensors function 中,您会看到它似乎使用多线程来毫无问题地创建张量。

#include <torch/torch.h>

torch::Tensor get_tensors(int id) {
    torch::Tensor one = torch::rand({8});
    return one.unsqueeze(0);
}

torch::Tensor all_episode_steps;
int main() {

    std::thread ths[100];

    for (int id=0; id<12; id++) {

        ths[id] = std::thread(get_tensors, id);

        all_episode_steps = torch::cat({ths[id], all_episode_steps});
    }
    for (int id=0; id<12; id++) {
        ths[id].join();
    }

}

如果您想自己构建它,您可以在此处安装 LibTorch
下面是上面代码的 CMakeLists.txt 文件。

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

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

# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET example-app
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:example-app>)
endif (MSVC)

线程不能返回张量,但可以通过指针修改张量。 试试这个(未经测试,可能需要一些调整):

void get_tensors(torch::Tensor* out) {
    torch::Tensor one = torch::rand({8});
    *out = one.unsqueeze(0);
}

int main() {
    std::thread ths[12];
    std::vector<torch::Tensor> results(12);

    for (int id=0; id<12; id++) {
        ths[id] = std::thread(get_tensors, &results[id]);
    }

    for (int id=0; id<12; id++) {
        ths[id].join();
    }

    auto result2d = torch::cat(results);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM