位图解包生成稀疏矩阵

如何解决位图解包生成稀疏矩阵

我正在尝试将二进制文件加载到内存中。它有一个特定的编码和有限的字母表,我的意思是每两位代表一个整数值(00 -> 0、01 -> 1、10 -> 2 和 11 -> 3),就是这样。

现在 11 或基本上 3 代表缺失值。我可以使用 BMI2 解包并将缺失值设置为零并填充主矩阵(完整的数据没有缺失),但想创建一个地图或基本上稀疏的数组来存储缺失值的索引。

以下是我目前(使用 BMI2)如何做的演示代码,因为与 2 字节解包相比,我没有发现 avx2 或其他方法有益。如果您不同意,请告诉我!

所以真正的问题是:如何在不使用简单循环的情况下使用 result_and_mask 变量将字节索引存储在数组中?

只是为了添加更多上下文,这是在一个循环中发生的,该循环使用 memmap 读取一列数据并进行解包和存储。

#include <immintrin.h>
#include <stdio.h>
#include <inttypes.h>

void printBits(size_t const size,void const * const ptr,int put)
{
    unsigned char *b = (unsigned char*) ptr;
    unsigned char byte;
    int i,j;
    
    for (i = size-1; i >= 0; i--) {
        for (j = 7; j >= 0; j--) {
            byte = (b[i] >> j) & 1;
            if (byte == 0) {
                printf(".");
            } else{
                printf("1");
            }
            // printf("%u",byte);
            if (j % 2 == 0){
                printf(" ");
            }
        }
        if (put != 0) printf("|");
    }
    if (put != 0) puts("");
}


int main() {
    uint16_t ww = 0xCA07;
    uint64_t result;
    const int PRINT_LINE = 1;
    
    #  define S_CAST(type,val) ((type)(val))

    static const uintptr_t kMask0303 = (~S_CAST(uintptr_t,0)) / 85;
    static const uintptr_t kMask1111 = (~S_CAST(uintptr_t,0)) / 15;

    printf("ww    : ");
    printBits(sizeof(ww),&ww,PRINT_LINE);

    printf("mask03: ");
    printBits(sizeof(kMask0303),&kMask0303,PRINT_LINE);

    printf("result: ");
    result = _pdep_u64(ww,kMask0303);
    printBits(sizeof(result),&result,PRINT_LINE);

    uint64_t result_shift = result >> 1;
    printf("shift : ");
    printBits(sizeof(result_shift),&result_shift,PRINT_LINE);

    uint64_t result_and   = result & result_shift;
    printf("and   : ");
    printBits(sizeof(result_and),&result_and,PRINT_LINE);

    printf("mask01: ");  
    printBits(sizeof(kMask1111),&kMask1111,PRINT_LINE);

    uint64_t result_and_mask   = result_and & kMask1111;
    printf("miss  : ");
    printBits(sizeof(result_and_mask),&result_and_mask,PRINT_LINE);

    // printf("sub   : ");
    // uint64_t result_and_mask_mult   = result_and_mask * 3;
    // printBits(sizeof(result_and_mask_mult),&result_and_mask_mult,PRINT_LINE);


    // printf("fin   : ");
    // uint64_t final   = result - result_and_mask_mult;
    // printBits(sizeof(final),&final,PRINT_LINE);
}

示例输出:

ww    : 11 .. 1. 1. |.. .. .1 11 |
mask03: .. .. .. 11 |.. .. .. 11 |.. .. .. 11 |.. .. .. 11 |.. .. .. 11 |.. .. .. 11 |.. .. .. 11 |.. .. .. 11 |
result: .. .. .. 11 |.. .. .. .. |.. .. .. 1. |.. .. .. 1. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |.. .. .. 11 |
shift : .. .. .. .1 |1. .. .. .. |.. .. .. .1 |.. .. .. .1 |.. .. .. .. |.. .. .. .. |.. .. .. .. |1. .. .. .1 |
and   : .. .. .. .1 |.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |
mask01: .. .1 .. .1 |.. .1 .. .1 |.. .1 .. .1 |.. .1 .. .1 |.. .1 .. .1 |.. .1 .. .1 |.. .1 .. .1 |.. .1 .. .1 |
miss  : .. .. .. .1 |.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |

更新:进一步说明

  • 所需的输出是索引的左压缩列表。在上面的例子中,我需要 0,7。基本上,我有一个指向数组中下一个空点的指针来存储缺失值的索引,比如说它指向元素 100(到目前为止找到了 99 个缺失值),现在我正在读取索引 1000 处的 col 元素,所以我想做的是将(1000 + 7,1000 + 0,顺序无关紧要)添加到缺失的数组中。现在指针将指向 102。
  • 平台最低avx2,这是一个科学程序,主要用于云和/或HPC,CPU主要是intel和Skylake向上(avx-512很常见)所以我假设至少存在 avx2。

更新:用于解包的 AVX2 演示

这是我第一次尝试使用 avx2 进行解包:

#include <immintrin.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdint.h>
#include <string.h> // for memset

void printBits(size_t const size,byte);
            if (j % 2 == 0){
                printf(" ");
            }
        }
        if (put != 0) printf("|");
    }
    if (put != 0) puts("");
}


int main() {
    uint64_t x = 0x1360EA787654321;
    const int PRINT_LINE = 1;

    // mask: where to put each byte
    static const char mask1a[32] = {
        0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07
    };

    static const char mask2a[32] = {
        0x03,0x0c,0x30,0xC0,};

    static const char mask3a[32] = {
        0x00,0x08,0x0C,0x09,0x0D,0x0A,0x0E,0x0B,0x0F,};

    static const char mask4a[32] = {
        0x00,};

    unsigned char out[32];

    __m256i mask2 = _mm256_loadu_si256((__m256i*)mask2a);
    __m256i mask1  = _mm256_loadu_si256((__m256i*)mask1a);
    __m256i mask3  = _mm256_loadu_si256((__m256i*)mask3a);
    __m256i mask4  = _mm256_loadu_si256((__m256i*)mask4a);
    
    union combine { __m128i reg; uint8_t value; };
    const union combine THREE = {.value = 3};
    __m256i mult3  = _mm256_broadcastb_epi8(THREE.reg);

    __m256i y =    _mm256_set1_epi64x(x);
    __m256i z =    _mm256_shuffle_epi8(y,mask1);
    z = _mm256_and_si256(z,mask2);

    _mm256_storeu_si256((__m256i*)out,z);
    
    printf("\nX   : ");
    printBits(sizeof(x),&x,PRINT_LINE);

    for(int i=28; i>=0; i-=4) {
        uint8_t orig = *((uint8_t *)&(x) + (i / 4));
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(orig),&orig,0);
        printf(" --->  ");
        printBits(sizeof(num),&num,PRINT_LINE);
    } printf("\n");

    printf("Result with Missing: \n");
    for(int i=28; i>=0; i-=4) {
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(num),PRINT_LINE);
    } printf("\n");

    z = _mm256_shuffle_epi8(z,mask3);
    _mm256_storeu_si256((__m256i*)out,z);
    printf("Transpose: \n");
    for(int i=28; i>=0; i-=4) {
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(num),PRINT_LINE);
    } printf("\n");


    z = _mm256_srlv_epi32(z,mask4);

    _mm256_storeu_si256((__m256i*)out,z);
    printf("Shift: \n");
    for(int i=28; i>=0; i-=4) {
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(num),PRINT_LINE);
    } printf("\n");


    __m256i z_shifted = _mm256_srli_epi32(z,1);
    _mm256_storeu_si256((__m256i*)out,z_shifted);
    printf("Shifted: \n");
    for(int i=28; i>=0; i-=4) {
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(num),PRINT_LINE);
    } printf("\n");

    __m256i z_and = _mm256_and_si256(z,z_shifted);
    _mm256_storeu_si256((__m256i*)out,z_and);
    printf("Bytes with Missing value (11): \n");
    for(int i=28; i>=0; i-=4) {
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(num),PRINT_LINE);
    } printf("\n");


    __m256i z_and_shift_back = _mm256_slli_epi32(z_and,z_and_shift_back);
    printf("SHIFT BACK: \n");
    for(int i=28; i>=0; i-=4) {
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(num),PRINT_LINE);
    } printf("\n");

    __m256i z_and_shift_back_or = _mm256_or_si256(z_and,z_and_shift_back);
    _mm256_storeu_si256((__m256i*)out,z_and_shift_back_or);
    printf("OR: \n");
    for(int i=28; i>=0; i-=4) {
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(num),PRINT_LINE);
    } printf("\n");

    __m256i z_sub = _mm256_sub_epi8(z,z_and_shift_back_or);
    _mm256_storeu_si256((__m256i*)out,z_sub);
    printf("Result with Missing set to zero: \n");
    for(int i=28; i>=0; i-=4) {
        uint32_t num = *(uint32_t *)&out[i];
        printBits(sizeof(num),PRINT_LINE);
    } printf("\n");
    


}

输出:

X   : .. .. .. .1 |.. 11 .1 1. |.. .. 11 1. |1. 1. .1 11 |1. .. .1 11 |.1 1. .1 .1 |.1 .. .. 11 |.. 1. .. .1 |
.. .. .. .1  --->  .. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |
.. 11 .1 1.  --->  .. .. .. .. |.. 11 .. .. |.. .. .1 .. |.. .. .. 1. |
.. .. 11 1.  --->  .. .. .. .. |.. .. .. .. |.. .. 11 .. |.. .. .. 1. |
1. 1. .1 11  --->  1. .. .. .. |.. 1. .. .. |.. .. .1 .. |.. .. .. 11 |
1. .. .1 11  --->  1. .. .. .. |.. .. .. .. |.. .. .1 .. |.. .. .. 11 |
.1 1. .1 .1  --->  .1 .. .. .. |.. 1. .. .. |.. .. .1 .. |.. .. .. .1 |
.1 .. .. 11  --->  .1 .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 11 |
.. 1. .. .1  --->  .. .. .. .. |.. 1. .. .. |.. .. .. .. |.. .. .. .1 |

Result with Missing:
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |
.. .. .. .. |.. 11 .. .. |.. .. .1 .. |.. .. .. 1. |
.. .. .. .. |.. .. .. .. |.. .. 11 .. |.. .. .. 1. |
1. .. .. .. |.. 1. .. .. |.. .. .1 .. |.. .. .. 11 |
1. .. .. .. |.. .. .. .. |.. .. .1 .. |.. .. .. 11 |
.1 .. .. .. |.. 1. .. .. |.. .. .1 .. |.. .. .. .1 |
.1 .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 11 |
.. .. .. .. |.. 1. .. .. |.. .. .. .. |.. .. .. .1 |

Transpose:
.. .. .. .. |.. .. .. .. |.. .. .. .. |1. .. .. .. |
.. .. .. .. |.. 11 .. .. |.. .. .. .. |.. 1. .. .. |
.. .. .. .. |.. .. .1 .. |.. .. 11 .. |.. .. .1 .. |
.. .. .. .1 |.. .. .. 1. |.. .. .. 1. |.. .. .. 11 |
1. .. .. .. |.1 .. .. .. |.1 .. .. .. |.. .. .. .. |
.. .. .. .. |.. 1. .. .. |.. .. .. .. |.. 1. .. .. |
.. .. .1 .. |.. .. .1 .. |.. .. .. .. |.. .. .. .. |
.. .. .. 11 |.. .. .. .1 |.. .. .. 11 |.. .. .. .1 |

Shift:
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 1. |
.. .. .. .. |.. .. .. 11 |.. .. .. .. |.. .. .. 1. |
.. .. .. .. |.. .. .. .1 |.. .. .. 11 |.. .. .. .1 |
.. .. .. .1 |.. .. .. 1. |.. .. .. 1. |.. .. .. 11 |
.. .. .. 1. |.. .. .. .1 |.. .. .. .1 |.. .. .. .. |
.. .. .. .. |.. .. .. 1. |.. .. .. .. |.. .. .. 1. |
.. .. .. .1 |.. .. .. .1 |.. .. .. .. |.. .. .. .. |
.. .. .. 11 |.. .. .. .1 |.. .. .. 11 |.. .. .. .1 |

Transpose:
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |
.. .. .. .. |.. .. .. 11 |.. .. .. .1 |.. .. .. 1. |
.. .. .. .. |.. .. .. .. |.. .. .. 11 |.. .. .. 1. |
.. .. .. 1. |.. .. .. 1. |.. .. .. .1 |.. .. .. 11 |
.. .. .. 1. |.. .. .. .. |.. .. .. .1 |.. .. .. 11 |
.. .. .. .1 |.. .. .. 1. |.. .. .. .1 |.. .. .. .1 |
.. .. .. .1 |.. .. .. .. |.. .. .. .. |.. .. .. 11 |
.. .. .. .. |.. .. .. 1. |.. .. .. .. |.. .. .. .1 |

Shifted:
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. .1 |1. .. .. .. |1. .. .. .1 |
.. .. .. .. |.. .. .. .. |.. .. .. .1 |1. .. .. .1 |
.. .. .. .1 |.. .. .. .1 |.. .. .. .. |1. .. .. .1 |
.. .. .. .1 |.. .. .. .. |.. .. .. .. |1. .. .. .1 |
.. .. .. .. |1. .. .. .1 |.. .. .. .. |1. .. .. .. |
.. .. .. .. |1. .. .. .. |.. .. .. .. |.. .. .. .1 |
.. .. .. .. |.. .. .. .1 |.. .. .. .. |.. .. .. .. |

Bytes with Missing value (11):
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. .1 |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. .1 |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |

SHIFT BACK:
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. 1. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. 1. |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 1. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 1. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 1. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |

OR:
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. 11 |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. 11 |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 11 |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 11 |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 11 |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .. |

Result with Missing set to zero:
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. .1 |
.. .. .. .. |.. .. .. .. |.. .. .. .1 |.. .. .. 1. |
.. .. .. .. |.. .. .. .. |.. .. .. .. |.. .. .. 1. |
.. .. .. 1. |.. .. .. 1. |.. .. .. .1 |.. .. .. .. |
.. .. .. 1. |.. .. .. .. |.. .. .. .1 |.. .. .. .. |
.. .. .. .1 |.. .. .. 1. |.. .. .. .1 |.. .. .. .1 |
.. .. .. .1 |.. .. .. .. |.. .. .. .. |.. .. .. .. |
.. .. .. .. |.. .. .. 1. |.. .. .. .. |.. .. .. .1 |

我正在使用结果而不遗漏存储完整数组。现在我想使用“缺少值的字节(11)”来获取左打包索引以将索引存储到稀疏数组中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res