作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基于 data_ptr 创建一个 gpu 张量?
int main{
auto ten=torch::randn({3,10},torch::kCuda);
auto p=ten.data_ptr<float>();//I believe "p" is a gpu pointer?
auto ten2=torch::from_blob(p,{3,10});//ten2's device=cpu,that's the problem
cout<<ten2;//then I got error:"interrupted by signal 11 SIGSEGV"
}
有什么建议吗?
最佳答案
我相信你应该声明 ten2
的设备是 GPU,就像那样:
int main{
auto ten=torch::randn({3,10},torch::kCUDA);
auto p=ten.data_ptr<float>();//I believe "p" is a gpu pointer?
auto options = torch::TensorOptions().device(torch::kCUDA);
auto ten2=torch::from_blob(p,{3,10}, options);
cout<<ten2;
}
附带说明一下,当您使用 from_blob
时要小心,生成的张量不会取得底层内存的所有权。因此,如果 ten2
超出范围,它将释放它的内存,这也是 ten2
的基础数据。如果您知道 ten2
将在 ten2
之前超出范围,您应该调用 clone :
auto ten2=torch::from_blob(p,{3,10}, options).clone();
关于c++ - libtorch : How to create a gpu tensor base on data_ptr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65424643/
我是一名优秀的程序员,十分优秀!