Mercurial > hg > Members > masakoha > testcode
annotate parallel_processing/ppb_data_shared/ppb_data_shared.cc @ 10:51177cc6ab38
add ppb_data_shared
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 03 Jan 2014 02:03:11 +0900 |
parents | parallel_processing/ppb_data_split/ppb_data_split.cc@fe8c1c25296a |
children | f522b6bf6789 |
rev | line source |
---|---|
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
1 #include <stdio.h> |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
2 #include <pthread.h> |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
3 |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
4 #define THREAD_NUM 2 |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
5 #define DATA_NUM 10 |
9
fe8c1c25296a
fix ppb_data_split
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
8
diff
changeset
|
6 #define SPLIT_DATA_NUM (DATA_NUM / THREAD_NUM) |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
7 |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
8 typedef struct _thread_arg { |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
9 int thread_no; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
10 int *data; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
11 } thread_arg_t; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
12 |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
13 void * |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
14 thread_func(void *arg) |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
15 { |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
16 thread_arg_t *targ = (thread_arg_t *)arg; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
17 |
9
fe8c1c25296a
fix ppb_data_split
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
8
diff
changeset
|
18 for (int i = 0; i < SPLIT_DATA_NUM; i++) { |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
19 printf("thread%d : %d + 1 = %d\n", |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
20 targ->thread_no, targ->data[i], targ->data[i] + 1); |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
21 } |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
22 return 0; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
23 } |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
24 |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
25 int |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
26 main() |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
27 { |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
28 pthread_t handle[THREAD_NUM]; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
29 thread_arg_t targ[THREAD_NUM]; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
30 int data[DATA_NUM]; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
31 int i; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
32 |
7 | 33 /* initialize */ |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
34 for (i = 0; i < DATA_NUM; i++) data[i] = i; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
35 |
7 | 36 /* spawn thread a number of THREAD_NUM */ |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
37 for (i = 0; i < THREAD_NUM; i++) { |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
38 targ[i].thread_no = i; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
39 |
9
fe8c1c25296a
fix ppb_data_split
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
8
diff
changeset
|
40 /* divide a data into THREAD_NUM*/ |
fe8c1c25296a
fix ppb_data_split
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
8
diff
changeset
|
41 targ[i].data = &data[SPLIT_DATA_NUM * i]; |
7 | 42 /* spawn thread*/ |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
43 pthread_create(&handle[i], NULL, &thread_func, (void*)&targ[i]); |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
44 } |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
45 |
7 | 46 /* wait for running all thread */ |
6
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
47 for (i = 0; i < THREAD_NUM; i++) pthread_join(handle[i], NULL); |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
48 return 0; |
7b0b23f3538d
first pthread programming
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
49 } |