Mercurial > hg > Members > kono > Cerium
diff example/word_count/main.cc @ 393:72b73beb3325
add word_count
author | e065725@localhost.localdomain |
---|---|
date | Tue, 15 Sep 2009 16:30:46 +0900 |
parents | |
children | e08d0aa94367 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/word_count/main.cc Tue Sep 15 16:30:46 2009 +0900 @@ -0,0 +1,177 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include "TaskManager.h" +#include "Func.h" + +extern void task_init(void); + +const char *usr_help_str = "Usage: ./dependency [-cpu spe_num] [-nodepend]\n\ + -nodepend No Depenendcy"; + +typedef struct { + caddr_t file_mmap; + off_t size; +} st_mmap_t; + + +/*与えられたsizeをfix_byte_sizeの倍数にする(丸め込むっていうのかな?)*/ +int +fix_byte(int size,int fix_byte_size) +{ + size = (size/fix_byte_size)*fix_byte_size + ((size%fix_byte_size)!= 0)*fix_byte_size; + + return size; +} + + +st_mmap_t +my_mmap(char *filename) +{ + + /*マッピングだよ!*/ + int fd = -1; + int map = MAP_PRIVATE; + st_mmap_t st_mmap; + struct stat sb; + + if ((fd=open(filename,O_RDONLY,0666))==0) { + fprintf(stderr,"can't open %s\n",filename); + } + + if (fstat(fd,&sb)) { + fprintf(stderr,"can't fstat %s\n",filename); + } + + printf("file size %d\n",(int)sb.st_size); + + /*sizeをページングサイズの倍数にあわせる*/ + st_mmap.size = fix_byte(sb.st_size,4096); + + printf("fix 4096byte file size %d\n",st_mmap.size); + + st_mmap.file_mmap = (char*)mmap(NULL,st_mmap.size,PROT_READ,map,fd,(off_t)0); + if (st_mmap.file_mmap == (caddr_t)-1) { + fprintf(stderr,"Can't mmap file\n"); + perror(NULL); + exit(0); + } + + return st_mmap; + +} + + +void +run_start(char *filename) +{ + HTaskPtr t_exec; + HTaskPtr t_print; + + st_mmap_t st_mmap; + st_mmap = my_mmap(filename); + + + /*sizeはdivision_sizeの倍数にしている。*/ + int size = st_mmap.size; + char *file_mmap = st_mmap.file_mmap; + + /* 1task分のデータサイズ(byte) */ + int division_size; + if (size >= 4096*4) { + division_size = 4096 * 4;/*16kbyte*/ + } + else { + division_size = size; + } + + division_size = 4096; + + printf("dvision_size %d\n",division_size); + + /* "word num" and "line num" */ + int status_num = 2; + /* taskの数 */ + int task_num = size / division_size; + printf("task_num %d\n",task_num); + /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(8byte)を使用 */ + int division_out_size = 16; + /* out用のデータのサイズ。*/ + int out_size = division_out_size*task_num; + unsigned long long *o_data = (unsigned long long*)manager->allocate(out_size); + + + printf("out size %d\n",out_size); + + /*各SPEの結果を合計して出力するタスク*/ + + t_print = manager->create_task(TASK_PRINT); + t_print->add_inData(o_data, out_size); + t_print->add_param(task_num); + t_print->add_param(status_num); + + int length = division_size; + + printf("length %d\n",length); + + /*各SPEに処理するデータを割り振る。*/ + + /*渡すデータの最後が文字かどうか。(スペース、改行以外)*/ + int word_flag = 0; + int i; + for (i = 0; i < task_num; i++) { + + t_exec = manager->create_task(TASK_EXEC); + t_exec->add_inData(file_mmap + i*division_size, division_size); + t_exec->add_outData(o_data + i*status_num, division_out_size); + t_exec->add_param(length); + t_exec->add_param(word_flag); + t_exec->set_cpu(SPE_ANY); + t_print->wait_for(t_exec); + t_exec->spawn(); + + word_flag = ((file_mmap[(i+1)*division_size-1] != 0x20) && (file_mmap[(i+1)*division_size-1] != 0x0A)); + + } + + t_print->spawn(); +} + +char* +init(int argc, char **argv) +{ + + char *filename; + + for (int i = 1; argv[i]; ++i) { + + if (strcmp(argv[i], "-file") == 0) { + filename = argv[i+1]; + } + + } + + return filename; +} + +int +TMmain(int argc, char *argv[]) +{ + + char *filename = init(argc, argv); + + + if (filename < 0) { + return -1; + } + + task_init(); + run_start(filename); + + return 0; +}