Data Ownership

The Node class provides two ways to hold data, the data is either owned or externally described:

  • If a Node owns data, the Node allocated the memory holding the data and is responsible or deallocating it.

  • If a Node externally describes data, the Node holds a pointer to the memory where the data resides and is not responsible for deallocating it.

set vs set_external

The Node.set methods support creating owned data and copying data values in both the owned and externally described cases. The Node.set_external methods allow you to create externally described data:

  • set(…): Makes a copy of the data passed into the Node. This will trigger an allocation if the current data type of the Node is incompatible with what was passed. The Node assignment operators use their respective set variants, so they follow the same copy semantics.

  • set_external(…): Sets up the Node to describe data passed and access the data externally. Does not copy the data.

  • Python Example:

vals = numpy.zeros((5,),dtype=numpy.float64)
n = conduit.Node()
n["v_owned"].set(vals)
n["v_external"].set_external(vals)

print(n.info())
print(n)
vals[0] = 3.1415
print(n)
print(vals)
  • Output:

 
 mem_spaces: 
   0x7ff801065e60: 
     path: "v_owned"
     type: "allocated"
     bytes: 40
     allocator_id: 0
   0x7ff7ff45fdf0: 
     path: "v_external"
     type: "external"
 total_bytes_allocated: 40
 total_bytes_mmaped: 0
 total_bytes_compact: 80
 total_strided_bytes: 80
 
 
 v_owned: [0.0, 0.0, 0.0, 0.0, 0.0]
 v_external: [0.0, 0.0, 0.0, 0.0, 0.0]
 
 
 v_owned: [0.0, 0.0, 0.0, 0.0, 0.0]
 v_external: [3.1415, 0.0, 0.0, 0.0, 0.0]
 
 [3.1415 0.     0.     0.     0.    ]