diff options
author | Alan Mishchenko <alanmi@berkeley.edu> | 2012-10-02 22:35:45 -0700 |
---|---|---|
committer | Alan Mishchenko <alanmi@berkeley.edu> | 2012-10-02 22:35:45 -0700 |
commit | e6196fb4625d6c704ac5ab3bd13dabace587f790 (patch) | |
tree | 29e5d22752c69308671a490390a808032f253a6d /src/base/cmd | |
parent | 6c1c45b90f689a548c2c35b5d5913cee76e7cbca (diff) | |
download | abc-e6196fb4625d6c704ac5ab3bd13dabace587f790.tar.gz abc-e6196fb4625d6c704ac5ab3bd13dabace587f790.tar.bz2 abc-e6196fb4625d6c704ac5ab3bd13dabace587f790.zip |
Added command 'starter' to call ABC concurrently.
Diffstat (limited to 'src/base/cmd')
-rw-r--r-- | src/base/cmd/cmd.c | 28 | ||||
-rw-r--r-- | src/base/cmd/cmdStarter.c | 88 |
2 files changed, 85 insertions, 31 deletions
diff --git a/src/base/cmd/cmd.c b/src/base/cmd/cmd.c index 50d3fdb8..3fb26531 100644 --- a/src/base/cmd/cmd.c +++ b/src/base/cmd/cmd.c @@ -57,7 +57,7 @@ static int CmdCommandVersion ( Abc_Frame_t * pAbc, int argc, char ** argv static int CmdCommandSis ( Abc_Frame_t * pAbc, int argc, char ** argv ); static int CmdCommandMvsis ( Abc_Frame_t * pAbc, int argc, char ** argv ); static int CmdCommandCapo ( Abc_Frame_t * pAbc, int argc, char ** argv ); -static int Cmd_CommandStarter ( Abc_Frame_t * pAbc, int argc, char ** argv ); +static int CmdCommandStarter ( Abc_Frame_t * pAbc, int argc, char ** argv ); extern int Cmd_CommandAbcLoadPlugIn( Abc_Frame_t * pAbc, int argc, char ** argv ); @@ -104,7 +104,7 @@ void Cmd_Init( Abc_Frame_t * pAbc ) Cmd_CommandAdd( pAbc, "Various", "sis", CmdCommandSis, 1 ); Cmd_CommandAdd( pAbc, "Various", "mvsis", CmdCommandMvsis, 1 ); Cmd_CommandAdd( pAbc, "Various", "capo", CmdCommandCapo, 0 ); - Cmd_CommandAdd( pAbc, "Various", "starter", Cmd_CommandStarter, 0 ); + Cmd_CommandAdd( pAbc, "Various", "starter", CmdCommandStarter, 0 ); Cmd_CommandAdd( pAbc, "Various", "load_plugin", Cmd_CommandAbcLoadPlugIn, 0 ); } @@ -2024,15 +2024,16 @@ usage: SeeAlso [] ***********************************************************************/ -int Cmd_CommandStarter( Abc_Frame_t * pAbc, int argc, char ** argv ) +int CmdCommandStarter( Abc_Frame_t * pAbc, int argc, char ** argv ) { - extern void Cmd_RunStarter( char * pFileName, int nCores ); + extern void Cmd_RunStarter( char * pFileName, char * pBinary, char * pCommand, int nCores ); FILE * pFile; char * pFileName; + char * pCommand = NULL; int c, nCores = 3; int fVerbose = 0; Extra_UtilGetoptReset(); - while ( ( c = Extra_UtilGetopt( argc, argv, "Nvh" ) ) != EOF ) + while ( ( c = Extra_UtilGetopt( argc, argv, "NCvh" ) ) != EOF ) { switch ( c ) { @@ -2047,6 +2048,15 @@ int Cmd_CommandStarter( Abc_Frame_t * pAbc, int argc, char ** argv ) if ( nCores < 0 ) goto usage; break; + case 'C': + if ( globalUtilOptind >= argc ) + { + Abc_Print( -1, "Command line switch \"-C\" should be followed by a string (possibly in quotes).\n" ); + goto usage; + } + pCommand = argv[globalUtilOptind]; + globalUtilOptind++; + break; case 'v': fVerbose ^= 1; break; @@ -2074,15 +2084,17 @@ int Cmd_CommandStarter( Abc_Frame_t * pAbc, int argc, char ** argv ) } fclose( pFile ); // run commands - Cmd_RunStarter( pFileName, nCores ); + Cmd_RunStarter( pFileName, pAbc->sBinary, pCommand, nCores ); return 0; usage: - Abc_Print( -2, "usage: starter [-N num] [-vh]\n" ); - Abc_Print( -2, "\t executes command listed in <file> concurrently on <num> CPUs\n" ); + Abc_Print( -2, "usage: starter [-N num] [-C cmd] [-vh] <file>\n" ); + Abc_Print( -2, "\t executes command lines in <file> concurrently on <num> CPUs\n" ); Abc_Print( -2, "\t-N num : the number of concurrent jobs counting the controler [default = %d]\n", nCores ); + Abc_Print( -2, "\t-C cmd : (optional) ABC command line to execute on benchmarks in <file>\n" ); Abc_Print( -2, "\t-v : toggle printing verbose information [default = %s]\n", fVerbose? "yes": "no" ); Abc_Print( -2, "\t-h : print the command usage\n"); + Abc_Print( -2, "\t<file> : file name with ABC command lines (or benchmark names, if <cmd> is given)\n"); return 1; } diff --git a/src/base/cmd/cmdStarter.c b/src/base/cmd/cmdStarter.c index 17c52723..e8149f28 100644 --- a/src/base/cmd/cmdStarter.c +++ b/src/base/cmd/cmdStarter.c @@ -23,6 +23,7 @@ #include <string.h> #include <assert.h> #include "misc/util/abc_global.h" +#include "misc/extra/extra.h" // comment out this line to disable pthreads #define ABC_USE_PTHREADS @@ -39,14 +40,14 @@ #endif ABC_NAMESPACE_IMPL_START - + //////////////////////////////////////////////////////////////////////// /// DECLARATIONS /// //////////////////////////////////////////////////////////////////////// #ifndef ABC_USE_PTHREADS -void Cmd_RunStarter( char * pFileName, int nCores ) {} +void Cmd_RunStarter( char * pFileName, char * pBinary, char * pCommand, int nCores ) {} #else // pthreads are used @@ -106,19 +107,19 @@ void * Abc_RunThread( void * pCommand ) SeeAlso [] ***********************************************************************/ -void Cmd_RunStarter( char * pFileName, int nCores ) +void Cmd_RunStarter( char * pFileName, char * pBinary, char * pCommand, int nCores ) { - FILE * pFile, * pOutput = stdout; + FILE * pFile, * pFileTemp; pthread_t * pThreadIds; char * BufferCopy, * Buffer; - int nLines, LineMax, Line; + int nLines, LineMax, Line, Len; int i, c, status, Counter; clock_t clk = clock(); // check the number of cores if ( nCores < 2 ) { - fprintf( pOutput, "The number of cores (%d) should be more than 1.\n", nCores ); + fprintf( stdout, "The number of cores (%d) should be more than 1.\n", nCores ); return; } @@ -126,7 +127,7 @@ void Cmd_RunStarter( char * pFileName, int nCores ) pFile = fopen( pFileName, "rb" ); if ( pFile == NULL ) { - fprintf( pOutput, "Input file \"%s\" cannot be opened.\n", pFileName ); + fprintf( stdout, "Input file \"%s\" cannot be opened.\n", pFileName ); return; } @@ -141,28 +142,72 @@ void Cmd_RunStarter( char * pFileName, int nCores ) LineMax = Abc_MaxInt( LineMax, Line ); Line = 0; } - LineMax += 10; nLines += 10; + LineMax += LineMax + 100; + LineMax += pBinary ? strlen(pBinary) : 0; + LineMax += pCommand ? strlen(pCommand) : 0; // allocate storage Buffer = ABC_ALLOC( char, LineMax ); pThreadIds = ABC_ALLOC( pthread_t, nLines ); + // check if all files can be opened + if ( pCommand != NULL ) + { + // read file names + rewind( pFile ); + for ( i = 0; fgets( Buffer, LineMax, pFile ) != NULL; i++ ) + { + // remove trailing spaces + for ( Len = strlen(Buffer) - 1; Len >= 0; Len-- ) + if ( Buffer[Len] == '\n' || Buffer[Len] == '\r' || Buffer[Len] == '\t' || Buffer[Len] == ' ' ) + Buffer[Len] = 0; + else + break; + + // get command from file + if ( Buffer[0] == 0 || Buffer[0] == '\n' || Buffer[0] == '\r' || Buffer[0] == '\t' || Buffer[0] == ' ' || Buffer[0] == '#' ) + continue; + + // try to open the file + pFileTemp = fopen( Buffer, "rb" ); + if ( pFileTemp == NULL ) + { + fprintf( stdout, "Starter cannot open file \"%s\".\n", Buffer ); + fflush( stdout ); + ABC_FREE( pThreadIds ); + ABC_FREE( Buffer ); + fclose( pFile ); + return; + } + } + } + // read commands and execute at most <num> of them at a time rewind( pFile ); for ( i = 0; fgets( Buffer, LineMax, pFile ) != NULL; i++ ) { - // get the command from the file - if ( Buffer[0] == '\n' || Buffer[0] == '\r' || Buffer[0] == '\t' || - Buffer[0] == ' ' || Buffer[0] == '#') - { + // remove trailing spaces + for ( Len = strlen(Buffer) - 1; Len >= 0; Len-- ) + if ( Buffer[Len] == '\n' || Buffer[Len] == '\r' || Buffer[Len] == '\t' || Buffer[Len] == ' ' ) + Buffer[Len] = 0; + else + break; + + // get command from file + if ( Buffer[0] == 0 || Buffer[0] == '\n' || Buffer[0] == '\r' || Buffer[0] == '\t' || Buffer[0] == ' ' || Buffer[0] == '#' ) continue; - } - if ( Buffer[strlen(Buffer)-1] == '\n' ) - Buffer[strlen(Buffer)-1] = 0; - if ( Buffer[strlen(Buffer)-1] == '\r' ) - Buffer[strlen(Buffer)-1] = 0; + // create command + if ( pCommand != NULL ) + { + BufferCopy = ABC_ALLOC( char, LineMax ); + sprintf( BufferCopy, "%s -c \"%s; %s\" > %s", pBinary, Buffer, pCommand, Extra_FileNameGenericAppend(Buffer, ".txt") ); + } + else + BufferCopy = Abc_UtilStrsav( Buffer ); + fprintf( stdout, "Calling: %s\n", (char *)BufferCopy ); + fflush( stdout ); // wait till there is an empty thread while ( 1 ) @@ -180,11 +225,7 @@ void Cmd_RunStarter( char * pFileName, int nCores ) nThreadsRunning++; status = pthread_mutex_unlock(&mutex); assert(status == 0); - printf( "Calling: %s\n", (char *)Buffer ); - fflush( stdout ); - // create thread to execute this command - BufferCopy = Abc_UtilStrsav( Buffer ); status = pthread_create( &pThreadIds[i], NULL, Abc_RunThread, (void *)BufferCopy ); assert(status == 0); assert( i < nLines ); } @@ -204,9 +245,10 @@ void Cmd_RunStarter( char * pFileName, int nCores ) // cleanup status = pthread_mutex_destroy(&mutex); assert(status == 0); -// assert(mutex == NULL); - printf( "Finished processing commands in file \"%s\". ", pFileName ); + mutex = PTHREAD_MUTEX_INITIALIZER; + fprintf( stdout, "Finished processing commands in file \"%s\". ", pFileName ); Abc_PrintTime( 1, "Total wall time", clock() - clk ); + fflush( stdout ); } #endif |