Conduit Basics¶
Ascent’s API is based on Conduit. Both mesh data and action descriptions are passed to Ascent as Conduit trees. The Conduit C++ and Python interfaces are very similar, with the C++ interface heavily influenced by the ease of use of Python. These examples provide basic knowledge about creating Conduit Nodes to use with Ascent. You can also find more introductory Conduit examples in Conduit’s Tutorial Docs .
Creating key-value entries¶
#include <iostream>
#include <vector>
#include "conduit.hpp"
using namespace conduit;
int main()
{
//
// The core of Conduit's data model is `Node` object that
// holds a dynamic hierarchical key value tree
//
// Here is a simple example that creates
// a key value pair in a Conduit Node:
//
Node n;
n["key"] = "value";
std::cout << n.to_yaml() << std::endl;
}
Output
key: "value"
import conduit
import numpy as np
#
# The core of Conduit's data model is `Node` object that
# holds a dynamic hierarchical key value tree
#
# Here is a simple example that creates
# a key value pair in a Conduit Node:
#
n = conduit.Node()
n["key"] = "value";
print(n.to_yaml())
Output
key: "value"
Creating a path hierarchy¶
#include <iostream>
#include <vector>
#include "conduit.hpp"
using namespace conduit;
int main()
{
//
// Using hierarchical paths imposes a tree structure
//
Node n;
n["dir1/dir2/val1"] = 100.5;
std::cout << n.to_yaml() << std::endl;
}
Output
dir1:
dir2:
val1: 100.5
import conduit
import numpy as np
#
# Using hierarchical paths imposes a tree structure
#
n = conduit.Node()
n["dir1/dir2/val1"] = 100.5;
print(n.to_yaml())
Output
dir1:
dir2:
val1: 100.5
Setting array data¶
#include <iostream>
#include <vector>
#include "conduit.hpp"
using namespace conduit;
int main()
{
//
// Conduit's Node trees hold strings or bitwidth style numeric array leaves
//
// In C++: you can pass raw pointers to numeric arrays or
// std::vectors with numeric values
//
// In python: numpy ndarrays are used for arrays
//
Node n;
int *A = new int[10];
A[0] = 0;
A[1] = 1;
for (int i = 2 ; i < 10 ; i++)
A[i] = A[i-2]+A[i-1];
n["fib"].set(A, 10);
std::cout << n.to_yaml() << std::endl;
delete [] A;
}
Output
fib: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
import conduit
import numpy as np
#
# Conduit's Node trees hold strings or bitwidth style numeric array leaves
#
# In C++: you can pass raw pointers to numeric arrays or
# std::vectors with numeric values
#
# In python: numpy ndarrays are used for arrays
#
n = conduit.Node()
a = np.zeros(10, dtype=np.int32)
a[0] = 0
a[1] = 1
for i in range(2,10):
a[i] = a[i-2] + a[i-1]
n["fib"].set(a);
print(n.to_yaml());
Output
fib: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Zero-copy vs deep copy of array data¶
#include <iostream>
#include <vector>
#include "conduit.hpp"
using namespace conduit;
using std::vector;
int main()
{
//
// Conduit supports zero copy, allowing a Conduit Node to describe and
// point to externally allocated data.
//
// set_external() is method used to zero copy data into a Node
//
Node n;
vector<int> A1(10);
A1[0] = 0;
A1[1] = 1;
for (int i = 2 ; i < 10 ; i++)
A1[i] = A1[i-2]+A1[i-1];
n["fib_deep_copy"].set(A1);
// create another array to demo difference
// between set and set_external
vector<int> A2(10);
A2[0] = 0;
A2[1] = 1;
for (int i = 2 ; i < 10 ; i++)
{
A2[i] = A2[i-2]+A2[i-1];
}
n["fib_shallow_copy"].set_external(A2);
A1[9] = -1;
A2[9] = -1;
std::cout << n.to_yaml() << std::endl;
}
Output
fib_deep_copy: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
fib_shallow_copy: [0, 1, 1, 2, 3, 5, 8, 13, 21, -1]
import conduit
import numpy as np
#
# Conduit supports zero copy, allowing a Conduit Node to describe and
# point to externally allocated data.
#
# set_external() is method used to zero copy data into a Node
#
n = conduit.Node()
a1 = np.zeros(10, dtype=np.int32)
a1[0] = 0
a1[1] = 1
for i in range(2,10):
a1[i] = a1[i-2] + a1[i-1]
# create another array to demo difference
# between set and set_external
a2 = np.zeros(10, dtype=np.int32)
a2[0] = 0
a2[1] = 1
for i in range(2,10):
a2[i] = a2[i-2] + a2[i-1]
n["fib_deep_copy"].set(a1);
n["fib_shallow_copy"].set_external(a2);
a1[-1] = -1
a2[-1] = -1
print(n.to_yaml())
Output
fib_deep_copy: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
fib_shallow_copy: [0, 1, 1, 2, 3, 5, 8, 13, 21, -1]