ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
cmdStarter.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
26#include "misc/extra/extra.h"
27
28#ifdef ABC_USE_PTHREADS
29
30#ifdef _WIN32
31#include "../lib/pthread.h"
32#else
33#include <pthread.h>
34#include <unistd.h>
35#endif
36
37#endif
38
40
44
45#ifndef ABC_USE_PTHREADS
46
47void Cmd_RunStarter( char * pFileName, char * pBinary, char * pCommand, int nCores, int fVerbose ) {}
48
49#else // pthreads are used
50
51// the number of concurrently running threads
52static volatile int nThreadsRunning = 0;
53
54// mutex to control access to the number of threads
55pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
56
60
72void * Abc_RunThread( void * pCommand )
73{
74 int status;
75 // perform the call
76 if ( system( (char *)pCommand ) )
77 {
78 fprintf( stderr, "The following command has returned non-zero exit status:\n" );
79 fprintf( stderr, "\"%s\"\n\n", (char *)pCommand );
80 fflush( stdout );
81 }
82 free( pCommand );
83
84 // decrement the number of threads runining
85 status = pthread_mutex_lock(&mutex); assert(status == 0);
86 nThreadsRunning--;
87 status = pthread_mutex_unlock(&mutex); assert(status == 0);
88
89 // quit this thread
90 //printf("...Finishing %s\n", (char *)Command);
91 pthread_exit( NULL );
92 assert(0);
93 return NULL;
94}
95
107void Cmd_RunStarter( char * pFileName, char * pBinary, char * pCommand, int nCores, int fVerbose )
108{
109 FILE * pFile, * pFileTemp;
110 pthread_t * pThreadIds;
111 char * BufferCopy, * Buffer;
112 int nLines, LineMax, Line, Len;
113 int i, c, status, Counter;
114 abctime clk = Abc_Clock();
115
116 // check the number of cores
117 if ( nCores < 2 )
118 {
119 fprintf( stdout, "The number of cores (%d) should be more than 1.\n", nCores );
120 return;
121 }
122
123 // open the file and make sure it is available
124 pFile = fopen( pFileName, "rb" );
125 if ( pFile == NULL )
126 {
127 fprintf( stdout, "Input file \"%s\" cannot be opened.\n", pFileName );
128 return;
129 }
130
131 // count the number of lines and the longest line
132 nLines = LineMax = Line = 0;
133 while ( (c = fgetc(pFile)) != EOF )
134 {
135 Line++;
136 if ( c != '\n' )
137 continue;
138 nLines++;
139 LineMax = Abc_MaxInt( LineMax, Line );
140 Line = 0;
141 }
142 nLines += 10;
143 LineMax += LineMax + 100;
144 LineMax += pBinary ? strlen(pBinary) : 0;
145 LineMax += pCommand ? strlen(pCommand) : 0;
146
147 // allocate storage
148 Buffer = ABC_ALLOC( char, LineMax );
149 pThreadIds = ABC_ALLOC( pthread_t, nLines );
150
151 // check if all files can be opened
152 if ( pCommand != NULL )
153 {
154 // read file names
155 rewind( pFile );
156 for ( i = 0; fgets( Buffer, LineMax, pFile ) != NULL; i++ )
157 {
158 // remove trailing spaces
159 for ( Len = strlen(Buffer) - 1; Len >= 0; Len-- )
160 if ( Buffer[Len] == '\n' || Buffer[Len] == '\r' || Buffer[Len] == '\t' || Buffer[Len] == ' ' )
161 Buffer[Len] = 0;
162 else
163 break;
164
165 // get command from file
166 if ( Buffer[0] == 0 || Buffer[0] == '\n' || Buffer[0] == '\r' || Buffer[0] == '\t' || Buffer[0] == ' ' || Buffer[0] == '#' )
167 continue;
168
169 // try to open the file
170 pFileTemp = fopen( Buffer, "rb" );
171 if ( pFileTemp == NULL )
172 {
173 fprintf( stdout, "Starter cannot open file \"%s\".\n", Buffer );
174 fflush( stdout );
175 ABC_FREE( pThreadIds );
176 ABC_FREE( Buffer );
177 fclose( pFile );
178 return;
179 }
180 fclose( pFileTemp );
181 }
182 }
183
184 // read commands and execute at most <num> of them at a time
185 rewind( pFile );
186 for ( i = 0; fgets( Buffer, LineMax, pFile ) != NULL; i++ )
187 {
188 // remove trailing spaces
189 for ( Len = strlen(Buffer) - 1; Len >= 0; Len-- )
190 if ( Buffer[Len] == '\n' || Buffer[Len] == '\r' || Buffer[Len] == '\t' || Buffer[Len] == ' ' )
191 Buffer[Len] = 0;
192 else
193 break;
194
195 // get command from file
196 if ( Buffer[0] == 0 || Buffer[0] == '\n' || Buffer[0] == '\r' || Buffer[0] == '\t' || Buffer[0] == ' ' || Buffer[0] == '#' )
197 continue;
198
199 // create command
200 if ( pCommand != NULL )
201 {
202 BufferCopy = ABC_ALLOC( char, LineMax );
203 sprintf( BufferCopy, "%s -c \"%s; %s\" > %s", pBinary, Buffer, pCommand, Extra_FileNameGenericAppend(Buffer, ".txt") );
204 }
205 else
206 BufferCopy = Abc_UtilStrsav( Buffer );
207 if ( fVerbose ) {
208 fprintf( stdout, "Calling: %s\n", (char *)BufferCopy );
209 fflush( stdout );
210 }
211
212 // wait till there is an empty thread
213 while ( 1 )
214 {
215 status = pthread_mutex_lock(&mutex); assert(status == 0);
216 Counter = nThreadsRunning;
217 status = pthread_mutex_unlock(&mutex); assert(status == 0);
218 if ( Counter < nCores - 1 )
219 break;
220// Sleep( 100 );
221 }
222
223 // increament the number of threads running
224 status = pthread_mutex_lock(&mutex); assert(status == 0);
225 nThreadsRunning++;
226 status = pthread_mutex_unlock(&mutex); assert(status == 0);
227
228 // create thread to execute this command
229 status = pthread_create( &pThreadIds[i], NULL, Abc_RunThread, (void *)BufferCopy ); assert(status == 0);
230 assert( i < nLines );
231 }
232 ABC_FREE( pThreadIds );
233 ABC_FREE( Buffer );
234 fclose( pFile );
235
236 // wait for all the threads to finish
237 while ( 1 )
238 {
239 status = pthread_mutex_lock(&mutex); assert(status == 0);
240 Counter = nThreadsRunning;
241 status = pthread_mutex_unlock(&mutex); assert(status == 0);
242 if ( Counter == 0 )
243 break;
244 }
245
246 // cleanup
247// status = pthread_mutex_destroy(&mutex); assert(status == 0);
248// mutex = PTHREAD_MUTEX_INITIALIZER;
249 fprintf( stdout, "Finished processing commands in file \"%s\". ", pFileName );
250 Abc_PrintTime( 1, "Total wall time", Abc_Clock() - clk );
251 fflush( stdout );
252}
253
254#endif // pthreads are used
255
259
260
262
ABC_INT64_T abctime
Definition abc_global.h:332
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
ABC_NAMESPACE_IMPL_START void Cmd_RunStarter(char *pFileName, char *pBinary, char *pCommand, int nCores, int fVerbose)
DECLARATIONS ///.
Definition cmdStarter.c:47
#define Len
Definition deflate.h:78
char * Extra_FileNameGenericAppend(char *pBase, char *pSuffix)
void * Abc_RunThread(void *Command)
Definition starter.c:60
pthread_mutex_t mutex
Definition starter.c:44
#define assert(ex)
Definition util_old.h:213
int strlen()
int system()
char * sprintf()
VOID_HACK free()
VOID_HACK rewind()