1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#!/usr/bin/perl
# 0 line number
# 1 keyword
# 2 action
open DIRECTIVES, "| cat -n | grep '//_def_' | sed 's/ .*\\\/\\\/_def_/ / ' > directives__" or die "writing directives" ;
open TEMP1, "> tmp1__" or die "making copy of code" ;
while (<STDIN>) {
print DIRECTIVES ;
print TEMP1 ;
}
close DIRECTIVES ;
close TEMP1 ;
open DEBUG, "> debug__" or die ;
## first pass, preprocess the directives
open DIRECTIVES, "< directives__" or die ;
while (<DIRECTIVES>) {
print TEMP1 ;
$line = $_ ;
@t = split( ' ',$line ) ;
$keyword = $t[1] ;
$action = $t[2] ;
@actionlist = split( ';', $action ) ;
foreach $act ( @actionlist ) {
@dim_vlist = split ( ':', $act ) ;
$dim = $dim_vlist[0] ; $vlist = $dim_vlist[1] ;
foreach $v ( split( ',', $vlist ) ) {
$vars{$v} = $v ;
$dimensionality{$v} = $dim ;
if ( $keyword eq "arg" ) {
$key{$v} = $keyword ;
if ( $dim eq "ikj" ) { $ikj_args{$v} = $v ; }
if ( $dim eq "ij" ) { $ij_args{$v} = $v ; }
}
if ( $keyword eq "local" ) {
$key{$v} = $keyword ;
if ( $dim eq "k" ) { $k_local{$v} = $v ; }
}
if ( $keyword eq "register" ) {
$key{$v} = $keyword ;
if ( $dim eq "0" ) { $register{$v} = $v ; }
}
if ( $keyword eq "copy_up_mem" ) {
if ( $key{$v} ne "arg" ) {
print "//warning: copy_up_mem of $v when $v is not arg.\n" ;
} else {
$copy_up_mem{$v} = $dim ;
if ( $dim eq "ikj" ) { $ikj_shared{$v} = $v ; }
if ( $dim eq "ij" ) { $ij_shared{$v} = $v ; }
}
}
if ( $keyword eq "shared_mem_local" ) {
$key{$v} = $keyword ;
$shared_mem_local{$v} = $dim ;
if ( $dim eq "ikj" ) { $ikj_shared{$v} = $v ; }
if ( $dim eq "ij" ) { $ij_shared{$v} = $v ; }
}
if ( $keyword eq "copy_down_mem" ) {
if ( $key{$v} ne "arg" ) {
print "//warning: copy_down_mem of $v when $v is not arg.\n" ;
} else {
$copy_down_mem{$v} = $dim ;
if ( $dim eq "ikj" ) { $ikj_shared{$v} = $v ; }
if ( $dim eq "ij" ) { $ij_shared{$v} = $v ; }
}
}
}
}
}
close DIRECTIVES ;
## seond pass, modify the code
## and preprocess deferred directives
$spton=0 ;
open TEMP1, "< tmp1__" or die ;
while (<TEMP1>) {
$line = $_ ;
# toggle on and off between SPTSTART and SPTSTOP
if ( $line =~ "SPTSTART" ) { $spton = 1 ; }
elsif ( $line =~ "SPTSTOP" ) { $spton = 0 ; }
if ( $spton == 1 ) {
# handle copy_up_mem and copy_down_mem directives in line
if ( $line =~ m/\/\/\s*_def_\s+copy_up_mem\s/ ) {
@t = split( ' ',$line ) ;
$action = $t[2] ;
@dim_vlist = split ( ':', $action ) ;
$vlist = $dim_vlist[1] ;
foreach $v ( split( ',', $vlist ) ) {
print "LOCSM(${v}_s,bx*by*kx) ;\n" ;
}
print "{ int k ; \n" ;
foreach $v ( split( ',', $vlist ) ) {
print "for(k=kps-1;k<kpe;k++){${v}_s[S3(ti,k,tj)]=${v}[P3(ti,k,tj)];}\n" ;
}
print "}\n" ;
}
elsif ( $line =~ m/\/\/\s*_def_\s+register\s/ ) {
@t = split( ' ',$line ) ;
$action = $t[2] ;
@dim_vlist = split ( ':', $action ) ;
$vlist = $dim_vlist[1] ;
foreach $v ( split( ',', $vlist ) ) {
print "float ${v}_reg ;\n" ;
}
}
elsif ( $line =~ m/\/\/\s*_def_\s+shared_mem_local\s/ ) {
@t = split( ' ',$line ) ;
$action = $t[2] ;
@dim_vlist = split ( ':', $action ) ;
$vlist = $dim_vlist[1] ;
foreach $v ( split( ',', $vlist ) ) {
print "LOCSM(${v}_s,bx*by*kx) ;\n" ;
}
}
elsif ( $line =~ m/\/\/\s*_def_\s+copy_down_mem\s/ ) {
@t = split( ' ',$line ) ;
$action = $t[2] ;
@dim_vlist = split ( ':', $action ) ;
$vlist = $dim_vlist[1] ;
print "{ int k ; \n" ;
foreach $v ( split( ',', $vlist ) ) {
print "for(k=kps-1;k<kpe;k++){${v}[P3(ti,k,tj)]=${v}_s[S3(ti,k,tj)];}\n" ;
}
print "}\n" ;
}
elsif ( $line =~ m/\/\/\s*_def_\s+local\s/ ) {
$line = $_ ;
@t = split( ' ',$line ) ;
$keyword = $t[1] ;
$action = $t[2] ;
@actionlist = split( ';', $action ) ;
foreach $act ( @actionlist ) {
@dim_vlist = split ( ':', $act ) ;
$dim = $dim_vlist[0] ; $vlist = $dim_vlist[1] ;
foreach $v ( split( ',', $vlist ) ) {
if ( $dim eq "k" ) {
print "#if (FLOAT_4 == 4)\n" ;
print " Float4 ${v}[MKX] ; \n" ;
print "#else\n" ;
print " float ${v}[MKX] ; \n" ;
print "#endif\n" ;
}
}
}
}
# otherwise do not touch lines with // in them
elsif ( ! ($line =~ m/\/\//) ) {
@t = split( /\W+/,$line ) ;
%seen = "" ;
foreach $token ( @t ) {
if ( ! $seen{$token} ) {
$seen{$token} = $token ;
foreach $v ( keys %vars ) {
if ( "$v" eq "$token" ) {
$dim = $dimensionality{$v} ;
$keyw = $key{$v} ;
$nodex=0 ;
if ( $keyw eq "arg" || $keyw eq "shared_mem_local" ) {
if ( $copy_up_mem{$v} || $shared_mem_local{$v} ) {
if ( $dim eq "ikj" ) { $orig = $v."\\[\(.*\?\)\\]" ; $repl1 = $v."_s"."A|S3(ti," ; $repl2 = ",tj)B|" ; }
elsif ( $dim eq "ij" ) { $orig = $v ; $repl1 = $v."_s"."A|S2(ti,tj)B|" ; $repl2 = "" ; }
} else {
if ( $dim eq "ikj" ) { $orig = $v."\\[\(.*\?\)\\]" ; $repl1 = $v."A|P3(ti," ; $repl2 = ",tj)B|" ; }
elsif ( $dim eq "ij" ) { $orig = $v ; $repl1 = $v."A|P2(ti,tj)B|" ; $repl2 = "" ; }
}
} elsif ( $keyw eq "register" ) {
if ( $dim eq "0" ) { $orig = $v."\\[\(.*\?\)\\]" ; $repl1 = $v."_reg" ; $repl2 = "" ; $nodex=1}
} elsif ( $keyw eq "local" ) {
if ( $dim eq "k" ) { $orig = $v."\\[\(.*\?\)\\]" ; $repl1 = $v."A|" ; $repl2 = "B|" ; }
}
# these repetitions are to handle multiple instances of the
# variable being indexed differently on the same line.
if ( $nodex == 0 ) {
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$2$repl2$3/g ;
} else {
$line =~ s/(\W)$orig(\W)/$1$repl1$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$repl2$3/g ;
$line =~ s/(\W)$orig(\W)/$1$repl1$repl2$3/g ;
}
$line =~ s/A\|/[/g ;
$line =~ s/B\|/]/g ;
}
}
}
}
}
}
print $line ;
}
close TEMP1 ;
close DEBUG ;
unlink "directives__" ;
unlink "tmp1__" ;
unlink "debug__" ;
|