オッサンはDesktopが好き

自作PCや機械学習、自転車のことを脈絡無く書きます

Debug the cpp library built with Boost.numpy

This is personal note.

  • Debugging boost.numpy libraries for python using gdb.
  • The article is continued from *1.

1. Write cpp to build libraries as executable

test_boost.cpp

#include "./sample.cpp"

#include <iostream>
#include <fstream>
#include <random>
#include <sys/stat.h>
using namespace std;

double rand_uniform(double min, double max)
{
    double width = max - min;
    std::random_device rnd;
    std::mt19937 mt(rnd());
    std::uniform_int_distribution<> rand10000(0, 10000*width);
    int randi = rand10000(mt);
    double val = min + (float)randi/10000.0;
    return val;
}

int main(int argc, char* argv[])
{
    cout << "Hellow" << endl;
    Py_Initialize();
    np::initialize();
    
    double a[5*5];
    double b[5*5];
    int m_size = 5*5;
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            a[i*5 + j] = rand_uniform(-1.0, 1.0); //random.uniform(-10.0, 10.0)
            b[i*5 + j] = rand_uniform(-1.0, 1.0); //random.uniform(-10.0, 10.0)
        }
    }

    cout << "input a:" << endl;
    cout << a[0] << "," << a[1] << "," << a[2] << "," << a[3] << "," << a[4] << endl;
    cout << a[5] << "," << a[6] << "," << a[7] << "," << a[8] << "," << a[9] << endl;
    cout << a[10] << "," << a[11] << "," << a[12] << "," << a[13] << "," << a[14] << endl;
    cout << a[15] << "," << a[16] << "," << a[17] << "," << a[18] << "," << a[19] << endl;
    cout << a[20] << "," << a[21] << "," << a[22] << "," << a[23] << "," << a[24] << endl;

    cout << "input b:" << endl;
    cout << b[0] << "," << b[1] << "," << b[2] << "," << b[3] << "," << b[4] << endl;
    cout << b[5] << "," << b[6] << "," << b[7] << "," << b[8] << "," << b[9] << endl;
    cout << b[10] << "," << b[11] << "," << b[12] << "," << b[13] << "," << b[14] << endl;
    cout << b[15] << "," << b[16] << "," << b[17] << "," << b[18] << "," << b[19] << endl;
    cout << b[20] << "," << b[21] << "," << b[22] << "," << b[23] << "," << b[24] << endl;

    p::tuple shape = p::make_tuple(m_size);
    p::tuple stride = p::make_tuple(sizeof(double));
    np::dtype dt = np::dtype::get_builtin<double>();
    np::ndarray a_np = np::from_data(&a[0], dt, shape, stride, p::object());
    np::ndarray b_np = np::from_data(&b[0], dt, shape, stride, p::object());

    np::ndarray result_np = multiply_matrix(a_np, b_np, 5);

    double *result = reinterpret_cast<double *>(result_np.get_data());
    cout << "result:" << endl;
    cout << result[0] << "," << result[1] << "," << result[2] << "," << result[3] << "," << result[4] << endl;
    cout << result[5] << "," << result[6] << "," << result[7] << "," << result[8] << "," << result[9] << endl;
    cout << result[10] << "," << result[11] << "," << result[12] << "," << result[13] << "," << result[14] << endl;
    cout << result[15] << "," << result[16] << "," << result[17] << "," << result[18] << "," << result[19] << endl;
    cout << result[20] << "," << result[21] << "," << result[22] << "," << result[23] << "," << result[24] << endl;

    return 0;
}

A Little worry about casting 2 dimensional array to double*....

2. Modify task.json

{
    "tasks": [
        {
            "type": "shell",
            "label": "g++-7 build active file",
            "command": "/usr/bin/g++-7",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++11",
                "-I/opt/boost_1_67_0/include",
                "-I/usr/include/python3.6",
                "-I/usr/include/x86_64-linux-gnu/python3.6",
                "-L/usr/bin",
                "-lpython3.6m",
                "-L/opt/boost_1_67_0/lib",
                "-lboost_numpy36",
                "-lboost_python36"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}

f:id:changlikesdesktop:20210128170240p:plain:w400
task.json. . Modify the green-surrounded part according to your environment

3. Register boost libraries

.bashrc

export LD_LIBRARY_PATH="/opt/boost_1_67_0/lib:$PATH"

4. Degug from VSCode

Good luck!!!