Пример #1
0
int main()
{
	spe_context_ptr_t ctx;
	unsigned int entry = SPE_DEFAULT_ENTRY;
	init_matrix();

	if((ctx = spe_context_create(0, NULL)) == NULL) {
		perror ("Failed creating context");
		exit (1);
	}

	if(spe_program_load(ctx, &lab10_spu)) {
		perror ("Failed loading program");
		exit (1);
	}

	printf("SPU:\n");
	if(spe_context_run(ctx, &entry, 0, (void*)&v, (void*)M, NULL) < 0) {
		perror ("Failed running context");
		exit (1);
	}

	if(spe_context_destroy(ctx) != 0) {
		perror("Failed destroying context");
		exit (1);
	}

	printf("PPU:\n");
	printf("received: %d %d \n", v[0][0].x, v[0][0].y);
	printf("correct: %d\n", (destination.x == v[0][0].x) && (destination.y == v[0][0].y));

	return 0;
}
Пример #2
0
static void free_memory()
{
  spe_context_destroy(context);
  spe_image_close(spe_program);
  free((void *) mControlBlockAddr);
  free(binary);
}
Пример #3
0
void *ppu_pthread_function(void *thread_arg) {

	spe_context_ptr_t ctx;
	struct package_t *arg = (struct package_t *) thread_arg;

	/* Create SPE context */
	if ((ctx = spe_context_create (0, NULL)) == NULL) {
		perror ("Failed creating context");
		exit (1);
	}

	/* Load SPE program into context */
	if (spe_program_load (ctx, &lab8_spu)) {
		perror ("Failed loading program");
		exit (1);
	}

	/* Run SPE context */
	unsigned int entry = SPE_DEFAULT_ENTRY;

	/* transferul adresei structurii initiale */
	if (spe_context_run(ctx, &entry, 0, (void *)arg, (void *)sizeof(struct package_t), NULL) < 0) {  
		perror ("Failed running context");
		exit (1);
	}  

	/* Destroy context */
	if (spe_context_destroy (ctx) != 0) {
		perror("Failed destroying context");
		exit (1);
	}

	pthread_exit(NULL);
}
Пример #4
0
int
main (void)
{
  pthread_t pts;
  spe_context_ptr_t ctx;
  unsigned int value;
  unsigned int pid;

  ctx = spe_context_create (0, NULL);
  pthread_create (&pts, NULL, &spe_thread, &ctx);

  /* Wait until the SPU thread is running.  */
  spe_out_intr_mbox_read (ctx, &value, 1, SPE_MBOX_ALL_BLOCKING);

  pid = fork ();
  if (pid == 0)
    {
      /* This is the child.  Just exit immediately.  */
      exit (0);
    }
  else
    {
      /* This is the parent.  Wait for the child to exit.  */
      waitpid (pid, NULL, 0);
    }

  /* Tell SPU to continue.  */
  spe_in_mbox_write (ctx, &value, 1, SPE_MBOX_ALL_BLOCKING);
  
  pthread_join (pts, NULL);
  spe_context_destroy (ctx);

  return 0;
}
int main()
{
  spe_context_ptr_t speid;
  unsigned int flags = 0;
  unsigned int entry = SPE_DEFAULT_ENTRY;
  void *argp = NULL;
  void *envp = NULL;
  spe_stop_info_t stop_info;
  int rc;

  speid = spe_context_create(0,NULL);
  if (speid==NULL)
  {
    perror("spe_context_create");
    return -1;
  }
  
  //Load SPE executable object into the SPE context local store
  if (spe_program_load(speid, &hello_spu))
  {
    perror("spe_program_load");
    return -2;
  }
  
  //Run the SPE context
  rc = spe_context_run(speid, &entry, flags, argp, envp, &stop_info);
  if (rc<0) perror("spe_context_run");
  
  //Destroy the SPE context
  spe_context_destroy(speid);
  
  return 0;
}
Пример #6
0
int main (void)
{
  int thread_id[nr_t];
  pthread_t pts[nr_t];
  spe_context_ptr_t ctx[nr_t];

  int cnt;

  char var_char = 'c';
  short var_short = 7;
  int var_int = 1337;
  long var_long = 123456;
  long long var_longlong = 123456789;
  float var_float = 1.23;
  double var_double = 2.3456;
  long double var_longdouble = 3.45678;

  for (cnt = 0; cnt < nr_t; cnt++)
    {
      ctx[cnt] = spe_context_create(0, NULL);
      thread_id[cnt]
	= pthread_create (&pts[cnt], NULL, &spe_thread, &ctx[cnt]);
    }

  for (cnt = 0; cnt < nr_t; cnt++)
    pthread_join (pts[cnt], NULL);

  for (cnt = 0; cnt < nr_t; cnt++)
    spe_context_destroy (ctx[cnt]);

  return 0;
}
Пример #7
0
int
main (void)
{
  int thread_id[nr_t];
  pthread_t pts[nr_t];
  spe_context_ptr_t ctx[nr_t];
  int value = 1;
  int cnt;

  spe_callback_handler_register (indirect_handler, 0x11, SPE_CALLBACK_NEW);
  spe_callback_handler_register (crash_handler, 0x12, SPE_CALLBACK_NEW);

  for (cnt = 0; cnt < nr_t; cnt++)
    {
      ctx[cnt] = spe_context_create (0, NULL);
      thread_id[cnt]
	= pthread_create (&pts[cnt], NULL, &spe_thread, &ctx[cnt]);
    }

  for (cnt = 0; cnt < nr_t; cnt++)
    pthread_join (pts[cnt], NULL);

  for (cnt = 0; cnt < nr_t; cnt++)
    spe_context_destroy (ctx[cnt]);

  return 0;
}
Пример #8
0
int
main (void)
{
  int thread_id[nr_t];
  pthread_t pts[nr_t];
  spe_context_ptr_t ctx[nr_t];
  int value = 1;
  int cnt;
  static int test_var;

  test_var = 5;
  for (cnt = 0; cnt < nr_t; cnt++) /* Marker PPUEA */
    {
      ctx[cnt] = spe_context_create (0, NULL);
      thread_id[cnt]
	= pthread_create (&pts[cnt], NULL, &spe_thread, &ctx[cnt]);
    }

  for (cnt = 0; cnt < nr_t; cnt++)
    pthread_join (pts[cnt], NULL);

  for (cnt = 0; cnt < nr_t; cnt++)
    spe_context_destroy (ctx[cnt]);

  return 0;
}
Пример #9
0
///tell the task scheduler we are done with the SPU tasks
void SpuLibspe2Support::stopSPU()
{
	// wait for all threads to finish 
	int i;
	for ( i = 0; i < this->numThreads; i++ ) 
	{ 
		
		unsigned int event = Spu_Mailbox_Event_Shutdown;
		spe_context_ptr_t context = data[i].context;
		spe_in_mbox_write(context, &event, 1, SPE_MBOX_ALL_BLOCKING);
		pthread_join (data[i].pthread, NULL); 
		
	} 
	// close SPE program 
	spe_image_close(program); 
	// destroy SPE contexts 
	for ( i = 0; i < this->numThreads; i++ ) 
	{ 
		if(data[i].context != NULL)
		{
			spe_context_destroy (data[i].context);
		}
	} 
	
	m_activeSpuStatus.clear();
	
}
Пример #10
0
int main(int argc, char **argv)
{
    int i;
    int ret;

    spe_context_ptr_t spe;
    spe_program_handle_t *prog;
    unsigned int entry;
    spe_stop_info_t stop_info;

    prog = spe_image_open("vec_abs_spe.elf");
    if (!prog) {
        perror("spe_image_open");
        exit(1);
    }

    spe = spe_context_create(0, NULL);
    if (!spe) {
        perror("spe_context_create");
        exit(1);
    }

    ret = spe_program_load(spe, prog);
    if (ret) {
        perror("spe_program_load");
        exit(1);
    }

    abs_params.ea_in  = (unsigned long) in;
    abs_params.ea_out = (unsigned long) out;
    abs_params.size   = SIZE;

    entry = SPE_DEFAULT_ENTRY;
    ret = spe_context_run(spe, &entry, 0, &abs_params, NULL, &stop_info);
    if (ret < 0) {
        perror("spe_context_run");
        exit(1);
    }

    ret = spe_context_destroy(spe);
    if (ret) {
        perror("spe_context_destroy");
        exit(1);
    }

    ret = spe_image_close(prog);
    if (ret) {
        perror("spe_image_close");
        exit(1);
    }

    for (i = 0; i < SIZE; i++) {
        printf("out[%02d]=%0.0f\n", i, out[i]);
    }

    return 0;
}
Пример #11
0
int main(int argc, char **argv)
{
    int i;
    int ret;
 
    spe_context_ptr_t spe[NUM_SPE];
    spe_program_handle_t *prog;
    pthread_t thread[NUM_SPE];

    prog = spe_image_open("increment_spe.elf");
    if (!prog) {
        perror("spe_image_open");
        exit(1);
    }

    for (i = 0; i < NUM_SPE; i++) {
        spe[i] = spe_context_create(0, NULL);
        if (!spe) {
            perror("spe_context_create");
            exit(1);
        }

        ret = spe_program_load(spe[i], prog);
        if (ret) {
            perror("spe_program_load");
            exit(1);
        }
    }

    for (i = 0; i < NUM_SPE; i++) {
        ret = pthread_create(&thread[i], NULL, run_increment_spe, &spe[i]);
        if (ret) {
            perror("pthread_create");
            exit(1);
        }
    }

    for (i = 0; i < NUM_SPE; i++) {
        pthread_join(thread[i], NULL);
        ret = spe_context_destroy(spe[i]);
        if (ret < 0) {
            perror("spe_context_destroy");
            exit(1);
        }
    }

    ret = spe_image_close(prog);
    if (ret) {
        perror("spe_image_close");
        exit(1);
    }

    printf("result=%d\n", counter[0]);

    return 0;
}
Пример #12
0
int main()
{
	int i, spu_threads;
	spe_context_ptr_t ctxs[MAX_SPU_THREADS];
	pthread_t threads[MAX_SPU_THREADS];
	 
	/*
	  * Determine the number of SPE threads to create.
	  */
	 
	spu_threads = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1);
	if (spu_threads > MAX_SPU_THREADS) spu_threads = MAX_SPU_THREADS;
	 
	/*
	  * Create several SPE-threads to execute 'simple_spu'.
	  */
	 
	for(i=0; i<spu_threads; i++) {
		/* Create context */
		if ((ctxs[i] = spe_context_create (0, NULL)) == NULL) {
			perror ("Failed creating context");
			exit (1);
		}
	 
		/* Load program into context */
		if (spe_program_load (ctxs[i], &simple_spu)) {
			perror ("Failed loading program");
			exit (1);
		}
		 
		/* Create thread for each SPE context */
		if (pthread_create (&threads[i], NULL, &ppu_pthread_function, &ctxs[i])) {
			perror ("Failed creating thread");
			exit (1);
		}
	}
		 
		/* Wait for SPU-thread to complete execution. */
	for (i=0; i<spu_threads; i++) {
		if (pthread_join (threads[i], NULL)) {
			perror("Failed pthread_join");
			exit (1);
		}
		 
		/* Destroy context */
		if (spe_context_destroy (ctxs[i]) != 0) {
			perror("Failed destroying context");
			exit (1);
		}
	}
	 
	printf("\nThe program has successfully executed.\n");
	return 0;
}
Пример #13
0
void sws_yuvscaler_destroy(yuvscaler_t* arg)
{
	unsigned int message=STOP;
	struct yuvscaler_s * arg_ptr;
	arg_ptr=(struct yuvscaler_s *) arg;

	spe_in_mbox_write(arg_ptr->ctx,&message,1,SPE_MBOX_ALL_BLOCKING);
	
	pthread_join(arg_ptr->pts,NULL);
	spe_context_destroy(arg_ptr->ctx);
	
}
Пример #14
0
int main(int argc, char **argv)
{
    int ret;

    spe_context_ptr_t spe;
    spe_program_handle_t *prog;
    unsigned int entry;
    spe_stop_info_t stop_info;

    unsigned long param;

    prog = spe_image_open("print_param_spe.elf");
    if (!prog) {
        perror("spe_image_open");
        exit(1);
    }

    spe = spe_context_create(0, NULL);
    if (!spe) {
        perror("spe_context_create");
        exit(1);
    }

    ret = spe_program_load(spe, prog);
    if (ret) {
        perror("spe_program_load");
        exit(1);
    }

    param = 12345678;
    printf("[PPE] param=%ld\n", param);

    entry = SPE_DEFAULT_ENTRY;
    ret = spe_context_run(spe, &entry, 0, (void *) param, NULL, &stop_info);
    if (ret < 0) {
        perror("spe_context_run");
        exit(1);
    }

    ret = spe_context_destroy(spe);
    if (ret) {
        perror("spe_context_destroy");
        exit(1);
    }

    ret = spe_image_close(prog);
    if (ret) {
        perror("spe_image_close");
        exit(1);
    }

    return 0;
}
void *pthread_run_spe(void *arg){
  spe_context_ptr_t spe_ctx;
  context *data = (context *)arg;
  void *argp;
  unsigned int entry;

  spe_ctx = spe_context_create(0, NULL);
  spe_program_load (spe_ctx, &spu_pi);

  entry=SPE_DEFAULT_ENTRY;
  argp=data;

  spe_context_run(spe_ctx, &entry,0,argp,NULL,NULL);
  spe_context_destroy(spe_ctx);

  pthread_exit(NULL);
}
spe_context_ptr_t ps3_assign_context_to_program(spe_program_handle_t *program)
{
  static spe_context_ptr_t      cached_context;
  static spe_program_handle_t  *cached_program;
  static int                    cached_pid;
  
  int current_pid  = getpid();
  int thread_index = 99; /* Todo: get true cruncher index */
  int retval;
  
  if (cached_context)
  {
    if (cached_pid != current_pid)
    {
      Log("!!! FATAL !!! Cached SPE context forked from another pid (%d)\n", cached_pid);
      abort();
    }
    if (cached_program != program)
    {
      // Log("Replacing SPE context because SPE program changed\n");
      if (spe_context_destroy(cached_context))
        Log("Alert SPE%d! spe_context_destroy() failed, errno=%d\n", thread_index, errno);
      cached_context = NULL;
    }
  }
  
  if (cached_context == NULL)
  {
    cached_context = spe_context_create(0, NULL);
    if (cached_context == NULL)
    {
      Log("Alert SPE#%d! spe_context_create() failed\n", thread_index);
      abort();
    }
    retval = spe_program_load(cached_context, program);
    if (retval != 0)
    {
      Log("Alert SPE#%d: spe_program_load() returned %d\n", thread_index, retval);
      abort();
    }
    cached_program = program;
    cached_pid     = current_pid;
  }
  
  return cached_context;
}
Пример #17
0
int SPE_Shutdown(_THIS, spu_data_t * spe_data)
{
	if (spe_data->keepalive && spe_data->booted) {
		SPE_SendMsg(this, spe_data, SPU_EXIT);
		SPE_Stop(this, spe_data);
	}

	
	deprintf(2, "[PS3->SPU] Destroy SPE context: %s\n", spe_data->program_name);
	if (spe_context_destroy(spe_data->ctx)) {
		deprintf(2, "[PS3->SPU] Failed destroying context: %s\n", spe_data->program_name);
		SDL_SetError("[PS3->SPU] Failed destroying context");
		return -1;
	}
	deprintf(2, "[PS3->SPU] SPE shutdown successful: %s\n", spe_data->program_name);
	return 0;
}
Пример #18
0
int CompleteSPEThreads( PpuPthreadData_t *spedata )
{
	// Wait for the SPE threads to end
	if ( pthread_join ( spedata->pthread, NULL ) )
	{
 	 	perror ("Failed joining thread");
  		return -1;
	}

	// Destroy context
	if ( spe_context_destroy ( spedata->spe_ctx ) != 0 )
	{
		perror( "Failed destroying context" );
		return -1;
	}

	return 1;
}
Пример #19
0
int main(int argc, char **argv) {
   int ret;
   spe_context_ptr_t ctx;
   unsigned int entry_point;
   spe_stop_info_t stop_info;
   
   /* Display the EA of the array */
   printf("PPU array location: %#llx\n", 
      (unsigned long long)prime);
   
   /* Create the SPE Context */
   ctx = spe_context_create(0, NULL);
   if (!ctx) {
      perror("spe_context_create");
      exit(1);
   }

   /* Load the program into the context */
   ret = spe_program_load(ctx, &spu_prime_handle);
   if (ret) {
      perror("spe_program_load");
      exit(1);
   }
   
   /* Run the program */
   entry_point = SPE_DEFAULT_ENTRY;
   ret = spe_context_run(ctx, &entry_point, 0, 
      NULL, NULL, &stop_info);
   if (ret < 0) {
      perror("spe_context_run");
      exit(1);
   }
   
   /* Deallocate the context */
   ret = spe_context_destroy(ctx);
   if (ret) {
      perror("spe_context_destroy");
      exit(1);
   }
 
   return 0;
}
Пример #20
0
void *ppu_pthread_function(void *thread_arg) {

	spe_context_ptr_t ctx;
	pointers_t *arg = (pointers_t *) thread_arg;

	/* Create SPE context */
	if ((ctx = spe_context_create (0, NULL)) == NULL) {
                perror ("Failed creating context");
                exit (1);
        }

	/* Load SPE program into context */
	if (spe_program_load (ctx, &ex1_spu)) {
                perror ("Failed loading program");
                exit (1);
        }

        pthread_t mbox_thread;
	if (pthread_create (&mbox_thread, NULL, &mailbox_pthread_function, &ctx))  {
                perror ("Failed creating thread");
                exit (1);
        }

	/* Run SPE context */
	unsigned int entry = SPE_DEFAULT_ENTRY;
	if (spe_context_run(ctx, &entry, 0, arg, (void*)sizeof(pointers_t), NULL) < 0) {  
		perror ("Failed running context");
		exit (1);
	}

	/* Destroy context */
	if (spe_context_destroy (ctx) != 0) {
                perror("Failed destroying context");
                exit (1);
        }

        return NULL;
}
Пример #21
0
//PPU Code
int main(void){
	int retval;
	unsigned int entry_point = SPE_DEFAULT_ENTRY; // Required for continuing
	  //execution, SPE_DEFAULT_ENTRY is the standard starting offset.
	spe_context_ptr_t my_context;
	spe_stop_info_t stopinfo;	
	int stop_counter = 0;

 	spe_callback_handler_register(null_callback, 0x11, SPE_CALLBACK_NEW);

  while(true) {
	  // Create the SPE Context
	  my_context = spe_context_create(SPE_EVENTS_ENABLE|SPE_MAP_PS, NULL);

	  // Load the embedded code into this context
	  spe_program_load(my_context, &spe_program_zero);	
  
    entry_point = SPE_DEFAULT_ENTRY;	

	  do {
		  printf("before running the spu code\n");
		  retval = spe_context_run(my_context, &entry_point, 0, NULL, NULL, &stopinfo);
      /* consume the stop info so we don't get the spu_stop in loop bug */
      spe_stop_info_read(my_context, &stopinfo);
		  stop_counter++;
		  printf("after running the spu code (%d)\n", stop_counter);
      printf("retval = %d\n", retval);
   
      if(retval == 0x10) /* spu_stop(0x10) is sent from the spe when the loop is done */
       {
         break;
       }
	  } while (retval > 0); // Run until exit or error

    spe_context_destroy(my_context);
  }
	printf("finished with computation\n");
}
Пример #22
0
void *spe_code_launch_6(void *data) 
{
//	printf("inside of thread function\n");
	int retval;
	unsigned int entry_point = SPE_DEFAULT_ENTRY; /* Required for continuing 
      execution, SPE_DEFAULT_ENTRY is the standard starting offset. */
	spe_context_ptr_t my_context;
//	printf("before creating context\n");
	/* Create the SPE Context */
	my_context = spe_context_create(SPE_EVENTS_ENABLE|SPE_MAP_PS, NULL);
//	printf("context created\n");
	/* Load the embedded code into this context */
	spe_program_load(my_context, &spe_code);
//	printf("program loaded\n");
	/* Run the SPE program until completion */
	do 
	{	
		retval = spe_context_run(my_context, &entry_point, 0, spe6_Data, 6, NULL);
	} 
	while (retval > 0); /* Run until exit or error */
	spe_context_destroy(my_context);	
	pthread_exit(NULL);
}
Пример #23
0
int main(int argc, char **argv)
{
    int i;
    int ret;

    spe_context_ptr_t spe;
    spe_program_handle_t *prog;
    unsigned int entry;
    spe_stop_info_t stop_info;

    if (argc == 1) {
        fprintf(stderr, "usage: %s <spu_image>\n", argv[0]);
        return -1;
    }

    prog = spe_image_open(argv[1]);
    if (!prog) {
        perror("spe_image_open");
        exit(1);
    }

    spe = spe_context_create(0, NULL);
    if (!spe) {
        perror("spe_context_create");
        exit(1);
    }

    ret = spe_program_load(spe, prog);
    if (ret) {
        perror("spe_program_load");
        exit(1);
    }

    abs_params.ea_in  = (unsigned long) in;
    abs_params.ea_out = (unsigned long) out;
    abs_params.size   = SIZE;

    entry = SPE_DEFAULT_ENTRY;
    ret = spe_context_run(spe, &entry, 0, &abs_params, NULL, &stop_info);
    if (ret < 0) {
        perror("spe_context_run");
        exit(1);
    }

    ret = spe_context_destroy(spe);
    if (ret) {
        perror("spe_context_destroy");
        exit(1);
    }

    ret = spe_image_close(prog);
    if (ret) {
        perror("spe_image_close");
        exit(1);
    }

    for (i = 0; i < SIZE; i++) {
        printf("%5.0f ", i, out[i]);
        if ((i+1) % 4 == 0) printf("\n");
    }

    return 0;
}
Пример #24
0
/* Spu control function */
void * spuCtrlFunc(void *arg) {
	int i;
	SpuThreadData * spu_data;
	struct ctrl_thread_args * ctrlArgs = *((struct ctrl_thread_args **) ctrlArgs);
	
	if ((spu_data = (SpuThreadData *) malloc(sizeof(SpuThreadData))) == NULL) {
		perror("Failed to allocate SPU data\n");
	}

	spu_data->boot_record = ctrlArgs->boot_record;
	spu_data->main_boot_record = ctrlArgs->main_boot_record;

	// initialize threads
	startSpuThreads(ctrlArgs->no_spus, spu_data);
	initJavaThreads(spu_data, MAX_JAVA_SPU_THREADS);

	free(ctrlArgs);

	// register interest in events
	registerEventHandlers(spu_data);

	// wait for the threads to signal that they have started
	waitForSpus(spu_data);

	// transfer runtime code to spus
	loadRuntimeOnSpus(spu_data);

	// wait for SPU to transition to Java runtime
	waitForRuntime(spu_data);

	global_spu_data = spu_data;

	// start support threads
	startSpuSupportThreads(spu_data);

	// signal completion of SPU boot process
	spu_data->boot_record->noSubArchProcs = spu_data->no_spu_threads;
	spu_data->boot_record->subArchBootComplete = 1;

	// now sit waiting for the kill signal
	pthread_mutex_lock(&exit_mutex);
	pthread_cond_wait(&exit_signal, &exit_mutex);
  pthread_mutex_unlock(&exit_mutex);

	/* shutdown other threads */		
	for (i=0; i<spu_data->no_spu_threads; i++) {
		if (pthread_cancel (spu_data->spus[i].boot_thread)) {
			perror("Failed to shutdown Cell SPU thread");
			exit (1);
		}
		
		if (pthread_cancel (spu_data->spus[i].support_thread)) {
			perror("Failed to shutdown Cell SPU support thread");
			exit (1);
		}

		// destroy resources
		spe_event_handler_destroy(spu_data->spus[i].evnt_handler);
		spe_context_destroy(spu_data->spus[i].ctx);
	}
	spe_gang_context_destroy(spu_data->gang);
}
Пример #25
0
void SPEContext::Destroy() {
	if(ptr)
		if(( spe_context_destroy(ptr) ) != 0)
			THROW("Error while destroying context: ", strerror(errno));
}
Пример #26
0
int main(int argc, char **argv)
{
	int i, n, retval, nspus;
	char temp[256];
	struct dirent **spu_files;
	FILE *fh;
	unsigned int one = 1;

	nspus = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, 0);
	printf(" [PPU]: # usable synergistic processing units = %d\n", nspus);

	for (i = 0; i < nspus; ++i) {
		if (NULL == (data[i].id = spe_context_create(0, NULL))) {
			perror("spe_context_create");
			exit(128);
		}

		retval = spe_program_load(data[i].id, &spu_program);
		if (unlikely(retval)) {
			perror("spe_program_load");
			exit(128);
		}

		data[i].argp = (void *)(ull )i;

		retval = pthread_create(&data[i].pthread, NULL,
		                        ppu_pthread_function, &data[i]);
		if (unlikely(retval)) {
			perror("pthread_create");
			exit(128);
		}
	}

	n = scandir("/spu", &spu_files, NULL, alphasort);
	while (n--) {
		if (!strncmp(spu_files[n]->d_name, "spethread", 9)) {
			snprintf(temp, sizeof(temp), "/spu/%s/phys-id",
				 spu_files[n]->d_name);
			if (NULL == (fh = fopen(temp, "r"))) {
				perror("fopen");
				exit(128);
			}

			fgets(temp, 128, fh);
			fclose(fh);

			printf(" [PPU]: context = %s: physical id = %s",
			       spu_files[n]->d_name, temp);
		}
	}
	free(spu_files);

	for (i = 0; i < nspus; ++i) {
		retval = spe_in_mbox_write(data[i].id, &one, 1,
		                           SPE_MBOX_ALL_BLOCKING);
		if (unlikely(1 != retval)) {
			perror("spe_in_mbox_write");
			exit(128);
		}

		retval = pthread_join(data[i].pthread, NULL);
		if (unlikely(retval)) {
			perror("pthread_join");
			exit(128);
		}

		retval = spe_context_destroy(data[i].id);
		if (unlikely(retval)) {
			perror("spe_context_destroy");
			exit(128);
		}
	}

	return 0;
}
Пример #27
0
int main(int argc, char **argv) {
   int i, retval, spus;
   
   /* Determine number of available SPUs */
   spus = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, 0);
   if (argc != 2) {
      printf("Usage: 'ppu_threads <1-%u>'\n", spus);
      exit(1);
   }
   else if ((atoi(argv[1]) < 1) || 
      (atoi(argv[1]) > spus)) {
      printf("Usage: 'ppu_threads <1-%u>'\n", spus);
      exit(1);
   }
   else {   
      spus = atoi(argv[1]);
   }
   
   /* Create a context and thread for each SPU */
   for (i=0; i<spus; i++) {

      /* Create context */
      if ((data[i].speid = spe_context_create(0, NULL)) == NULL)
      {
         perror("spe_context_create");
         exit(1);
      }
      
      /* Load program into the context */
      if ((retval =
          spe_program_load(data[i].speid, &spu_threads)) != 0)
      {
         perror("spe_program_load");
         exit (1);
      }
      
      /* Initialize control block and thread data */
      control_block = i;
      data[i].argp = (void*)control_block;
     
      /* Create thread */
      if ((retval = 
          pthread_create(
              &data[i].pthread,
              NULL,
              &ppu_pthread_function,
              &data[i])) != 0)
      {
         perror("pthread_create");
         exit (1);
      }
   }
   
   /* Wait for the threads to finish processing */
   for (i = 0; i < spus; i++)
   {
      if ((retval = pthread_join(data[i].pthread, NULL)) != 0)
      {
          perror("pthread_join");
          exit (1);
      }

      if ((retval = spe_context_destroy (data[i].speid)) != 0)
      {
          perror("spe_context_destroy");
          exit (1);
      }
   }
   return 0;
}
Пример #28
0
int main(int argc, char** argv) 
{
	double		begin;
	double		end;
	int		errnum;
	size_t		nthread = P;
	size_t		i;
	size_t		nvertex;
	unsigned int	x;		// sent to each SPU
	int		code;		// status;
	unsigned int	reply;		// from SPU
	arg_t		data[nthread];
	param_t		param[nthread] A16;

	argc 		= argc; 	// to silence gcc...
	progname	= argv[0];
	nvertex		= atoi(argv[2]);

	printf("nthread   = %zu\n", nthread);
	printf("nvertex   = %zu\n", nvertex);
	printf("ctx   = %zu\n", sizeof(param_t));
	printf("arg   = %zu\n", sizeof(arg_t));

	begin = sec();

	for (i = 0; i < nthread; ++i) {
		param[i].proc = i;
		param[i].nvertex = nvertex;

		if ((data[i].ctx = spe_context_create (0, NULL)) == NULL) {
			perror ("Failed creating context");
			exit(1);
		}

		if (spe_program_load (data[i].ctx, &dataflow))  {
			perror ("Failed loading program");
			exit(1);
		}

		data[i].arg = &param[i];
		printf("i=%d param=%p\n", i, data[i].arg);

		if (pthread_create (&data[i].pthread, NULL, work, &data[i])) {
			perror ("Failed creating thread");
			exit(1);
		}
	}

	// send some data to each SPU and wait for a reply.

	x = 42;

	for (i = 0; i < nthread; ++i) {
        reply = 0;
        code = spe_out_mbox_read(data[i].ctx, &reply, 1);
        printf("spu-%d reply-0: %u\tcode: %d\n",i, reply, code);
		code = spe_in_mbox_write(data[i].ctx, &x, 1, 1);
        code = spe_out_mbox_read(data[i].ctx, &reply, 1);
        printf("spu-%d reply-1: %u\tcode: %d\n",i, reply, code);
        code = spe_out_mbox_read(data[i].ctx, &reply, 1);
        printf("spu-%d reply-2: %u\tcode: %d\n",i, reply, code);
	}

	end = sec();

	printf("%1.3lf s\n", end-begin);

	for (i = 0; i < nthread; ++i) {
		printf("joining with PPU pthread %zu...\n", i);
		errnum = pthread_join(data[i].pthread, NULL);
		if (errnum != 0)
			syserror(errnum, "pthread_join failed");

		if (spe_context_destroy (data[i].ctx) != 0) {
			perror("Failed destroying context");
			exit(1);
		}
	}

	return 0;
}
Пример #29
0
float calc_integral(float start, float end, float delta)
{
    int i;
    int ret;
    float sum = 0.0f;

    spe_program_handle_t *prog;
    spe_context_ptr_t spe[NUM_SPE];
    pthread_t thread[NUM_SPE];
    thread_arg_t arg[NUM_SPE];

    prog = spe_image_open("integral_spe.elf");
    if (!prog) {
        perror("spe_image_open");
        exit(1);
    }

    for (i = 0; i < NUM_SPE; i++) {
        spe[i] = spe_context_create(0, NULL);
        if (!spe) {
            perror("spe_context_create");
            exit(1);
        }

        ret = spe_program_load(spe[i], prog);
        if (ret) {
            perror("spe_program_load");
            exit(1);
        }
    }

    for (i = 0; i < NUM_SPE; i++) {
        integral_params[i].start = start + (end-start)/NUM_SPE * i;
        integral_params[i].end   = start + (end-start)/NUM_SPE * (i+1);
        integral_params[i].delta = delta;
        integral_params[i].sum   = 0.0f;

        arg[i].spe = spe[i];
        arg[i].integral_params = &integral_params[i];

        ret = pthread_create(&thread[i], NULL, run_integral_spe, &arg[i]);
        if (ret) {
            perror("pthread_create");
            exit(1);
        }
    }

    for (i = 0; i < NUM_SPE; i++) {
        pthread_join(thread[i], NULL);
        ret = spe_context_destroy(spe[i]);
        if (ret) {
            perror("spe_context_destroy");
            exit(1);
        }
    }

    ret = spe_image_close(prog);
    if (ret) {
        perror("spe_image_close");
        exit(1);
    }

    for (i = 0; i < NUM_SPE; i++) {
        printf("[PPE] sum = %f\n", integral_params[i].sum);
        sum += integral_params[i].sum;
    }

    return sum;
}
Пример #30
0
int main( int argc, char *argv[] )
{
   int i, j, dummy;
   int tmi, tmj;
   pthread_t         threads     [ NUM_THREADS ];
   spe_context_ptr_t spe_contexts[ NUM_THREADS ];
   thread_args_t     thread_args [ NUM_THREADS ];
   
   int rows;
   dummy = argc; dummy = (int)argv;
   
   // initialize initial & final matrix
   
   for(i = 0; i < tsize; i++)
   {
      for(j = 0; j < tsize; j++)
      {
	 tmi = tsize-i;
	 tmj = tsize-j;
	 Amatrix[i][j] = 3*tmi+tmj ;
	 Bmatrix[i][j] = 3*tmi+tmj ;
	 Cmatrix[i][j] = 0 ;
	 Dmatrix[i][j] = 0 ;
      }
   }
   
   // perform multiply
   printf( "SPE: Multiply \n");
   
   gettimeofday( &time0, &tzone );
   
   // start jobs
   
   rows = ((tsize/32)+NUM_THREADS-1)/NUM_THREADS ;
   // determine amount of work each spe should do
   for (i = 0; i < NUM_THREADS; i++ )
   {   
      // set arguments
      args.Amat   = (float (*)[tsize][tsize])Amatrix ;
      args.Bmat   = (float (*)[tsize][tsize])Bmatrix ;
      args.Cmat   = (float (*)[tsize][tsize])Cmatrix ;
      args.i_initial = i*rows ;
      
      spe_contexts[i] = spe_context_create( 0, NULL ); // (flags, gang)
      spe_program_load( spe_contexts[i], &multiply_spu );
      
      thread_args[i].spe_context = spe_contexts[i];
      thread_args[i].argp = &args ;
      thread_args[i].envp = NULL;
      
      pthread_create( &threads[i], NULL, &spe_thread, &thread_args[i] );  
   }
	
   // wait for tasks to complete
   for (i = 0; i < NUM_THREADS; i++)
   {
      pthread_join( threads[i], NULL ); 
   }        // wait for threads
   
   gettimeofday( &time1, &tzone );
   
   // print time to complete
   sec  = time1.tv_sec  - time0.tv_sec ;
   usec = time1.tv_usec - time0.tv_usec ;
   if ( usec < 0 )
   {
      sec--;
      usec+=1000000 ;
   }

   printf(
      "SPE: Multiply Done -- matrix[%d][%d]: time=%d.%06d\n",
      tsize,
      tsize,
      sec,
      usec);
      
   for (i = 0; i < NUM_THREADS; i++)
   {
      spe_context_destroy( spe_contexts[i] );
   }  // destroy threads
	
   // Check for correctness of final matrix
   {
      int error, i, j, k ;

      for(i = 0; i < tsize; i++)
      {
	 for(j = 0; j < tsize; j++)
	 {
	    for(k = 0; k < tsize; k++)
	    {
	       Dmatrix[i][j] += Amatrix[i][k] * Bmatrix[k][j] ;
	       
	    }
	 }
      }

      error = 0;

      for(i = 0; i < tsize; i++)
      {
	 for(j = 0; j < tsize; j++)
	 {
	    if ( Cmatrix[i][j] != Dmatrix[i][j] )
	       error = 1 ;
	 }
      }

      if (error)
      {
	 printf("Error in Multiply.\n");
      }
      else
      {
	 printf("Multiply is correct.\n"); fflush(stdout);
      }
   }

   return 0;
}