Mercurial > hg > Game > Cerium
changeset 1240:cdd04411b8a4 draft
did'nt moved
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Eratosthenes/Func.h Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,6 @@ +enum { +#include "SysTasks.h" + Prime, + SaveTask, + PrintTask, +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Eratosthenes/Makefile Wed Oct 26 09:16:18 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/Eratosthenes/Makefile.cell Wed Oct 26 09:16:18 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/Eratosthenes/Makefile.def Wed Oct 26 09:16:18 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++ +#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/Eratosthenes/Makefile.linux Wed Oct 26 09:16:18 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/Eratosthenes/Makefile.macosx Wed Oct 26 09:16:18 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/Eratosthenes/README Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,16 @@ +- 概要 + +指定された範囲の素数を計算するプログラムです。 + +- 実行方法 + +% ./prime [-num number] [-print] + + -num 出力する素数の範囲 + -print 計算した素数を出力 + +- 実行例 (-cpu は Cerium 標準のオプションです) + +% ./prime -num 100 -print -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/Eratosthenes/bitmap.h Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,113 @@ +#ifndef BITMAP_H_ +#define BITMAP_H_ +/* + * $Id: bitmap.c,v 0.2 2010/07/27 15:27:02 dankogai Exp dankogai $ + */ +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include "bitmap.h" + +typedef struct { + int fd; + size_t size; + char *map; +} bitmap; + +bitmap *bitmap_free(bitmap *b){ + if (b){ + if (b->map) { + if (b->fd){ + munmap(b->map, b->size); + close(b->fd); + }else{ + free(b->map); + } + } + free(b); + } + return (bitmap *)NULL; +} + +bitmap *bitmap_new(size_t size, const char *filename){ + bitmap *b = (bitmap *)malloc(sizeof(bitmap)); + struct stat st; + if (!b) return (bitmap *)NULL; + if (filename){ + b->fd = open(filename, + size ? O_RDWR|O_CREAT : O_RDONLY, + size ? (mode_t)0644 : (mode_t)0444); + if (b->fd == -1) { + perror(filename); + return bitmap_free(b); + } + if (size){ + if (ftruncate(b->fd, size >> 3) == -1){ + perror(filename); + return bitmap_free(b); + } + b->map = (char *)mmap(0, size >> 3, PROT_READ|PROT_WRITE, + MAP_SHARED, b->fd, 0); + + } + else{ + fstat(b->fd, &st); + size = st.st_size << 3; + b->map = (char *)mmap(0, size >> 3, PROT_READ, + MAP_PRIVATE, b->fd, 0); + } + if (b->map == MAP_FAILED) return bitmap_free(b); + } + else{ + if (!size) return (bitmap *)NULL; + b->map = (char *)malloc(size >> 3); + if (!b->map) return bitmap_free(b); + b->fd = 0; + } + b->size = size; + return b; +} + +int bitmap_save(bitmap *b, const char *filename){ + int ok = 0, fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, (mode_t)0644); + if (fd != -1){ + if (write(fd, b->map, (b->size >> 3)) != -1) ok = 1; + } + if (!ok) perror(filename); + close(fd); + return ok; +} + +void bitmap_fill(bitmap *b, int val){ + size_t i; + for (i = 0; i < (b->size >> 3); i++) b->map[i] = val ? 0xff : 0; +} + +static const int bits[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; + +inline int bitmap_set(bitmap *b, size_t where, int val){ + if (val) b->map[where >> 3] |= bits[where & 7]; + else b->map[where >> 3] &= ~bits[where & 7]; + return val; +} + +inline int bitmap_get(bitmap *b, size_t where){ + return !!(b->map[where >> 3] & bits[where & 7]); +} + +#ifdef TEST +#include <errno.h> +int main (int argc, char **argv){ + bitmap *b = bitmap_new(4096, (argc > 1 ? argv[1] : NULL)); + if (!b) return errno; + bitmap_set(b, 0, 1); + printf("%d\n", bitmap_get(b, 0)); + bitmap_free(b); + return 0; +} +#endif + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Eratosthenes/main.cc Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,129 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <errno.h> +#include <math.h> +#include "TaskManager.h" +#include "Func.h" + +#include <stdint.h> +typedef uint32_t U32; +typedef uint64_t U64; + +#include "bitmap.h" + +const char* const sieveFile = "sieve32.bm"; + +/* task_initを宣言 */ +extern void task_init(void); + +/* TaskManagerを宣言 */ +extern TaskManager *manager; + +static U64 start = 0; +static U64 size = 1000 * 1000 * 1000; +static bool print_flag = false; + +/* help文章 */ +const char *usr_help_str = "Usage: ./prime [-cpu spe_num] [-num N]\n\ + -cpu Number of SPE (default 1) \n\ + -start Calculate of Prime scope start (default 0) \n\ + -size Calculate of Prime scope size (default 1000 * 1000 * 1000)"; + + int +init(int argc, char **argv) +{ + for (int i = 1; argv[i]; ++i) { + if (strcmp(argv[i], "-start") == 0) { + start = atoll(argv[++i]); + } + else if (strcmp(argv[i], "-size") == 0) { + size = atoll(argv[++i]); + } + else if (strcmp(argv[i], "-print") == 0) { + print_flag = true; + } + } + return 0; +} + + void +prime_init(TaskManager *manager) +{ + bitmap *sieve = bitmap_new(0, sieveFile); + if (!sieve) { + perror(sieveFile); + exit(errno); + } + + bitmap *b = bitmap_new(size >> 1, NULL); + bitmap_fill(b, 1); + if (start == 0) bitmap_set(b, 0, 0); // 1は素数ではない + U64 pmax = (U64)sqrtl(start+size); + + HTask *save = manager->create_task(SaveTask); + + HTask *print = manager->create_task(PrintTask); + + +#ifdef VERBOSE + printf("pmax = %llu\n", pmax); +#endif + U64 p, i; + for (p = 3; p < pmax;) { + +#ifndef VERBOSE + printf("sieving %llu\r", p); + fflush(stdout); +#endif + + HTask *prime = manager->create_task(Prime); + prime->set_param(0, b); + prime->set_param(1, p); // ふるいの値 + prime->set_param(2, start); + prime->set_param(3, size); + prime->set_cpu(SPE_ANY); + + save->wait_for(prime); + + prime->spawn(); + + for(p += 2; !bitmap_get(sieve, p>>1); p += 2); + } + + char filename[256]; + snprintf(filename, 256, "%llu~%llu.bm", start, start+size); + + save->set_param(0, b); + save->set_param(1, filename); + save->set_cpu(CPU_PPE); + + print->wait_for(save); + +#ifdef VERBOSE + printf("saving %s\n", filename); +#endif + + save->spawn(); + + if ( print_flag ) { + print->set_param(0, b); + print->set_param(1, start); + 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/Eratosthenes/ppe/.#Hello.cc Wed Oct 26 09:16:18 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/Eratosthenes/ppe/Prime.cc Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,29 @@ +#include <stdio.h> +#include "SchedTask.h" +#include "Prime.h" +#include "Func.h" + +#include <stdint.h> +typedef uint32_t U32; +typedef uint64_t U64; + +#include "bitmap.h" + +SchedDefineTask1(Prime, prime); + + static int +prime(SchedTask *smanager, void *rbuf, void *wbuf) +{ + bitmap *b = (bitmap*)smanager->get_param(0); + U64 p = (U64)smanager->get_param(1); + U64 start = (U64)smanager->get_param(2); + U64 size = (U64)smanager->get_param(3); + + + for ( U64 i = p + p - (start % p) ; i <= size; i += p) { + if ((i & 1) == 0) continue; + bitmap_set( b, i>>1, 0); + } + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Eratosthenes/ppe/Prime.h Wed Oct 26 09:16:18 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/Eratosthenes/ppe/PrintTask.cc Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <errno.h> +#include "SchedTask.h" +#include "PrintTask.h" +#include "Func.h" + +#include "bitmap.h" + +SchedDefineTask1(PrintTask, print); + + static int +print(SchedTask *smanager, void *rbuf, void *wbuf) +{ + bitmap *b = (bitmap*)smanager->get_param(0); + unsigned long long offset = (unsigned long long)smanager->get_param(1); + + size_t i; + for (i = 0; i < b->size; i++){ + if (bitmap_get(b, i)) printf("%llu ", offset + (i<<1) + 1); + } + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Eratosthenes/ppe/PrintTask.h Wed Oct 26 09:16:18 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/Eratosthenes/ppe/SaveTask.cc Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,19 @@ +#include <stdio.h> +#include "SchedTask.h" +#include "SaveTask.h" +#include "Func.h" + +#include "bitmap.h" + +SchedDefineTask1(SaveTask, save); + + static int +save(SchedTask *smanager, void *rbuf, void *wbuf) +{ + bitmap *b = (bitmap*)smanager->get_param(0); + char *filename = (char*)smanager->get_param(1); + + bitmap_save(b, filename); + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Eratosthenes/ppe/SaveTask.h Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,6 @@ +#ifndef INCLUDED_TASK_SAVETASK +#define INCLUDED_TASK_SAVETASK + +#include "SchedTask.h" + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Eratosthenes/ppe/task_init.cc Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,14 @@ +#include "Func.h" +#include "Scheduler.h" + +SchedExternTask(Prime); +SchedExternTask(SaveTask); +SchedExternTask(PrintTask); + + void +task_init() +{ + SchedRegister(Prime); + SchedRegister(SaveTask); + SchedRegister(PrintTask); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Eratosthenes/spe/Makefile Wed Oct 26 09:16:18 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/Eratosthenes/spe/Prime.cc Wed Oct 26 09:16:18 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/Eratosthenes/spe/Prime.h Wed Oct 26 09:16:18 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/Eratosthenes/spe/PrintTask.cc Wed Oct 26 09:16:18 2011 +0900 @@ -0,0 +1,28 @@ +#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 print_flag = (long)smanager->get_param(1); //プリントするかどうか + + if (print_flag < 0) { + return 0; + } + + 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/Eratosthenes/spe/PrintTask.h Wed Oct 26 09:16:18 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/Eratosthenes/spe/spe-main.cc Wed Oct 26 09:16:18 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); +} +
--- a/example/basic/Makefile.def Fri Oct 21 19:25:41 2011 +0900 +++ b/example/basic/Makefile.def Wed Oct 26 09:16:18 2011 +0900 @@ -11,7 +11,5 @@ OPT = -g CFLAGS = $(OPT) -Wall -ABIBIT = 32 - INCLUDE = -I${CERIUM}/include/TaskManager -I. -I.. LIBS = -L${CERIUM}/TaskManager
--- a/example/basic/Makefile.macosx Fri Oct 21 19:25:41 2011 +0900 +++ b/example/basic/Makefile.macosx Wed Oct 26 09:16:18 2011 +0900 @@ -4,6 +4,7 @@ SRCS_EXCLUDE = # 除外するファイルを書く SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP)) OBJS = $(SRCS:.cc=.o) +ABIBIT=64 TASK_DIR = ppe TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc)