Generators¶
Using Generator instances to parse JSON schemas¶
The Generator class is used to parse conduit JSON schemas into a Node.
Generator g("{test: {dtype: float64, value: 100.0}}","conduit_json");
Node n;
g.walk(n);
std::cout << n["test"].as_float64() <<std::endl;
n.print();
n.print_detailed();
100
{
"test": 100.0
}
{
"test": {"dtype":"float64", "number_of_elements": 1, "offset": 0, "stride": 8, "element_bytes": 8, "endianness": "little", "value": 100.0}
}
The Generator can also parse pure json. For leaf nodes: wide types such as int64, uint64, and float64 are inferred.
Generator g("{test: 100.0}","json");
Node n;
g.walk(n);
std::cout << n["test"].as_float64() <<std::endl;
n.print_detailed();
n.print();
100
{
"test": {"dtype":"float64", "number_of_elements": 1, "offset": 0, "stride": 8, "element_bytes": 8, "endianness": "little", "value": 100.0}
}
{
"test": 100.0
}
Schemas can easily be bound to in-core data.
float64 vals[2];
Generator g("{a: {dtype: float64, value: 100.0}, b: {dtype: float64, value: 200.0} }",
"conduit_json",
vals);
Node n;
100 vs 100
200 vs 200
{
"a": 100.0,
"b": 200.0
}
{
"mem_spaces":
{
"0x7fff533bfff0":
{
"path": "a",
"type": "external"
}
},
"total_bytes_allocated": 0,
"total_bytes_mmaped": 0,
"total_bytes_compact": 16,
"total_strided_bytes": 16
}
Compacting Nodes¶
Nodes can be compacted to transform sparse data.
CONDUIT_INFO("json_generator_compact");
float64 vals[] = { 100.0,-100.0,
200.0,-200.0,
300.0,-300.0,
400.0,-400.0,
500.0,-500.0};
// stride though the data with two different views.
Generator g1("{dtype: float64, length: 5, stride: 16}",
"conduit_json",
vals);
Generator g2("{dtype: float64, length: 5, stride: 16, offset:8}",
"conduit_json",
vals);
Node n1;
g1.walk_external(n1);
n1.print();
Node n2;
g2.walk_external(n2);
n2.print();
// look at the memory space info for our two views
Node ninfo;
n1.info(ninfo);
ninfo.print();
n2.info(ninfo);
ninfo.print();
// compact data from n1 to a new node
Node n1c;
n1.compact_to(n1c);
// look at the resulting compact data
n1c.print();
n1c.schema().print();
n1c.info(ninfo);
ninfo.print();
// compact data from n2 to a new node
Node n2c;
n2.compact_to(n2c);
[100.0, 200.0, 300.0, 400.0, 500.0]
[-100.0, -200.0, -300.0, -400.0, -500.0]
{
"mem_spaces":
{
"0x7fff533bffb0":
{
"path": "",
"type": "external"
}
},
"total_bytes_allocated": 0,
"total_bytes_mmaped": 0,
"total_bytes_compact": 40,
"total_strided_bytes": 72
}
{
"mem_spaces":
{
"0x7fff533bffb0":
{
"path": "",
"type": "external"
}
},
"total_bytes_allocated": 0,
"total_bytes_mmaped": 0,
"total_bytes_compact": 40,
"total_strided_bytes": 72
}
[100.0, 200.0, 300.0, 400.0, 500.0]
{"dtype":"float64", "number_of_elements": 5, "offset": 0, "stride": 8, "element_bytes": 8, "endianness": "little"}
{
"mem_spaces":
{
"0x7fdf90406820":
{
"path": "",
"type": "allocated",
"bytes": 40
}
},
"total_bytes_allocated": 40,
"total_bytes_mmaped": 0,
"total_bytes_compact": 40,
"total_strided_bytes": 40
}
[-100.0, -200.0, -300.0, -400.0, -500.0]
{
"mem_spaces":
{
"0x7fdf90405f70":
{
"path": "",
"type": "allocated",
"bytes": 40
}
},
"total_bytes_allocated": 40,
"total_bytes_mmaped": 0,
"total_bytes_compact": 40,
"total_strided_bytes": 40
}