spdl.io.cpu_storage

cpu_storage(size: int, pin_memory=True) CPUStorage[source]

Allocate a block of memory.

This function allocates a block of memory. The intended usage is to make the data transfer from CPU to GPU faster and overlaps the data tansfer and GPU computation.

Example: Use page-locked memory for faster CUDA transfer.

>>> packets = spdl.io.demux_image(sample.path)
>>> frames = spdl.io.decode_packets(packets)
>>> size = frames.width * frames.height * 3
>>> storage = spdl.lib._libspdl.cpu_storage(size, pin_memory=True)
>>> buffer = spdl.io.convert_frames(frames, storage=storage)
>>> stream = torch.cuda.Stream(device=0)
>>> cuda_config = spdl.io.cuda_config(device_index=0, stream=stream.cuda_stream)
>>> buffer = spdl.io.transfer_buffer(buffer, cuda_config=cuda_config)
>>> tensor = spdl.io.to_torch(buffer)
Parameters:
  • size – The size of memory to allocate in bytes.

  • pin_memory – If True, the memory region is page-locked, so that GPUs can access them independently without help from CPU.

Returns:

The resulting memory block.