Skip to content

Utilities

Operator developer Framework developer

Smaller tools of onika/ used by operators and applications: logging, strings, files, YAML helpers and small containers. Examples were compiled and run against Onika 1.1.0.

Logging

onika/log.h declares the log streams. Use them instead of std::cout:

Stream Use
onika::lout Normal output. Printed by MPI rank 0 only, unless --logging-parallel true. Redirected with --logging-log_file.
onika::lerr Errors and warnings (--logging-err_file).
ldbg Debug messages, printed only with --logging-debug true. Inside an operator, ldbg is filtered by --debug-filter (regular expressions on operator paths).
onika::fatal_error() Prints the message and a stack trace, then aborts all processes. See Errors.
onika::lout << "normal" << std::endl;
ldbg << "only with --logging-debug true" << std::endl;
onika::lout.progress_bar( "computing ", 0.42 );   // progress bar on a terminal

In lambdas and OpenMP blocks, use onika::lout_stream(), lerr_stream() and ldbg_stream() to avoid capturing globals.

Strings

onika/string_utils.h:

onika::format_string("step %05d , t = %.3e", 42, 1.5e-3);   // "step 00042 , t = 1.500e-03" (printf formats)
onika::split_string("rx,ry,rz", ',');                         // { "rx", "ry", "rz" }
onika::large_integer_to_string(123456789);                    // "123,456,789"
"particle" + onika::plurial_suffix(3);                        // "particles"

Also: str_tolower, str_indent, format_string_buffer (into a fixed buffer, usable without allocation), function_name_and_args("f(a,b)", name, args).

onika/print_utils.h and onika/type_utils.h: pretty_short_type<T>() (readable type names, used in error messages), print_if_possible, append_to_file.

Files

onika/file_utils.h:

Function Meaning
data_file_path(path) Finds a data file: a relative path is searched in the directories of ONIKA_DATA_PATH (.:./data:<install>/data by default, set by setup-env.sh), the first match is returned; if none is found, a warning lists the directories searched. Use it for every input data file read by an operator.
config_file_path(path) Same for input files (.msp), in ONIKA_CONFIG_PATH. Used by includes.
set_data_file_dirs("dir1:dir2") Changes the data search path.
dirname, is_relative_path, concat_dir_path, resolve_file_path Path helpers.
onika::set_data_file_dirs( ".:/tmp" );
std::string f = onika::data_file_path( "potential.dat" );   // "./potential.dat" or "/tmp/potential.dat"

YAML helpers

Enumerations read from input files. EXANB_YAML_ENUM(namespace, Name, values...) (onika/yaml/yaml_enum.h) declares a small enum type with a YAML conversion and operator <<:

#include <onika/yaml/yaml_enum.h>

EXANB_YAML_ENUM(my_plugin, Integrator, VERLET, LEAPFROG);

// in an operator:
ADD_SLOT( my_plugin::Integrator , scheme , INPUT , my_plugin::Integrator::VERLET );
if( *scheme == my_plugin::Integrator::LEAPFROG ) { ... }
- my_operator: { scheme: LEAPFROG }

An unknown name gives the value UNDEFINED.

Node helpers (onika/yaml/yaml_utils.h, namespace onika::yaml):

long steps = onika::yaml::read<long>( node, "steps", 1 );   // value, or default if the key is missing
YAML::Node m = onika::yaml::merge_nodes( a, b );           // b overrides a (as for 'includes')
onika::yaml::dump_node_to_stream( onika::lout, m );        // print

Also remove_map_key, dump_node_to_file, resolve_config_file_includes, yaml_load_file_abort_on_except.

Small containers

Fixed-size containers usable in GPU functors:

Type Header Meaning
onika::oarray_t<T,N> onika/oarray.h Like std::array, usable on GPU.
onika::inplace_vector<T,N> onika/oarray.h Vector with at most N elements, stored inline.
onika::FixedCapacityVector<T> onika/fixed_capacity_vector.h Vector interface over storage you provide (FixedCapacityVector<int> v(ptr, capacity)); never allocates.
onika::nd_array<T,I,J,...> onika/nd_array.h Multi-dimensional fixed array.
onika::FlatTuple<T...> onika/flat_tuple.h Tuple usable on GPU (element access with tuple_index<I>).

Others: onika/hash_utils.h (multi_hash(a, b, ...), combined std::hash), onika/thread.h (atomic_lock_spin_mutex, usable on GPU, and omp_spin_mutex), onika/color_scale.h (RGB colors), onika/plot1d.h (Plot1D, sampled curves).

Unit tests

onika/test/unit_test.h (ONIKA_UNIT_TEST, ONIKA_TEST_ASSERT) embeds unit tests in plugins: see Testing.