Debian Codegraph @

easel/esl_avx512.c

raw symbols SCIP
1 /* Vectorized routines for x86 AVX-512 instructions
2 *
3 * Most speed-critical code is in the .h file, to facilitate inlining.
4 *
5 * Contents:
6 * 1. Debugging/development routines
7 * 2. Unit tests
8 * 3. Test driver
9 *
10 * This code is conditionally compiled, only when <eslENABLE_AVX512> was
11 * set in <esl_config.h> by the configure script, and that will only
12 * happen on x86 platforms. When <eslENABLE_AVX512> is not set, we
13 * include some dummy code to silence compiler and ranlib warnings
14 * about empty translation units and no symbols, and dummy drivers
15 * that do nothing but declare success.
16 */
17 #include <esl_config.h>
18 #ifdef eslENABLE_AVX512
19
20 #include <stdio.h>
21 #include <x86intrin.h>
22
23 #include "easel.h"
24 #include "esl_avx512.h"
25
26 /*****************************************************************
27 * 1. Debugging/development routines
28 *****************************************************************/
29
30 void
31 esl_avx512_dump_512i_hex8(__m512i v)
32 {
33 uint64_t *val = (uint64_t*) &v;
34 printf("%016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n",
35 val[7], val[6], val[5], val[4], val[3], val[2], val[1], val[0]);
36 }
37
38 /*****************************************************************
39 * 2. Unit tests
40 *****************************************************************/
41 #ifdef eslAVX512_TESTDRIVE
42
43 #include "esl_random.h"
44
45 static void
46 utest_hmax_epu8(ESL_RANDOMNESS *rng)
47 {
48 union { __m512i v; uint8_t x[64]; } u;
49 uint8_t r1, r2;
50 int i,z;
51
52 for (i = 0; i < 100; i++)
53 {
54 r1 = 0;
55 for (z = 0; z < 64; z++)
56 {
57 u.x[z] = (uint8_t) (esl_rnd_Roll(rng, 256)); // 0..255
58 if (u.x[z] > r1) r1 = u.x[z];
59 }
60 r2 = esl_avx512_hmax_epu8(u.v);
61 if (r1 != r2) esl_fatal("hmax_epu8 utest failed");
62 }
63 }
64
65 static void
66 utest_hmax_epi8(ESL_RANDOMNESS *rng)
67 {
68 union { __m512i v; int8_t x[64]; } u;
69 int8_t r1, r2;
70 int i,z;
71
72 for (i = 0; i < 100; i++)
73 {
74 r1 = 0;
75 for (z = 0; z < 64; z++)
76 {
77 u.x[z] = (int8_t) (esl_rnd_Roll(rng, 256) - 128); // -128..127
78 if (u.x[z] > r1) r1 = u.x[z];
79 }
80 r2 = esl_avx512_hmax_epi8(u.v);
81 if (r1 != r2) esl_fatal("hmax_epi8 utest failed");
82 }
83 }
84
85
86 static void
87 utest_hmax_epi16(ESL_RANDOMNESS *rng)
88 {
89 union { __m512i v; int16_t x[32]; } u;
90 int16_t r1, r2;
91 int i,z;
92
93 for (i = 0; i < 100; i++)
94 {
95 r1 = -32768;
96 for (z = 0; z < 32; z++)
97 {
98 u.x[z] = (int16_t) (esl_rnd_Roll(rng, 65536) - 32768); // -32768..32767
99 if (u.x[z] > r1) r1 = u.x[z];
100 }
101 r2 = esl_avx512_hmax_epi16(u.v);
102 if (r1 != r2) esl_fatal("hmax_epi16 utest failed %d %d", r1, r2);
103 }
104 }
105
106 #endif /*eslAVX512_TESTDRIVE*/
107
108 /*****************************************************************
109 * 3. Test driver
110 *****************************************************************/
111
112 #ifdef eslAVX512_TESTDRIVE
113 #include <esl_config.h>
114
115 #include <stdio.h>
116 #include <math.h>
117
118 #include "easel.h"
119 #include "esl_cpu.h"
120 #include "esl_getopts.h"
121 #include "esl_random.h"
122 #include "esl_avx512.h"
123
124 static ESL_OPTIONS options[] = {
125 /* name type default env range toggles reqs incomp help docgroup*/
126 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
127 { "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
128 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
129 };
130 static char usage[] = "[-options]";
131 static char banner[] = "test driver for avx512 module";
132
133 int
134 main(int argc, char **argv)
135 {
136 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
137 ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s"));;
138
139 fprintf(stderr, "## %s\n", argv[0]);
140 fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng));
141
142 if (esl_cpu_has_avx512())
143 {
144 utest_hmax_epu8(rng);
145 utest_hmax_epi8(rng);
146 utest_hmax_epi16(rng);
147 }
148 else
149 {
150 fprintf(stderr, "processor does not support our AVX-512 code; skipping tests.\n");
151 fprintf(stderr, " (we need KNL's F,CD,ER,PF subsets, plus DQ,BW)\n");
152 }
153
154 fprintf(stderr, "# status = ok\n");
155
156 esl_randomness_Destroy(rng);
157 esl_getopts_Destroy(go);
158 return 0;
159 }
160
161 #endif /*eslAVX512_TESTDRIVE*/
162
163
164
165 #else // ! eslENABLE_AVX512
166 #include <stdio.h>
167 void esl_avx512_silence_hack(void) { return; }
168 #if defined eslAVX512_TESTDRIVE || eslAVX512_EXAMPLE || eslAVX512_BENCHMARK
169 int main(void) { fprintf(stderr, "# AVX512 support not compiled.\n"); return 0; }
170 #endif
171 #endif // eslENABLE_AVX512