1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include <stdio.h>
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <stdlib.h>
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 #include <string.h>
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include "TaskManager.h"
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 #include "Func.h"
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 /* task_initを宣言 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 extern void task_init(void);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 /* TaskManagerを宣言 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 extern TaskManager *manager;
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12
|
1232
|
13 static long prime_num = 256; /* 素数を出力する範囲 */
|
|
14 static long print_flag = -1;
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 /* help文章 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 const char *usr_help_str = "Usage: ./prime [-cpu spe_num] [-num N]\n\
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 -cpu Number of SPE (default 1) \n\
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 -num Print of Prime scope (default 256) ";
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 int
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 init(int argc, char **argv)
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 {
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 for (int i = 1; argv[i]; ++i) {
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 if (strcmp(argv[i], "-num") == 0) {
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 prime_num = atoi(argv[++i]);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 }
|
1194
|
28 else if (strcmp(argv[i], "-print") == 0) {
|
|
29 print_flag = 0;
|
|
30 }
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 return 0;
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 void
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 prime_init(TaskManager *manager)
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 {
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38
|
1232
|
39 long div_size = 256; /* 分割するサイズ */
|
|
40 long task_num = (prime_num + div_size - 1) / div_size; /* タスクの数 */
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41
|
1232
|
42 bool *output = (bool*)manager->allocate(sizeof(bool)*task_num*div_size); /* 判定結果を収める配列 */
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 /* 出力用のタスク */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 HTask *print = manager->create_task(PrintTask);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 /* 計算したタスクの数だけタスクを生成する */
|
1232
|
48 for (long i = 0; i < task_num; i++) {
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 /* 素数計算タスク */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 HTask *prime = manager->create_task(Prime);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 /* 判定結果を収める配列を渡す */
|
1232
|
54 prime->set_outData(0,&output[i*div_size],sizeof(bool)*div_size);
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 /* SPEを順に使う */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 prime->set_cpu(SPE_ANY);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 /* 素数判定の範囲を渡す */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 prime->set_param(0,(memaddr)(i*div_size)); /* 開始地点 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 prime->set_param(1,(memaddr)((i+1)*div_size)); /* 終了地点 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 /* 出力用タスクの依存関係 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 print->wait_for(prime);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 /* タスクを登録 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 prime->spawn();
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 /* 出力用のタスクに判定結果を収めた配列を渡す */
|
1232
|
71 print->set_inData(0,output,sizeof(bool)*prime_num);
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 /* 出力する数を渡す */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 print->set_param(0,(memaddr)prime_num);
|
1194
|
74 /* printするかどうかを渡す */
|
|
75 print->set_param(1,(memaddr)print_flag);
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 /* PPEを使うように指示 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 print->set_cpu(CPU_PPE);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 /* タスクを登録 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 print->spawn();
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 int
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 TMmain(TaskManager *manager, int argc, char *argv[])
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 {
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 if (init(argc, argv) < 0) {
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 return -1;
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89 task_init();
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 prime_init(manager);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 return 0;
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 }
|