2 # Permission is hereby granted, free of charge, to any person obtaining a
3 # copy of this software and associated documentation files (the "Software"),
4 # to deal in the Software without restriction, including without limitation
5 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 # and/or sell copies of the Software, and to permit persons to whom the
7 # Software is furnished to do so, subject to the following conditions:
9 # The above copyright notice and this permission notice (including the next
10 # paragraph) shall be included in all copies or substantial portions of the
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 from __future__
import print_function
, division
, absolute_import
28 from modules
import utils
29 from genclbuiltins
import MAX_VALUES
, DATA_SIZES
45 VEC_SIZES
= ['2', '4', '8', '16']
48 DIR_NAME
= os
.path
.join("cl", "builtin", "misc")
51 def gen_array(size
, m
):
52 return [random
.randint(0, m
) for i
in six
.moves
.range(size
)]
55 def permute(data
, mask
, ssize
, dsize
):
56 return [data
[(m
% ssize
) + ((i
// dsize
) * ssize
)]
57 for i
, m
in enumerate(mask
)]
60 def ext_req(type_name
):
61 if type_name
[:6] == "double":
62 return "require_device_extensions: cl_khr_fp64"
63 if type_name
[:4] == "half":
64 return "require_device_extensions: cl_khr_fp16"
68 def print_config(f
, type_name
, utype_name
):
69 f
.write(textwrap
.dedent(("""\
72 name: shuffle {type_name} {utype_name}
74 """ + ext_req(type_name
))
75 .format(type_name
=type_name
, utype_name
=utype_name
)))
78 def begin_test(type_name
, utype_name
):
79 fileName
= os
.path
.join(DIR_NAME
, 'builtin-shuffle-{}-{}.cl'.format(type_name
, utype_name
))
81 f
= open(fileName
, 'w')
82 print_config(f
, type_name
, utype_name
)
88 utils
.safe_makedirs(DIR_NAME
)
90 for t
, ut
in six
.iteritems(TYPES
):
92 for ss
, ds
in itertools
.product(VEC_SIZES
, VEC_SIZES
):
93 ssize
= int(ss
) * ELEMENTS
94 dsize
= int(ds
) * ELEMENTS
98 data
= gen_array(ssize
, MAX_VALUES
['ushort'])
99 mask
= gen_array(dsize
, MAX_VALUES
[ut
])
100 perm
= permute(data
, mask
, int(ss
), int(ds
))
101 f
.write(textwrap
.dedent("""
103 name: shuffle {stype_name} {utype_name}
104 global_size: {elements} 0 0
105 kernel_name: test_shuffle_{stype_name}_{utype_name}
106 arg_out: 0 buffer {dtype_name}[{elements}] {perm}
107 arg_in: 1 buffer {stype_name}[{elements}] {data}
108 arg_in: 2 buffer {utype_name}[{elements}] {mask}
109 """.format(stype_name
=stype_name
, utype_name
=utype_name
,
110 dtype_name
=dtype_name
, elements
=ELEMENTS
,
111 perm
=' '.join([str(x
) for x
in perm
]),
112 data
=' '.join([str(x
) for x
in data
]),
113 mask
=' '.join([str(x
) for x
in mask
]))))
115 f
.write(textwrap
.dedent("""!*/"""))
118 f
.write(textwrap
.dedent("""
119 #pragma OPENCL EXTENSION cl_khr_fp64: enable
122 f
.write(textwrap
.dedent("""
123 #pragma OPENCL EXTENSION cl_khr_fp16: enable
126 for ss
, ds
in itertools
.product(VEC_SIZES
, VEC_SIZES
):
129 f
.write(textwrap
.dedent("""
130 kernel void test_shuffle_{type_name}{ssize}_{utype_name}{dsize}(global {type_name}* out, global {type_name}* in, global {utype_name}* mask) {{
131 vstore{dsize}(shuffle(vload{ssize}(get_global_id(0), in), vload{dsize}(get_global_id(0), mask)), get_global_id(0), out);
133 """.format(type_name
=t
, utype_name
=ut
, ssize
=ss
, dsize
=ds
)))
138 if __name__
== '__main__':