| 1 |
#ifdef ABPOA_SIMD_DISPATCH |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include "abpoa.h" |
| 5 |
|
| 6 |
|
| 7 |
#define SIMD_SSE 0x1 |
| 8 |
#define SIMD_SSE2 0x2 |
| 9 |
#define SIMD_SSE3 0x4 |
| 10 |
#define SIMD_SSSE3 0x8 |
| 11 |
#define SIMD_SSE4_1 0x10 |
| 12 |
#define SIMD_SSE4_2 0x20 |
| 13 |
#define SIMD_AVX 0x40 |
| 14 |
#define SIMD_AVX2 0x80 |
| 15 |
#define SIMD_AVX512F 0x100 |
| 16 |
#define SIMD_AVX512BW 0x200 |
| 17 |
|
| 18 |
#ifndef _MSC_VER |
| 19 |
// adapted from https://github.com/01org/linux-sgx/blob/master/common/inc/internal/linux/cpuid_gnu.h |
| 20 |
#define __cpuidex abpoa___cpuidex |
| 21 |
void __cpuidex(int cpuid[4], int func_id, int subfunc_id) { |
| 22 |
#if defined(__x86_64__) |
| 23 |
__asm__ volatile ("cpuid" |
| 24 |
: "=a" (cpuid[0]), "=b" (cpuid[1]), "=c" (cpuid[2]), "=d" (cpuid[3]) |
| 25 |
: "0" (func_id), "2" (subfunc_id)); |
| 26 |
#else // on 32bit, ebx can NOT be used as PIC code |
| 27 |
__asm__ volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" |
| 28 |
: "=a" (cpuid[0]), "=r" (cpuid[1]), "=c" (cpuid[2]), "=d" (cpuid[3]) |
| 29 |
: "0" (func_id), "2" (subfunc_id)); |
| 30 |
#endif |
| 31 |
} |
| 32 |
#endif |
| 33 |
|
| 34 |
static int x86_simd(void) { |
| 35 |
int flag = 0, cpuid[4], max_id; |
| 36 |
__cpuidex(cpuid, 0, 0); |
| 37 |
max_id = cpuid[0]; |
| 38 |
if (max_id == 0) return 0; |
| 39 |
__cpuidex(cpuid, 1, 0); |
| 40 |
if (cpuid[3]>>25&1) flag |= SIMD_SSE; |
| 41 |
if (cpuid[3]>>26&1) flag |= SIMD_SSE2; |
| 42 |
if (cpuid[2]>>0 &1) flag |= SIMD_SSE3; |
| 43 |
if (cpuid[2]>>9 &1) flag |= SIMD_SSSE3; |
| 44 |
if (cpuid[2]>>19&1) flag |= SIMD_SSE4_1; |
| 45 |
if (cpuid[2]>>20&1) flag |= SIMD_SSE4_2; |
| 46 |
if (cpuid[2]>>28&1) flag |= SIMD_AVX; |
| 47 |
if (max_id >= 7) { |
| 48 |
__cpuidex(cpuid, 7, 0); |
| 49 |
if (cpuid[1]>>5 &1) flag |= SIMD_AVX2; |
| 50 |
if (cpuid[1]>>16&1) flag |= SIMD_AVX512F; |
| 51 |
if (cpuid[1]>>30&1) flag |= SIMD_AVX512BW; |
| 52 |
} |
| 53 |
return flag; |
| 54 |
} |
| 55 |
|
| 56 |
static int simd_flag = -1; |
| 57 |
|
| 58 |
|
| 59 |
int simd_abpoa_align_sequence_to_subgraph(abpoa_t *ab, abpoa_para_t *abpt, int beg_node_id, int end_node_id, uint8_t *query, int qlen, abpoa_res_t *res) { |
| 60 |
extern int simd_sse2_abpoa_align_sequence_to_subgraph(abpoa_t *ab, abpoa_para_t *abpt, int beg_node_id, int end_node_id, uint8_t *query, int qlen, abpoa_res_t *res); |
| 61 |
extern int simd_sse41_abpoa_align_sequence_to_subgraph(abpoa_t *ab, abpoa_para_t *abpt, int beg_node_id, int end_node_id, uint8_t *query, int qlen, abpoa_res_t *res); |
| 62 |
extern int simd_avx2_abpoa_align_sequence_to_subgraph(abpoa_t *ab, abpoa_para_t *abpt, int beg_node_id, int end_node_id, uint8_t *query, int qlen, abpoa_res_t *res); |
| 63 |
extern int simd_avx512_abpoa_align_sequence_to_subgraph(abpoa_t *ab, abpoa_para_t *abpt, int beg_node_id, int end_node_id, uint8_t *query, int qlen, abpoa_res_t *res); |
| 64 |
if (simd_flag < 0) simd_flag = x86_simd(); |
| 65 |
if (simd_flag & SIMD_AVX512BW) { |
| 66 |
// fprintf(stderr, "AVX512\n"); |
| 67 |
simd_avx512_abpoa_align_sequence_to_subgraph(ab, abpt, beg_node_id, end_node_id, query, qlen, res); |
| 68 |
} else if (simd_flag & SIMD_AVX2) { |
| 69 |
// fprintf(stderr, "AVX2\n"); |
| 70 |
simd_avx2_abpoa_align_sequence_to_subgraph(ab, abpt, beg_node_id, end_node_id, query, qlen, res); |
| 71 |
} else if (simd_flag & SIMD_SSE4_1) { |
| 72 |
// fprintf(stderr, "SSE41\n"); |
| 73 |
simd_sse41_abpoa_align_sequence_to_subgraph(ab, abpt, beg_node_id, end_node_id, query, qlen, res); |
| 74 |
} else if (simd_flag & SIMD_SSE2) { |
| 75 |
// fprintf(stderr, "SSE2\n"); |
| 76 |
simd_sse2_abpoa_align_sequence_to_subgraph(ab, abpt, beg_node_id, end_node_id, query, qlen, res); |
| 77 |
} else abort(); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
int simd_abpoa_align_sequence_to_graph(abpoa_t *ab, abpoa_para_t *abpt, uint8_t *query, int qlen, abpoa_res_t *res) { |
| 81 |
return simd_abpoa_align_sequence_to_subgraph(ab, abpt, ABPOA_SRC_NODE_ID, ABPOA_SINK_NODE_ID, query, qlen, res); |
| 82 |
} |
| 83 |
|
| 84 |
#endif |