pybind11 与 numpy 的交互
最编程
2024-07-15 09:18:09
...
1 #include <pybind11/pybind11.h>
2 #include <pybind11/numpy.h>
3
4 namespace py = pybind11;
5
6 py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
7 py::buffer_info buf1 = input1.request(), buf2 = input2.request();
8
9 if (buf1.ndim != 1 || buf2.ndim != 1)
10 throw std::runtime_error("Number of dimensions must be one");
11
12 if (buf1.size != buf2.size)
13 throw std::runtime_error("Input shapes must match");
14
15 /* No pointer is passed, so NumPy will allocate the buffer */
16 auto result = py::array_t<double>(buf1.size);
17
18 py::buffer_info buf3 = result.request();
19
20 double *ptr1 = (double *) buf1.ptr,
21 *ptr2 = (double *) buf2.ptr,
22 *ptr3 = (double *) buf3.ptr;
23
24 for (size_t idx = 0; idx < buf1.shape[0]; idx++)
25 ptr3[idx] = ptr1[idx] + ptr2[idx];
26
27 return result;
28 }
29
30 PYBIND11_MODULE(test, m) {
31 m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
32 }
推荐阅读
-
Python与Redis的交互:玩转哈希数据结构
-
实用技巧分享:Sip与WebRTC的交互对接——介绍SRProxy开源库
-
构建三层交换与VTP、STP及Eth-Trunk融合实验:基于双核架构实现高效、稳定、冗余网络" 实验目标: 利用双核心结构,整合三层交换技术、VTP (Virtual Trunking Protocol)、生成树协议 (STP) 和以太网通道 (Eth-Trunk),达成网络高速切换、高可用性、高可靠性和备份连接,适用于双核心拓扑场景。 实验步骤: 1. VLAN划分与IP地址规划: - VLAN10:192.168.10.0/24,默认网关192.168.10.1 - VLAN20:192.168.20.0/24,默认网关192.168.20.1 - VLAN30:192.168.30.0/24,默认网关192.168.30.1 - VLAN40:192.168.40.0/24,默认网关192.168.40.1 2. MSW1三层交换机配置: - 开启三层路由功能:`MSW1(config)#ip routing` - 配置Trunk接口:`MSW1(config-if-range)#switchport trunk encapsulation dot1q; switchport mode trunk` - 设置VTP域和模式:`MSW1(config)#vtp domain test; vtp mode server; vtp password cisco; vtp version 2` - 创建并命名VLAN:`MSW1(config)#vlan 10; MSW1(config-vlan)#name caiwu; ... (为其他VLAN重复操作)` - 分配VLAN IP地址:`MSW1(config-if)#int vlan 10; ip address 192.168.10.1 255.255.255.0; no shutdown; ... (为其他VLAN重复操作)` - 配置生成树优先级:`MSW1(config)#spanning-tree vlan 10,20 root primary; MSW1(config)#spanning-tree vlan 30,40 root secondary` - 链接Eth-Trunk通道:`MSW1(config-if-range)#channel-group 1 mode on; ... (为其他相关接口重复操作)` 3. MSW2三层交换机配置类似MSW1,只需更改VLAN名称和IP地址以及相关端口配置即可。 通过以上步骤,两台三层交换机之间可通过Eth-Trunk实现高速数据交互,并通过VTP统一管理VLAN资源,STP则确保了网络流量的负载均衡,从而构建了一个高效、稳定且具备冗余保护的网络环境。
-
Mac原生激活版原则:无缝对接Sketch与Figma的交互设计利器
-
C# 实时监控Redis:轻松掌握C#与Redis的交互指南
-
HTML/JS 与 Android 的跨平台交互方法
-
探索4种不同的Form表单交互方式:前端与后端的连接艺术
-
用Numba优化Numpy运算时遇到的问题与解决策略
-
pybind11 与 C++ 绑定类和回调函数的交互
-
pybind11 与 numpy 的交互