File: collisions.c

package info (click to toggle)
ga 5.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,472 kB
  • sloc: ansic: 192,963; fortran: 53,761; f90: 11,218; cpp: 5,784; makefile: 2,248; sh: 1,945; python: 1,734; perl: 534; csh: 134; asm: 106
file content (188 lines) | stat: -rw-r--r-- 4,417 bytes parent folder | download | duplicates (10)
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
#if HAVE_CONFIG_H
#   include "config.h"
#endif

#if HAVE_STDIO_H
#   include <stdio.h>
#endif
#if HAVE_STDLIB_H
#   include <stdlib.h>
#endif

int **Patches,  *Col, *lastCol;
int p;
   
#define hash(ilo,ihi,p) (((ilo)+(ihi))%(p))
#define hash2(ilo,ihi,jlo,jhi,p) ( ((ilo)+(ihi) + (jlo)+(jhi))%(p) )

int LocFound(int *patch);


int **idim2(row,col)
int row,col;
{
   register int **prow, *pdata, i;
   
   pdata = (int*) calloc(row*col, sizeof(int));
   if(pdata == (int*) NULL){
      printf("Memory allocation failed - data: %dx%d\n",row,col);
      exit(1);
   }
   prow = (int **)calloc(row,sizeof(int *));
   if(prow == (int**) NULL){
      printf("memory allocation failed for prow");
      exit(1);
   }
   for(i=0;i<row;i++){
      prow[i]= pdata;
      pdata += col;
   }
   return(prow);
}

float **fdim2(row,col)
int row,col;
{
   int i;
   register float **prow, *pdata;
   
   pdata = (float*) calloc(row*col, sizeof(float));
   if(pdata == (float*) NULL){
      printf("Memory allocation failed - data: %dx%d\n",row,col);
      exit(1);
   }
   prow = (float **)calloc(row,sizeof(float*));
   if(prow == (float**) NULL){
      printf("memory allocation failed for prow");
      exit(1);
   }
   for(i=0;i<row;i++){
      prow[i]= pdata;
      pdata += col;
   }
   return(prow);
}

int main(argc,argv)
int argc;
char **argv;
{
FILE *fin;
char ln[100];
int i,j,k,patch[5],from,to;
int flag, cur_col, loc, max_col=0, last_col;
unsigned long time;

   if(argc<5){
     printf("Usage: collisions <number of processors> <filename> <from> <to>\n");
     printf("(from,to) specifies the range of collision numbers to be printed\n");
     exit(1);
   }
   sscanf(argv[1],"%d",&p);
   if(p>1000||p<1){
      printf("%d processors ? Please be reasonable ...\n",p);
      exit(1);
   }
   fin = fopen(argv[2],"r");
   if(!fin){
          printf("%s: File Not Found, Exiting ...\n",argv[2]);
          exit(1);
   }

   sscanf(argv[3],"%d",&from);
   sscanf(argv[4],"%d",&to);
   if(from<0||from>p||to>p||to<from){
      printf("Wrong values of <from> and <to> \n");
      exit(1);
   }
    if(!(Col = (int*)calloc(p,sizeof(int)))){
                     printf("couldn't allocate memory 2\n");
                     exit(2);
   }

   Patches = idim2(p,5);
   for(i=0;i<p;i++){
      Patches[0][i]=0;
   }

   for(j=0;;j++){
     fgets(ln,100,fin);
     if(feof(fin))break;
     if(9>sscanf(ln,"%d%d%d%d%d%d%d%d%lu",
            &i,&k,patch+1,patch+2,patch+3,patch+4,&i,&flag,&time))continue;

     loc = LocFound(patch);  
     if(!Patches[loc][0])for(i=1;i<5;i++)Patches[loc][i]=patch[i];
     last_col = Patches[loc][0];

     if(flag == 1){ 
       cur_col = ++Patches[loc][0];
       if(cur_col>p){
         printf("%d -- Error in collision number. Record: %d\n",cur_col,j);
         exit(3);
       }
       Col[cur_col-1]++;
       if(max_col<cur_col) max_col=cur_col;
     }else{
       cur_col = Patches[loc][0]--;
       if(cur_col<0){
         printf("%d -- Error in collision number. Record: %d\n",cur_col,j);
         exit(3);
       }
       Col[cur_col-1] --;
       if(!Col[max_col-1])max_col--;
     }

     /* selective output */
     if((last_col>=from && last_col<=to) || (cur_col>=from && cur_col<=to)){
       printf("%f ",1e-6*time);
       for(i=from-1;i<to;i++)printf("%d ",Col[i]);
       printf("\n");
     }
   }

   return 0;
}


int diff(a,b,n)
int *a,*b,n;
{
  int i;
  for (i=0;i<n;i++) if(a[i]!=b[i])return(1);
  return(0);
}


int LocFound(patch)
int *patch;
/* Uses 2 hash functions and exhaustive search to find existing patch *
 * if not found then returns the first found empty slot               */
{
int loc, empty, i;

  empty = -1;
  loc = hash(patch[1],patch[2],p);
  if(!Patches[loc][0]) empty = loc;
       /* check if this is our patch */
  else if(!diff(Patches[loc]+1,patch+1,4)) return(loc);

  /* not yet found so check another location */
  loc = hash2(patch[1],patch[2],patch[3],patch[4],p);

  if(!Patches[loc][0]) empty = (empty != -1)? empty: loc;
  else if(!diff(Patches[loc]+1,patch+1,4)) return(loc);

  /* all is left is to search */ 

  for(loc=0;loc<p;loc++){
    if(!Patches[loc][0]) empty = (empty != -1)? empty: loc;
    else if(!diff(Patches[loc]+1,patch+1,4)) return(loc); 
  }
  if(empty != -1) return(empty);

  printf("Error in  LocFound ");
  for(i=0;i<5; i++)printf("%d ",patch[i]); printf("\n");
  exit(5);
  return(0); /* never gets here */
}