利用DuckDB luajit插件和openblas库对表中数据做矩阵运算

  1. 安装openblas库

    C:\d>wsl
    root@DESKTOP-59T6U68:/mnt/c/d# docker start gcc
    gcc
    root@DESKTOP-59T6U68:/mnt/c/d# docker exec -it gcc bash
    root@6ae32a5ffcde:/# cd /par
    root@6ae32a5ffcde:/par# whereis libopenblas.so
    libopenblas.so:
    root@6ae32a5ffcde:/par# apt install libopenblas0
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following additional packages will be installed:
    libgfortran5 libopenblas0-pthread
    The following NEW packages will be installed:
    libgfortran5 libopenblas0 libopenblas0-pthread
    0 upgraded, 3 newly installed, 0 to remove and 117 not upgraded.
    Need to get 7534 kB of archives.
    After this operation, 47.4 MB of additional disk space will be used.
    N: Ignoring file 'debian.sources_org' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension
    Do you want to continue? [Y/n]
    Get:1 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/main amd64 libgfortran5 amd64 12.2.0-14+deb12u1 [793 kB]
    Get:2 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/main amd64 libopenblas0-pthread amd64 0.3.21+ds-4 [6709 kB]
    Get:3 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/main amd64 libopenblas0 amd64 0.3.21+ds-4 [32.6 kB]
    Fetched 7534 kB in 0s (15.4 MB/s)
    debconf: delaying package configuration, since apt-utils is not installed
    Selecting previously unselected package libgfortran5:amd64.
    (Reading database ... 65497 files and directories currently installed.)
    Preparing to unpack .../libgfortran5_12.2.0-14+deb12u1_amd64.deb ...
    Unpacking libgfortran5:amd64 (12.2.0-14+deb12u1) ...
    Selecting previously unselected package libopenblas0-pthread:amd64.
    Preparing to unpack .../libopenblas0-pthread_0.3.21+ds-4_amd64.deb ...
    Unpacking libopenblas0-pthread:amd64 (0.3.21+ds-4) ...
    Selecting previously unselected package libopenblas0:amd64.
    Preparing to unpack .../libopenblas0_0.3.21+ds-4_amd64.deb ...
    Unpacking libopenblas0:amd64 (0.3.21+ds-4) ...
    Setting up libgfortran5:amd64 (12.2.0-14+deb12u1) ...
    Setting up libopenblas0-pthread:amd64 (0.3.21+ds-4) ...
    update-alternatives: using /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 to provide /usr/lib/x86_64-linux-gnu/libblas.so.3 (libblas.so.3-x86_64-linux-gnu) in auto mode
    update-alternatives: using /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 to provide /usr/lib/x86_64-linux-gnu/liblapack.so.3 (liblapack.so.3-x86_64-linux-gnu) in auto mode
    update-alternatives: using /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblas.so.0 to provide /usr/lib/x86_64-linux-gnu/libopenblas.so.0 (libopenblas.so.0-x86_64-linux-gnu) in auto mode
    Setting up libopenblas0:amd64 (0.3.21+ds-4) ...
    Processing triggers for libc-bin (2.36-9+deb12u13) ...

2.安装duckdb luajit插件linalg库,如果网络不畅安装失败,可以再试

复制代码
root@6ae32a5ffcde:/par# ./duckdb155
DuckDB v1.5.5 (Variegata)
Enter ".help" for usage hints.
memory D load luajit;
memory D SELECT * FROM luajit_module(mode := 'install', sql_name := 'linalg');
┌─────────┬─────────┬─────────┬───────────────────────────────────────────────────────────────────┬─────────┬──────────┐
│   ok    │  mode   │  phase  │                              message                              │ detail  │ sql_name │
│ boolean │ varchar │ varchar │                              varchar                              │ varchar │ varchar  │
├─────────┼─────────┼─────────┼───────────────────────────────────────────────────────────────────┼─────────┼──────────┤
│ false   │ install │         │ install 'linalg' failed: IO Error: Timeout was reached error for  │         │ linalg   │
│         │         │         │ HTTP HEAD to 'https://raw.githubusercontent.com/alitrack/duckdb-l │         │          │
│         │         │         │ uajit-libs/main/INDEX'                                            │         │          │
└─────────┴─────────┴─────────┴───────────────────────────────────────────────────────────────────┴─────────┴──────────┘
memory D SELECT * FROM luajit_module(mode := 'install', sql_name := 'linalg');
┌─────────┬─────────┬─────────┬──────────────────────────────────────┬──────────────────────────────────────┬──────────┐
│   ok    │  mode   │  phase  │               message                │                detail                │ sql_name │
│ boolean │ varchar │ varchar │               varchar                │               varchar                │ varchar  │
├─────────┼─────────┼─────────┼──────────────────────────────────────┼──────────────────────────────────────┼──────────┤
│ true    │ install │ install │ installed 'linalg' (UDF) --- cached at │ luajit_table('linalg') or luajit_s(' │ linalg   │
│         │         │         │  /root/.duckdb/luajit-libs/linalg.lu │ linalg', ...)                        │          │
│         │         │         │ a                                    │                                      │          │
└─────────┴─────────┴─────────┴──────────────────────────────────────┴──────────────────────────────────────┴──────────┘

3.计算常量和表中数据的运算结果

复制代码
memory D SELECT luajit_s('linalg', {'op':'matmul', 'a':[1,2,3,4], 'm':2, 'n':2,'b':[5,6,7,8], 'mb':2, 'nb':2});
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ luajit_s('linalg', main.struct_pack(op := 'matmul', a := main.list_value(1, 2, 3, 4), m := 2, n := 2, b := main.list │
│                                        _value(5, 6, 7, 8), mb := 2, nb := 2))                                        │
│                                                       varchar                                                        │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"c":[19,22,43,50]}                                                                                                  │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
memory D create table d (a float[],b float[]);
memory D insert into d select range(1,101)::float[],range(2,202,2)::float[];
memory D SELECT luajit_s('linalg', {'op':'matmul', 'a':a, 'm':2, 'n':50,'b':b, 'mb':50, 'nb':2})from d where length(a)=100;
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ luajit_s('linalg', main.struct_pack(op := 'matmul', a := a, m := 2, n := 50, b := b, mb := 50, nb := 2)) │
│                                                 varchar                                                  │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"c":[169150,171700,419150,426700]}                                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────┘

如果把内存数据库改成文件数据库,下次打开数据库时,已安装的luajia库会自动加载。

相关推荐
kv110几秒前
Android studio国内开发有关
java·数据库·android studio
哭泣方源炼蛊37 分钟前
正则表达式c++内容汇集
数据库·c++·mysql·正则表达式
余槐i1 小时前
EXPLAIN 显示走了索引查询仍慢:回表与选择性在 500 万行表上拉开 10 倍耗时
数据库·postgresql·性能优化·vacuum·explain
深度智能Ai1 小时前
IndexTTS‑2高清音质接口对接文档
数据库
香瓜子rd2 小时前
MySQL InnoDB 并发控制核心原理:事务、隔离级别、MVCC 与锁机制
数据库·后端
Elastic 中国社区官方博客2 小时前
Elasticsearch Serverless 如何通过 hollow shards 将索引节点关闭次数降低 30%
大数据·数据库·elasticsearch·搜索引擎·serverless·全文检索
ycvv2 小时前
手机启动失败后的数据提取评估:系统状态、加密条件与文件验收
服务器·数据库·智能手机
不恋水的雨2 小时前
gbase中union all导致末尾多出空格的坑
数据库·sql·mysql
AI 算法大模型备案~当当3 小时前
各地备案数量怎么看:一份属地公告的认读与台账方法
java·数据库·人工智能
梦帮科技4 小时前
量子张量网络破局大模型:从矩阵乘积态 (MPS) 到张量列 (TT-SVD) 低秩收缩全推导
网络·数据结构·数据库·线性代数·矩阵·架构·模拟退火算法