    /***********************************************************************
                   Copyright (c) 2007 SKY Computers, Inc.
    
     Redistribution and use in source and binary forms are permitted
     provided that this notice is preserved and that due credit is given to
     SKY Computers, Inc. The name of SKY Computers, Inc. may not be used to
     endorse or promote products derived from this software without
     specific prior written permission. This software is provided ``as is''
     without express or implied warranty.
    ************************************************************************/

#include "sky_ex_inc.h"
#include "re_tcb.h"

/*
 *  TimeTrac Externals -- 
 *      Do the following if you want the TimeTrac variables to
 *      disappear when the TimeTrac function calls disappear
 *      (see the Makefile).
 */
#ifdef TIME_TRAC
extern TTHandle Trace_Log[NUM_THREADS];

extern TTEventHandle tt_hello_bgn[NUM_THREADS], tt_hello_end[NUM_THREADS];
extern TTEventHandle tt_wait_bgn, tt_wait_end;
extern TTEventHandle tt_vsum_bgn, tt_vsum_end;
#endif

/* ???? */
#define BUG   (YES)

/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *   int compute_sum (TCB *tcb):
 *       TCB *t: Task block defining this thread.
 *
 *       This is the compute thread entry point for the "sum" child threads.
 *       After startup, it waits for commands from the main thread.
 *   
 *       Returns: Does not.
 >--
 */

int compute_sum (TCB *tcb)
{
  int i;                         /* misc loop counter */
  int pass;                      /* counts number of computes we have done */
  int bug = BUG;
  float *src[NUM_CO_THREADS];
  float *dst0;
    
  /* The TimeTrac parameters were set up in th_main(). */
    
  printf ("%2d -- Thread starting.\n", tcb->Rank);
  fflush (stdout);
  
  /* Allocate storage for the arrays. */
  for (i = 0; i < NUM_CO_THREADS; i++)
    {
      src[i] = (float *) ex_mem_alloc (tcb->Rank, V_SIZE * sizeof (float), 64, "Array Src");
    }
  dst0 = src[0];
  
  /* We are now running. */
  tcb->Status = STAT_RUN;
    
  /* Process commands from the main thread. */
  for (pass = 0; ; pass++)
    {
      /* Wait for the next command to run or exit. */
      time_trac_record (Trace_Log[tcb->Rank], tt_wait_bgn, (float)tcb->Pass);
      while (tcb->Comm != COMM_COMP)
	{
	  if (tcb->Comm == COMM_EXIT)
	    break;
	  
	  sched_yield ();
	}
      time_trac_record (Trace_Log[tcb->Rank], tt_wait_end, (float)tcb->Pass);

      if (tcb->Comm == COMM_EXIT)
	break;
	
      time_trac_record (Trace_Log[tcb->Rank], tt_vsum_bgn, (float)tcb->Pass);
      for (i = 1; i < NUM_CO_THREADS; i++)
	{
	  vsum (dst0, src[i], dst0, V_SIZE);
	}
      time_trac_record (Trace_Log[tcb->Rank], tt_vsum_end, (float)tcb->Pass);
	
      /* We are done, give another thread a chance to execute. */
      tcb->Comm = COMM_NONE;
      tcb->Status = STAT_DONE;
      sched_yield ();
    }

  /* Inform the master thread we are done. */
  tcb->Status = STAT_EXIT;
    
  /* Thread is done, exit when prent exits. */
  printf ("%2d -- Thread waiting for exit.\n", tcb->Rank);
  while (1)
    sleep (1);
}

/* --------------------------- End of Module ---------------------------- */

