| ag_buffer | R Documentation |
A matrix of rows x capacity kept on the compute device without a
gradient: written in place by column ranges with ag_buffer_write()
and read by column indices with ag_get_rows() inside ag_*
computations, including ag_capture() recordings. Typical use
is a replay buffer: one column per transition.
ag_buffer(rows, capacity, dtype = "f32")
ag_buffer_write(buf, data, col_offset)
ag_buffer_read(buf, col_offset = 0, k = buf$capacity - col_offset)
ag_buffer_free(buf)
rows |
Number of rows (values per column). |
capacity |
Number of columns. |
dtype |
Storage type. Only |
buf |
An |
data |
Numeric matrix with |
col_offset |
0-based index of the first column written or read. |
k |
Number of columns to read. |
On the CPU the buffer is an R matrix and the same calls work on it. On the
GPU the memory is allocated when the buffer is created (or first used on the
device) and zero-filled; creation fails with an error when the buffer needs
more than 85% of the free memory the device reports (driver estimates are
approximate). A device switch (ag_device("cpu")) downloads the value
to the host in 16 MB parts before the backend is released (the host copy is
an R double matrix, about twice the buffer's device size), and the switch
fails with an error if that download does; the next use on the GPU uploads
it again, and recordings that read the buffer are then made anew.
Writes are synchronous: when ag_buffer_write() returns, the data is on
the device, and any graph computed afterwards, including a captured replay,
reads it. Graph nodes queued before the write are computed first, so they
read the old value.
ag_buffer_write() writes columns col_offset to
col_offset + ncol(data) - 1; the range must lie inside the buffer
(a ring buffer wraps with two writes).
ag_buffer_read() returns columns col_offset to
col_offset + k - 1 as a rows x k matrix; reading a large
buffer in parts bounds the host memory used.
ag_buffer_free() releases the memory at once instead of at
garbage collection; the buffer cannot be used afterwards.
An object of class ag_buffer with fields rows,
capacity and dtype.
ag_get_rows
buf <- ag_buffer(3, 10)
ag_buffer_write(buf, matrix(1:6, 3, 2), col_offset = 4)
ag_buffer_read(buf, 4, 2)
as.matrix(ag_get_rows(buf, c(5, 4, 5)))
ag_buffer_free(buf)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.