Mercurial > hg > Gears > GearsAgda
annotate src/parallel_execution/generate_context.pl @ 479:b8b412a7670a
Fix segmentation fault if multithread
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 28 Dec 2017 19:51:06 +0900 |
parents | 08a93fc2f0d3 |
children | 5859bed4edff |
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+) \{/) { | |
436
08a93fc2f0d3
Fix CudaExecutor but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
435
diff
changeset
|
109 $dataGear{$1} = 'struct'; |
435
af0ec811b20e
Add CUDAExecutor
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
410
diff
changeset
|
110 } elsif (/^\s{4}(\w+) (\w+);/) { # primitive type |
436
08a93fc2f0d3
Fix CudaExecutor but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
435
diff
changeset
|
111 $dataGear{$1} = 'primitive'; |
186 | 112 } |
436
08a93fc2f0d3
Fix CudaExecutor but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
435
diff
changeset
|
113 $dataGear{"Context"} = "struct"; |
186 | 114 } |
115 } | |
116 | |
117 sub generateContext { | |
284 | 118 $codeGear{"start_code"} = "$ddir/$name-context.c"; |
119 $codeGear{"exit_code"} = "$ddir/$name-context.c"; | |
285 | 120 $mCodeGear{"start_code"} = "$ddir/$name-context.c"; |
121 $mCodeGear{"exit_code"} = "$ddir/$name-context.c"; | |
254 | 122 open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!"); |
186 | 123 for my $code ( sort keys %codeGear ) { |
124 print $fd "extern __code ${code}_stub(struct Context*);\n"; | |
125 } | |
277 | 126 for my $impl ( sort keys %constructor ) { |
127 my ($interface, $constructorArgs) = @{$constructor{$impl}}; | |
278 | 128 print $fd "extern ${interface}* create${impl}($constructorArgs);\n"; |
277 | 129 } |
186 | 130 print $fd "\n"; |
131 | |
254 | 132 open my $fd,">","$ddir/enumCode.h" or die("can't open $ddir/enumCode.h $!"); |
186 | 133 print $fd "enum Code {\n"; |
134 for my $code ( sort keys %codeGear ) { | |
135 print $fd " C_${code},\n"; | |
136 } | |
137 print $fd "};\n"; | |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
138 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
139 my $code_init = ''; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
140 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
|
141 $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
|
142 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
143 |
410
85b0ddbf458e
Fix CudaWorker
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
406
diff
changeset
|
144 my $data_num = keys(%dataGear); |
85b0ddbf458e
Fix CudaWorker
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
406
diff
changeset
|
145 $data_num++; |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
146 my $context_c = << "EOFEOF"; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
147 #include <stdlib.h> |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
148 |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
149 #include "../context.h" |
277 | 150 |
246
421ea91dd76c
Change initContext type from __code to void
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
210
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 context->heap = context->heapStart; |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
157 // context->codeNum = Exit; |
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 $code_init |
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 #include "dataGearInit.c" |
410
85b0ddbf458e
Fix CudaWorker
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
406
diff
changeset
|
162 context->dataNum = $data_num; |
398
fc4fcd441700
Fix spanwTasks
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
397
diff
changeset
|
163 context->tasks = createSingleLinkedQueue(context); |
210
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
164 } |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
165 EOFEOF |
ba56dab79dc4
separate code init for each executable
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
208
diff
changeset
|
166 |
254 | 167 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
|
168 print $fd $context_c; |
277 | 169 |
170 my $meta_call = <<"EOFEOF"; | |
171 __code meta(struct Context* context, enum Code next) { | |
280 | 172 // printf("meta %d\\n",next); |
406
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
173 if (context->task == NULL) { |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
174 goto (context->code[next])(context); |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
175 } |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
176 context->task = NULL; |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
177 goto (context->code[Gearef(context, TaskManager)->taskManager->TaskManager.spawnTasks])(context); |
277 | 178 } |
179 | |
180 __code start_code(struct Context* context) { | |
181 goto meta(context, context->next); | |
182 } | |
183 | |
184 __code start_code_stub(struct Context* context) { | |
185 goto start_code(context); | |
186 } | |
187 | |
188 __code exit_code(struct Context* context) { | |
189 free(context->code); | |
190 free(context->data); | |
191 free(context->heapStart); | |
192 goto exit(0); | |
193 } | |
194 | |
195 __code exit_code_stub(struct Context* context) { | |
196 goto exit_code(context); | |
197 } | |
198 | |
280 | 199 // end context_c |
277 | 200 EOFEOF |
186 | 201 |
406
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
202 print $fd $meta_call; |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
203 |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
204 open my $fd,">","$ddir/enumData.h" or die("can't open $ddir/enumData.h $!"); |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
205 print $fd "enum DataType {\n"; |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
206 print $fd " D_Code,\n"; |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
207 for my $data ( sort keys %dataGear ) { |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
208 print $fd " D_${data},\n"; |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
209 } |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
210 print $fd "};\n\n"; |
278 | 211 |
406
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
212 open my $fd,">","$ddir/typedefData.h" or die("can't open $ddir/typedefData.h $!"); |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
213 for my $data ( sort keys %dataGear ) { |
436
08a93fc2f0d3
Fix CudaExecutor but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
435
diff
changeset
|
214 if ($dataGear{$data} eq 'struct') { |
08a93fc2f0d3
Fix CudaExecutor but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
435
diff
changeset
|
215 print $fd "typedef struct ${data} ${data};\n"; |
08a93fc2f0d3
Fix CudaExecutor but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
435
diff
changeset
|
216 } |
406
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
217 } |
186 | 218 |
406
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
219 open my $fd,">","$ddir/dataGearInit.c" or die("can't open $ddir/dataGearInit.c $!"); |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
220 for my $data ( sort keys %dataGear ) { |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
221 print $fd " ALLOC_DATA(context, ${data});\n"; |
9b35e6581b5c
Use task member of context by par goto meta
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
398
diff
changeset
|
222 } |
186 | 223 } |
224 | |
225 # end |