kadpeer team mailing list archive
-
kadpeer team
-
Mailing list archive
-
Message #00007
Fwd: [kadpeer] r18 committed - refine code by Devil Wang
*
*
---------- Forwarded message ----------
From: <kadpeer@xxxxxxxxxxxxxx>
Date: Tue, Jul 13, 2010 at 3:20 PM
Subject: [kadpeer] r18 committed - refine code by Devil Wang
To: wxjeacen@xxxxxxxxx
Revision: 18
Author: wxjeacen
Date: Tue Jul 13 00:19:25 2010
Log: refine code by Devil Wang
http://code.google.com/p/kadpeer/source/detail?r=18
Added:
/trunk/src/TRNG.cpp
/trunk/src/TTimer.cpp
Deleted:
/trunk/src/TRNG.cc
/trunk/src/TTimer.cc
/trunk/src/main.cc
Modified:
/trunk/src/Makefile
/trunk/src/main.cpp
=======================================
--- /dev/null
+++ /trunk/src/TRNG.cpp Tue Jul 13 00:19:25 2010
@@ -0,0 +1,163 @@
+//
+// Project : misc
+// File : TRNG.cc
+// Author : Ronald Kriemann
+// Purpose : class for a random-number-generator
+//
+// arch-tag: bfc157b0-9bbc-4ed8-84e8-b71ed20a1d75
+//
+
+#include "TRNG.hh"
+
+/* A C-program for MT19937, with initialization improved 2002/2/10.*/
+/* Coded by Takuji Nishimura and Makoto Matsumoto. */
+/* This is a faster version by taking Shawn Cokus's optimization, */
+/* Matthe Bellew's simplification, Isaku Wada's real version. */
+
+/* Before using, initialize the state by using init_genrand(seed) */
+/* or init_by_array(init_key, key_length). */
+
+/* This library is free software. */
+/* This library is distributed in the hope that it will be useful, */
+/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
+/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
+
+/* Copyright (C) 1997, 2002 Makoto Matsumoto and Takuji Nishimura. */
+/* Any feedback is very welcome. */
+/* http://www.math.keio.ac.jp/matumoto/emt.html */
+/* email: matumoto@xxxxxxxxxxxxxxx */
+
+//
+// some defines/numbers wich are used in the RNG
+//
+
+#define N 624
+#define M 397
+#define MATRIX_A 0x9908b0dfUL // constant vector a
+#define UMASK 0x80000000UL // most significant w-r bits
+#define LMASK 0x7fffffffUL // least significant r bits
+#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
+#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
+
+////////////////////////////////////////////
+//
+// access rng
+//
+
+//
+// return random number between 0 and max
+//
+double
+TRNG::rand ( double max )
+{
+ unsigned long y;
+
+ if ( --_left == 0 )
+ next_state();
+
+ y = *_next++;
+
+ /* Tempering */
+ y ^= (y >> 11);
+ y ^= (y << 7) & 0x9d2c5680UL;
+ y ^= (y << 15) & 0xefc60000UL;
+ y ^= (y >> 18);
+
+ /* divided by 2^32-1 */
+ return max * double( y ) * (1.0/4294967295.0);
+}
+
+//
+// initialise rng
+//
+void
+TRNG::init ( unsigned long seed )
+{
+ _state[0] = seed & 0xffffffffUL;
+
+ for ( int j = 1; j < N; j++ )
+ {
+ _state[j] = (1812433253UL * (_state[j-1] ^ (_state[j-1] >> 30)) +
j);
+
+ // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
+ // In the previous versions, MSBs of the seed affect
+ // only MSBs of the array state[].
+ // 2002/01/09 modified by Makoto Matsumoto
+ _state[j] &= 0xffffffffUL; // for >32 bit machines
+ }// for
+
+ _left = 1;
+}
+
+//
+// initialise rng by array
+//
+void
+TRNG::init ( unsigned long init_key[], unsigned long key_length )
+{
+ int i, j, k;
+
+ init( 19650218UL );
+
+ i = 1;
+ j = 0;
+ k = (int) (N > key_length ? N : key_length);
+
+ for ( ; k; k-- )
+ {
+ _state[i] = ((_state[i] ^ ((_state[i-1] ^ (_state[i-1] >> 30)) *
1664525UL))
+ + init_key[j] + j); // non linear
+ _state[i] &= 0xffffffffUL; // for WORDSIZE > 32 machines
+
+ i++;
+ j++;
+
+ if (i >= N)
+ {
+ _state[0] = _state[N-1];
+ i = 1;
+ }// if
+
+ if (((unsigned long) j) >= key_length )
+ j = 0;
+ }// for
+
+ for ( k = N-1; k; k-- )
+ {
+ _state[i] = ((_state[i] ^ ((_state[i-1] ^ (_state[i-1] >> 30)) *
1566083941UL))
+ - i); // non linear
+ _state[i] &= 0xffffffffUL; // for WORDSIZE > 32 machines
+
+ i++;
+
+ if (i >= N)
+ {
+ _state[0] = _state[N-1];
+ i = 1;
+ }// if
+ }// for
+
+ _state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
+ _left = 1;
+}
+
+//
+// set next state in RNG
+//
+void
+TRNG::next_state ()
+{
+ unsigned long * p = _state;
+ int j;
+
+ _left = N;
+ _next = _state;
+
+ for ( j = N-M+1; --j; p++ )
+ *p = p[M] ^ TWIST(p[0], p[1]);
+
+ for ( j = M; --j; p++ )
+ *p = p[M-N] ^ TWIST(p[0], p[1]);
+
+ *p = p[M-N] ^ TWIST(p[0], _state[0]);
+}
=======================================
--- /dev/null
+++ /trunk/src/TTimer.cpp Tue Jul 13 00:19:25 2010
@@ -0,0 +1,109 @@
+//
+// Project : misc
+// File : timer.cc
+// Author : Ronald Kriemann
+// Purpose : class for a timer (for speed tests)
+//
+// arch-tag: ba44c609-187f-4fdf-9fc4-5112d138b760
+//
+
+#include <sys/times.h>
+#include <limits.h>
+
+#include <cmath>
+
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "TTimer.hh"
+
+//
+// get time of system (usertime or real)
+//
+double
+TTimer::system_time ()
+{
+ double sec, usec;
+
+ switch ( _type )
+ {
+ case REAL_TIME :
+ gettimeofday( & _timeval_data, NULL );
+
+ sec = _timeval_data.tv_sec;
+ usec = _timeval_data.tv_usec;
+
+ break;
+
+ case CPU_TIME :
+#ifdef SUNOS
+ _lwp_info( & _lwpinfo_data );
+ sec = _lwpinfo_data.lwp_utime.tv_sec +
_lwpinfo_data.lwp_stime.tv_sec;
+ usec = _lwpinfo_data.lwp_utime.tv_nsec +
_lwpinfo_data.lwp_stime.tv_nsec;
+ usec /= 1000;
+#else
+ getrusage( RUSAGE_SELF, & _rusage_data );
+ sec = _rusage_data.ru_utime.tv_sec +
_rusage_data.ru_stime.tv_sec;
+ usec = _rusage_data.ru_utime.tv_usec +
_rusage_data.ru_stime.tv_usec;
+#endif
+
+ break;
+
+ case USER_TIME :
+#ifdef SUNOS
+ _lwp_info( & _lwpinfo_data );
+ sec = _lwpinfo_data.lwp_utime.tv_sec;
+ usec = _lwpinfo_data.lwp_utime.tv_nsec;
+ usec /= 1000;
+#else
+ getrusage( RUSAGE_SELF, & _rusage_data );
+ sec = _rusage_data.ru_utime.tv_sec;
+ usec = _rusage_data.ru_utime.tv_usec;
+#endif
+ break;
+
+ case SYSTEM_TIME :
+#ifdef SUNOS
+ _lwp_info( & _lwpinfo_data );
+ sec = _lwpinfo_data.lwp_stime.tv_sec;
+ usec = _lwpinfo_data.lwp_stime.tv_nsec;
+ usec /= 1000;
+#else
+ getrusage( RUSAGE_SELF, & _rusage_data );
+ sec = _rusage_data.ru_stime.tv_sec;
+ usec = _rusage_data.ru_stime.tv_usec;
+#endif
+ break;
+
+ default :
+ cout << "TTimer::system_time : unsupported type of time" <<
std::endl;
+ sec = usec = 0.0;
+ }// switch
+
+ return sec + (usec * 1e-6);
+}
+
+//
+// stream output (only the time)
+//
+std::ostream &
+operator << ( std::ostream & os, const TTimer & timer )
+{
+ double time;
+ long seconds, mseconds;
+
+ time = 0 > timer.diff() ? 0 : timer.diff();
+ seconds = long( floor( time ) );
+ mseconds = long(floor( (time - double(seconds)) * 1000.0 + 0.5 ));
+
+ os << seconds << ',';
+
+ if (mseconds < 10) os << "00";
+ else if (mseconds < 100) os << "0";
+
+ os << mseconds << "s";
+
+ return os;
+}
=======================================
--- /trunk/src/TRNG.cc Tue Jul 13 00:02:19 2010
+++ /dev/null
@@ -1,163 +0,0 @@
-//
-// Project : misc
-// File : TRNG.cc
-// Author : Ronald Kriemann
-// Purpose : class for a random-number-generator
-//
-// arch-tag: bfc157b0-9bbc-4ed8-84e8-b71ed20a1d75
-//
-
-#include "TRNG.hh"
-
-/* A C-program for MT19937, with initialization improved 2002/2/10.*/
-/* Coded by Takuji Nishimura and Makoto Matsumoto. */
-/* This is a faster version by taking Shawn Cokus's optimization, */
-/* Matthe Bellew's simplification, Isaku Wada's real version. */
-
-/* Before using, initialize the state by using init_genrand(seed) */
-/* or init_by_array(init_key, key_length). */
-
-/* This library is free software. */
-/* This library is distributed in the hope that it will be useful, */
-/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
-/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
-
-/* Copyright (C) 1997, 2002 Makoto Matsumoto and Takuji Nishimura. */
-/* Any feedback is very welcome. */
-/* http://www.math.keio.ac.jp/matumoto/emt.html */
-/* email: matumoto@xxxxxxxxxxxxxxx */
-
-//
-// some defines/numbers wich are used in the RNG
-//
-
-#define N 624
-#define M 397
-#define MATRIX_A 0x9908b0dfUL // constant vector a
-#define UMASK 0x80000000UL // most significant w-r bits
-#define LMASK 0x7fffffffUL // least significant r bits
-#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
-#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
-
-////////////////////////////////////////////
-//
-// access rng
-//
-
-//
-// return random number between 0 and max
-//
-double
-TRNG::rand ( double max )
-{
- unsigned long y;
-
- if ( --_left == 0 )
- next_state();
-
- y = *_next++;
-
- /* Tempering */
- y ^= (y >> 11);
- y ^= (y << 7) & 0x9d2c5680UL;
- y ^= (y << 15) & 0xefc60000UL;
- y ^= (y >> 18);
-
- /* divided by 2^32-1 */
- return max * double( y ) * (1.0/4294967295.0);
-}
-
-//
-// initialise rng
-//
-void
-TRNG::init ( unsigned long seed )
-{
- _state[0] = seed & 0xffffffffUL;
-
- for ( int j = 1; j < N; j++ )
- {
- _state[j] = (1812433253UL * (_state[j-1] ^ (_state[j-1] >> 30)) +
j);
-
- // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
- // In the previous versions, MSBs of the seed affect
- // only MSBs of the array state[].
- // 2002/01/09 modified by Makoto Matsumoto
- _state[j] &= 0xffffffffUL; // for >32 bit machines
- }// for
-
- _left = 1;
-}
-
-//
-// initialise rng by array
-//
-void
-TRNG::init ( unsigned long init_key[], unsigned long key_length )
-{
- int i, j, k;
-
- init( 19650218UL );
-
- i = 1;
- j = 0;
- k = (int) (N > key_length ? N : key_length);
-
- for ( ; k; k-- )
- {
- _state[i] = ((_state[i] ^ ((_state[i-1] ^ (_state[i-1] >> 30)) *
1664525UL))
- + init_key[j] + j); // non linear
- _state[i] &= 0xffffffffUL; // for WORDSIZE > 32 machines
-
- i++;
- j++;
-
- if (i >= N)
- {
- _state[0] = _state[N-1];
- i = 1;
- }// if
-
- if (((unsigned long) j) >= key_length )
- j = 0;
- }// for
-
- for ( k = N-1; k; k-- )
- {
- _state[i] = ((_state[i] ^ ((_state[i-1] ^ (_state[i-1] >> 30)) *
1566083941UL))
- - i); // non linear
- _state[i] &= 0xffffffffUL; // for WORDSIZE > 32 machines
-
- i++;
-
- if (i >= N)
- {
- _state[0] = _state[N-1];
- i = 1;
- }// if
- }// for
-
- _state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
- _left = 1;
-}
-
-//
-// set next state in RNG
-//
-void
-TRNG::next_state ()
-{
- unsigned long * p = _state;
- int j;
-
- _left = N;
- _next = _state;
-
- for ( j = N-M+1; --j; p++ )
- *p = p[M] ^ TWIST(p[0], p[1]);
-
- for ( j = M; --j; p++ )
- *p = p[M-N] ^ TWIST(p[0], p[1]);
-
- *p = p[M-N] ^ TWIST(p[0], _state[0]);
-}
=======================================
--- /trunk/src/TTimer.cc Tue Jul 13 00:02:19 2010
+++ /dev/null
@@ -1,109 +0,0 @@
-//
-// Project : misc
-// File : timer.cc
-// Author : Ronald Kriemann
-// Purpose : class for a timer (for speed tests)
-//
-// arch-tag: ba44c609-187f-4fdf-9fc4-5112d138b760
-//
-
-#include <sys/times.h>
-#include <limits.h>
-
-#include <cmath>
-
-#include <iostream>
-
-using std::cout;
-using std::endl;
-
-#include "TTimer.hh"
-
-//
-// get time of system (usertime or real)
-//
-double
-TTimer::system_time ()
-{
- double sec, usec;
-
- switch ( _type )
- {
- case REAL_TIME :
- gettimeofday( & _timeval_data, NULL );
-
- sec = _timeval_data.tv_sec;
- usec = _timeval_data.tv_usec;
-
- break;
-
- case CPU_TIME :
-#ifdef SUNOS
- _lwp_info( & _lwpinfo_data );
- sec = _lwpinfo_data.lwp_utime.tv_sec +
_lwpinfo_data.lwp_stime.tv_sec;
- usec = _lwpinfo_data.lwp_utime.tv_nsec +
_lwpinfo_data.lwp_stime.tv_nsec;
- usec /= 1000;
-#else
- getrusage( RUSAGE_SELF, & _rusage_data );
- sec = _rusage_data.ru_utime.tv_sec +
_rusage_data.ru_stime.tv_sec;
- usec = _rusage_data.ru_utime.tv_usec +
_rusage_data.ru_stime.tv_usec;
-#endif
-
- break;
-
- case USER_TIME :
-#ifdef SUNOS
- _lwp_info( & _lwpinfo_data );
- sec = _lwpinfo_data.lwp_utime.tv_sec;
- usec = _lwpinfo_data.lwp_utime.tv_nsec;
- usec /= 1000;
-#else
- getrusage( RUSAGE_SELF, & _rusage_data );
- sec = _rusage_data.ru_utime.tv_sec;
- usec = _rusage_data.ru_utime.tv_usec;
-#endif
- break;
-
- case SYSTEM_TIME :
-#ifdef SUNOS
- _lwp_info( & _lwpinfo_data );
- sec = _lwpinfo_data.lwp_stime.tv_sec;
- usec = _lwpinfo_data.lwp_stime.tv_nsec;
- usec /= 1000;
-#else
- getrusage( RUSAGE_SELF, & _rusage_data );
- sec = _rusage_data.ru_stime.tv_sec;
- usec = _rusage_data.ru_stime.tv_usec;
-#endif
- break;
-
- default :
- cout << "TTimer::system_time : unsupported type of time" <<
std::endl;
- sec = usec = 0.0;
- }// switch
-
- return sec + (usec * 1e-6);
-}
-
-//
-// stream output (only the time)
-//
-std::ostream &
-operator << ( std::ostream & os, const TTimer & timer )
-{
- double time;
- long seconds, mseconds;
-
- time = 0 > timer.diff() ? 0 : timer.diff();
- seconds = long( floor( time ) );
- mseconds = long(floor( (time - double(seconds)) * 1000.0 + 0.5 ));
-
- os << seconds << ',';
-
- if (mseconds < 10) os << "00";
- else if (mseconds < 100) os << "0";
-
- os << mseconds << "s";
-
- return os;
-}
=======================================
--- /trunk/src/main.cc Tue Jul 13 00:02:19 2010
+++ /dev/null
@@ -1,199 +0,0 @@
-//
-// arch-tag: 2dcf5618-b51f-4a04-9f3a-86ab829bc7d8
-//
-
-#include <stdlib.h>
-#include <cmath>
-
-#include "TThreadPool.hh"
-#include "TTimer.hh"
-#include "TRNG.hh"
-
-//
-// for some benchmarking
-//
-
-static int job_number = 0;
-
-class TBenchJob : public TThreadPool::TJob
-{
-protected:
- int _size;
-
-public:
- TBenchJob ( int i, int s ) : TThreadPool::TJob( i ), _size(s) {}
-
- virtual void run ( void * )
- {
- //std::cout << " starting thread (" << job_number++ << "), size =
" << _size << std::endl;
-
- double * _matrix = new double[ _size * _size ];
-
- for ( int i = 0; i < _size; i++ )
- {
- for ( int j = 0; j < _size; j++ )
- {
- _matrix[ (i*_size) + j ] = std::sin( double(j) * M_PI *
std::cos( double(i) ));
- }// for
- }// for
-
- delete _matrix;
- }
-};
-
-#define MAX_SIZE 1000
-#define MAX_RAND 500
-
-void
-recursion ( int level, TRNG & rng )
-{
- if ( level == 0 )
- {
- TBenchJob * job = new TBenchJob( -1, int(rng.rand( MAX_RAND )) +
MAX_SIZE );
-
- tp_run( job, NULL, true );
- }// if
- else
- {
- recursion( level-1, rng );
- recursion( level-1, rng );
- recursion( level-1, rng );
- recursion( level-1, rng );
- }// else
-}
-
-void
-bench1 ( int argc, char ** argv )
-{
- TRNG rng;
- int i = 1;
- int thr_count = 16;
- int rec_depth = 6;
-
- if ( argc > 1 ) thr_count = atoi( argv[1] );
- if ( argc > 2 ) rec_depth = atoi( argv[2] );
-
- for ( int j = 0; j < rec_depth; j++ )
- i *= 4;
-
- tp_init( thr_count );
-
- std::cout << "executing " << i << " jobs using " << thr_count << "
thread(s)" << std::endl;
-
- TTimer timer( REAL_TIME );
-
- timer.start();
-
- recursion( rec_depth, rng );
-
- tp_sync_all();
-
- timer.stop();
- std::cout << "time for recursion = " << timer << std::endl;
-}
-
-class TBench2Job : public TThreadPool::TJob
-{
-public:
- TBench2Job ( int i ) : TThreadPool::TJob( i ) {}
-
- virtual void run ( void * )
- {
- // do nothing
- }
-};
-
-class TBench2Thr : public TThread
-{
-public:
- TBench2Thr ( int i ) : TThread( i ) {}
-
- virtual void run ()
- {
- // do nothing
- }
-};
-
-void
-bench2 ( int argc, char ** argv )
-{
- int max_jobs = 10000;
-
- tp_init( 4 );
-
- TTimer timer( REAL_TIME );
-
- timer.start();
-
- for ( int i = 0; i < max_jobs; i++ )
- {
- TBench2Job * job = new TBench2Job( i );
- delete job;
- }
-
- timer.stop();
- std::cout << "time to create jobs = " << timer << std::endl;
-
- timer.start();
-
- for ( int i = 0; i < max_jobs; i++ )
- {
- TBench2Thr * job = new TBench2Thr( i );
- delete job;
- }
-
- timer.stop();
- std::cout << "time to create threads = " << timer << std::endl;
-
- timer.start();
-
- for ( int i = 0; i < max_jobs; i++ )
- {
- TBench2Job * job = new TBench2Job( i );
-
- tp_run( job );
- tp_sync( job );
-
- delete job;
- }
-
- timer.stop();
- std::cout << "time for thread pool = " << timer << std::endl;
-
- timer.start();
-
- for ( int i = 0; i < max_jobs; i++ )
- {
- TBench2Thr * job = new TBench2Thr( i );
-
- job->create( false, false );
- job->join();
-
- delete job;
- }
-
- timer.stop();
-
- std::cout << "time for lwp-threads = " << timer << std::endl;
- timer.start();
-
- for ( int i = 0; i < max_jobs; i++ )
- {
- TBench2Thr * job = new TBench2Thr( i );
-
- job->create( false, true );
- job->join();
-
- delete job;
- }
-
- timer.stop();
- std::cout << "time for hwp-threads = " << timer << std::endl;
-}
-
-int
-main ( int argc, char ** argv )
-{
- bench1( argc, argv );
- // bench2( argc, argv );
-}
=======================================
--- /trunk/src/Makefile Wed Jul 7 05:23:52 2010
+++ /trunk/src/Makefile Tue Jul 13 00:19:25 2010
@@ -3,7 +3,7 @@
CFLAGES=-g3 -Wno-deprecated
INCDIR=-I../include\
-I../include/common
-LIBS=-L../lib -lcommon
+LIBS=-L../lib -lcommon -lpthread
SRC=$(shell ls *.cpp)
OBJ=$(SRC:.cpp=.o)
Target=kadpeer
=======================================
--- /trunk/src/main.cpp Tue Jul 13 00:02:19 2010
+++ /trunk/src/main.cpp Tue Jul 13 00:19:25 2010
@@ -1,159 +1,199 @@
-/*
- * shatest.cpp
- *
- * Copyright (C) 1998, 2009
- * Paul E. Jones <paulej@xxxxxxxxxxxxxx>
- * All Rights Reserved
- *
-
*****************************************************************************
- * $Id: shatest.cpp 12 2009-06-22 19:34:25Z paulej $
-
*****************************************************************************
- *
- * Description:
- * This file will exercise the SHA1 class and perform the three
- * tests documented in FIPS PUB 180-1.
- *
- * Portability Issues:
- * None.
- *
- */
-
-#include <iostream>
-#include "sha1.h"
-
-using namespace std;
-
-/*
- * Define patterns for testing
- */
-#define TESTA "abc"
-#define TESTB "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
-
-/*
- * Function prototype
- */
-void DisplayMessageDigest(unsigned *message_digest);
-
-/*
- * main
- *
- * Description:
- * This is the entry point for the program
- *
- * Parameters:
- * None.
- *
- * Returns:
- * Nothing.
- *
- * Comments:
- *
- */
-int main()
-{
- SHA1 sha;
- unsigned message_digest[5];
-
- /*
- * Perform test A
- */
- cout << endl << "Test A: 'abc'" << endl;
-
- sha.Reset();
- sha << TESTA;
-
- if (!sha.Result(message_digest))
- {
- cerr << "ERROR-- could not compute message digest" << endl;
- }
-
+//
+// arch-tag: 2dcf5618-b51f-4a04-9f3a-86ab829bc7d8
+//
+
+#include <stdlib.h>
+#include <cmath>
+
+#include "TThreadPool.hh"
+#include "TTimer.hh"
+#include "TRNG.hh"
+
+//
+// for some benchmarking
+//
+
+static int job_number = 0;
+
+class TBenchJob : public TThreadPool::TJob
+{
+protected:
+ int _size;
+
+public:
+ TBenchJob ( int i, int s ) : TThreadPool::TJob( i ), _size(s) {}
+
+ virtual void run ( void * )
+ {
+ //std::cout << " starting thread (" << job_number++ << "), size =
" << _size << std::endl;
+
+ double * _matrix = new double[ _size * _size ];
+
+ for ( int i = 0; i < _size; i++ )
+ {
+ for ( int j = 0; j < _size; j++ )
+ {
+ _matrix[ (i*_size) + j ] = std::sin( double(j) * M_PI *
std::cos( double(i) ));
+ }// for
+ }// for
+
+ delete _matrix;
+ }
+};
+
+#define MAX_SIZE 1000
+#define MAX_RAND 500
+
+void
+recursion ( int level, TRNG & rng )
+{
+ if ( level == 0 )
+ {
+ TBenchJob * job = new TBenchJob( -1, int(rng.rand( MAX_RAND )) +
MAX_SIZE );
+
+ tp_run( job, NULL, true );
+ }// if
else
{
- DisplayMessageDigest(message_digest);
- cout << "Should match:" << endl;
- cout << '\t' << "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D" <<
endl;
-
-
- }
- sha.Reset();
- sha<<TESTA;
- BigInt::Rossi digest;
- if ( sha.Result(digest) )
- {
- cout<<digest.toStrHex()<<endl;
- cout<<digest.toStrDec()<<endl;
- }
- /*
- * Perform test B
- */
- cout << endl << "Test B: " << TESTB << endl;
-
- sha.Reset();
- sha << TESTB;
-
- if (!sha.Result(message_digest))
- {
- cerr << "ERROR-- could not compute message digest" << endl;
- }
- else
- {
- DisplayMessageDigest(message_digest);
- cout << "Should match:" << endl;
- cout << '\t' << "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1" <<
endl;
+ recursion( level-1, rng );
+ recursion( level-1, rng );
+ recursion( level-1, rng );
+ recursion( level-1, rng );
+ }// else
+}
+
+void
+bench1 ( int argc, char ** argv )
+{
+ TRNG rng;
+ int i = 1;
+ int thr_count = 16;
+ int rec_depth = 6;
+
+ if ( argc > 1 ) thr_count = atoi( argv[1] );
+ if ( argc > 2 ) rec_depth = atoi( argv[2] );
+
+ for ( int j = 0; j < rec_depth; j++ )
+ i *= 4;
+
+ tp_init( thr_count );
+
+ std::cout << "executing " << i << " jobs using " << thr_count << "
thread(s)" << std::endl;
+
+ TTimer timer( REAL_TIME );
+
+ timer.start();
+
+ recursion( rec_depth, rng );
+
+ tp_sync_all();
+
+ timer.stop();
+ std::cout << "time for recursion = " << timer << std::endl;
+}
+
+class TBench2Job : public TThreadPool::TJob
+{
+public:
+ TBench2Job ( int i ) : TThreadPool::TJob( i ) {}
+
+ virtual void run ( void * )
+ {
+ // do nothing
+ }
+};
+
+class TBench2Thr : public TThread
+{
+public:
+ TBench2Thr ( int i ) : TThread( i ) {}
+
+ virtual void run ()
+ {
+ // do nothing
+ }
+};
+
+void
+bench2 ( int argc, char ** argv )
+{
+ int max_jobs = 10000;
+
+ tp_init( 4 );
+
+ TTimer timer( REAL_TIME );
+
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Job * job = new TBench2Job( i );
+ delete job;
}
- /*
- * Perform test C
- */
- cout << endl << "Test C: One million 'a' characters" << endl;
-
- sha.Reset();
- for(int i = 1; i <= 1000000; i++) sha.Input('a');
-
- if (!sha.Result(message_digest))
- {
- cerr << "ERROR-- could not compute message digest" << endl;
- }
- else
- {
- DisplayMessageDigest(message_digest);
- cout << "Should match:" << endl;
- cout << '\t' << "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F" <<
endl;
+ timer.stop();
+ std::cout << "time to create jobs = " << timer << std::endl;
+
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Thr * job = new TBench2Thr( i );
+ delete job;
+ }
+
+ timer.stop();
+ std::cout << "time to create threads = " << timer << std::endl;
+
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Job * job = new TBench2Job( i );
+
+ tp_run( job );
+ tp_sync( job );
+
+ delete job;
}
- return 0;
-}
-
-/*
- * DisplayMessageDigest
- *
- * Description:
- * Display Message Digest array
- *
- * Parameters:
- * None.
- *
- * Returns:
- * Nothing.
- *
- * Comments:
- *
- */
-void DisplayMessageDigest(unsigned *message_digest)
-{
- ios::fmtflags flags;
-
- cout << '\t';
-
- flags = cout.setf(ios::hex|ios::uppercase,ios::basefield);
- cout.setf(ios::uppercase);
-
- for(int i = 0; i < 5 ; i++)
- {
- cout << message_digest[i] << ' ';
+ timer.stop();
+ std::cout << "time for thread pool = " << timer << std::endl;
+
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Thr * job = new TBench2Thr( i );
+
+ job->create( false, false );
+ job->join();
+
+ delete job;
+ }
+
+ timer.stop();
+
+ std::cout << "time for lwp-threads = " << timer << std::endl;
+ timer.start();
+
+ for ( int i = 0; i < max_jobs; i++ )
+ {
+ TBench2Thr * job = new TBench2Thr( i );
+
+ job->create( false, true );
+ job->join();
+
+ delete job;
}
- cout << endl;
-
- cout.setf(flags);
-}
+ timer.stop();
+ std::cout << "time for hwp-threads = " << timer << std::endl;
+}
+
+int
+main ( int argc, char ** argv )
+{
+ bench1( argc, argv );
+ // bench2( argc, argv );
+}
--
Thanks & Regards
Linux Developer : Devil Wang