4

滑动对角向量包含 16 个元素,每个元素都是 8 位无符号整数。

如果没有 SSE 并稍加简化,它在 C 语言中会看起来像这样:

int width=1000000; // a big number
uint8_t matrix[width][16];
fill_matrix_with_interesting_values(&matrix);

for (int i=0; i < width - 16; ++i) {
  uint8_t diagonal_vector[16];
  for (int j=0; j<16; ++j) {
    diagonal_vector[j] = matrix[i+j][j];
  }
  do_something(&diagonal_vector);
}

但在我的情况下,我只能使用_mm_load_si128内在函数从矩阵中按列(垂直)加载。滑动对角向量水平移动,因此我需要提前加载 16 个列向量,并使用每个列向量中的一个元素来创建对角向量。

是否可以使用 SSE 对此进行快速的低内存实现?

2016 年 11 月 14 日更新:提供更多详细信息。就我而言,我从FASTA 格式的文本文件中读取单字母代码。每个字母代表某种氨基酸。每个氨基酸都有一个与之相关的特定列向量。该列向量是从常量表( BLOSUM矩阵)中查找的。在 C 代码中,它看起来像这样

while (uint8_t c = read_next_letter_from_file()) {
   column_vector = lookup_from_const_table(c)
   uint8_t diagonal_vector[16];
   ... rearrange the values from the latest column
       vectors into the diagonal_vector ...

   do_something(&diagonal_vector)
}
4

1 回答 1

3

我将介绍的实现每次迭代只需加载一列。首先我们初始化一些变量

const __m128i mask1=_mm_set_epi8(0,0,0,0,0,0,0,0,255,255,255,255,255,255,255,255);
const __m128i mask2=_mm_set_epi8(0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255);
const __m128i mask3=_mm_set_epi8(0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255);
const __m128i mask4=_mm_set_epi8(0,255,0,255,0,255,0,255,0,255,0,255,0,255,0,255);
__m128i v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15;

然后对于每一步,变量v_column_load都与下一列一起加载。

v15 = v_column_load;
v7 = _mm_blendv_epi8(v7,v15,mask1);
v3 = _mm_blendv_epi8(v3,v7,mask2);
v1 = _mm_blendv_epi8(v1,v3,mask3);
v0 = _mm_blendv_epi8(v0,v1,mask4);
v_diagonal = v0;

在下一步中v0v1, v3, v7,v15中的变量名数字加 1 并调整到 0 到 15 的范围内。换句话说:newnumber = (oldnumber + 1) 模 16。

v0 = v_column_load;
v8 = _mm_blendv_epi8(v8,v0,mask1);
v4 = _mm_blendv_epi8(v4,v8,mask2);
v2 = _mm_blendv_epi8(v2,v4,mask3);
v1 = _mm_blendv_epi8(v1,v2,mask4);
v_diagonal = v1;

16 次迭代后,v_diagonal将开始包含正确的对角线值。

查看mask1, mask2, mask3, mask4,我们看到了一种模式,该模式可用于将该算法推广到其他向量长度 (2^n)。

例如,对于长度为 8 的向量,我们只需要 3 个掩码,迭代步骤如下所示:

v7 = a a a a a a a a
v6 =
v5 =
v4 =
v3 =         a a a a
v2 =
v1 =             a a
v0 =               a

v0 = b b b b b b b b
v7 = a a a a a a a a
v6 =
v5 =
v4 =         b b b b
v3 =         a a a a
v2 =             b b
v1 =             a b

v1 = c c c c c c c c
v0 = b b b b b b b b
v7 = a a a a a a a a
v6 =
v5 =         c c c c
v4 =         b b b b
v3 =         a a c c
v2 =           a b c

v2 = d d d d d d d d
v1 = c c c c c c c c
v0 = b b b b b b b b
v7 = a a a a a a a a
v6 =         d d d d
v5 =         c c c c
v4 =         b b d d
v3 =         a a c d

v3 = e e e e e e e e
v2 = d d d d d d d d
v1 = c c c c c c c c
v0 = b b b b b b b b
v7 = a a a a e e e e
v6 =         d d d d
v5 =     a a c c e e
v4 =       a b b d a

v4 = f f f f f f f f
v3 = e e e e e e e e
v2 = d d d d d d d d
v1 = c c c c c c c c
v0 = b b b b f f f f
v7 = a a a a e e e e
v6 =     b b d d f f
v5 =     a b c d e f

v5 = g g g g g g g g
v4 = f f f f f f f f
v3 = e e e e e e e e
v2 = d d d d d d d d
v1 = c c c c g g g g
v0 = b b b b f f f f
v7 = a a c c e e g g
v6 =   a b c d e f g

v6 = h h h h h h h h
v5 = g g g g g g g g
v4 = f f f f f f f f
v3 = e e e e e e e e
v2 = d d d d h h h h
v1 = c c c c g g g g
v0 = b b d d f f h h
v7 = a b c d e f g h  <-- this vector now contains the diagonal

v7 = i i i i i i i i
v6 = h h h h h h h h
v5 = g g g g g g g g
v4 = f f f f f f f f
v3 = e e e e i i i i
v2 = d d d d h h h h
v1 = c c e e g g i i
v0 = b c d e f g h i  <-- this vector now contains the diagonal

v0 = j j j j j j j j
v7 = i i i i i i i i
v6 = h h h h h h h h
v5 = g g g g g g g g
v4 = f f f f j j j j
v3 = e e e e i i i i
v2 = d d f f h h j j
v1 = c d e f g h i j  <-- this vector now contains the diagonal

旁注:我在实现 Smith-Waterman 算法时发现了这种加载对角线向量的方法。可以在旧的 SourceForge 项目网页上找到更多信息。

于 2013-03-04T09:12:42.957 回答