Wednesday, July 7, 2021

Assembly of Python External C++ procedure using 2D vector

I was unable to return 2D vector from C++ to Python via Python API. Thus content of 2D vector has been written to file and later on the Python Runtime Module displayed the content of file been created inside C++ procedure.

Original task supposed to be solved :-

Let M be the sum of divisors as close as possible to square the root of the number, not counting the unit and the number itself. Square root (i) should not be considered as a divisor. If there are no divisors other than square root, one and the number itself, the value of M is considered equal 0. Write a program that iterates over integers greater than 713000218 in ascending order and searches among them for those for which the value of M multiples of 10 and more M for the previous found number. Output the first 25 found numbers and the corresponding values of M.

The numbers here are significantly increased compare with original version of #25 of E.Djobs's stream as of 05/16/2021 to make pure python performance extremely low versus C++

 (.env) [boris@fedora33server ABCFORWARD]$ cat procVector.h

#pragma once

#include <iostream>

#include <vector>

#include <cstddef>

namespace abc {

std::vector<std::vector<int> >  resultList(int m, int depth);

}

(.env) [boris@fedora33server ABCFORWARD]$ cat procVector.cpp

#include <iostream>

#include <vector>

#include <cstddef>

#include <cmath>

#pragma GCC diagnostic ignored "-Wsign-compare"

namespace abc {

std::vector<std::vector<int> >  resultList(int m, int depth)

{

   std::vector<std::vector<int> > vec;

   std::vector<int> v1;

    int M,minDiv,currMV,div;

    M = 0;

    for (int i = m; i < 10000000000 ; i++) {

        minDiv = 0;    

        for (div = (int)sqrt(i); div > 1; div--)

        {

          if (i%div == 0 && div*div != i)

             {

              minDiv = div ;

              break ;

             }

         }

        if (minDiv != 0)

           currMV = (i/minDiv) + minDiv;

        if (currMV % 10 == 0 && currMV > M)

         {

           v1.push_back(i);

           v1.push_back(currMV);

           vec.push_back(v1);

           M = currMV;

           while (!v1.empty())

           {    

             v1.pop_back();

           }  

          }

        if (vec.size() == depth)

              break;

     }

 return vec;

 };

} //namespace

(.env) [boris@fedora33server ABCFORWARD]$ cat mainVector.cpp
#include <Python.h>
#include <iostream>
#include <vector>
#include <cstddef>
#include <fstream>
#include <cstdlib>
#include "procVector.h"
using std::endl;
using std::ofstream;
#pragma GCC diagnostic ignored "-Wsign-compare"

extern "C"{}

namespace {

static PyObject *resList(PyObject* self, PyObject* args)

{
    ofstream outdata;
    int m,n;

    if(!PyArg_ParseTuple(args,"ii",&m,&n))

        return NULL;

    outdata.open("./outvector.dat"); 

    std::vector<std::vector<int> > rez = abc::resultList(m,n);

    // Writing to file the 2D vector
    for (int i = 0; i < rez.size(); i++) {
       for (int j = 0; j < rez[i].size(); j++)
           outdata << rez[i][j] << " ";
       outdata << endl;
  }

    return Py_None ;

};

// Our Module's Function Definition structure
// We require this `NULL` to signal the end of our method
// definition

static PyMethodDef myMethodsTable[] = {

    { "resList", resList, METH_VARARGS, "Tracking factorial" },
    { NULL, NULL, 0, NULL }

};

// Our Module Definition struct
static struct PyModuleDef myModule = {
    PyModuleDef_HEAD_INIT,
    "myModule",
    "Test Module",
    -1,
    myMethodsTable
};

// Initializes our module using our above struct
PyMODINIT_FUNC PyInit_myModule(void)
{
    return PyModule_Create(&myModule);
};
} //namespace

(.env) [boris@fedora33server ABCFORWARD]$ cat setup.py
from distutils.core import setup, Extension
import sysconfig

language = 'c++'
std = 'c++20'
default_compile_args = sysconfig.get_config_var('CFLAGS').split()
extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]
setup(name = 'myModule', version = '1.0',  \
   ext_modules = [Extension('myModule', ['mainVector.cpp','procVector.cpp'])])

(.env) [boris@fedora33server ABCFORWARD]$ python setup.py install
running install
running build
running build_ext
building 'myModule' extension
creating build
creating build/temp.linux-x86_64-3.9
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/ABCFORWARD/.env/include -I/usr/include/python3.9 -c mainVector.cpp -o build/temp.linux-x86_64-3.9/mainVector.o
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/ABCFORWARD/.env/include -I/usr/include/python3.9 -c procVector.cpp -o build/temp.linux-x86_64-3.9/procVector.o
creating build/lib.linux-x86_64-3.9
g++ -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g build/temp.linux-x86_64-3.9/mainVector.o build/temp.linux-x86_64-3.9/procVector.o -L/usr/lib64 -o build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so
running install_lib
copying build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so -> /home/boris/ABCFORWARD/.env/lib64/python3.9/site-packages
running install_egg_info
Removing /home/boris/ABCFORWARD/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info
Writing /home/boris/ABCFORWARD/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info





















(.env) [boris@fedora33server ABCFORWARD]$ cat MyProg.py
import myModule
a = int(input("Input start number : "))
b = int(input("Input depth number : "))
myModule.resList(a,b)
lines = []
with open('./outvector.dat') as f:
    lines = f.readlines()
count = 0
for line in lines:
    count += 1
    print(f'{count}: {line}')

(.env) [boris@fedora33server ABCFORWARD]$ python MyProg.py
Input start number : 713000218
Input depth number : 25
1: 713000221 67330 

2: 713000261 437190 

3: 713000269 11688590 

4: 713000279 23000040 

5: 713000571 237666860 

6: 713001381 237667130 

7: 713001441 237667150 

8: 713001621 237667210 

9: 713001651 237667220 

10: 713001711 237667240 

11: 713001831 237667280 

12: 713001921 237667310 

13: 713002011 237667340 

14: 713002071 237667360 

15: 713002161 237667390 

16: 713002431 237667480 

17: 713002611 237667540 

18: 713002701 237667570 

19: 713002881 237667630 

20: 713003091 237667700 

21: 713003241 237667750 

22: 713003271 237667760 

23: 713003451 237667820 

24: 713003721 237667910 

25: 713003901 237667970 























No comments:

Post a Comment