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