#include "mt_ppu.h"extern spe_program_handle_t mt_spu;unsigned long buffer[32] __attribute__ ((aligned(128)));#define ACTIVE_SPUS 4void mt_init(mt_state_t *state) { unsigned long int *const mt = state->mt; int i; for (i = 0; i < N*ACTIVE_SPUS; i++) { mt[i] = (rand() << 16) | rand(); //if(i % 83 == 0) // printf("fun %d %x \n", i,mt[i]); }}int main(){ /* SPE Management */ speid_t speid[ACTIVE_SPUS]; int status[ACTIVE_SPUS]; int i; control_block * cb = malloc_align(128,7); /* Control block */ cb->first = 0; cb->last = ACTIVE_SPUS - 1; cb->memory = &buffer[0]; /****** Seeds for MT ******/ mt_state_t * statePtr = malloc_align(sizeof(mt_state_t),7); mt_init(statePtr); printf("-------------- PPU 1 Creating threads \n"); /* Create the threads */ for (i=0;i<ACTIVE_SPUS;i++){ speid[i] = spe_create_thread(0, &mt_spu, NULL, NULL, -1, 0); } printf("-------------- PPU 2 Determining local stores / buffers \n"); /* Get the location of the result buffers */ for (i=0;i<ACTIVE_SPUS;i++){ /* Get the local store of each SPE */ cb->lstore[i] = spe_get_ls(speid[i]); /* Wait until there is something in the mailbox */ while (!spe_stat_out_mbox(speid[i])) {} /* 2 Add this to the local store address */ cb->lstore[i] += spe_read_out_mbox(speid[i]); } printf("-------------- PPU 3 Share control block \n"); /* 3 Give control block pointer */ for (i=0;i<ACTIVE_SPUS;i++) spe_write_in_mbox(speid[i],(unsigned int) cb); printf("-------------- PPU 4 Provide ID to SPEs\n"); /* 4 Write SPE number to box */ for (i=0;i<ACTIVE_SPUS;i++){ spe_write_in_mbox(speid[i],i); } printf("-------------- PPU 5 Seeed SPEs for MT\n"); /* 5 Give MT state pointer */ for (i=0;i<ACTIVE_SPUS;i++) { spe_write_in_mbox(speid[i], (unsigned long) statePtr); /* Wait until the right SPE picks it up */ while (!spe_stat_out_mbox(speid[i])) {} spe_read_out_mbox(speid[i]); } printf("-------------- PPU 6 Reached main loop\n"); unsigned int notDone = 5; while(notDone > 0) { for (i=0;i<ACTIVE_SPUS;i++) { /* Wait until there is something in the mailbox */ while (!spe_stat_out_mbox(speid[i])) {} spe_read_out_mbox(speid[i]); unsigned long * tmp = cb->lstore[i]; printf("PPU says %lu on behalf of %d. Thank you, now get back to work! \n", tmp[31], i); spe_write_in_mbox(speid[i],1); } notDone--; } /* Wait for SPEs to finish */ for (i=0;i<ACTIVE_SPUS;i++) { printf("PPU terminating %d \n", i); spe_write_in_mbox(speid[i],0); spe_wait(speid[i], &status[i], 0); } return 0;}