Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about host-allocated memory to external guide #862

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs_input/external.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ take ownership, and defaults to *false*. By setting `owning` to *true*, MatX wil
By default it uses its own allocator, but users can pass in their own PMR-compatible allocator if they wish. For more information
see :ref:`creating`.

MatX does not know the "space" or "kind" of the memory when a pointer is passed in. This is by design since looking up what kind
of pointer is passed (device, host, managed, etc) is expensive and error-prone. In addition, what can be done with a pointer of a
certain type is dependent on the system. For example, a Grace-Hopper system can share pointers allocated with `malloc` between the
the GPU and CPU, but x86 cannot. It is up to the user to make sure that all memory types used in an expression are compatible.

.. code-block:: cpp

auto host_mem = reinterpret_cast<float*>(malloc(10 * sizeof(float)));
auto host_tensor = make_tensor<float>(host_mem, {10});
auto device_tensor = make_tensor<float>({10}, MATX_DEVICE_MEMORY);
(device_tensor = host_tensor).run();

The code above attempts to copy memory from a malloc'd pointer on the host to device memory. This may or may not work depending
on the system. If unsure, it's usually safe to make a tensor with managed memory, copy data into it, then use it on both host or
device.


Passing MatX Operators to External Code/Libraries
-------------------------------------------------

Expand Down