Mercurial > hg > GearsTemplate
annotate src/parallel_execution/generate_context.pl @ 398:fc4fcd441700
Fix spanwTasks
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 29 Aug 2017 01:01:44 +0900 |
parents | c43ec0e3fa84 |
children | 9b35e6581b5c |
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 |
340 | 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 } | |
352
3e01e963eb2d
Fix compile error for calc example but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
340
diff
changeset
|
111 $dataGear{"Context"} = "Context"; |
186 | 112 } |
113 } | |
114 | |
115 sub generateContext { | |
284 | 116 $codeGear{"start_code"} = "$ddir/$name-context.c"; |
117 $codeGear{"exit_code"} = "$ddir/$name-context.c"; | |
285 | 118 $mCodeGear{"start_code"} = "$ddir/$name-context.c"; |
119 $mCodeGear{"exit_code"} = "$ddir/$name-context.c"; | |
254 | 120 open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!"); |
186 | 121 for my $code ( sort keys %codeGear ) { |
122 print $fd "extern __code ${code}_stub(struct Context*);\n"; | |
123 } | |
277 | 124 for my $impl ( sort keys %constructor ) { |
125 my ($interface, $constructorArgs) = @{$constructor{$impl}}; | |
278 | 126 print $fd "extern ${interface}* create${impl}($constructorArgs);\n"; |
277 | 127 } |
186 | 128 print $fd "\n"; |
129 | |
254 | 130 open my $fd,">","$ddir/enumCode.h" or die("can't open $ddir/enumCode.h $!"); |
186 | 131 print $fd "enum Code {\n"; |
132 for my $code ( sort keys %codeGear ) { | |
133 print $fd " C_${code},\n"; | |
134 } | |
135 print $fd "};\n"; | |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
136 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
137 my $code_init = ''; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
138 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
|
139 $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
|
140 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
141 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
142 my $context_c = << "EOFEOF"; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
143 #include <stdlib.h> |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
144 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
145 #include "../context.h" |
277 | 146 |
246
421ea91dd76c
Change initContext type from __code to void
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
210
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 context->heap = context->heapStart; |
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" |
355 | 158 context->dataNum = D_Worker; |
398
fc4fcd441700
Fix spanwTasks
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
397
diff
changeset
|
159 context->tasks = createSingleLinkedQueue(context); |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
160 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
161 EOFEOF |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
162 |
254 | 163 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
|
164 print $fd $context_c; |
277 | 165 |
166 my $meta_call = <<"EOFEOF"; | |
397 | 167 __code par_meta(struct Context* context, enum Code spawns, enum Code next) { |
398
fc4fcd441700
Fix spanwTasks
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
397
diff
changeset
|
168 Gearef(context, Queue)->queue = (union Data*)context->tasks; |
fc4fcd441700
Fix spanwTasks
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
397
diff
changeset
|
169 Gearef(context, Queue)->whenEmpty = next; |
fc4fcd441700
Fix spanwTasks
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
397
diff
changeset
|
170 Gearef(context, Queue)->next = spawns; |
fc4fcd441700
Fix spanwTasks
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
397
diff
changeset
|
171 goto (context->code[context->tasks->isEmpty])(context); |
397 | 172 } |
277 | 173 |
174 __code meta(struct Context* context, enum Code next) { | |
280 | 175 // printf("meta %d\\n",next); |
277 | 176 goto (context->code[next])(context); |
177 } | |
178 | |
179 __code start_code(struct Context* context) { | |
180 goto meta(context, context->next); | |
181 } | |
182 | |
183 __code start_code_stub(struct Context* context) { | |
184 goto start_code(context); | |
185 } | |
186 | |
187 __code exit_code(struct Context* context) { | |
188 free(context->code); | |
189 free(context->data); | |
190 free(context->heapStart); | |
191 goto exit(0); | |
192 } | |
193 | |
194 __code exit_code_stub(struct Context* context) { | |
195 goto exit_code(context); | |
196 } | |
197 | |
280 | 198 // end context_c |
277 | 199 EOFEOF |
186 | 200 |
278 | 201 print $fd $meta_call; |
202 | |
254 | 203 open my $fd,">","$ddir/enumData.h" or die("can't open $ddir/enumData.h $!"); |
186 | 204 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
|
205 print $fd " D_Code,\n"; |
186 | 206 for my $data ( sort keys %dataGear ) { |
207 print $fd " D_${data},\n"; | |
208 } | |
209 print $fd "};\n\n"; | |
210 | |
254 | 211 open my $fd,">","$ddir/typedefData.h" or die("can't open $ddir/typedefData.h $!"); |
186 | 212 for my $data ( sort keys %dataGear ) { |
213 print $fd "typedef struct ${data} ${data};\n"; | |
214 } | |
215 | |
254 | 216 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!"); |
186 | 217 for my $data ( sort keys %dataGear ) { |
218 print $fd " ALLOC_DATA(context, ${data});\n"; | |
219 } | |
220 } | |
221 | |
222 # end |