2 * Copyright © 2013-2018 Inria. All rights reserved.
3 * See COPYING in top-level directory.
7 * \brief Topology differences.
14 #error Please include the main hwloc.h instead
25 /** \defgroup hwlocality_diff Topology differences
27 * Applications that manipulate many similar topologies, for instance
28 * one for each node of a homogeneous cluster, may want to compress
29 * topologies to reduce the memory footprint.
31 * This file offers a way to manipulate the difference between topologies
32 * and export/import it to/from XML.
33 * Compression may therefore be achieved by storing one topology
34 * entirely while the others are only described by their differences
36 * The actual topology can be reconstructed when actually needed by
37 * applying the precomputed difference to the reference topology.
39 * This interface targets very similar nodes.
40 * Only very simple differences between topologies are actually
41 * supported, for instance a change in the memory size, the name
42 * of the object, or some info attribute.
43 * More complex differences such as adding or removing objects cannot
44 * be represented in the difference structures and therefore return
46 * Differences between object sets cannot be represented either.
48 * It means that there is no need to apply the difference when
49 * looking at the tree organization (how many levels, how many
50 * objects per level, what kind of objects, CPU and node sets, etc)
51 * and when binding to objects.
52 * However the difference must be applied when looking at object
53 * attributes such as the name, the memory size or info attributes.
59 /** \brief Type of one object attribute difference.
61 typedef enum hwloc_topology_diff_obj_attr_type_e
{
62 /** \brief The object local memory is modified.
63 * The union is a hwloc_topology_diff_obj_attr_u::hwloc_topology_diff_obj_attr_uint64_s
64 * (and the index field is ignored).
66 HWLOC_TOPOLOGY_DIFF_OBJ_ATTR_SIZE
,
68 /** \brief The object name is modified.
69 * The union is a hwloc_topology_diff_obj_attr_u::hwloc_topology_diff_obj_attr_string_s
70 * (and the name field is ignored).
73 HWLOC_TOPOLOGY_DIFF_OBJ_ATTR_NAME
,
74 /** \brief the value of an info attribute is modified.
75 * The union is a hwloc_topology_diff_obj_attr_u::hwloc_topology_diff_obj_attr_string_s.
77 HWLOC_TOPOLOGY_DIFF_OBJ_ATTR_INFO
78 } hwloc_topology_diff_obj_attr_type_t
;
80 /** \brief One object attribute difference.
82 union hwloc_topology_diff_obj_attr_u
{
83 struct hwloc_topology_diff_obj_attr_generic_s
{
84 /* each part of the union must start with these */
85 hwloc_topology_diff_obj_attr_type_t type
;
88 /** \brief Integer attribute modification with an optional index. */
89 struct hwloc_topology_diff_obj_attr_uint64_s
{
90 /* used for storing integer attributes */
91 hwloc_topology_diff_obj_attr_type_t type
;
92 hwloc_uint64_t index
; /* not used for SIZE */
93 hwloc_uint64_t oldvalue
;
94 hwloc_uint64_t newvalue
;
97 /** \brief String attribute modification with an optional name */
98 struct hwloc_topology_diff_obj_attr_string_s
{
99 /* used for storing name and info pairs */
100 hwloc_topology_diff_obj_attr_type_t type
;
101 char *name
; /* not used for NAME */
108 /** \brief Type of one element of a difference list.
110 typedef enum hwloc_topology_diff_type_e
{
111 /** \brief An object attribute was changed.
112 * The union is a hwloc_topology_diff_obj_attr_u::hwloc_topology_diff_obj_attr_s.
114 HWLOC_TOPOLOGY_DIFF_OBJ_ATTR
,
116 /** \brief The difference is too complex,
117 * it cannot be represented. The difference below
118 * this object has not been checked.
119 * hwloc_topology_diff_build() will return 1.
121 * The union is a hwloc_topology_diff_obj_attr_u::hwloc_topology_diff_too_complex_s.
123 HWLOC_TOPOLOGY_DIFF_TOO_COMPLEX
124 } hwloc_topology_diff_type_t
;
126 /** \brief One element of a difference list between two topologies.
128 typedef union hwloc_topology_diff_u
{
129 struct hwloc_topology_diff_generic_s
{
130 /* each part of the union must start with these */
131 hwloc_topology_diff_type_t type
;
132 union hwloc_topology_diff_u
* next
; /* pointer to the next element of the list, or NULL */
135 /* A difference in an object attribute. */
136 struct hwloc_topology_diff_obj_attr_s
{
137 hwloc_topology_diff_type_t type
; /* must be ::HWLOC_TOPOLOGY_DIFF_OBJ_ATTR */
138 union hwloc_topology_diff_u
* next
;
139 /* List of attribute differences for a single object */
142 union hwloc_topology_diff_obj_attr_u diff
;
145 /* A difference that is too complex. */
146 struct hwloc_topology_diff_too_complex_s
{
147 hwloc_topology_diff_type_t type
; /* must be ::HWLOC_TOPOLOGY_DIFF_TOO_COMPLEX */
148 union hwloc_topology_diff_u
* next
;
149 /* Where we had to stop computing the diff in the first topology */
153 } * hwloc_topology_diff_t
;
156 /** \brief Compute the difference between 2 topologies.
158 * The difference is stored as a list of ::hwloc_topology_diff_t entries
159 * starting at \p diff.
160 * It is computed by doing a depth-first traversal of both topology trees
163 * If the difference between 2 objects is too complex to be represented
164 * (for instance if some objects have different types, or different numbers
165 * of children), a special diff entry of type ::HWLOC_TOPOLOGY_DIFF_TOO_COMPLEX
167 * The computation of the diff does not continue below these objects.
168 * So each such diff entry means that the difference between two subtrees
169 * could not be computed.
171 * \return 0 if the difference can be represented properly.
173 * \return 0 with \p diff pointing to NULL if there is no difference
174 * between the topologies.
176 * \return 1 if the difference is too complex (see above). Some entries in
177 * the list will be of type ::HWLOC_TOPOLOGY_DIFF_TOO_COMPLEX.
179 * \return -1 on any other error.
181 * \note \p flags is currently not used. It should be 0.
183 * \note The output diff has to be freed with hwloc_topology_diff_destroy().
185 * \note The output diff can only be exported to XML or passed to
186 * hwloc_topology_diff_apply() if 0 was returned, i.e. if no entry of type
187 * ::HWLOC_TOPOLOGY_DIFF_TOO_COMPLEX is listed.
189 * \note The output diff may be modified by removing some entries from
190 * the list. The removed entries should be freed by passing them to
191 * to hwloc_topology_diff_destroy() (possible as another list).
193 HWLOC_DECLSPEC
int hwloc_topology_diff_build(hwloc_topology_t topology
, hwloc_topology_t newtopology
, unsigned long flags
, hwloc_topology_diff_t
*diff
);
195 /** \brief Flags to be given to hwloc_topology_diff_apply().
197 enum hwloc_topology_diff_apply_flags_e
{
198 /** \brief Apply topology diff in reverse direction.
201 HWLOC_TOPOLOGY_DIFF_APPLY_REVERSE
= (1UL<<0)
204 /** \brief Apply a topology diff to an existing topology.
206 * \p flags is an OR'ed set of ::hwloc_topology_diff_apply_flags_e.
208 * The new topology is modified in place. hwloc_topology_dup()
209 * may be used to duplicate it before patching.
211 * If the difference cannot be applied entirely, all previous applied
212 * elements are unapplied before returning.
214 * \return 0 on success.
216 * \return -N if applying the difference failed while trying
217 * to apply the N-th part of the difference. For instance -1
218 * is returned if the very first difference element could not
221 HWLOC_DECLSPEC
int hwloc_topology_diff_apply(hwloc_topology_t topology
, hwloc_topology_diff_t diff
, unsigned long flags
);
223 /** \brief Destroy a list of topology differences.
225 * \note The \p topology parameter must be a valid topology
226 * but it is not required that it is related to \p diff.
228 HWLOC_DECLSPEC
int hwloc_topology_diff_destroy(hwloc_topology_t topology
, hwloc_topology_diff_t diff
);
230 /** \brief Load a list of topology differences from a XML file.
232 * If not \c NULL, \p refname will be filled with the identifier
233 * string of the reference topology for the difference file,
234 * if any was specified in the XML file.
235 * This identifier is usually the name of the other XML file
236 * that contains the reference topology.
238 * \note The \p topology parameter must be a valid topology
239 * but it is not required that it is related to \p diff.
241 * \note the pointer returned in refname should later be freed
244 HWLOC_DECLSPEC
int hwloc_topology_diff_load_xml(hwloc_topology_t topology
, const char *xmlpath
, hwloc_topology_diff_t
*diff
, char **refname
);
246 /** \brief Export a list of topology differences to a XML file.
248 * If not \c NULL, \p refname defines an identifier string
249 * for the reference topology which was used as a base when
250 * computing this difference.
251 * This identifier is usually the name of the other XML file
252 * that contains the reference topology.
253 * This attribute is given back when reading the diff from XML.
255 * \note The \p topology parameter must be a valid topology
256 * but it is not required that it is related to \p diff.
258 HWLOC_DECLSPEC
int hwloc_topology_diff_export_xml(hwloc_topology_t topology
, hwloc_topology_diff_t diff
, const char *refname
, const char *xmlpath
);
260 /** \brief Load a list of topology differences from a XML buffer.
262 * If not \c NULL, \p refname will be filled with the identifier
263 * string of the reference topology for the difference file,
264 * if any was specified in the XML file.
265 * This identifier is usually the name of the other XML file
266 * that contains the reference topology.
268 * \note The \p topology parameter must be a valid topology
269 * but it is not required that it is related to \p diff.
271 * \note the pointer returned in refname should later be freed
274 HWLOC_DECLSPEC
int hwloc_topology_diff_load_xmlbuffer(hwloc_topology_t topology
, const char *xmlbuffer
, int buflen
, hwloc_topology_diff_t
*diff
, char **refname
);
276 /** \brief Export a list of topology differences to a XML buffer.
278 * If not \c NULL, \p refname defines an identifier string
279 * for the reference topology which was used as a base when
280 * computing this difference.
281 * This identifier is usually the name of the other XML file
282 * that contains the reference topology.
283 * This attribute is given back when reading the diff from XML.
285 * The returned buffer ends with a \0 that is included in the returned
288 * \note The XML buffer should later be freed with hwloc_free_xmlbuffer().
290 * \note The \p topology parameter must be a valid topology
291 * but it is not required that it is related to \p diff.
293 HWLOC_DECLSPEC
int hwloc_topology_diff_export_xmlbuffer(hwloc_topology_t topology
, hwloc_topology_diff_t diff
, const char *refname
, char **xmlbuffer
, int *buflen
);
303 #endif /* HWLOC_DIFF_H */