MCPcopy Index your code
hub / github.com/NVIDIA/warp / from_ptr

Method from_ptr

warp/_src/codegen.py:608–644  ·  view source on GitHub ↗
(self, ptr)

Source from the content-addressed store, hash-verified

606
607 # constructs a Warp struct instance from a pointer to the ctype
608 def from_ptr(self, ptr):
609 if not ptr:
610 raise RuntimeError("NULL pointer exception")
611
612 # create a new struct instance
613 instance = self()
614
615 for name, var in self.vars.items():
616 offset = getattr(self.ctype, name).offset
617 if matches_array_class(var.type, array):
618 # We could reconstruct wp.array from array_t, but it's problematic.
619 # There's no guarantee that the original wp.array is still allocated and
620 # no easy way to make a backref.
621 # Instead, we just create a stub annotation, which is not a fully usable array object.
622 setattr(instance, name, array(dtype=var.type.dtype, ndim=var.type.ndim))
623 elif matches_array_class(var.type, indexedarray):
624 # Same as regular arrays: return an annotation stub only.
625 setattr(instance, name, indexedarray(dtype=var.type.dtype, ndim=var.type.ndim))
626 elif isinstance(var.type, Struct):
627 # nested struct
628 value = var.type.from_ptr(ptr + offset)
629 setattr(instance, name, value)
630 elif issubclass(var.type, ctypes.Array):
631 # vector/matrix
632 value = var.type.from_ptr(ptr + offset)
633 setattr(instance, name, value)
634 else:
635 # scalar
636 cvalue = ctypes.cast(ptr + offset, ctypes.POINTER(var.type._type_)).contents
637 if var.type == warp.float16:
638 setattr(instance, name, half_bits_to_float(cvalue.value))
639 elif var.type == warp.bfloat16:
640 setattr(instance, name, bfloat16_bits_to_float(cvalue.value))
641 else:
642 setattr(instance, name, cvalue.value)
643
644 return instance
645
646
647class Reference:

Callers

nothing calls this directly

Calls 5

matches_array_classFunction · 0.85
arrayClass · 0.85
indexedarrayClass · 0.85
half_bits_to_floatFunction · 0.85
bfloat16_bits_to_floatFunction · 0.85

Tested by

no test coverage detected