view test/scheduler/test_sched.cbc @ 1:aef83aed7a07

scheduler test success.
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Sun, 20 Dec 2009 20:46:53 +0900
parents 5b089096921f
children 803d6bf22e6d
line wrap: on
line source

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

#include "Task.h"
#include "TaskScheduler.h"

#define __DEBUG(f, args...) \
	fprintf(stderr, "in %s: "f, __FUNCTION__, ## args)

__code task0(Scheduler scheduler, void *rbuff, void *wbuff);
__code task1 (Scheduler scheduler, void *rbuff, void *wbuff);
__code task2 (Scheduler scheduler, void *rbuff, void *wbuff);
int main (int argc, char **argv);
__code startcode (void *arg);
__code checkNewCode ();
__code exitCode ();

void * allocate (size_t size);

#define NUM 3
__code
task0(Scheduler scheduler, void *rbuff, void *wbuff)
{
	int i,j;
	double *in = rbuff;
	double *out = wbuff;
	double a[NUM][NUM] = {
		{ 1.0, 0.0, 0.0 },
		{ 0.0, 1.0, 0.0 },
		{ 0.0, 0.0, 1.0 },
	};
	for (i=0; i<NUM; i++) {
		out[i] = 0.0;
		for (j=0; j<NUM; j++) {
			out[i] += a[i][j] * in[j];
		}
	}
	__DEBUG("\n");
	__DEBUG("%p %p\n", rbuff, wbuff);
	__DEBUG("%3.2lf %3.2lf %3.2lf\n", in[0],in[1],in[2]);
	__DEBUG("%3.2lf %3.2lf %3.2lf\n", out[0],out[1],out[2]);
	//goto task1(scheduler, wbuff, rbuff);
	goto scheduler(task1, wbuff, rbuff);
}
__code
task1(Scheduler scheduler, void *rbuff, void *wbuff)
{
	int i,j;
	double *in = rbuff;
	double *out = wbuff;
	double a[NUM][NUM] = {
		{ 1.0, 0.0, 0.0 },
		{ 0.0, 2.0, 0.0 },
		{ 0.0, 0.0, 3.0 },
	};
	for (i=0; i<NUM; i++) {
		out[i] = 0.0;
		for (j=0; j<NUM; j++) {
			out[i] += a[i][j] * in[j];
		}
	}
	__DEBUG("\n");
	__DEBUG("%p %p\n", rbuff, wbuff);
	__DEBUG("%3.2lf %3.2lf %3.2lf\n", in[0],in[1],in[2]);
	__DEBUG("%3.2lf %3.2lf %3.2lf\n", out[0],out[1],out[2]);
	//goto task2(scheduler, wbuff, rbuff);
	goto scheduler(task2, wbuff, rbuff);
}
__code
task2(Scheduler scheduler, void *rbuff, void *wbuff)
{
	int i,j;
	double *in = rbuff;
	double *out = wbuff;
	double a[NUM][NUM] = {
		{ 1.0, 0.0, 0.0 },
		{ 0.0, 1.0, 0.0 },
		{ 0.0, 0.0, 1.0 },
	};
	for (i=0; i<NUM; i++) {
		out[i] = 0.0;
		for (j=0; j<NUM; j++) {
			out[i] += a[i][j] * in[j];
		}
	}
	__DEBUG("\n");
	__DEBUG("%p %p\n", rbuff, wbuff);
	__DEBUG("%3.2lf %3.2lf %3.2lf\n", in[0],in[1],in[2]);
	__DEBUG("%3.2lf %3.2lf %3.2lf\n", out[0],out[1],out[2]);
	goto scheduler(END, rbuff, wbuff);
}


int
main(int argc, char **argv)
{
	__DEBUG("\n");
	goto initScheduler(startcode, NULL);

	return 0;
}

__code
startcode(void *arg) {
	double *readbuff, *writebuff;
	readbuff = malloc(sizeof(double)*NUM);
	writebuff = malloc(sizeof(double)*NUM);

	readbuff[0] = 2.00;
	readbuff[1] = 3.00;
	readbuff[2] = 4.00;
	__DEBUG("id=0 added.\n");
	goto addCode(0, task0, readbuff, writebuff);
}

__code
checkNewCode(int wait)
{
	static int t = 0;
	double *readbuff, *writebuff;
	readbuff = malloc(sizeof(double)*NUM);
	writebuff = malloc(sizeof(double)*NUM);

	if (wait) {
		__DEBUG("all tasks finished\n");
		exit(0);
	}

	if (t==0) {
		t++;
		__DEBUG("id=1 added.\n");
		goto addCode(1, task0, readbuff, writebuff);
	}
	__DEBUG("no code added.\n");
	goto selectCode();
}

__code
exitCode(int id)
{
	__DEBUG("exit task%d\n", id);
	goto selectCode();
}

void *
allocate(size_t size)
{
	void *rtn = malloc (size);
	if (!rtn) {
		perror("malloc");
		exit(1);
	}
	return rtn;
}