Pin the graph structure and node/edge data to the page-locked memory for GPU zero-copy access. This is an **inplace** method. The graph structure must be on CPU to be pinned. If the graph struture is already pinned, the function directly returns it. Materialization
(self)
| 5771 | return self |
| 5772 | |
| 5773 | def pin_memory_(self): |
| 5774 | """Pin the graph structure and node/edge data to the page-locked memory for |
| 5775 | GPU zero-copy access. |
| 5776 | |
| 5777 | This is an **inplace** method. The graph structure must be on CPU to be pinned. |
| 5778 | If the graph struture is already pinned, the function directly returns it. |
| 5779 | |
| 5780 | Materialization of new sparse formats for pinned graphs is not allowed. |
| 5781 | To avoid implicit formats materialization during training, |
| 5782 | you should create all the needed formats before pinning. |
| 5783 | But cloning and materialization is fine. See the examples below. |
| 5784 | |
| 5785 | Returns |
| 5786 | ------- |
| 5787 | DGLGraph |
| 5788 | The pinned graph. |
| 5789 | |
| 5790 | Examples |
| 5791 | -------- |
| 5792 | The following example uses PyTorch backend. |
| 5793 | |
| 5794 | >>> import dgl |
| 5795 | >>> import torch |
| 5796 | |
| 5797 | >>> g = dgl.graph((torch.tensor([1, 0]), torch.tensor([1, 2]))) |
| 5798 | >>> g.pin_memory_() |
| 5799 | |
| 5800 | Materialization of new sparse formats is not allowed for pinned graphs. |
| 5801 | |
| 5802 | >>> g.create_formats_() # This would raise an error! You should do this before pinning. |
| 5803 | |
| 5804 | Cloning and materializing new formats is allowed. The returned graph is **not** pinned. |
| 5805 | |
| 5806 | >>> g1 = g.formats(['csc']) |
| 5807 | >>> assert not g1.is_pinned() |
| 5808 | |
| 5809 | The pinned graph can be access from both CPU and GPU. The concrete device depends |
| 5810 | on the context of ``query``. For example, ``eid`` in ``find_edges()`` is a query. |
| 5811 | When ``eid`` is on CPU, ``find_edges()`` is executed on CPU, and the returned |
| 5812 | values are CPU tensors |
| 5813 | |
| 5814 | >>> g.unpin_memory_() |
| 5815 | >>> g.create_formats_() |
| 5816 | >>> g.pin_memory_() |
| 5817 | >>> eid = torch.tensor([1]) |
| 5818 | >>> g.find_edges(eids) |
| 5819 | (tensor([0]), tensor([2])) |
| 5820 | |
| 5821 | Moving ``eid`` to GPU, ``find_edges()`` will be executed on GPU, and the returned |
| 5822 | values are GPU tensors. |
| 5823 | |
| 5824 | >>> eid = eid.to('cuda:0') |
| 5825 | >>> g.find_edges(eids) |
| 5826 | (tensor([0], device='cuda:0'), tensor([2], device='cuda:0')) |
| 5827 | |
| 5828 | If you don't provide a ``query``, methods will be executed on CPU by default. |
| 5829 | |
| 5830 | >>> g.in_degrees() |