create a gpu tensor base on data_ptr?
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"
}
Any advice?
I believe you should declare ten2's device to be GPU, like that :
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;
}
On a side note, be careful when you use from_blob, the resulting tensor does not take ownership on the underlying memory. So if ten goes out of scope, it will free its memory which is also ten2's underlying data. If you know that ten will go out of scope before ten2, you should make a call to clone :
auto ten2=torch::from_blob(p,{3,10}, options).clone();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With