Skip to content

Instantly share code, notes, and snippets.

Exposing raw C++ buffers as NumPy arrays using Pybind11

Sometimes you have to work with C++ libraries that use raw pointers and arrays that you need to have access to from python. Indeed, working with C styles arrays is sometimes considered bad practice but many people still do...

The solution here is for the C++ side to provide the raw memory address of the data buffer, and then use numpy's array interface mechanism to create an ndarray that uses this buffer.

This allows the user to view and change elements of the buffers with no copies in a way that is visible to both C++ and Python sides.

@abidrahmank
abidrahmank / imagenet1000_clsid_to_human.txt
Created March 15, 2017 19:06 — forked from yrevar/imagenet1000_clsidx_to_labels.txt
text: imagenet 1000 class id to human readable labels
{0: 'tench, Tinca tinca',
1: 'goldfish, Carassius auratus',
2: 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias',
3: 'tiger shark, Galeocerdo cuvieri',
4: 'hammerhead, hammerhead shark',
5: 'electric ray, crampfish, numbfish, torpedo',
6: 'stingray',
7: 'cock',
8: 'hen',
9: 'ostrich, Struthio camelus',
@abidrahmank
abidrahmank / dilatedConvolution.ipynb
Created June 16, 2016 05:19
Dilated Convolution
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#include <stdio.h>
int square(int x);
int cube(int x);
void map(int, int, int func(int));
int main(){
map(1,5,square);
map(1,5,cube);
}
#include <stdio.h>
typedef unsigned int uint;
__global__ void square(uint *d_out, uint *d_in, int rows){
int loc = blockIdx.x*rows + 2*threadIdx.x;
int f = d_in[loc]+d_in[loc+1];
d_out[loc/2] = f*f;
// printf("%d %f %f %f %f %f \n", loc, d_in[loc], d_in[loc+1], f, temp, d_out[loc/2]);
}
@abidrahmank
abidrahmank / RGB2Gray_cuda.cu
Created October 17, 2013 08:02
RGB2Gray CUDA implementation
#include <stdio.h>
__global__ void kernel(unsigned char* d_in, unsigned char* d_out){
int idx = blockIdx.x;
int idy = threadIdx.x;
int gray_adr = idx*64 + idy; // calculating address for writing grayscale value
int clr_adr = 3*gray_adr; // calculating address for reading RGB values
if(gray_adr<(64*64))
@abidrahmank
abidrahmank / rgb2gray_main.cpp
Created October 17, 2013 07:56
RGB2Gray cpp file
#include <opencv2/core/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include <cuda.h>
#include <cuda_runtime.h>
using namespace std;
using namespace cv;
@abidrahmank
abidrahmank / gist:6913200
Created October 10, 2013 04:52
tablet spec
UNKNOWN DEZIRE
SYSTEM (Android 4.0.4)
Model: Dezire
Manufacturer: unknown
Device: nuclear-evb
Product: nuclear_evb
Brand: softwinners
Android Version: 4.0.4 (Ice Cream Sandwich)
API Level: 15
[ 22.904]
X.Org X Server 1.13.3
Release Date: 2013-03-07
[ 22.904] X Protocol Version 11, Revision 0
[ 22.904] Build Operating System: 2.6.32-358.2.1.el6.x86_64
[ 22.904] Current Operating System: Linux localhost.localdomain 3.10.11-100.fc18.x86_64 #1 SMP Mon Sep 9 13:06:31 UTC 2013 x86_64
[ 22.904] Kernel command line: BOOT_IMAGE=/vmlinuz-3.10.11-100.fc18.x86_64 root=UUID=21e9312d-15f0-4a51-99a6-446bfab1c7e4 ro rd.md=0 rd.lvm=0 rd.dm=0 rd.luks=0 vconsole.keymap=us rhgb quiet LANG=en_US.UTF-8 nouveau.modeset=0 rd.driver.blacklist=nouveau
[ 22.904] Build Date: 17 April 2013 04:51:09AM
[ 22.904] Build ID: xorg-x11-server 1.13.3-3.fc18
[ 22.904] Current version of pixman: 0.28.0
# nvidia-xconfig: X configuration file generated by nvidia-xconfig
# nvidia-xconfig: version 310.51 (buildmeister@swio-display-x86-rhel47-04) Wed May 1 18:10:19 PDT 2013
Section "ServerLayout"
Identifier "Layout0"
Screen 0 "Screen0"
InputDevice "Keyboard0" "CoreKeyboard"
InputDevice "Mouse0" "CorePointer"
EndSection