#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
//multimode 0 : no multimode 1 : multithread 2 : multiprocess
#ifndef multimode
#define multimode 2
#endif
#define NLOOPS 0x10000000
#define NLOOPS 0x100000
//typedef struct targ__
typedef struct
{
int start;
int jobid;
}targ, *ptarg;
typedef struct
{
pid_t mypid;
int jobid;
}pinfo, *ppinfo;
static const int nthread = 10;
static const int nprocs = 10;
volatile double x = 0.0;
volatile char managerproc = 1;
void *tfunc( void *arg )
{
int i;
int temp = NLOOPS/nthread;
ptarg tp = ( ptarg )arg;
printf( "My tid : %u jobid : %d\n", pthread_self( ), tp -> jobid );
//printf( "My tid : %u jobid : %d\n", tp -> jobid );
if( tp -> jobid != ( nthread - 1) )
{
for( i= tp->start; i < tp -> start + temp; i++ )
{
x = sin(i);
}
}
else
{
for( i= tp->start; i< NLOOPS; i++)
{
x = sin(i);
}
}
}
int main( int argc, char *argv[] )
{
unsigned long i;
int j;
long sec, usec;
long total;
int waitret;
struct timeval tstart, tend;
pid_t pidtmp;
#if ( multimode == 2 )
pinfo pp;
memset( &pp, 0x0, sizeof(pinfo) );
#elif ( multimode == 1 )
pthread_t *tid;
ptarg pt;
tid = (pthread_t *)malloc( nthread * sizeof(pthread_t) );
pt = ( ptarg )malloc( nthread * sizeof( targ ) );
for( j=0; j<nthread; j++ )
{
pt[j].start = (NLOOPS / nthread ) * j;
pt[j].jobid = j;
}
#endif
printf( "\n--------- simple parellel performance test ---------\n" );
printf( "Source File : %s\n", __FILE__ );
printf( "Excute file : %s\n", argv[0] );
printf( "Compile Date : %s\n", __DATE__ );
printf( "Compile Time : %s\n", __TIME__ );
gettimeofday( &tstart, NULL );
#if ( multimode == 0 )
// no parellel
printf( "\nMultiMode : single process\n\n" );
i = 0;
while( i < NLOOPS )
{
x = sin(i);
i++;
}
#elif ( multimode == 1 )
//multithread
printf( "\nMultiMode : multithread\n\n" );
for( j=0; j<nthread; j++ )
{
pthread_create( &tid[j], NULL, tfunc, pt+j );
}
for( j=0; j<nthread; j++ )
{
pthread_join( tid[j], NULL);
}
#elif ( multimode == 2)
//multi process
printf( "\nMultiMode : multiprocess\n" );
for( j=0; j<nprocs; j++ )
{
pidtmp = fork( );
if( pidtmp < 0)
goto err;
else
if( pidtmp > 0 )//the manage process
{
pp.jobid = -1;
//break;
}
else
{
pp.mypid = pidtmp;
pp.jobid = j;
managerproc = 0;
break;
}
}
if( 0 == managerproc )// job process
{
printf( "My pid : %u jobid : %d\n", getpid( ), pp.jobid );
for(i=pp.jobid; i<NLOOPS; i+=nprocs )
{
x = sin(i);
}
exit(0);
}
else //manager process
{
printf( "Manager process pid : %u\n", getpid( ));
for( i=0; i<nprocs; i++)
wait( &waitret );
}
#endif
gettimeofday( &tend, NULL );
sec = tend.tv_sec - tstart.tv_sec;
usec = tend.tv_usec - tstart.tv_usec;
if( usec < 0 )
{
sec--;
usec += 1000000;
}
total = sec*1000 + usec/1000;
printf( "\nOK done Total time : %ld ms\n\n", total );
exit(0);
err:
printf( "Multi process error exit\n" );
exit( -1 );
}
#include <stdio.h>
#include <pthread.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
//multimode 0 : no multimode 1 : multithread 2 : multiprocess
#ifndef multimode
#define multimode 2
#endif
#define NLOOPS 0x10000000
#define NLOOPS 0x100000
//typedef struct targ__
typedef struct
{
int start;
int jobid;
}targ, *ptarg;
typedef struct
{
pid_t mypid;
int jobid;
}pinfo, *ppinfo;
static const int nthread = 10;
static const int nprocs = 10;
volatile double x = 0.0;
volatile char managerproc = 1;
void *tfunc( void *arg )
{
int i;
int temp = NLOOPS/nthread;
ptarg tp = ( ptarg )arg;
printf( "My tid : %u jobid : %d\n", pthread_self( ), tp -> jobid );
//printf( "My tid : %u jobid : %d\n", tp -> jobid );
if( tp -> jobid != ( nthread - 1) )
{
for( i= tp->start; i < tp -> start + temp; i++ )
{
x = sin(i);
}
}
else
{
for( i= tp->start; i< NLOOPS; i++)
{
x = sin(i);
}
}
}
int main( int argc, char *argv[] )
{
unsigned long i;
int j;
long sec, usec;
long total;
int waitret;
struct timeval tstart, tend;
pid_t pidtmp;
#if ( multimode == 2 )
pinfo pp;
memset( &pp, 0x0, sizeof(pinfo) );
#elif ( multimode == 1 )
pthread_t *tid;
ptarg pt;
tid = (pthread_t *)malloc( nthread * sizeof(pthread_t) );
pt = ( ptarg )malloc( nthread * sizeof( targ ) );
for( j=0; j<nthread; j++ )
{
pt[j].start = (NLOOPS / nthread ) * j;
pt[j].jobid = j;
}
#endif
printf( "\n--------- simple parellel performance test ---------\n" );
printf( "Source File : %s\n", __FILE__ );
printf( "Excute file : %s\n", argv[0] );
printf( "Compile Date : %s\n", __DATE__ );
printf( "Compile Time : %s\n", __TIME__ );
gettimeofday( &tstart, NULL );
#if ( multimode == 0 )
// no parellel
printf( "\nMultiMode : single process\n\n" );
i = 0;
while( i < NLOOPS )
{
x = sin(i);
i++;
}
#elif ( multimode == 1 )
//multithread
printf( "\nMultiMode : multithread\n\n" );
for( j=0; j<nthread; j++ )
{
pthread_create( &tid[j], NULL, tfunc, pt+j );
}
for( j=0; j<nthread; j++ )
{
pthread_join( tid[j], NULL);
}
#elif ( multimode == 2)
//multi process
printf( "\nMultiMode : multiprocess\n" );
for( j=0; j<nprocs; j++ )
{
pidtmp = fork( );
if( pidtmp < 0)
goto err;
else
if( pidtmp > 0 )//the manage process
{
pp.jobid = -1;
//break;
}
else
{
pp.mypid = pidtmp;
pp.jobid = j;
managerproc = 0;
break;
}
}
if( 0 == managerproc )// job process
{
printf( "My pid : %u jobid : %d\n", getpid( ), pp.jobid );
for(i=pp.jobid; i<NLOOPS; i+=nprocs )
{
x = sin(i);
}
exit(0);
}
else //manager process
{
printf( "Manager process pid : %u\n", getpid( ));
for( i=0; i<nprocs; i++)
wait( &waitret );
}
#endif
gettimeofday( &tend, NULL );
sec = tend.tv_sec - tstart.tv_sec;
usec = tend.tv_usec - tstart.tv_usec;
if( usec < 0 )
{
sec--;
usec += 1000000;
}
total = sec*1000 + usec/1000;
printf( "\nOK done Total time : %ld ms\n\n", total );
exit(0);
err:
printf( "Multi process error exit\n" );
exit( -1 );
}