Mercurial > hg > Game > Cerium
diff example/Miller_Rabin/main.cc @ 1242:9d37fa6bc1da draft
add Miller_Rabin
author | Daichi Toma <amothic@gmail.com> |
---|---|
date | Tue, 01 Nov 2011 19:01:13 +0900 |
parents | |
children | cd50c48f45e7 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example/Miller_Rabin/main.cc Tue Nov 01 19:01:13 2011 +0900 @@ -0,0 +1,87 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "TaskManager.h" +#include "Func.h" + +typedef unsigned long long U64; + +/* task_initを宣言 */ +extern void task_init(void); + +/* TaskManagerを宣言 */ +extern TaskManager *manager; + +static U64 num = 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\ + -num Caluculate of Prime scope (default 1000 * 1000 * 1000) "; + + int +init(int argc, char **argv) +{ + for (int i = 1; argv[i]; ++i) { + if (strcmp(argv[i], "-num") == 0) { + num = atoi(argv[++i]); + } + else if (strcmp(argv[i], "-print") == 0) { + print_flag = true; + } + } + return 0; +} + + void +prime_init(TaskManager *manager) +{ + + U64 div_size = 1000; + U64 task_num = ((num >> 1) + div_size - 1) / div_size; + + bool *output = (bool*)manager->allocate(sizeof(bool)*task_num*div_size); /* 判定結果を収める配列 */ + + HTask *print = manager->create_task(PrintTask); + + for (U64 i = 0; i < task_num; i++) { + + HTask *prime = manager->create_task(Prime); + + prime->set_outData(0,&output[i*div_size],sizeof(bool)*div_size); + + prime->set_cpu(SPE_ANY); + + prime->set_param(0,(memaddr)(i*div_size)); // 開始地点 + prime->set_param(1,(memaddr)((i+1)*div_size - 1)); // 終了地点 + + print->wait_for(prime); + + prime->spawn(); + } + + /* 出力用のタスクに判定結果を収めた配列を渡す */ + print->set_inData(0,output,sizeof(bool)*task_num*div_size); + /* 出力する数を渡す */ + print->set_param(0,(memaddr)num); + /* printするかどうかを渡す */ + print->set_param(1,(memaddr)print_flag); + /* 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; +}