view spe/SpeSentry.cc @ 0:68a98d68d62a

commit
author yutaka@localhost.localdomain
date Sun, 28 Mar 2010 19:47:00 +0900
parents
children dcd83cefb980
line wrap: on
line source

#include <stdio.h>
#include <malloc.h>
#include <spu_intrinsics.h>
#include <spu_mfcio.h>
#include "SpeSentry.h"
#include "MailCommand.h"
#include "Dma.h"
#include "Run.h"

void*
fix_type(send_params_t *send_params, void* buff)
{

  char *p;
  p = (char*)buff;
  p = p + sizeof(send_params_head_t);

  send_params->head = (send_params_head_t*)buff;
  send_params->buff = p;

}

void*
allocate(int size)
{

  int alignment = 16;
  return memalign(alignment, size);

}

send_params_t
task_allocate(int size)
{

  if (size % 16) {
    //printf("allocate size is not multiple of 16\n");
  }

  char *buff;
  send_params_t send_params;

  void *head;
  head = memalign(16, size);

  buff = (char*)head;
  buff = buff + sizeof(send_params_head_t);

  send_params.head = (send_params_head_t*)head;
  send_params.buff = buff;

  return send_params;

}

int
main(unsigned long long spe, unsigned long long argp, unsigned long long envp)
{

  uint32_t data;
  int buff_num = 2;
  int dma_wait_time = 0;

  int in_tag[buff_num];
  int out_tag[buff_num];

  in_tag[0] = 0;
  in_tag[1] = 1;

  out_tag[0] = 2;
  out_tag[1] = 3;

  int head_tag = 4;

  send_params_head_t *head   = (send_params_head_t*)allocate(sizeof(send_params_head_t));
  send_params_t *send_params = (send_params_t*)allocate(sizeof(send_params_t));
  void *input_buff[buff_num];
  void *output_buff[buff_num];

  int cur;

  spu_write_out_mbox(WANT_DATA);

  while(1) {

    //mail check start
    if(spu_stat_in_mbox()) {

      data = spu_read_in_mbox();
      
      if (data == SPE_RUN_END) {

	break;

      } else {

	//printf("[SPE] data %p\n",data);

	dma_get(head, data, sizeof(send_params_head_t), head_tag);
	dma_wait(head_tag);

	input_buff[0] = allocate(head->next_task_size);

	dma_get(input_buff[0], head->next_task, head->next_task_size, in_tag[0]);
	dma_wait(in_tag[0]);

	fix_type(send_params,input_buff[0]);

	cur = 0;

	while(1) {

	  input_buff[cur^1] = allocate(send_params->head->next_task_size);	      
	  dma_get(input_buff[cur^1], send_params->head->next_task, send_params->head->next_task_size, in_tag[cur^1]);
	  
	  void *in  = send_params->buff;

	  //printf("hoge");
	  output_buff[cur] = allocate(send_params->head->size_out);
	  int data_length = send_params->head->size_out;

	  
	  //task********************************************
	  
	  run(in,output_buff[cur],data_length);
	  
	  //end*********************************************
	  
	  dma_put(output_buff[cur], send_params->head->ea_out, send_params->head->size_out, out_tag[cur]);

	  free(input_buff[cur]);

	  dma_wait(in_tag[cur^1]);
	  dma_wait(out_tag[cur^1]);
	  //printf("hoge");

	  free(output_buff[cur^1]);

	  cur ^= 1;

	  fix_type(send_params,input_buff[cur]);

	  if ((unsigned long)send_params->head->next_task == 0) {
	    break;
	  }
	
	}//while
 



	  void *in  = send_params->buff;

	  //printf("hoge");
	  output_buff[cur] = allocate(send_params->head->size_out);
	  int data_length = send_params->head->size_out;

	  
	  //task********************************************
	  
	  run(in,output_buff[cur],data_length);
	  
	  //end*********************************************
	  
	  dma_put(output_buff[cur], send_params->head->ea_out, send_params->head->size_out, out_tag[cur]);

	  free(input_buff[cur]);

	  dma_wait(out_tag[cur] | out_tag[cur^1] );
	  free(output_buff[cur]);
	  free(output_buff[cur^1]);


 
	  spu_write_out_mbox(WANT_DATA);
	  //printf("[SPE] send mail\n");
	  
      }//if
    }//if
    //mail check end


  }//while


  return 0;

}


/*end*/