/* PPE-Teil des Cell-Programms zur Berechnung von Pi nach dem Schrotflinten-Algorithmus */ /************************** * Peter Vaeterlein, 2008 * **************************/ #include #include #include #include #include #include #include "pi_libspe.h" spe_program_handle_t pi_libspe_spe; void *spu_pthread( void *arg ) { spe_context_ptr_t *ctx = ( spe_context_ptr_t *) arg; uint32_t entry = SPE_DEFAULT_ENTRY; if ( spe_context_run( *ctx, &entry, 0, NULL, NULL, NULL ) < 0 ) { perror( "Failed run SPE\ context" ); exit ( 1 ); } return ( 0 ); } int main( int argc, char **argv ) { spe_par_t *spe_par; spe_context_ptr_t *spe_ctx; pthread_t *spe_thread_handle; struct timeval tv; int i; int numspe; uint64_t rounds; uint64_t myaddr; float final_pi = 0.0; numspe = atoi( argv[2] ); rounds = atol( argv[1] ) / ( uint64_t ) numspe; rounds *= ( uint64_t ) numspe; printf( "Stochastische Berechnung\ von PI\n\n" ); printf( " ... mit %d SPEs\n", numspe ); printf( " ... und %ld \ Stichproben\n\n", rounds ); posix_memalign(( void ** ) &spe_par, 16, numspe * sizeof(spe_par_t)); posix_memalign(( void ** ) &spe_ctx, 16, numspe * sizeof(spe_context_ptr_t)); spe_thread_handle = malloc( numspe * sizeof( pthread_t )); for ( i = 0; i < numspe; i++ ) { spe_par[i].rounds = rounds / numspe; gettimeofday( &tv, NULL ); spe_par[i].seed = tv.tv_sec * 1000000 + tv.tv_usec; spe_par[i].value = 0.0; spe_ctx[i] = spe_context_create(0,NULL); spe_program_load( spe_ctx[i], &pi_libspe_spe ); pthread_create( &spe_thread_handle[i], NULL, &spu_pthread, &spe_ctx[i] ); myaddr = (uint64_t) &spe_par[i]; spe_in_mbox_write( spe_ctx[i], ( unsigned int * ) &myaddr, 2, SPE_MBOX_ANY_NONBLOCKING ); } for ( i = 0; i < numspe; i++ ) { pthread_join( spe_thread_handle[i], NULL); spe_context_destroy(spe_ctx[i]); } for ( i = 0; i < numspe; i++ ) { final_pi += spe_par[i].value; printf( "Ergebnis von SPE \ #%02d: %f\n", i, spe_par[i].value ); } printf( "\nDurchschnitt : %f\n\n", final_pi / numspe ); return 0; }