libtorch : How to create a gpu tensor base on data_ptr?

Viewed 1137

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?

1 Answers

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();
Related