changeset 1171:d910c8377a09 draft

add prime example
author Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
date Sat, 04 Jun 2011 17:26:14 +0900
parents 693e58f4a672
children 0e826d18f59a
files example/Prime/Func.h example/Prime/Makefile example/Prime/Makefile.cell example/Prime/Makefile.def example/Prime/Makefile.linux example/Prime/Makefile.macosx example/Prime/README example/Prime/main.cc example/Prime/ppe/.#Hello.cc example/Prime/ppe/Prime.cc example/Prime/ppe/Prime.h example/Prime/ppe/PrintTask.cc example/Prime/ppe/PrintTask.h example/Prime/ppe/task_init.cc example/Prime/spe/Makefile example/Prime/spe/Prime.cc example/Prime/spe/Prime.h example/Prime/spe/PrintTask.cc example/Prime/spe/PrintTask.h example/Prime/spe/spe-main.cc
diffstat 20 files changed, 455 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/Func.h	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,5 @@
+enum {
+#include "SysTasks.h"
+	Prime,
+	PrintTask,
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/Makefile	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,24 @@
+default: macosx
+
+macosx: FORCE
+	@echo "Make for Mac OS X"
+	@$(MAKE) -f Makefile.macosx
+
+fifo64: FORCE
+	@echo "Make for Mac OS X 64bit mode"
+	@$(MAKE) -f Makefile.macosx ABIBIT=64
+
+linux: FORCE
+	@echo "Make for Linux"
+	@$(MAKE) -f Makefile.linux
+
+cell: FORCE
+	@echo "Make for PS3 (Cell)"
+	@$(MAKE) -f Makefile.cell
+
+FORCE:
+
+clean:
+	@$(MAKE) -f Makefile.macosx clean
+	@$(MAKE) -f Makefile.linux clean
+	@$(MAKE) -f Makefile.cell clean
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/Makefile.cell	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,45 @@
+include ./Makefile.def
+
+ABIBIT=32
+CFLAGS += -m$(ABIBIT)  -D__CERIUM_CELL__
+
+SRCS_TMP = $(wildcard *.cc)
+SRCS_EXCLUDE =  # 除外するファイルを書く
+SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP))
+OBJS = $(SRCS:.cc=.o)
+
+TASK_DIR  = ppe
+TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc)
+TASK_SRCS_EXCLUDE = 
+TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP))
+TASK_OBJS = $(TASK_SRCS:.cc=.o)
+
+LIBS += -lCellManager -lspe2 -lpthread -Wl,--gc-sections 
+
+.SUFFIXES: .cc .o
+
+.cc.o:
+	$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
+
+all: $(TARGET) speobject
+
+$(TARGET): $(OBJS) $(TASK_OBJS)
+	$(CC) $(CFLAGS) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS)
+
+speobject:
+	cd spe; $(MAKE) ABIBIT=$(ABIBIT)
+
+run:
+	./$(TARGET) -cpu 6 
+
+link:
+	$(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS)
+
+debug: $(TARGET)
+	sudo ppu-gdb ./$(TARGET) 
+
+clean:
+	rm -f $(TARGET) $(OBJS) $(TASK_OBJS)
+	rm -f *~ \#*
+	rm -f ppe/*~ ppe/\#*
+	cd spe; $(MAKE) clean
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/Makefile.def	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,15 @@
+TARGET = prime
+
+# include/library path
+# ex  macosx
+#CERIUM = /Users/gongo/Source/Cerium
+
+# ex  linux/ps3
+CERIUM = ../../../Cerium
+
+CC      = g++ -m32
+CFLAGS  = -O9 -Wall
+#CFLAGS  = -g -Wall
+
+INCLUDE = -I${CERIUM}/include/TaskManager -I. -I..
+LIBS = -L${CERIUM}/TaskManager
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/Makefile.linux	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,36 @@
+include ./Makefile.def
+
+SRCS_TMP = $(wildcard *.cc)
+SRCS_EXCLUDE =  # 除外するファイルを書く
+SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP))
+OBJS = $(SRCS:.cc=.o)
+
+TASK_DIR  = ppe
+TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc)
+TASK_SRCS_EXCLUDE = 
+TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP))
+TASK_OBJS = $(TASK_SRCS:.cc=.o)
+
+LIBS += -lFifoManager
+
+.SUFFIXES: .cc .o
+
+.cc.o:
+	$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
+
+all: $(TARGET)
+
+$(TARGET): $(OBJS) $(TASK_OBJS)
+	$(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS)
+
+link:
+	$(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS)
+
+debug: $(TARGET)
+	sudo gdb ./$(TARGET) 
+
+clean:
+	rm -f $(TARGET) $(OBJS) $(TASK_OBJS)
+	rm -f *~ \#*
+	rm -f ppe/*~ ppe/\#*
+	rm -f spe/*~ spe/\#*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/Makefile.macosx	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,36 @@
+include ./Makefile.def
+
+SRCS_TMP = $(wildcard *.cc)
+SRCS_EXCLUDE =  # 除外するファイルを書く
+SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP))
+OBJS = $(SRCS:.cc=.o)
+
+TASK_DIR  = ppe
+TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc)
+TASK_SRCS_EXCLUDE = 
+TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP))
+TASK_OBJS = $(TASK_SRCS:.cc=.o)
+
+LIBS += -lFifoManager `sdl-config --libs`
+
+.SUFFIXES: .cc .o
+
+.cc.o:
+	$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
+
+all: $(TARGET)
+
+$(TARGET): $(OBJS) $(TASK_OBJS)
+	$(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS)
+
+link:
+	$(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS)
+
+debug: $(TARGET)
+	sudo gdb ./$(TARGET) 
+
+clean:
+	rm -f $(TARGET) $(OBJS) $(TASK_OBJS)
+	rm -f *~ \#*
+	rm -f ppe/*~ ppe/\#*
+	rm -f spe/*~ spe/\#*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/README	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,15 @@
+- 概要
+
+指定された範囲の素数を出力するプログラムです。
+
+- 実行方法
+
+% ./prime [-num number]
+
+  -num		出力する素数の範囲   
+
+- 実行例 (-cpu は Cerium 標準のオプションです)
+
+% ./prime -num 100 -cpu 6
+0 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/main.cc	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "TaskManager.h"
+#include "Func.h"
+
+/* task_initを宣言 */
+extern void task_init(void);
+
+/* TaskManagerを宣言 */
+extern TaskManager *manager;
+
+static int prime_num = 256;			/* 素数を出力する範囲 */
+
+/* help文章 */
+const char *usr_help_str = "Usage: ./prime [-cpu spe_num] [-num N]\n\
+							-cpu    Number of SPE (default 1) \n\
+							-num 	Print of Prime scope (default 256) ";
+
+	int
+init(int argc, char **argv)
+{
+	for (int i = 1; argv[i]; ++i) {
+		if (strcmp(argv[i], "-num") == 0) {
+			prime_num = atoi(argv[++i]);
+		}
+	}
+	return 0;
+}
+
+	void
+prime_init(TaskManager *manager)
+{
+
+	int div_size = 256;			/* 分割するサイズ */
+	int task_num = (prime_num + div_size - 1) / div_size;	/* タスクの数 */
+
+	int *output = (int*)manager->allocate(sizeof(int)*task_num*div_size); /* 判定結果を収める配列 */
+
+	/* 出力用のタスク */
+	HTask *print = manager->create_task(PrintTask);
+
+	/* 計算したタスクの数だけタスクを生成する */
+	for (int i = 0; i < task_num; i++) {
+
+		/* 素数計算タスク */
+		HTask *prime = manager->create_task(Prime);
+
+		/* 判定結果を収める配列を渡す */
+		prime->set_outData(0,&output[i*div_size],sizeof(int)*div_size);
+
+		/* SPEを順に使う */
+		prime->set_cpu(SPE_ANY);
+
+		/* 素数判定の範囲を渡す */
+		prime->set_param(0,(memaddr)(i*div_size));		/* 開始地点 */
+		prime->set_param(1,(memaddr)((i+1)*div_size));	/* 終了地点 */
+
+		/* 出力用タスクの依存関係 */
+		print->wait_for(prime);
+
+		/* タスクを登録 */
+		prime->spawn();
+	}
+
+	/* 出力用のタスクに判定結果を収めた配列を渡す */ 
+	print->set_inData(0,output,sizeof(int)*prime_num);
+	/* 出力する数を渡す */
+	print->set_param(0,(memaddr)prime_num);
+	/* PPEを使うように指示 */
+	print->set_cpu(CPU_PPE);
+	/* タスクを登録 */
+	print->spawn();
+}
+
+	int
+TMmain(TaskManager *manager, int argc, char *argv[])
+{
+	if (init(argc, argv) < 0) {
+		return -1;
+	}
+
+	task_init();
+	prime_init(manager);
+
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/ppe/.#Hello.cc	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,1 @@
+e065746@nakasone-hiroki-no-macbook.local.1456
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/ppe/Prime.cc	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <math.h>
+#include "SchedTask.h"
+#include "Prime.h"
+#include "Func.h"
+
+SchedDefineTask1(Prime, prime);
+
+	static int
+prime(SchedTask *smanager, void *rbuf, void *wbuf)
+{
+	int start = (int)smanager->get_param(0);	/* 素数判定の開始地点 */
+	int end   = (int)smanager->get_param(1);	/* 素数判定の終了地点 */
+	int range = end - start;					/* 判定する範囲		  */
+
+	/* 判定結果を収める配列を受け取る */
+	int *output = (int*)smanager->get_output(wbuf, 0);
+
+	/* 初期化 */
+	for (int i = 0; i < range; i++){
+		output[i] = 1;
+	}
+
+
+	for (int i = start, index = 0; i < end; i++, index++) {
+		int limit = (int)sqrt((double) i);		/* 割る数の最大値を求める */
+		for (int j = 2; j <= limit; j++) {
+			/* 割り切れた場合、0を代入し素数じゃないという判定を下す */
+			if (i % j == 0) {
+				output[index] = 0;
+				break;
+			}
+		}
+	}
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/ppe/Prime.h	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,6 @@
+#ifndef INCLUDED_TASK_PRIME
+#define INCLUDED_TASK_PRIME
+
+#include "SchedTask.h"
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/ppe/PrintTask.cc	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include "SchedTask.h"
+#include "PrintTask.h"
+#include "Func.h"
+
+SchedDefineTask1(PrintTask, print);
+
+	static int
+print(SchedTask *smanager, void *rbuf, void *wbuf)
+{
+	int length = (int)smanager->get_param(0);			/* 出力する範囲 */
+	int *input = (int*)smanager->get_input(rbuf, 0);	/* 出力する配列 */
+
+	/* 素数の判定結果が1ならば出力する */
+	for (int i = 0; i < length; i++) {
+		if ( input[i] == 1 ) {
+			printf("%d ",i);
+		}
+	}
+	printf("\n");
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/ppe/PrintTask.h	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,6 @@
+#ifndef INCLUDED_TASK_PRINTTASK
+#define INCLUDED_TASK_PRINTTASK
+
+#include "SchedTask.h"
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/ppe/task_init.cc	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,12 @@
+#include "Func.h"
+#include "Scheduler.h"
+
+SchedExternTask(Prime);
+SchedExternTask(PrintTask);
+
+	void
+task_init()
+{
+	SchedRegister(Prime);
+	SchedRegister(PrintTask);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/spe/Makefile	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,26 @@
+include ../Makefile.def
+
+TARGET = ../spe-main
+
+SRCS_TMP = $(wildcard *.cc)
+SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP))
+OBJS = $(SRCS:.cc=.o)
+
+CC      = spu-g++  -D__CERIUM_CELL__ -DABIBIT=$(ABIBIT)
+CFLAGS  = -O9 -g -Wall -fno-exceptions -fno-rtti#-DDEBUG
+INCLUDE = -I../${CERIUM}/include/TaskManager -I. -I..
+LIBS = -L../${CERIUM}/TaskManager -lspemanager  -Wl,--gc-sections 
+
+.SUFFIXES: .cc .o
+
+.cc.o:
+	$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
+
+all: $(TARGET)
+
+$(TARGET): $(OBJS)
+	$(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS)
+
+clean:
+	rm -f $(TARGET) $(OBJS)
+	rm -f *~ \#*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/spe/Prime.cc	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <math.h>
+#include "SchedTask.h"
+#include "Prime.h"
+#include "Func.h"
+
+SchedDefineTask1(Prime, prime);
+
+	static int
+prime(SchedTask *smanager, void *rbuf, void *wbuf)
+{
+	int start = (int)smanager->get_param(0);	/* 素数判定の開始地点 */
+	int end   = (int)smanager->get_param(1);	/* 素数判定の終了地点 */
+	int range = end - start;					/* 判定する範囲		  */
+
+	/* 判定結果を収める配列を受け取る */
+	int *output = (int*)smanager->get_output(wbuf, 0);
+
+	/* 初期化 */
+	for (int i = 0; i < range; i++){
+		output[i] = 1;
+	}
+
+
+	for (int i = start, index = 0; i < end; i++, index++) {
+		int limit = (int)sqrt((double) i);		/* 割る数の最大値を求める */
+		for (int j = 2; j <= limit; j++) {
+			/* 割り切れた場合、0を代入し素数じゃないという判定を下す */
+			if (i % j == 0) {
+				output[index] = 0;
+				break;
+			}
+		}
+	}
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/spe/Prime.h	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,6 @@
+#ifndef INCLUDED_TASK_PRIME
+#define INCLUDED_TASK_PRIME
+
+#include "SchedTask.h"
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/spe/PrintTask.cc	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include "SchedTask.h"
+#include "PrintTask.h"
+#include "Func.h"
+
+SchedDefineTask1(PrintTask, print);
+
+	static int
+print(SchedTask *smanager, void *rbuf, void *wbuf)
+{
+	int length = (int)smanager->get_param(0);			/* 出力する範囲 */
+	int *input = (int*)smanager->get_input(rbuf, 0);	/* 出力する配列 */
+
+	/* 素数の判定結果が1ならば出力する */
+	for (int i = 0; i < length; i++) {
+		if ( input[i] == 1 ) {
+			printf("%d ",i);
+		}
+	}
+	printf("\n");
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/spe/PrintTask.h	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,6 @@
+#ifndef INCLUDED_TASK_PRINTTASK
+#define INCLUDED_TASK_PRINTTASK
+
+#include "SchedTask.h"
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/Prime/spe/spe-main.cc	Sat Jun 04 17:26:14 2011 +0900
@@ -0,0 +1,13 @@
+#include "Func.h"
+#include "Scheduler.h"
+
+SchedExternTask(Prime);
+SchedExternTask(PrintTask);
+
+void
+task_init(Scheduler *s)
+{
+	SchedRegister(Prime);
+	SchedRegister(PrintTask);
+}
+