ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
starter.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
Include dependency graph for starter.c:

Go to the source code of this file.

Macros

#define MAX_COMM_NUM   1000
 
#define ABC_PRT(a, t)
 

Functions

char * Abc_UtilStrsav (char *s)
 
void * Abc_RunThread (void *Command)
 
int main (int argc, char *argv[])
 GLOBAL VARIABLES ///.
 

Variables

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
 

Macro Definition Documentation

◆ ABC_PRT

#define ABC_PRT ( a,
t )
Value:
(printf("%s = ", (a)), printf("%7.2f sec\n", (float)(t)/(float)(CLOCKS_PER_SEC)))

Definition at line 38 of file starter.c.

◆ MAX_COMM_NUM

#define MAX_COMM_NUM   1000

CFile****************************************************************

FileName [starter.c]

SystemName [ABC: Logic synthesis and verification system.]

PackageName [Wrapper for calling ABC.]

Synopsis [A demo program illustrating parallel execution of ABC.]

Author [Alan Mishchenko]

Affiliation [UC Berkeley]

Date [Ver. 1.0. Started - October 22, 2009.]

Revision [

Id
starter.c,v 1.00 2009/10/22 00:00:00 alanmi Exp

]

Definition at line 35 of file starter.c.

Function Documentation

◆ Abc_RunThread()

void * Abc_RunThread ( void * Command)

Function*************************************************************

Synopsis [This procedures executes one call to system().]

Description []

SideEffects []

SeeAlso []

Definition at line 60 of file starter.c.

61{
62 // perform the call
63 if ( system( (char *)Command ) )
64 {
65 assert(pthread_mutex_lock(&mutex) == 0);
66 fprintf( stderr, "The following command has returned non-zero exit status:\n" );
67 fprintf( stderr, "\"%s\"\n", (char *)Command );
68 fprintf( stderr, "Sorry for the inconvenience.\n" );
69 fflush( stdout );
70 assert(pthread_mutex_unlock(&mutex) == 0);
71 }
72
73 // decrement the number of threads runining
74 assert(pthread_mutex_lock(&mutex) == 0);
75 nThreadsRunning--;
76 assert(pthread_mutex_unlock(&mutex) == 0);
77
78 // quit this thread
79 //printf("...Finishing %s\n", (char *)Command);
80 free( Command );
81 pthread_exit( NULL );
82 assert(0);
83 return NULL;
84}
pthread_mutex_t mutex
Definition starter.c:44
#define assert(ex)
Definition util_old.h:213
int system()
VOID_HACK free()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Abc_UtilStrsav()

char * Abc_UtilStrsav ( char * s)

Definition at line 47 of file starter.c.

47{ return s ? strcpy(malloc(strlen(s)+1), s) : NULL; }
int strlen()
char * strcpy()
char * malloc()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int argc,
char * argv[] )

GLOBAL VARIABLES ///.

Function*************************************************************

Synopsis [Takes file with commands to be executed and the number of CPUs.]

Description []

SideEffects []

SeeAlso []

Definition at line 97 of file starter.c.

98{
99 FILE * pFile, * pOutput = stdout;
100 pthread_t ThreadIds[MAX_COMM_NUM];
101 char * pBufferCopy, Buffer[MAX_COMM_NUM];
102 int i, nCPUs = 0, nLines = 0, Counter;
103 clock_t clk = clock();
104
105 // check command line arguments
106 if ( argc != 3 )
107 { fprintf( stderr, "Wrong number of command line arguments.\n" ); goto usage; }
108
109 // get the number of CPUs
110 nCPUs = atoi( argv[1] );
111 if ( nCPUs <= 0 )
112 { fprintf( pOutput, "Cannot read an integer represting the number of CPUs.\n" ); goto usage; }
113
114 // open the file and make sure it is available
115 pFile = fopen( argv[2], "r" );
116 if ( pFile == NULL )
117 { fprintf( pOutput, "Input file \"%s\" cannot be opened.\n", argv[2] ); goto usage; }
118
119 // read commands and execute at most <num> of them at a time
120// assert(mutex == PTHREAD_MUTEX_INITIALIZER);
121 while ( fgets( Buffer, MAX_COMM_NUM, pFile ) != NULL )
122 {
123 // get the command from the file
124 if ( Buffer[0] == '\n' || Buffer[0] == '\r' || Buffer[0] == '\t' ||
125 Buffer[0] == ' ' || Buffer[0] == '#')
126 {
127 continue;
128 }
129
130 if ( Buffer[strlen(Buffer)-1] == '\n' )
131 Buffer[strlen(Buffer)-1] = 0;
132 if ( Buffer[strlen(Buffer)-1] == '\r' )
133 Buffer[strlen(Buffer)-1] = 0;
134
135 // wait till there is an empty thread
136 while ( 1 )
137 {
138 assert(pthread_mutex_lock(&mutex) == 0);
139 Counter = nThreadsRunning;
140 assert(pthread_mutex_unlock(&mutex) == 0);
141 if ( Counter < nCPUs - 1 )
142 break;
143// Sleep( 100 );
144 }
145
146 // increament the number of threads running
147 assert(pthread_mutex_lock(&mutex) == 0);
148 nThreadsRunning++;
149 printf( "Calling: %s\n", (char *)Buffer );
150 fflush( stdout );
151 assert(pthread_mutex_unlock(&mutex) == 0);
152
153 // create thread to execute this command
154 pBufferCopy = Abc_UtilStrsav( Buffer );
155 assert(pthread_create( &ThreadIds[nLines], NULL, Abc_RunThread, (void *)pBufferCopy ) == 0);
156 if ( ++nLines == MAX_COMM_NUM )
157 { fprintf( pOutput, "Cannot execute more than %d commands from file \"%s\".\n", nLines, argv[2] ); break; }
158 }
159
160 // wait for all the threads to finish
161 while ( 1 )
162 {
163 assert(pthread_mutex_lock(&mutex) == 0);
164 Counter = nThreadsRunning;
165 assert(pthread_mutex_unlock(&mutex) == 0);
166 if ( Counter == 0 )
167 break;
168 }
169
170 // cleanup
171 assert(pthread_mutex_destroy(&mutex) == 0);
172// assert(mutex == NULL);
173 fclose( pFile );
174 printf( "Finished processing commands in file \"%s\". ", argv[2] );
175 ABC_PRT( "Total time", clock() - clk );
176 return 0;
177
178usage:
179 // skip the path name till the binary name
180 for ( i = strlen(argv[0]) - 1; i > 0; i-- )
181 if ( argv[0][i-1] == '\\' || argv[0][i-1] == '/' )
182 break;
183 // print usage message
184 fprintf( pOutput, "usage: %s <num> <file>\n", argv[0]+i );
185 fprintf( pOutput, " executes command listed in <file> in parallel on <num> CPUs\n" );
186 fprintf( pOutput, "\n" );
187 return 1;
188
189}
usage()
Definition main.c:626
#define MAX_COMM_NUM
Definition starter.c:35
#define ABC_PRT(a, t)
Definition starter.c:38
void * Abc_RunThread(void *Command)
Definition starter.c:60
char * Abc_UtilStrsav(char *s)
Definition starter.c:47
Here is the call graph for this function:

Variable Documentation

◆ mutex

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 44 of file starter.c.