-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScoreOperator.cc
357 lines (307 loc) · 8.96 KB
/
ScoreOperator.cc
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
//////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 1999 The Regents of the University of California
// Permission to use, copy, modify, and distribute this software and
// its documentation for any purpose, without fee, and without a
// written agreement is hereby granted, provided that the above copyright
// notice and this paragraph and the following two paragraphs appear in
// all copies.
//
// IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
// LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
// EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON
// AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
// PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
//////////////////////////////////////////////////////////////////////////////
//
// BRASS source file
//
// SCORE runtime support
// $Revision: 1.21 $
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/file.h>
#include <unistd.h>
#include <string.h>
#include "ScoreOperator.h"
#include "ScoreConfig.h"
#include "ScoreOperatorMangle.h"
const char *DEFAULT_PATH=".";
ofstream* ScoreOperator::fout=NULL;
char *ScoreOperator::mangle(char *base, int nparam, int *params)
{
return mangleOpName(base, nparam, params);
}
char *ScoreOperator::resolve(char *base)
{
char *trial_path;
char *tname;
char *original_lpath=lpath;
// Added April 19th 2010 to avoid resolve() null ptrs.. wtf?
if(base==(char*)NULL) {
return(NULL);
}
// search through library path
if (lpath==(char *)NULL)
{
lpath=(char *)malloc(strlen(DEFAULT_PATH)+1);
strcpy(lpath,DEFAULT_PATH);
original_lpath=lpath;
}
trial_path=(char *)malloc(strlen(lpath)+strlen(pwd)+2); // just to be safe
// +1 for null, +1 for trailing slash, if necessary
tname=(char *)malloc(strlen(lpath)+strlen(pwd)+strlen(base)+5);
FILE *try_fd;
while (strlen(lpath)>0)
{
char * firstcolon=index(lpath,':');
int len=0;
if (firstcolon!=NULL)
len=(firstcolon-lpath);
else
len=strlen(lpath);
if (((len==1) && (lpath[0] == '.')) ||
((lpath[0] == '.') && (lpath[1] == '/'))){
strncpy(trial_path,pwd,strlen(pwd));
if (len == 1) {
len=strlen(pwd);
} else {
strncpy((trial_path+strlen(pwd)),(lpath+1),(len-1));
len=len-1+strlen(pwd);
}
} else {
strncpy(trial_path,lpath,len);
}
if (*(trial_path+len-1)!='/')
{
*(trial_path+len)='/';
len++;
}
*(trial_path+len)=(char)NULL;
sprintf(tname,"%s%s.so",trial_path,base);
if (VERBOSEDEBUG || DEBUG) {
cerr << "ScoreOperator.resolve trying [" <<
tname << "]" << endl;
}
// try to access file
// N.B. shouldn't really have to open it
// ...stat would do, but not sure, yet, what stat condition
// would suffice for this file...
try_fd=fopen(tname,"r");
if (try_fd!=NULL)
{
if (VERBOSEDEBUG || DEBUG) {
cerr << "ScoreOperator.resolve: found " <<
tname << endl;
}
fclose(try_fd);
free(trial_path);
lpath=original_lpath;
return(tname);
}
else
{
// DEBUG
if (VERBOSEDEBUG || DEBUG) {
cerr <<
"ScoreOperator.resolve: failed to find " <<
tname << endl;
}
}
if (firstcolon==NULL)
break;
else
lpath=firstcolon+1; // after : for next round
}
// fail case
free(trial_path);
free(tname);
lpath=original_lpath;
return(NULL);
}
int get_schedulerid()
{
int sidnum;
FILE *sid=fopen(SCORE_SCHEDULER_ID_FILE,"r");
if (sid!=NULL)
{
int fcnt=fread(&sidnum,4,1,sid);
fclose(sid);
if ((fcnt==1) & (sidnum!=-1)) // added size check 5/19/99 amd
return(sidnum);
}
// For safety?
if(errno) {
// perror("Yeah yeah... ");
errno=0;
}
return 0;
// cerr << "ScoreOperator.schedulerid: could not get scheduler id from "
// << SCORE_SCHEDULER_ID_FILE << endl;
// Nachiket edit: what is this??? Do we really need this
// exit(1);
}
ScoreOperatorElement *init_op_list()
{
//atexit(ScoreOperator::forAllOperators);
return((ScoreOperatorElement *)NULL);
}
char * ScoreOperator::lpath=getenv(SCORE_LIBRARY_PATH_ENV);
char * ScoreOperator::fpath=getenv(SCORE_FEEDBACK_DIR_ENV);
char * ScoreOperator::pwd=getenv("PWD");
int ScoreOperator::schedulerid=get_schedulerid();
ScoreOperatorElement *ScoreOperator::oplist=init_op_list();
ScoreOperatorElement *ScoreOperator::addOperator(char *name, int params,
int targs, int plocs)
{
// create new
ScoreOperatorElement * result=new ScoreOperatorElement(name,params,
targs,plocs,
(ScoreOperatorInstanceElement *)NULL,
oplist);
// link in at head of list
oplist=result;
return(result);
}
void ScoreOperator::addInstance(ScoreOperatorElement *elm, ScoreOperator* op, int *params)
{
// cout << "Setting operator=" << op->getName() << "in=" << op->getInputs() << " out=" << op->getOutputs() << endl;
elm->addInstance(op, params);
}
void ScoreOperator::addInstance(ScoreOperatorElement* elm, int *params)
{
// cout << "Setting operator=" << op->getName() << "in=" << op->getInputs() << " out=" << op->getOutputs() << endl;
elm->addInstance(NULL, params);
}
FILE *ScoreOperator::feedback_file (char *base)
{
if (fpath==(char *)NULL)
{
fpath=(char *)malloc(strlen(DEFAULT_PATH)+1);
strcpy(fpath,DEFAULT_PATH);
}
char *fname=(char *)malloc(strlen(fpath)+1+strlen(base)+1+1+
strlen(SCORE_AUTO_FEEDBACK_EXTENSION));
// deal with trailing slash (or lack thereof)
if (*(fpath+strlen(fpath)-1)!='/')
sprintf(fname,"%s/%s.%s",fpath,base,SCORE_AUTO_FEEDBACK_EXTENSION);
else
sprintf(fname,"%s%s.%s",fpath,base,SCORE_AUTO_FEEDBACK_EXTENSION);
FILE *tf=fopen(fname,"a");
if (tf!=(FILE *)NULL)
{
flock(fileno(tf),LOCK_EX); // blocking
return(tf);
}
else
{
cerr << "ScoreOperator: Error updating feedback file " <<
fname << endl;
perror("\t");
return(tf);
}
}
void ScoreOperator::forAllOperators()
{
if (VERBOSEDEBUG || EXTRA_DEBUG || 1) {
cerr << "now calling forAllOperators" << endl;
}
ScoreOperatorElement *eptr=oplist;
while (eptr!=(ScoreOperatorElement *)NULL)
{
int num_params=eptr->getParamCount();
if (VERBOSEDEBUG) {
cerr << "Operator: " << eptr->getName() <<
" params=" << num_params << endl;
}
// TODO: filter out duplicate parameter requests
FILE *fbfd=feedback_file(eptr->getName());
if (fbfd!=NULL)
{
int arg_count=eptr->getTotalArgs();
int plocs=eptr->getParamLocations();
ScoreOperatorInstanceElement *iptr=eptr->getInstance();
while (iptr!=(ScoreOperatorInstanceElement *)NULL)
{
fprintf(fbfd,"%s(",eptr->getName());
int *params=iptr->getParams();
for (int i=0;i<arg_count;i++)
{
if (i==(arg_count-1)) // last time
if (((plocs>>i) & 0x01)==0)
{ // nothing
}
else
{
fprintf(fbfd,"0x%x",*params); // no comma
params++;
}
else
if (((plocs>>i) & 0x01)==0)
fprintf(fbfd,",");
else
{
fprintf(fbfd,"0x%x,",*params);
params++;
}
}
fprintf(fbfd,");\n");
iptr=iptr->getNext();
}
// unlock file
flock(fileno(fbfd),LOCK_UN); // blocking
//close
fclose(fbfd);
}
if (VERBOSEDEBUG) {
ScoreOperatorInstanceElement *iptr=oplist->getInstance();
while (iptr!=(ScoreOperatorInstanceElement *)NULL)
{
int *params=iptr->getParams();
for (int i=0;i<num_params;i++)
{
cerr << params[i] << " ";
}
cerr << endl;
iptr=iptr->getNext();
}
}
eptr=eptr->getNext();
}
}
void ScoreOperator::dumpGraphviz(ofstream *fout) {
ScoreOperatorElement *eptr=oplist;
while (eptr!=(ScoreOperatorElement *)NULL)
{
ScoreOperatorInstanceElement *iptr=eptr->getInstance();
while (iptr!=(ScoreOperatorInstanceElement *)NULL)
{
// cout << "inside.." << eptr->getName() << endl;
ScoreOperator* op= iptr->getOperator();
if(op==(ScoreOperator*)NULL) {exit(1);}
cout << "inside.." << op->getName() << endl;
// iterate over outputs
for (int i = 0; i < op->outputs; i++) {
cout << "Processing output=" << i << endl;
ScoreGraphNode *src=op->getOutput(i)->src;
ScoreGraphNode *sink=op->getOutput(i)->sink;
if(src!=NULL && sink!=NULL) {
cout << src->getName() << "->" << sink->getName() << " label=["<<op->getOutput(i)->getName()<<"]" << endl;
}
}
iptr=iptr->getNext();
}
eptr=eptr->getNext();
}
}