Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
uvahotspot
GitHub Repository: uvahotspot/HotSpot
Path: blob/master/flp_desc.c
612 views
1
#include <stdio.h>
2
#ifdef _MSC_VER
3
#define strcasecmp _stricmp
4
#define strncasecmp _strnicmp
5
#else
6
#include <strings.h>
7
#endif
8
#include <string.h>
9
#include <stdlib.h>
10
11
#include "flp.h"
12
#include "shape.h"
13
#include "util.h"
14
15
/* get unit index from its name */
16
int desc_get_blk_index(flp_desc_t *flp_desc, char *name)
17
{
18
int i;
19
char msg[STR_SIZE];
20
21
if (!flp_desc)
22
fatal("null pointer in get_desc_blk_index\n");
23
24
for (i = 0; i < flp_desc->n_units; i++) {
25
if (!strcasecmp(name, flp_desc->units[i].name)) {
26
return i;
27
}
28
}
29
30
sprintf(msg, "block %s not found\n", name);
31
fatal(msg);
32
return -1;
33
}
34
35
36
/*
37
* find the number of units from the
38
* floorplan description file
39
*/
40
int desc_count_units(FILE *fp)
41
{
42
char str1[LINE_SIZE], str2[LINE_SIZE];
43
char name[STR_SIZE];
44
double area, min, max;
45
int rotable;
46
char *ptr;
47
int count = 0;
48
49
fseek(fp, 0, SEEK_SET);
50
while(!feof(fp)) {
51
fgets(str1, LINE_SIZE, fp);
52
if (feof(fp))
53
break;
54
strcpy(str2, str1);
55
56
/* ignore comments and empty lines */
57
ptr = strtok(str1, " \r\t\n");
58
if (!ptr || ptr[0] == '#')
59
continue;
60
61
/* lines describing functional block size and aspect ratio */
62
if (sscanf(str2, "%s%lf%lf%lf%d", name, &area, &min, &max, &rotable) == 5)
63
count++;
64
}
65
return count;
66
}
67
68
flp_desc_t *desc_alloc_init_mem(int count, flp_config_t *config)
69
{
70
int i;
71
flp_desc_t *flp_desc;
72
flp_desc = (flp_desc_t *) calloc (1, sizeof(flp_desc_t));
73
if(!flp_desc)
74
fatal("memory allocation error\n");
75
flp_desc->units = (unplaced_t *) calloc(count, sizeof(unplaced_t));
76
flp_desc->wire_density = (double **) calloc(count, sizeof(double *));
77
if (!flp_desc->units || !flp_desc->wire_density)
78
fatal("memory allocation error\n");
79
flp_desc->n_units = count;
80
flp_desc->config = *config;
81
82
for (i=0; i < count; i++) {
83
flp_desc->wire_density[i] = (double *) calloc(count, sizeof(double));
84
if (!flp_desc->wire_density[i])
85
fatal("memory allocation error\n");
86
}
87
return flp_desc;
88
}
89
90
/* populate block information */
91
void desc_populate_blks(flp_desc_t *flp_desc, FILE *fp)
92
{
93
int i=0;
94
char str1[LINE_SIZE], str2[LINE_SIZE];
95
char name1[STR_SIZE], name2[STR_SIZE];
96
double area, min, max, wire_density;
97
int rotable;
98
char *ptr;
99
int wrap_l2 = FALSE;
100
101
fseek(fp, 0, SEEK_SET);
102
while(!feof(fp)) {
103
fgets(str1, LINE_SIZE, fp);
104
if (feof(fp))
105
break;
106
strcpy(str2, str1);
107
108
/* ignore comments and empty lines */
109
ptr = strtok(str1, " \r\t\n");
110
if (!ptr || ptr[0] == '#')
111
continue;
112
113
/* lines describing functional block size and aspect ratio */
114
if (sscanf(str2, "%s%lf%lf%lf%d", name1, &area, &min, &max, &rotable) == 5) {
115
if (min > max)
116
fatal("minimum aspect ratio greater than maximum\n");
117
if (min <= 0 || max <= 0 || area <= 0)
118
fatal("invalid number in floorplan description\n");
119
/* L2 wrap around */
120
if (flp_desc->config.wrap_l2 && !strcasecmp(name1, flp_desc->config.l2_label)) {
121
wrap_l2 = TRUE;
122
/* min, max, rotable and shape curve have no meaning here */
123
strcpy(flp_desc->units[flp_desc->n_units - 1].name, name1);
124
flp_desc->units[flp_desc->n_units - 1].area = area;
125
} else {
126
strcpy(flp_desc->units[i].name, name1);
127
flp_desc->units[i].area = area;
128
flp_desc->units[i].min_aspect = min;
129
flp_desc->units[i].max_aspect = max;
130
flp_desc->units[i].rotable = rotable;
131
flp_desc->units[i].shape = shape_from_aspect(area, min, max, rotable,
132
flp_desc->config.n_orients);
133
i++;
134
}
135
} else if (sscanf(str2, "%s%s%lf", name1, name2, &wire_density) != 3)
136
fatal("invalid floorplan description file format\n");
137
}
138
139
if((wrap_l2 && i != flp_desc->n_units - 1) ||
140
(!wrap_l2 && i != flp_desc->n_units))
141
fatal("mismatch of number of units\n");
142
}
143
144
/* populate connectivity info */
145
void desc_populate_connects(flp_desc_t *flp_desc, FILE *fp)
146
{
147
char str1[LINE_SIZE], str2[LINE_SIZE];
148
char name1[STR_SIZE], name2[STR_SIZE];
149
double area, min, max, wire_density;
150
int rotable;
151
char *ptr;
152
153
fseek(fp, 0, SEEK_SET);
154
while(!feof(fp)) {
155
fgets(str1, LINE_SIZE, fp);
156
if (feof(fp))
157
break;
158
strcpy(str2, str1);
159
160
/* ignore comments and empty lines */
161
ptr = strtok(str1, " \r\t\n");
162
if (!ptr || ptr[0] == '#')
163
continue;
164
165
/* ignore lines describing functional block size and aspect ratio */
166
if (sscanf(str2, "%s%lf%lf%lf%d", name1, &area, &min, &max, &rotable) == 5)
167
continue;
168
169
/* lines with connectivity info */
170
else if (sscanf(str2, "%s%s%lf", name1, name2, &wire_density) == 3) {
171
int x, y;
172
x = desc_get_blk_index(flp_desc, name1);
173
y = desc_get_blk_index(flp_desc, name2);
174
175
if (x == y)
176
fatal("block connected to itself?\n");
177
178
if (!flp_desc->wire_density[x][y] && !flp_desc->wire_density[y][x])
179
flp_desc->wire_density[x][y] = flp_desc->wire_density[y][x] = wire_density;
180
else if((flp_desc->wire_density[x][y] != flp_desc->wire_density[y][x]) ||
181
(flp_desc->wire_density[x][y] != wire_density)) {
182
sprintf(str2, "wrong connectivity information for blocks %s and %s\n",
183
name1, name2);
184
fatal(str2);
185
}
186
} else
187
fatal("invalid floorplan description file format\n");
188
} /* end while */
189
}
190
191
/* read floorplan description and allocate memory */
192
flp_desc_t *read_flp_desc(char *file, flp_config_t *config)
193
{
194
char str[STR_SIZE];
195
FILE *fp = fopen(file, "r");
196
flp_desc_t *flp_desc;
197
int count;
198
199
if (!fp) {
200
sprintf(str, "error opening file %s\n", file);
201
fatal(str);
202
}
203
204
/* 1st pass - find n_units */
205
count = desc_count_units(fp);
206
if(!count)
207
fatal("no units specified in the floorplan description file\n");
208
209
/* allocate initial memory */
210
flp_desc = desc_alloc_init_mem(count, config);
211
212
/* 2nd pass - populate block info */
213
desc_populate_blks(flp_desc, fp);
214
215
/* 3rd pass - populate connectivity info */
216
desc_populate_connects(flp_desc, fp);
217
218
fclose(fp);
219
return flp_desc;
220
}
221
222
void free_flp_desc(flp_desc_t *flp_desc)
223
{
224
int i;
225
for (i=0; i < flp_desc->n_units; i++) {
226
/* wrapped L2 doesn't have a shape curve */
227
if (flp_desc->units[i].shape)
228
free_shape(flp_desc->units[i].shape);
229
free(flp_desc->wire_density[i]);
230
}
231
free(flp_desc->units);
232
free(flp_desc->wire_density);
233
free(flp_desc);
234
}
235
236
/* debug print */
237
void print_unplaced(unplaced_t *unit)
238
{
239
fprintf(stdout, "printing unit info for %s\n", unit->name);
240
fprintf(stdout, "area=%.5f\tmin=%.5f\tmax=%.5f\trotable=%d\n",
241
unit->area, unit->min_aspect, unit->max_aspect, unit->rotable);
242
print_shape(unit->shape);
243
fprintf(stdout, "\n");
244
}
245
246
void desc_print_wire_density(flp_desc_t *flp_desc)
247
{
248
int i, j;
249
250
fprintf(stdout, "printing wire density matrix\n\t");
251
252
/* header row */
253
for(i=0; i < flp_desc->n_units; i++)
254
fprintf(stdout, "%s\t", flp_desc->units[i].name);
255
fprintf(stdout, "\n");
256
257
for(i=0; i < flp_desc->n_units; i++) {
258
fprintf(stdout, "%s\t", flp_desc->units[i].name);
259
for(j=0; j < flp_desc->n_units; j++)
260
fprintf(stdout, "%.3f\t", flp_desc->wire_density[i][j]);
261
fprintf(stdout, "\n");
262
}
263
fprintf(stdout, "\n");
264
}
265
266
267
/* debug print */
268
void print_flp_desc(flp_desc_t *flp_desc)
269
{
270
int i;
271
272
for(i=0; i < flp_desc->n_units; i++)
273
print_unplaced(&flp_desc->units[i]);
274
275
desc_print_wire_density(flp_desc);
276
fprintf(stdout, "\n");
277
}
278
279
280