view ppe/Manager.cc @ 3:8b4d6bf8c43d

add entry_head function
author yutaka@localhost.localdomain
date Wed, 07 Apr 2010 17:35:34 +0900
parents 1e1b0d280427
children b25035e3282d
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <libspe2.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include "Manager.h"
#include "MailCommand.h"
#include "SIZE.h"
#include "Sys.h"
#include "Spe.h"
#include "Menu.h"
#include "Run.h"
#include "params.h"

void
ppe_run(task_head_t *head, int tmplist_num)
{

  task_head_t *tmphead;
  task_t *tmp = (task_t*)allocate(sizeof(task_t));
  
  //printf("hoge1\n");

  for (int j = 0; j < tmplist_num; j++) {

    //printf("list_num %d\n",tmplist_num);

    for (tmphead = (task_head_t*)head[j].next_task; tmphead != 0; tmphead = (task_head_t*)tmphead->next_task) {

      fix_type(tmp, tmphead);

      int *out = (int*)tmp->head->ea_out;
      int *in = (int*)tmp->input;

      int size_in  = tmp->head->size_in;
      int size_out = tmp->head->size_out;

      //printf("in[%d] %f\n",i,in[i]);
      //printf("out[%d] %f\n",i,out[i]);
     
      //printf("size->out %d\n",tmp->head->size_out);
 
      run(in, out, size_in, size_out);
	
      //printf("out_spe[%d] %f\n",i,out_spe[i]);
      //printf("out[%d] %f\n",i,out[i]);
      

    }
    
  }

}

void
check_output(task_head_t *head, int tmplist_num)
{

  task_head_t *tmphead;
  task_t *tmp = (task_t*)allocate(sizeof(task_t));
  
  for (int j = 0; j < tmplist_num; j++) {

    for (tmphead = (task_head_t*)head[j].next_task; tmphead != 0; tmphead = (task_head_t*)tmphead->next_task) {

      fix_type(tmp, (void*)tmphead);

      int *out = (int*)tmp->head->ea_out;
      int *out_spe = (int*)allocate(tmp->head->size_out);
      int *in = (int*)tmp->input;

      int size_in  = tmp->head->size_in;
      int size_out = tmp->head->size_out;

      //printf("in[%d] %f\n",i,in[i]);
      //printf("out[%d] %f\n",i,out[i]);
      
      run(in, out_spe, size_in, size_out);
	
      //printf("out_spe[%d] %f\n",i,out_spe[i]);
      //printf("out[%d] %f\n",i,out[i]);

      for (int i = 0; i < tmp->head->size_out / sizeof(int); i++) {
	
	printf("out_spe[%d] %d\n",i,out_spe[i]);

	if (out_spe[i] != out[i]) {
	  printf("!\n");
	}

      }
      
      free(out_spe);

    }
    
  }

}

void
send_task_params(int &list_num, int &index, task_head_t *head,
		 spe_context_ptr_t spe, int &flag)
{

  unsigned int data = 0;

  if ( list_num ) {
    
    data = (unsigned int) &head[index];
    
    //send task param mail 
    spe_in_mbox_write(spe,&data,1,SPE_MBOX_ALL_BLOCKING);
    
    list_num--;
    index++;

  } else { //task nothing, spe run end
    data = SPE_RUN_END;
    flag += 1;
    //send run end mail
    spe_in_mbox_write(spe,&data,1,SPE_MBOX_ALL_BLOCKING);  
    //printf("send run end mail\n");
  }
  
}


int
main(int argc, char *argv[])
{

  clock_t prog_s = 0;
  clock_t prog_e = 0;

  prog_s = clock();

  clock_t create_s = 0;
  clock_t create_e = 0;
  clock_t run_s = 0;
  clock_t run_e = 0;

  int opt;
  int check_flag = 0;
  int time_flag = 0;
  int ppe_flag = 0;

  while ( (opt = getopt(argc, argv, "ctp")) != -1) {
    switch( opt ) {
    case 'c' : check_flag = 1; break;
    case 't' : time_flag = 1; break;
    case 'p' : ppe_flag = 1; break;
    }
  }

  if (ppe_flag) {

    int list_num;
    task_head_t *head = menu(list_num);
    ppe_run(head, list_num);

    prog_e = clock();
    
    if (time_flag) {
      printf("program_run_time %f\n",(double)(prog_e - prog_s)/CLOCKS_PER_SEC);
    }

    return 0;

  }

  spe_program_handle_t *prog;
  //スタック上にとるのはだめだめ。malloc
  spe_context_ptr_t spe[SPE_NUM];
  pthread_t thread[SPE_NUM];

  prog = spe_image_open("spe-main");
  //thread run. spe run.
  spe_run_start(spe,prog,thread,SPE_NUM);

  int all_spe_run_end = SPE_NUM;
  int flag = 0;

  int list_num = 0;
  create_s = clock();
  task_head_t *head = menu(list_num);
  create_e = clock();
  int tmplist_num = list_num;

  int index = 0;
  unsigned int data = 0;

  run_s = clock();

  //mail check
  while(flag != all_spe_run_end) {
    
    for (int i = 0; i < SPE_NUM; i++) {
      if (spe_out_mbox_status(spe[i])) {
	spe_out_mbox_read(spe[i],&data,1);

	//spe wait come task
	//ppe send task or run end request mail for spe
	if (data == WANT_DATA) {
	  //printf("spe %p\n",spe[i]);
	  send_task_params(list_num, index, head, spe[i], flag);
	}

      }

    }// end for
 
  }//end while mail check

  spe_run_endwait(spe,thread,SPE_NUM);
  spe_image_close(prog);

  run_e = clock();

  if (check_flag) {
    printf("check now\n");
    check_output(head, tmplist_num);
  }

  prog_e = clock();

  if (time_flag) {
    printf("create_task_time %f\n",(double)(create_e - create_s)/CLOCKS_PER_SEC);
    printf("run_calc_time %f\n",(double)(run_e - run_s)/CLOCKS_PER_SEC);
    printf("program_run_time %f\n",(double)(prog_e - prog_s)/CLOCKS_PER_SEC);
  }

  //head[0]

  return 0;

}

/*end*/