Skip to content

Allow win32_handle_metadata to be an arbitrary object? #1263

Closed

Description

@leofang

I'd like to have input from the team on this. I feel taking a pointer address here is not the most pleasant design, but the challenge is that there is no official Python binding for the Windows SECURITY_ATTRIBUTES struct, so we either have to take arbitrary object or a pointer.

Originally posted by @leofang in #1242 (comment)

Activity

  1. added
    enhancementAny code-related improvements
    P0High priority - Must do!
    cuda.coreEverything related to the cuda.core module
    on Nov 18, 2025
  2. added this to the cuda.core beta 10 milestone on Nov 18, 2025
  3. leofang commented on Nov 18, 2025

    @leofang
    MemberAuthor

    This needs to be addressed before the next cuda.core release is out (v0.5.0), otherwise it'd become a breaking change.

  4. rwgk commented on Nov 18, 2025

    @rwgk
    Contributor

    xref: #1264 — possibly related, but NOT certainly

  5. kkraus14 commented on Nov 20, 2025

    @kkraus14
    Collaborator

    According to the CUDA Driver API docs: https://docs.nvidia.com/cuda/cuda-driver-api/structCUmemAllocationProp__v1.html#structCUmemAllocationProp__v1_1542262ad88e1d00f02b306c641270168

    This is a pointer to a OBJECT_ATTRIBUTES struct (https://learn.microsoft.com/en-us/windows/win32/api/ntdef/ns-ntdef-_object_attributes), not a SECURITY_ATTRIBUTES struct. Is there a relationship between these two in some way that makes us care about SECURITY_ATTRIBUTES?

  6. leofang commented on Nov 20, 2025

    @leofang
    MemberAuthor

    Right, good catch @kkraus14. Unfortunately I confused myself. Confirmed with Vishnu that we need to use OA (OBJECT_ATTRIBUTES).

    I used SA (SECURITY_ATTRIBUTES) because when we started exploring with mempools (#446) SA was what we needed to use:
    https://docs.nvidia.com/cuda/cuda-driver-api/structCUmemPoolProps__v1.html#structCUmemPoolProps__v1_1b153a37a3a73be7aaa98f271429b87a7
    I forgot that we are now dealing with VMM instead of mempools, and so I did not bother to double check this impression.

    FWIW, SA is only 24B whereas OA is 48B (on the Colossus system that I rented), so this is definitely a bug in the test suite that we must fix. This would, for example, answer why I saw weird things like this.

    Since OA must be initialized by InitializeObjectAttributes, which unfortunately is a macro function, compiling a bit of C code to wrap it is needed. I confirm this example works:

    from cffi import FFI
    prog = FFI()
    prog.cdef("uintptr_t do_stuff();")
    source = r"""
    #define _AMD64_
    #include <ntdef.h>
    
    static OBJECT_ATTRIBUTES objAttributes;
    
    extern "C"
    uintptr_t do_stuff() {
        static bool objAttributesConfigured = false;
        if (!objAttributesConfigured) {
            InitializeObjectAttributes(&objAttributes, NULL, 0, NULL, NULL);
            printf("there you go: %" PRIuPTR "\n",  (uintptr_t)(&objAttributes));
            printf("size of obj attr: %zu \n", sizeof(objAttributes));
            objAttributesConfigured = true;
        }
        return (uintptr_t)(&objAttributes);
    }
    """
    prog.set_source(
        "cpu_obj",
        source,
        source_extension=".cpp",
        extra_compile_args=["-std=c++11"],
    )
    import tempfile
    temp_dir = tempfile.mkdtemp()
    prog.compile()#tmpdir=temp_dir)
    
    import sys
    sys.path.append(temp_dir)
    from cpu_obj.lib import do_stuff
    print(f"{do_stuff()=}")
    
    
    from cuda.core.experimental import Device, VirtualMemoryResource, VirtualMemoryResourceOptions
    dev = Device()
    dev.set_current()
    
    options=VirtualMemoryResourceOptions(handle_type="win32", win32_handle_metadata=do_stuff())
    mr = VirtualMemoryResource(dev, options)
    buf = mr.allocate(1024)
    buf.close()
    print("DONE!")

    I'll integrate this example into the test suite.

  7. assigned and unassigned on Nov 20, 2025
  8. leofang commented on Nov 24, 2025

    @leofang
    MemberAuthor

    Update: on the Thursday team-sync we decided to disable win32 support for now, thereby bypassing this discussion, because the underlying OBJECT_ATTRIBUTES is very hard to produce in Python, and it is unclear to us how to ensure general correctness. If handle_type="win32" is passed we'll raise an exception asking users to reach out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P0High priority - Must do!cuda.coreEverything related to the cuda.core moduleenhancementAny code-related improvements

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions