Mercurial > hg > Game > Cerium
annotate example/fileread/main.cc @ 1722:264a36d30f7f draft
add time measurement function in fileread
author | Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 28 Oct 2013 15:25:07 +0900 |
parents | 797e3ec1ca74 |
children | 5a294d8e5643 |
rev | line source |
---|---|
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include <stdio.h> |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <stdlib.h> |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 #include <string.h> |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include <sys/stat.h> |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
5 #include <sys/time.h> |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 #include "TaskManager.h" |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 #include "Func.h" |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 extern void task_init(void); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
11 static double st_time; |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
12 static double ed_time; |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 static int spe_num = 1; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 static CPU_TYPE spe_cpu = SPE_ANY; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 static int ONE_TASK_SIZE = 4096 * 4; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 extern TaskManager *manager; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 const char *usr_help_str = "Usage: ./fileread [-cpu cpu_num] [-file filename]\n\ |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 -cpu Number of SPE (default 1) \n\ |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
20 -file Opne file\n"; |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
21 |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
22 static double |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
23 getTime(){ |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
24 struct timeval tv; |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
25 gettimeofday(&tv,NULL); |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
26 return tv.tv_sec + (double)tv.tv_usec*1e-6; |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
27 } |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
28 |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
29 void |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
30 TMend(TaskManager *manager) |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
31 { |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
32 ed_time = getTime(); |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
33 printf("Time: %0.6f\n",ed_time-st_time); |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
34 } |
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
35 |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 static char* |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 init(int argc, char **argv) |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 char *filename = 0; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 for (int i = 1; argv[i]; ++i) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 if (strcmp(argv[i], "-cpu") == 0) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 spe_num = atoi(argv[i+1]); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 } else if (strcmp(argv[i], "-file") == 0) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 filename = argv[i+1]; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 if (filename==0){ |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 puts(usr_help_str); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 exit(1); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 return filename; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 static void |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 run_start(TaskManager *manager,char *filename) |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 HTask *read; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 int *fd = (int*)manager->allocate(sizeof(int)); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 struct stat *sb = (struct stat*)manager->allocate(sizeof(struct stat)); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 if ((*fd=open(filename,O_RDONLY,0666))==0) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 fprintf(stderr,"can't open %s\n",filename); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 if (fstat(*fd,sb)) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 fprintf(stderr,"can't fstat %s\n",filename); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 int filesize = sb->st_size; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 int task_num = filesize / ONE_TASK_SIZE; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 task_num += ((filesize % ONE_TASK_SIZE) != 0); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 printf("filesize : %d\n",filesize); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 printf("one_task_size: %d\n",ONE_TASK_SIZE); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81 printf("task_num : %d\n",task_num); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 read = manager->create_task(READ_TASK); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 read->set_cpu(spe_cpu); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 //ファイルディスクリプタをそのままタスクに渡してあげる |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 read->set_inData(0,(memaddr)fd,sizeof(int*)); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89 read->set_param(0,ONE_TASK_SIZE); //1つのタスクが読み込む量 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 read->iterate(task_num); //タスク数分イテレートする |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 int |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
95 TMmain(TaskManager *manager, int argc, char *argv[]) |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
96 { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
97 char *filename = 0; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
98 filename = init(argc,argv); |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
100 if (filename < 0) { |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
101 return -1; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102 } |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
103 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
104 task_init(); |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
105 st_time = getTime(); |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 run_start(manager, filename); |
1722
264a36d30f7f
add time measurement function in fileread
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
1721
diff
changeset
|
107 manager->set_TMend(TMend); |
1721
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
109 return 0; |
797e3ec1ca74
add fileread only example
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 } |