/***********************************************************************
 * rw.c -- implement I/O in separate thread
 *
 * Author: Mark Hays <hays@math.arizona.edu>
 *
 * NOTE: see the notes in rwf.f for warnings/restrictions.
 *
 */

#include "pt.h"

#include <stdio.h>
#include <stdlib.h>

/* global data we'll pass around */
typedef struct _rw_info_t {
  float *buffer,*maindata;
  int count;
} rw_info_t;

/* master does this before slave executes */
void *setup(rw_info_t *info)
{
  float *src=info->maindata,*dst=info->buffer;
  int n=info->count,i;

  /* copy main program's data info I/O buffer */
  for (i=0; i<n; i++) *dst++=*src++;
}

/* slave runs this */
void *doio(rw_info_t *info)
{
  float *buf=info->buffer;
  int n=info->count,i;

  for (i=0; i<n; i++) printf("%12.4f ",*buf++);
  printf("\n");
}

#define N 4

int main(int argc,char *argv[])
{
  int i,j;
  float *data;
  rw_info_t info;
  pt_pipeline_t p;

  /* initialize info structure */
  info.buffer=(float *) malloc(N*sizeof(float));
  info.maindata=data=(float *) malloc(N*sizeof(float));
  info.count=N;

  /* initial data */
  for (j=0; j<N; j++) data[j]=j+1;

  /* set up the pipeline */
  pt_pipeline_init(&p,     /* pt_pipeline_t struct */
		   &info,  /* global (shared) data */
		   setup,  /* stage setup routine */
		   doio);  /* slave's code */
  /* main code */
  for (i=0; i<5; i++) {
    for (j=0; j<N; j++) data[j]*=2;
    pt_pipeline_execute(&p);
  }
  /* clean up and exit */
  pt_pipeline_destroy(&p);
  return(0);
}

/* EOF rw.c */

