summaryrefslogtreecommitdiffstats
path: root/src/phys/place/place_test.c
blob: 93f762f8b8c50159d52b7ec14d965e7d6549f477 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*===================================================================*/
//  
//     place_test.c
//
//        Aaron P. Hurst, 2003-2007
//              ahurst@eecs.berkeley.edu
//
/*===================================================================*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "place_base.h"

ABC_NAMESPACE_IMPL_START



// --------------------------------------------------------------------
// Hash type/functions
//
// --------------------------------------------------------------------

struct hash_element {
  ConcreteCell        *obj;
  struct hash_element *next;
} hash_element;

int hash_string(int hash_max, const char *str) {
  unsigned int hash = 0;
  int p;
  for(p = 0; p<strlen(str); p++)
    hash += str[p]*p;
  return hash % hash_max;
}

void hash_add(struct hash_element **hash, int hash_max,
              ConcreteCell *cell) {
  int key = hash_string(hash_max, cell->m_label);
  // printf("adding %s key = %d\n", cell->m_label, key);
  struct hash_element *element = malloc(sizeof(struct hash_element));
  assert(element);
  element->obj = cell;
  element->next = hash[key];
  hash[key] = element;
}

ConcreteCell *hash_find(struct hash_element **hash, int hash_max, const char *str) {
  int key = hash_string(hash_max, str);
  // printf("looking for %s key = %d\n", str, key);
  struct hash_element *next = hash[key];
  while(next) {
    if (!strcmp(str, next->obj->m_label))
      return next->obj;
    next = next->next;
  }
  return 0;
}

// --------------------------------------------------------------------
// Global variables
//
// --------------------------------------------------------------------

struct hash_element **hash_cellname;

int numCells = 0, numNets = 0;

AbstractCell *abstractCells;
ConcreteCell *concreteCells;
ConcreteNet  *concreteNets;

// --------------------------------------------------------------------
// Function implementations
//
// --------------------------------------------------------------------

void readBookshelfNets(char *filename) {
  char *tok;
  char buf[1024];
  const char *DELIMITERS = " \n\t:";
  int id = 0;
  int t;
  ConcreteCell *cell;

  FILE *netsFile = fopen(filename, "r");
  if (!netsFile) {
    printf("ERROR: Could not open .nets file\n");
    exit(1);
  }

  // line 1 : version
  while (fgets(buf, 1024, netsFile) && (buf[0] == '\n' || buf[0] == '#'));

  // line 2 : number of nets
  while (fgets(buf, 1024, netsFile) && (buf[0] == '\n' || buf[0] == '#'));
  tok = strtok(buf, DELIMITERS);
  tok = strtok(NULL, DELIMITERS);
  numNets = atoi(tok);
  printf("READ-20 : number of nets = %d\n", numNets);
  concreteNets = malloc(sizeof(ConcreteNet)*numNets);

  // line 3 : number of pins
  while (fgets(buf, 1024, netsFile) && (buf[0] == '\n' || buf[0] == '#'));

  // line XXX : net definitions
  while(fgets(buf, 1024, netsFile)) {
    if (buf[0] == '\n' || buf[0] == '#') continue;

    concreteNets[id].m_id = id;
    concreteNets[id].m_weight = 1.0;

    tok = strtok(buf, DELIMITERS);
    if (!!strcmp(tok, "NetDegree")) {
      printf("%s\n",buf);
      printf("ERROR: Incorrect format in .nets file\n");
      exit(1);
    }

    tok = strtok(NULL, DELIMITERS);
    concreteNets[id].m_numTerms = atoi(tok);
    if (concreteNets[id].m_numTerms < 0 ||
        concreteNets[id].m_numTerms > 100000) {
      printf("ERROR: Bad net degree\n");
      exit(1); 
    }
    concreteNets[id].m_terms = malloc(sizeof(ConcreteCell*)*
         concreteNets[id].m_numTerms);

    // read terms
    t = 0;
    while(t < concreteNets[id].m_numTerms &&
          fgets(buf, 1024, netsFile)) {
      if (buf[0] == '\n' || buf[0] == '#') continue;
      
      // cell name
      tok = strtok(buf, DELIMITERS);
      cell = hash_find(hash_cellname, numCells, tok);
      if (!cell) {
        printf("ERROR: Could not find cell %s in .nodes file\n", tok);
        exit(1);
      }
      concreteNets[id].m_terms[t] = cell;
      t++;
    }

    // add!
    addConcreteNet(&(concreteNets[id]));

    id++;
  }

  fclose(netsFile);
}

void readBookshelfNodes(char *filename) {
  char *tok;
  char buf[1024];
  const char *DELIMITERS = " \n\t:";
  int id = 0;

  FILE *nodesFile = fopen(filename, "r");
  if (!nodesFile) {
    printf("ERROR: Could not open .nodes file\n");
    exit(1);
  }

  // line 1 : version
  while (fgets(buf, 1024, nodesFile) && (buf[0] == '\n' || buf[0] == '#'));

  // line 2 : num nodes
  while (fgets(buf, 1024, nodesFile) && (buf[0] == '\n' || buf[0] == '#'));
  tok = strtok(buf, DELIMITERS);
  tok = strtok(NULL, DELIMITERS);
  numCells = atoi(tok);
  printf("READ-10 : number of cells = %d\n", numCells);
  concreteCells = malloc(sizeof(ConcreteCell)*numCells);
  abstractCells = malloc(sizeof(AbstractCell)*numCells);
  hash_cellname = calloc(numCells, sizeof(struct hash_element*));

  // line 3 : num terminals
  while (fgets(buf, 1024, nodesFile) && (buf[0] == '\n' || buf[0] == '#'));

  // line XXX : cell definitions
  while(fgets(buf, 1024, nodesFile)) {
    if (buf[0] == '\n' || buf[0] == '#') continue;

    tok = strtok(buf, DELIMITERS);
    concreteCells[id].m_id = id;;
    
    // label
    concreteCells[id].m_parent = &(abstractCells[id]);
    concreteCells[id].m_label = malloc(sizeof(char)*strlen(tok)+1);
    strcpy(concreteCells[id].m_label, tok);
    abstractCells[id].m_label = concreteCells[id].m_label;
    hash_add(hash_cellname, numCells, 
             &(concreteCells[id]));

    // dimensions
    tok = strtok(NULL, DELIMITERS);
    abstractCells[id].m_width = atof(tok);
    tok = strtok(NULL, DELIMITERS);
    abstractCells[id].m_height = atof(tok);
    tok = strtok(NULL, DELIMITERS);
    // terminal
    abstractCells[id].m_pad = tok && !strcmp(tok, "terminal");

    // add!
    addConcreteCell(&(concreteCells[id]));

    // DEBUG
    /*
    printf("\"%s\" : %f x %f\n", concreteCells[id].m_label,
           abstractCells[id].m_width,
           abstractCells[id].m_height);
    */
    id++;
  }

  fclose(nodesFile);
}

void readBookshelfPlacement(char *filename) {
  char *tok;
  char buf[1024];
  const char *DELIMITERS = " \n\t:";
  ConcreteCell *cell;

  FILE *plFile = fopen(filename, "r");
  FILE *netsFile = fopen(filename, "r");
  if (!plFile) {
    printf("ERROR: Could not open .pl file\n");
    exit(1);
  }
  if (!netsFile) {
    printf("ERROR: Could not open .nets file\n");
    exit(1);
  }

  // line 1 : version
  while (fgets(buf, 1024, plFile) && (buf[0] == '\n' || buf[0] == '#'));

  // line XXX : placement definitions
  while(fgets(buf, 1024, plFile)) {
    if (buf[0] == '\n' || buf[0] == '#') continue;

    tok = strtok(buf, DELIMITERS);

    // cell name
    cell = hash_find(hash_cellname, numCells, tok);
    if (!cell) {
      printf("ERROR: Could not find cell %s in .nodes file\n",tok);
      exit(1);
    }

    // position
    tok = strtok(NULL, DELIMITERS);
    cell->m_x = atof(tok);
    tok = strtok(NULL, DELIMITERS);
    cell->m_y = atof(tok);

    // hfixed
    cell->m_fixed = strtok(NULL, DELIMITERS) && 
      (tok = strtok(NULL, DELIMITERS)) &&
      !strcmp(tok, "\\FIXED");
  }
  
  fclose(plFile);
}

void writeBookshelfPlacement(char *filename) {
  int c = 0;

  FILE *plFile = fopen(filename, "w");
  if (!plFile) {
    printf("ERROR: Could not open .pl file\n");
    exit(1);
  }

  fprintf(plFile, "UCLA pl 1.0\n");
  for(c=0; c<numCells; c++) {
    fprintf(plFile, "%s %f %f : N %s\n", 
            concreteCells[c].m_label,
            concreteCells[c].m_x,
            concreteCells[c].m_y,
            (concreteCells[c].m_fixed ? "\\FIXED" : ""));
  }

  fclose(plFile);
}

// deletes all connections to a cell
void delNetConnections(ConcreteCell *cell) {
  int n, t, t2, count = 0;
  ConcreteCell **old = malloc(sizeof(ConcreteCell*)*g_place_numCells);

  for(n=0; n<g_place_numNets; n++) if (g_place_concreteNets[n]) {
    ConcreteNet *net = g_place_concreteNets[n];
    count = 0;
    for(t=0; t<net->m_numTerms; t++)
      if (net->m_terms[t] == cell) count++;
    if (count) {
      memcpy(old, net->m_terms, sizeof(ConcreteCell*)*net->m_numTerms);
      net->m_terms = realloc(net->m_terms, 
                             sizeof(ConcreteCell*)*(net->m_numTerms-count));
      t2 = 0;
      for(t=0; t<net->m_numTerms; t++)
        if (old[t] != cell) net->m_terms[t2++] = old[t];
      net->m_numTerms -= count;
    }
  }
  free(old);
}

int main(int argc, char **argv) {

  if (argc != 4) {
    printf("Usage: %s [nodes] [nets] [pl]\n", argv[0]);
    exit(1);
  }

  readBookshelfNodes(argv[1]);
  readBookshelfNets(argv[2]);
  readBookshelfPlacement(argv[3]);

  globalPreplace(0.8);
  globalPlace();

  // DEBUG net/cell removal/addition
  /*
  int i;
  for(i=1000; i<2000; i++) {
    delConcreteNet(g_place_concreteNets[i]);
    delNetConnections(g_place_concreteCells[i]);
    delConcreteCell(g_place_concreteCells[i]);
  }
  
  ConcreteCell newCell[2];
  newCell[0].m_id = g_place_numCells+1;
  newCell[0].m_x = 1000;
  newCell[0].m_y = 1000;
  newCell[0].m_fixed = false;
  newCell[0].m_parent = &(abstractCells[1000]);
  newCell[0].m_label = " ";
  addConcreteCell(&newCell[0]);
  newCell[1].m_id = g_place_numCells+3;
  newCell[1].m_x = 1000;
  newCell[1].m_y = 1000;
  newCell[1].m_fixed = false;
  newCell[1].m_parent = &(abstractCells[1000]);
  newCell[1].m_label = " ";
  addConcreteCell(&newCell[1]);
  */

  globalIncremental();

  writeBookshelfPlacement(argv[3]);

  free(hash_cellname);

  return 0;
}
ABC_NAMESPACE_IMPL_END