Mercurial > hg > Members > Moririn
annotate src/parallel_execution/generate_context.pl @ 313:4addbc7469ee
fix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 15 Feb 2017 11:36:10 +0900 |
parents | 7dd5a7d52a67 |
children | 0d720487291f |
rev | line source |
---|---|
186 | 1 #!/usr/bin/perl |
2 | |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
3 use Getopt::Std; |
254 | 4 use strict; |
186 | 5 |
254 | 6 # |
7 # generrate Gears OS context heaader and initializer from CbC sources | |
8 # | |
186 | 9 # CodeGear |
10 # | |
254 | 11 # get stub information from # *.c |
12 # __code taskManager_stub(struct Context* context) { | |
13 # | |
14 # generate CodeGear indexn in context.h | |
15 # C_taskManager, | |
186 | 16 # |
254 | 17 # generate CodeGear stub reference in context.h |
18 # extern __code taskManager_stub(struct Context*); | |
186 | 19 # |
254 | 20 # generate CodeGear stub reference in $name-context.h for each module |
21 # context->code[C_taskManager] = taskManager_stub; | |
186 | 22 # |
23 # DataGear | |
24 # | |
254 | 25 # get DataGear information from context.h |
26 # struct Worker { | |
186 | 27 # int id; |
28 # struct Context* contexts; | |
29 # enum Code execute; | |
30 # enum Code taskSend; | |
31 # enum Code taskRecive; | |
32 # enum Code shutdown; | |
33 # struct Queue* tasks; | |
34 # } Worker; | |
35 # | |
254 | 36 # generate typedefs and DataGear index in context.h |
37 # typedef struct Worker Worker; | |
38 # D_Worker, | |
186 | 39 # |
254 | 40 # generate DataGear allocator in context.h |
41 # ALLOC_DATA(context, Worker); | |
42 # | |
186 | 43 |
254 | 44 my $ddir = "c"; |
186 | 45 |
254 | 46 our($opt_o,$opt_d,$opt_h); |
47 getopts('o:d:h'); | |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
48 |
284 | 49 my $name = $opt_o?$opt_o:"gears"; |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
50 |
254 | 51 if ($opt_d) { |
52 $ddir = $opt_d; | |
53 } | |
54 | |
55 if ( ! -d $ddir) { | |
56 mkdir $ddir; | |
57 } | |
58 | |
59 if ($opt_h) { | |
60 print "$0 [-d distdir] [-h]\n"; | |
61 exit; | |
62 } | |
63 | |
64 my %codeGear; | |
65 my %dataGear; | |
277 | 66 my %constructor; |
254 | 67 |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
257
diff
changeset
|
68 # gather module Information for code table initialization |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
69 for (@ARGV) { |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
70 next if (/context.c/); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
71 &getStubInfo($_); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
72 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
73 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
74 my (%mCodeGear) = (%codeGear); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
75 |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
257
diff
changeset
|
76 # anyway we gather all Gears Information |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
257
diff
changeset
|
77 while (<*.c test/*.c>) { |
257 | 78 next if (/context.c/); |
79 &getStubInfo($_); | |
80 } | |
81 | |
186 | 82 &generateContext(); |
83 | |
84 sub getStubInfo { | |
85 my ($filename) = @_; | |
86 open my $fd,"<",$filename or die("can't open $filename $!"); | |
87 while (<$fd>) { | |
312
7dd5a7d52a67
USE_CUDAWorker flag only for CUDAtwice
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
285
diff
changeset
|
88 if (/^__code (\w+)_stub\(struct *Context *\* *context\)/) { |
186 | 89 $codeGear{$1} = $filename; |
312
7dd5a7d52a67
USE_CUDAWorker flag only for CUDAtwice
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
285
diff
changeset
|
90 } elsif (/^(\w+)(\*)+ *create(\w+)\(([^]]*)\)/) { |
277 | 91 my $interface = $1; |
92 my $implementation = $3; | |
93 my $constructorArgs = $4; | |
94 $constructor{$implementation} = [$interface, $constructorArgs]; | |
186 | 95 } |
96 } | |
97 | |
98 open my $cx,"<","context.h" or die("can't open context.h $!"); | |
254 | 99 my $inUnionData = 0; |
186 | 100 while (<$cx>) { |
101 if (! $inUnionData) { | |
102 if ( /^union Data/) { | |
103 $inUnionData = 1; | |
104 } | |
105 next; | |
106 } | |
107 last if (/union Data end/); | |
108 if (/struct (\w+) \{/) { | |
109 $dataGear{$1} = $1; | |
110 } | |
111 } | |
112 } | |
113 | |
114 sub generateContext { | |
284 | 115 $codeGear{"start_code"} = "$ddir/$name-context.c"; |
116 $codeGear{"exit_code"} = "$ddir/$name-context.c"; | |
285 | 117 $mCodeGear{"start_code"} = "$ddir/$name-context.c"; |
118 $mCodeGear{"exit_code"} = "$ddir/$name-context.c"; | |
254 | 119 open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!"); |
186 | 120 for my $code ( sort keys %codeGear ) { |
121 print $fd "extern __code ${code}_stub(struct Context*);\n"; | |
122 } | |
277 | 123 for my $impl ( sort keys %constructor ) { |
124 my ($interface, $constructorArgs) = @{$constructor{$impl}}; | |
278 | 125 print $fd "extern ${interface}* create${impl}($constructorArgs);\n"; |
277 | 126 } |
186 | 127 print $fd "\n"; |
128 | |
254 | 129 open my $fd,">","$ddir/enumCode.h" or die("can't open $ddir/enumCode.h $!"); |
186 | 130 print $fd "enum Code {\n"; |
131 for my $code ( sort keys %codeGear ) { | |
132 print $fd " C_${code},\n"; | |
133 } | |
134 print $fd "};\n"; | |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
135 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
136 my $code_init = ''; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
137 for my $code ( sort keys %mCodeGear ) { |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
138 $code_init .= " context->code[C_${code}] = ${code}_stub;\n"; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
139 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
140 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
141 my $context_c = << "EOFEOF"; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
142 #include <stdlib.h> |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
143 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
144 #include "../context.h" |
277 | 145 |
246
421ea91dd76c
Change initContext type from __code to void
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
210
diff
changeset
|
146 void initContext(struct Context* context) { |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
147 context->heapLimit = sizeof(union Data)*ALLOCATE_SIZE; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
148 context->code = (__code(**) (struct Context*)) NEWN(ALLOCATE_SIZE, void*); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
149 context->data = NEWN(ALLOCATE_SIZE, union Data*); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
150 context->heapStart = NEWN(context->heapLimit, char); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
151 context->heap = context->heapStart; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
152 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
153 // context->codeNum = Exit; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
154 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
155 $code_init |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
156 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
157 #include "dataGearInit.c" |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
158 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
159 // context->data[D_ActiveQueue] = createSynchronizedQueue(context); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
160 // context->data[D_WaitQueue] = createSynchronizedQueue(context); |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
161 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
162 context->dataNum = D_Queue; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
163 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
164 EOFEOF |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
165 |
254 | 166 open my $fd,">","$ddir/$name-context.c" or die("can't open $ddir/$name-context.c $!"); |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
167 print $fd $context_c; |
277 | 168 |
169 my $meta_call = <<"EOFEOF"; | |
170 | |
171 __code meta(struct Context* context, enum Code next) { | |
280 | 172 // printf("meta %d\\n",next); |
277 | 173 goto (context->code[next])(context); |
174 } | |
175 | |
176 __code start_code(struct Context* context) { | |
177 goto meta(context, context->next); | |
178 } | |
179 | |
180 __code start_code_stub(struct Context* context) { | |
181 goto start_code(context); | |
182 } | |
183 | |
184 __code exit_code(struct Context* context) { | |
185 free(context->code); | |
186 free(context->data); | |
187 free(context->heapStart); | |
188 goto exit(0); | |
189 } | |
190 | |
191 __code exit_code_stub(struct Context* context) { | |
192 goto exit_code(context); | |
193 } | |
194 | |
280 | 195 // end context_c |
277 | 196 EOFEOF |
186 | 197 |
278 | 198 print $fd $meta_call; |
199 | |
254 | 200 open my $fd,">","$ddir/enumData.h" or die("can't open $ddir/enumData.h $!"); |
186 | 201 print $fd "enum DataType {\n"; |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
257
diff
changeset
|
202 print $fd " D_Code,\n"; |
186 | 203 for my $data ( sort keys %dataGear ) { |
204 print $fd " D_${data},\n"; | |
205 } | |
206 print $fd "};\n\n"; | |
207 | |
254 | 208 open my $fd,">","$ddir/typedefData.h" or die("can't open $ddir/typedefData.h $!"); |
186 | 209 for my $data ( sort keys %dataGear ) { |
210 print $fd "typedef struct ${data} ${data};\n"; | |
211 } | |
212 | |
254 | 213 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!"); |
186 | 214 for my $data ( sort keys %dataGear ) { |
215 print $fd " ALLOC_DATA(context, ${data});\n"; | |
216 } | |
217 } | |
218 | |
219 # end |