Mercurial > hg > Game > Cerium
comparison 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 |
comparison
equal
deleted
inserted
replaced
1241:b96b1604ab85 | 1242:9d37fa6bc1da |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include "TaskManager.h" | |
5 #include "Func.h" | |
6 | |
7 typedef unsigned long long U64; | |
8 | |
9 /* task_initを宣言 */ | |
10 extern void task_init(void); | |
11 | |
12 /* TaskManagerを宣言 */ | |
13 extern TaskManager *manager; | |
14 | |
15 static U64 num = 1000; | |
16 static bool print_flag = false; | |
17 | |
18 /* help文章 */ | |
19 const char *usr_help_str = "Usage: ./prime [-cpu spe_num] [-num N]\n\ | |
20 -cpu Number of SPE (default 1) \n\ | |
21 -num Caluculate of Prime scope (default 1000 * 1000 * 1000) "; | |
22 | |
23 int | |
24 init(int argc, char **argv) | |
25 { | |
26 for (int i = 1; argv[i]; ++i) { | |
27 if (strcmp(argv[i], "-num") == 0) { | |
28 num = atoi(argv[++i]); | |
29 } | |
30 else if (strcmp(argv[i], "-print") == 0) { | |
31 print_flag = true; | |
32 } | |
33 } | |
34 return 0; | |
35 } | |
36 | |
37 void | |
38 prime_init(TaskManager *manager) | |
39 { | |
40 | |
41 U64 div_size = 1000; | |
42 U64 task_num = ((num >> 1) + div_size - 1) / div_size; | |
43 | |
44 bool *output = (bool*)manager->allocate(sizeof(bool)*task_num*div_size); /* 判定結果を収める配列 */ | |
45 | |
46 HTask *print = manager->create_task(PrintTask); | |
47 | |
48 for (U64 i = 0; i < task_num; i++) { | |
49 | |
50 HTask *prime = manager->create_task(Prime); | |
51 | |
52 prime->set_outData(0,&output[i*div_size],sizeof(bool)*div_size); | |
53 | |
54 prime->set_cpu(SPE_ANY); | |
55 | |
56 prime->set_param(0,(memaddr)(i*div_size)); // 開始地点 | |
57 prime->set_param(1,(memaddr)((i+1)*div_size - 1)); // 終了地点 | |
58 | |
59 print->wait_for(prime); | |
60 | |
61 prime->spawn(); | |
62 } | |
63 | |
64 /* 出力用のタスクに判定結果を収めた配列を渡す */ | |
65 print->set_inData(0,output,sizeof(bool)*task_num*div_size); | |
66 /* 出力する数を渡す */ | |
67 print->set_param(0,(memaddr)num); | |
68 /* printするかどうかを渡す */ | |
69 print->set_param(1,(memaddr)print_flag); | |
70 /* PPEを使うように指示 */ | |
71 print->set_cpu(CPU_PPE); | |
72 /* タスクを登録 */ | |
73 print->spawn(); | |
74 } | |
75 | |
76 int | |
77 TMmain(TaskManager *manager, int argc, char *argv[]) | |
78 { | |
79 if (init(argc, argv) < 0) { | |
80 return -1; | |
81 } | |
82 | |
83 task_init(); | |
84 prime_init(manager); | |
85 | |
86 return 0; | |
87 } |