xref: /netbsd-src/usr.bin/nbperf/graph2.h (revision 7c219dd6db4315f574c078369f92489252c284c0)
1*7c219dd6Sjoerg /*	$NetBSD: graph2.h,v 1.2 2021/01/07 16:03:08 joerg Exp $	*/
203c8ba1cSjoerg /*-
303c8ba1cSjoerg  * Copyright (c) 2009 The NetBSD Foundation, Inc.
403c8ba1cSjoerg  * All rights reserved.
503c8ba1cSjoerg  *
603c8ba1cSjoerg  * This code is derived from software contributed to The NetBSD Foundation
703c8ba1cSjoerg  * by Joerg Sonnenberger.
803c8ba1cSjoerg  *
903c8ba1cSjoerg  * Redistribution and use in source and binary forms, with or without
1003c8ba1cSjoerg  * modification, are permitted provided that the following conditions
1103c8ba1cSjoerg  * are met:
1203c8ba1cSjoerg  *
1303c8ba1cSjoerg  * 1. Redistributions of source code must retain the above copyright
1403c8ba1cSjoerg  *    notice, this list of conditions and the following disclaimer.
1503c8ba1cSjoerg  * 2. Redistributions in binary form must reproduce the above copyright
1603c8ba1cSjoerg  *    notice, this list of conditions and the following disclaimer in
1703c8ba1cSjoerg  *    the documentation and/or other materials provided with the
1803c8ba1cSjoerg  *    distribution.
1903c8ba1cSjoerg  *
2003c8ba1cSjoerg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2103c8ba1cSjoerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2203c8ba1cSjoerg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2303c8ba1cSjoerg  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2403c8ba1cSjoerg  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2503c8ba1cSjoerg  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2603c8ba1cSjoerg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2703c8ba1cSjoerg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2803c8ba1cSjoerg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2903c8ba1cSjoerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3003c8ba1cSjoerg  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3103c8ba1cSjoerg  * SUCH DAMAGE.
3203c8ba1cSjoerg  */
3303c8ba1cSjoerg 
3403c8ba1cSjoerg /*
35*7c219dd6Sjoerg  * Implementation of common 2/3-graph routines:
36*7c219dd6Sjoerg  * - build a 2/3-graph with hash-pairs as edges
37*7c219dd6Sjoerg  * - check a 2/3-graph for acyclicness and compute an output order
38*7c219dd6Sjoerg *
39*7c219dd6Sjoerg  * For each vertex in the 2/3-graph, the incidence lists need to kept.
40*7c219dd6Sjoerg  * Avoid storing the full list by just XORing the indices of the still
41*7c219dd6Sjoerg  * incident edges and the number of such edges as that's all the peeling
42*7c219dd6Sjoerg  * computation needs. This is inspired by:
43*7c219dd6Sjoerg  *   Cache-Oblivious Peeling of Random Hypergraphs by Djamal Belazzougui,
44*7c219dd6Sjoerg  *   Paolo Boldi, Giuseppe Ottaviano, Rossano Venturini, and Sebastiano
45*7c219dd6Sjoerg  *   Vigna. https://arxiv.org/abs/1312.0526
46*7c219dd6Sjoerg  *
47*7c219dd6Sjoerg  * Unlike in the paper, we don't care about external storage and have
48*7c219dd6Sjoerg  * the edge list at hand all the time. As such, no ordering is necessary
49*7c219dd6Sjoerg  * and the vertices of the edge don't have to be copied.
50*7c219dd6Sjoerg  *
51*7c219dd6Sjoerg  * The core observation of the paper above is that for a degree of one,
52*7c219dd6Sjoerg  * the incident edge can be obtained directly.
5303c8ba1cSjoerg  */
5403c8ba1cSjoerg 
55*7c219dd6Sjoerg #ifndef GRAPH_SIZE
56*7c219dd6Sjoerg #define GRAPH_SIZE 2
57*7c219dd6Sjoerg #endif
58*7c219dd6Sjoerg 
59*7c219dd6Sjoerg #define SIZED__(n, i) n ## i
60*7c219dd6Sjoerg #define SIZED_(n, i) SIZED__(n, i)
61*7c219dd6Sjoerg #define SIZED(n) SIZED_(n, GRAPH_SIZE)
62*7c219dd6Sjoerg #define SIZED2__(n, i, m) n ## i ## m
63*7c219dd6Sjoerg #define SIZED2_(n, i, m) SIZED2__(n, i, m)
64*7c219dd6Sjoerg #define SIZED2(n) SIZED2_(graph, GRAPH_SIZE, n)
65*7c219dd6Sjoerg 
SIZED(vertex)66*7c219dd6Sjoerg struct SIZED(vertex) {
67*7c219dd6Sjoerg 	uint32_t degree, edges;
6803c8ba1cSjoerg };
6903c8ba1cSjoerg 
SIZED(edge)70*7c219dd6Sjoerg struct SIZED(edge) {
71*7c219dd6Sjoerg 	uint32_t vertices[GRAPH_SIZE];
7203c8ba1cSjoerg };
7303c8ba1cSjoerg 
SIZED(graph)74*7c219dd6Sjoerg struct SIZED(graph) {
75*7c219dd6Sjoerg 	struct SIZED(vertex) *verts;
76*7c219dd6Sjoerg 	struct SIZED(edge) *edges;
7703c8ba1cSjoerg 	uint32_t output_index;
7803c8ba1cSjoerg 	uint32_t *output_order;
7903c8ba1cSjoerg 	uint8_t *visited;
8003c8ba1cSjoerg 	uint32_t e, v;
81*7c219dd6Sjoerg 	int hash_fudge;
8203c8ba1cSjoerg };
8303c8ba1cSjoerg 
84*7c219dd6Sjoerg void	SIZED2(_setup)(struct SIZED(graph) *, uint32_t, uint32_t);
85*7c219dd6Sjoerg void	SIZED2(_free)(struct SIZED(graph) *);
8603c8ba1cSjoerg 
87*7c219dd6Sjoerg int	SIZED2(_hash)(struct nbperf *, struct SIZED(graph) *);
88*7c219dd6Sjoerg int	SIZED2(_output_order)(struct SIZED(graph) *graph);
89