annotate example/Prime/main.cc @ 1767:1a68c05eeadf draft

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