annotate example/Prime/ppe/Prime.cc @ 1439:20e935f170c1 draft

add Makefile for gpu
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Sat, 07 Apr 2012 11:43:05 +0900
parents 4e6a6e620fc4
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 <math.h>
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 #include "SchedTask.h"
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 #include "Prime.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 SchedDefineTask1(Prime, prime);
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 static int
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 prime(SchedTask *smanager, void *rbuf, void *wbuf)
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 {
1232
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
12 long start = (long)smanager->get_param(0); /* 素数判定の開始地点 */
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
13 long end = (long)smanager->get_param(1); /* 素数判定の終了地点 */
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
14 long range = end - start; /* 判定する範囲 */
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 /* 判定結果を収める配列を受け取る */
1232
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
17 bool *output = (bool*)smanager->get_output(wbuf, 0);
1171
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 /* 初期化 */
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 for (int i = 0; i < range; i++){
1232
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
21 output[i] = true;
1171
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 }
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
1232
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
25
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
26 for (long i = start, index = 0; i < end; i++, index++) {
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
27 long limit = (long)sqrt((double) i); /* 割る数の最大値を求める */
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
28 for (long j = 2; j <= limit; j++) {
1171
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 /* 割り切れた場合、0を代入し素数じゃないという判定を下す */
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 if (i % j == 0) {
1232
2dfac651288e fix Prime
Daichi Toma <amothic@gmail.com>
parents: 1189
diff changeset
31 output[index] = false;
1171
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 break;
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 }
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 return 0;
d910c8377a09 add prime example
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 }