abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
starter.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>

Go to the source code of this file.

Macros

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

Functions

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

Variables

static int nThreadsRunning = 0
 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
 

Macro Definition Documentation

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

Definition at line 38 of file starter.c.

#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

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);
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 }
static int nThreadsRunning
Definition: starter.c:41
VOID_HACK free()
pthread_mutex_t mutex
Definition: starter.c:44
int system()
#define assert(ex)
Definition: util_old.h:213
char* Abc_UtilStrsav ( char *  s)

Definition at line 47 of file starter.c.

47 { return s ? strcpy(malloc(strlen(s)+1), s) : NULL; }
char * malloc()
char * strcpy()
int strlen()
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);
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);
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 
178 usage:
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 }
static int nThreadsRunning
Definition: starter.c:41
#define MAX_COMM_NUM
Definition: starter.c:35
#define ABC_PRT(a, t)
Definition: starter.c:38
pthread_mutex_t mutex
Definition: starter.c:44
void * Abc_RunThread(void *Command)
Definition: starter.c:60
static int Counter
#define assert(ex)
Definition: util_old.h:213
int strlen()
char * Abc_UtilStrsav(char *s)
Definition: starter.c:47

Variable Documentation

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 44 of file starter.c.

int nThreadsRunning = 0
static

Definition at line 41 of file starter.c.