summaryrefslogtreecommitdiffhomepage
path: root/ir/kaps/html_dumper.c
blob: c73a9cd6473c89b44125cc4023d02946cb148f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief   HTML dumper for PBQP.
 * @date    03.10.2008
 * @author  Sebastian Buchwald
 */
#include "html_dumper.h"

#include "adt/array.h"
#include "kaps.h"
#include "optimal.h"
#include "pbqp_edge_t.h"
#include "pbqp_node_t.h"
#include <assert.h>

/* Caution: Due to static buffer use only once per statement */
static const char *cost2a(num const cost)
{
	if (cost == INF_COSTS)
		return "inf";

	static char buf[10];

#if KAPS_USE_UNSIGNED
	sprintf(buf, "%u", cost);
#else
	sprintf(buf, "%10lld", cost);
#endif

	return buf;
}

/* print vector */
static void dump_vector(FILE *f, vector_t *vec)
{
	unsigned len = vec->len;
	assert(len > 0);

	fprintf(f, "<span class=\"vector\">( ");

	for (unsigned index = 0; index < len; ++index) {
#if KAPS_ENABLE_VECTOR_NAMES
		fprintf(f, "<span title=\"%s\">%s</span> ",
		        vec->entries[index].name, cost2a(vec->entries[index].data));
#else
		fprintf(f, "%s ", cost2a(vec->entries[index].data));
#endif
	}

	fprintf(f, " )</span>\n");
}

static void dump_matrix(FILE *f, pbqp_matrix_t *mat)
{
	assert(mat->cols > 0);
	assert(mat->rows > 0);

	num *p = mat->entries;

	fprintf(f, "\t\\begin{pmatrix}\n");

	for (unsigned row = 0; row < mat->rows; ++row) {
		fprintf(f, "\t %s", cost2a(*p++));

		for (unsigned col = 1; col < mat->cols; ++col) {
			fprintf(f, "& %s", cost2a(*p++));
		}

		fprintf(f, "\\\\\n");
	}

	fprintf(f, "\t\\end{pmatrix}\n");
}

void pbqp_dump_edge(FILE *file, pbqp_edge_t *edge)
{
	fputs("<tex>\n", file);
	fprintf(file, "\t\\overline\n{C}_{%u,%u}=\n",
	        edge->src->index, edge->tgt->index);
	dump_matrix(file, edge->costs);
	fputs("</tex><br>", file);
}

static void dump_edge_costs(pbqp_t *pbqp)
{
	fputs("<p>", pbqp->dump_file);

	for (unsigned src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
		pbqp_node_t *src_node = get_node(pbqp, src_index);
		size_t edge_index;
		size_t len;

		if (!src_node)
			continue;

		len = ARR_LEN(src_node->edges);
		for (edge_index = 0; edge_index < len; ++edge_index) {
			pbqp_edge_t *edge      = src_node->edges[edge_index];
			unsigned     tgt_index = edge->tgt->index;
			if (src_index < tgt_index) {
				pbqp_dump_edge(pbqp->dump_file, edge);
			}
		}
	}

	fputs("</p>", pbqp->dump_file);
}

void pbqp_dump_node(FILE *file, pbqp_node_t *node)
{
	if (node) {
		fprintf(file, "\tc<sub>%u</sub> = ", node->index);
		dump_vector(file, node->costs);
		fputs("<br>\n", file);
	}
}

static void dump_node_costs(pbqp_t *pbqp)
{
	/* dump node costs */
	fputs("<p>", pbqp->dump_file);

	for (unsigned index = 0; index < pbqp->num_nodes; ++index) {
		pbqp_dump_node(pbqp->dump_file, get_node(pbqp, index));
	}

	fputs("</p>", pbqp->dump_file);
}

void pbqp_dump_section(FILE *f, int level, const char *txt)
{
	fprintf(f, "<h%d>%s</h%d>\n", level, txt, level);
}

void pbqp_dump_graph(pbqp_t *pbqp)
{
	fputs("<p>\n<graph>\n\tgraph input {\n", pbqp->dump_file);

	for (unsigned src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
		pbqp_node_t *node = get_node(pbqp, src_index);

		if (node && !node_is_reduced(node)) {
			fprintf(pbqp->dump_file, "\t n%u;\n", src_index);
		}
	}

	for (unsigned src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
		pbqp_node_t *node = get_node(pbqp, src_index);

		if (!node)
			continue;

		if (node_is_reduced(node))
			continue;

		unsigned len = ARR_LEN(node->edges);

		for (unsigned edge_index = 0; edge_index < len; ++edge_index) {
			pbqp_node_t *tgt_node  = node->edges[edge_index]->tgt;
			unsigned     tgt_index = tgt_node->index;

			if (node_is_reduced(tgt_node))
				continue;

			if (src_index < tgt_index) {
				fprintf(pbqp->dump_file, "\t n%u -- n%u;\n", src_index,
				        tgt_index);
			}
		}
	}

	fputs("\t}\n</graph>\n</p>\n", pbqp->dump_file);
}

void pbqp_dump_input(pbqp_t *pbqp)
{
	pbqp_dump_section(pbqp->dump_file, 1, "1. PBQP Problem");
	pbqp_dump_section(pbqp->dump_file, 2, "1.1 Topology");
	pbqp_dump_graph(pbqp);
	pbqp_dump_section(pbqp->dump_file, 2, "1.2 Cost Vectors");
	dump_node_costs(pbqp);
	pbqp_dump_section(pbqp->dump_file, 2, "1.3 Cost Matrices");
	dump_edge_costs(pbqp);
}

void pbqp_dump_simplifyedge(pbqp_t *pbqp, pbqp_edge_t *edge)
{
	pbqp_dump_node(pbqp->dump_file, edge->src);
	pbqp_dump_edge(pbqp->dump_file, edge);
	pbqp_dump_node(pbqp->dump_file, edge->tgt);
}