[英]How to concatenate LibTorch tensors created with a multi-thread process std::thread in C++?
A multi-thread process in C++ returns tensors and I want to concatenate them into one tensor in order. C++ 中的多线程进程返回张量,我想将它们按顺序连接成一个张量。
In C++ I have a single function which returns a single 1x8 tensor.在 C++ 我有一个 function 返回一个 1x8 张量。
I call this function multiple times simultaneously with std::thread
and I want to concatenate the tensors it returns into one large tensor.我用std::thread
同时多次调用这个 function 并且我想将它返回的张量连接成一个大张量。 For example, I call it 12 times I expect to have a 12x8 tensor when it's finished.例如,我称它为 12 次,我希望它完成后会有一个 12x8 的张量。
I need for them to be concatenated in order, that is, the tensor called with 0 should always go in the 0th position followed by the 1st in the 1st position, and so on.我需要将它们按顺序连接起来,也就是说,用 0 调用的张量应该总是第 0 个 position 中的 go ,然后是第 1 个 Z4757FE07FD492A8BE0EA6A760 中的第一个 Z4757FE07FD492A8BE0EAZ,以此类推。
I'm aware I could just have the function return a 12x8 tensor, but I need to solve the problem of how to grab the tensors produced during the multithreading process.我知道我可以让 function 返回一个 12x8 张量,但我需要解决如何获取多线程过程中产生的张量的问题。
In my attempt below I try to concatenate the tensors into the all_episode_steps
tensor but this returns an error.在下面的尝试中,我尝试将张量连接到all_episode_steps
张量中,但这会返回错误。
If you comment out the all_episode_steps
line and put std::cout << one;
如果您注释掉all_episode_steps
行并将std::cout << one;
in the get_tensors
function above the return statement you see it appears to be using multithreading to create the tensors with no problems.在返回语句上方的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();
}
}
If you want to build this yourself you can install LibTorch here .如果您想自己构建它,您可以在此处安装 LibTorch 。
Below is the CMakeLists.txt file for the code above.下面是上面代码的 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)
Threads can't return tensors, but they can modify tensors via pointers.线程不能返回张量,但可以通过指针修改张量。 Try this (untested, may need a bit of tweaking):试试这个(未经测试,可能需要一些调整):
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.