This forum has been archived. All content is frozen. Please use KDE Discuss instead.

PyBind11 Sparse Solver

Tags: None
(comma "," separated)
ejaro
Registered Member
Posts
1
Karma
0

PyBind11 Sparse Solver

Thu Feb 09, 2023 12:30 pm
Hello,

I try to build a fast sparse solver with Eigen and OpenMP for Python. For the interface between this solver and Python I use the PyBind11 package. Basically, the solver works fine, but unfortunately it only runs on one core and I cannot figure how to use all cores of my cpu.

Although the OpenMP test functions uses the entire cpu.

Here is the C++ code:
Code: Select all
#include <iostream>
#include <cmath>
#include <omp.h>
#include <unistd.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

#include <Eigen/Sparse>
#include <Eigen/IterativeLinearSolvers>

void openmp_test()
{
    int N;

    N = 8;

    // omp_set_num_threads(4);

    #pragma omp parallel for
    for (int i=0; i<N; i=i+1)
    {
        sleep(10);
    }

}

void eigen_test(int N)
{
    Eigen::SparseMatrix<double> A(N, N);
    Eigen::SparseMatrix<double> b(N, 1);
    Eigen::SparseMatrix<double> x(N, 1);
    Eigen::BiCGSTAB<Eigen::SparseMatrix<double>> solver;

    A.reserve(5*N);
    b.reserve(N);
    x.reserve(N);

    for(int i=0; i<N; i++)
    {   
        b.insert(i, 0) = 20.0;

        for(int j=(i-2); j<=(i+2); j++)
        {
            if(j == i)
            {
                A.insert(j, i) = 10.0;
            }
            else if((j >= 0) && (j < N))
            {
                A.insert(j, i) = 5.0;
            }
        }
    }

    solver.compute(A);
    x = solver.solve(b);
}

PYBIND11_MODULE(mytest, m)
{
    m.def("openmp_test", &openmp_test, py::call_guard<py::gil_scoped_release>());
    m.def("eigen_test", &eigen_test, py::call_guard<py::gil_scoped_release>());
}


Here the Compiler code:
Code: Select all
g++ \
    -O3 \
    -Wall \
    -shared \
    -std=c++14 \
    -fopenmp \
    -fPIC \
    -I /usr/local/lib/python3.10/dist-packages/pybind11/include \
    -I /usr/include/python3.10 \
    -I /workspaces/pybind11/external/eigen \
    mytest.cpp \
    -o mytest.so


And finally here the Python code:
Code: Select all
from time import time
import mytest

N = 10 * 10**3

start = time()
mytest.openmp_test()
print("runtime: {:.3f} s".format(time() - start))


start = time()
mytest.eigen_test(N)
print("runtime: {:.3e} s".format(time() - start))


Has anyone an idea how to fix this problem?

Thanks a lot.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], kesang, Yahoo [Bot]