Notice that Python module
(.env) [boris@fedora33server CATALAN]$ cat mainCatalan.py
def Catl(n):
if n == 0: return 1
s = 0
for i in range(n):
s += Catl(i)*Catl(n-1-i)
return s
def main():
a = 0
sequence = []
a = int(input("Input desired number of Catalan sequence : "))
for i in range(a):
sequence.append(Catl(i))
print(sequence)
if __name__ == "__main__":
main()
Actually hangs ( run for ever) when value of "a" becomes equal 20. Oops. It exits in 4 min 30 seconds
(.env) [boris@sever33fedora CatStrike]$ cat CatStrike.h
#pragma once
#include <vector>
#include <cstddef>
namespace abc {
class CatStrike {
public:
double *mem;
long fftSize;
CatStrike(long fftSize);
~CatStrike();
std::vector<long> filter(long freq,long fqn);
long CatalanIt(int n);
};
}
(.env) [boris@sever33fedora CatStrike]$ cat CatStrike.cpp
#include <cstdio>
#include <vector>
#include <cstddef>
#include "CatStrike.h"
namespace abc
{
CatStrike::CatStrike(long fftSize) {
printf("c++ constructor of CatStrike\n");
this->fftSize=fftSize;
mem=new double[fftSize];
}
CatStrike::~CatStrike() { delete [] mem; }
std::vector<long> CatStrike::filter(long freq,long nv) {
printf("c++ CatStrike filter method\n");
std::vector<long> vect;
for(int i=1; i <= nv; i++)
vect.push_back(CatalanIt(i));
return vect;
}
long CatStrike::CatalanIt(int n)
{
long catalan[n + 1];
catalan[0] = catalan[1] = 1;
for (int i = 2; i <= n; i++) {
catalan[i] = 0;
for (int j = 0; j < i; j++)
catalan[i] += catalan[j] * catalan[i - j - 1];
}
return catalan[n];
}
}
(.env) [boris@sever33fedora CatStrike]$ cat CatStrikeWrapper.cpp
#include <Python.h>
#include <cstdio>
#include <vector>
#include <cstddef>
#include "CatStrike.h"
#pragma GCC diagnostic ignored "-Wsign-compare"
using abc::CatStrike;
typedef struct {
PyObject_HEAD
CatStrike * ptrObj;
} PyCatStrike;
static PyModuleDef blademodule = {
PyModuleDef_HEAD_INIT,
"blade",
"Example module that wrapped a C++ object",
-1,
NULL, NULL, NULL, NULL, NULL
};
static int PyCatStrike_init(PyCatStrike *self, PyObject *args, PyObject *kwds)
{
long fftSize;
if (! PyArg_ParseTuple(args, "l", &fftSize))
return -1;
self->ptrObj=new CatStrike(fftSize);
return 0;
}
static void PyCatStrike_dealloc(PyCatStrike * self)
// destruct the object
{
delete self->ptrObj;
Py_TYPE(self)->tp_free(self);
}
static PyObject * PyCatStrike_filter(PyCatStrike* self, PyObject* args)
{
long freq;
long sequence ;
if (! PyArg_ParseTuple(args, "ll", &freq,&sequence))
return Py_False;
std::vector<long> retval = (self->ptrObj)->filter(freq,sequence);
PyObject* result = PyList_New(retval.size());
for(int i = 0; i < retval.size(); i++) {
PyList_SetItem(result, i, PyLong_FromLong(retval[i]));
}
return result;
}
static PyMethodDef PyCatStrike_methods[] = {
{ "filter", (PyCFunction)PyCatStrike_filter,METH_VARARGS,"filter the mem blade" },
{NULL}
};
static PyTypeObject PyCatStrikeType = { PyVarObject_HEAD_INIT(NULL, 0)
"blade.CatStrike" /* tp_name */
};
PyMODINIT_FUNC PyInit_blade(void)
// create the module
{
PyObject* m;
PyCatStrikeType.tp_new = PyType_GenericNew;
PyCatStrikeType.tp_basicsize=sizeof(PyCatStrike);
PyCatStrikeType.tp_dealloc=(destructor) PyCatStrike_dealloc;
PyCatStrikeType.tp_flags=Py_TPFLAGS_DEFAULT;
PyCatStrikeType.tp_doc="CatStrike objects";
PyCatStrikeType.tp_methods=PyCatStrike_methods;
PyCatStrikeType.tp_init=(initproc)PyCatStrike_init;
if (PyType_Ready(&PyCatStrikeType) < 0)
return NULL;
m = PyModule_Create(&blademodule);
if (m == NULL)
return NULL;
Py_INCREF(&PyCatStrikeType);
PyModule_AddObject(m, "CatStrike", (PyObject *)&PyCatStrikeType);
return m;
}
setup(name='bladePkg', version='1.0', \
ext_modules=[Extension('blade', ['CatStrikeWrapper.cpp','CatStrike.cpp'])])
running build
running build_ext
building 'blade' 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_FORTI
FY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasyn
chronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwra
pv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_F
ORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -f
asynchronous-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=generi
c -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fP
IC -fwrapv -fPIC -I/home/boris/VOLUME/.env/include -I/usr/include/python3.9 -c CatStrike.cpp
O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTI
FY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasyn
chronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwra
pv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_F
ORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -f
asynchronous-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=generi
c -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fP
IC -fwrapv -fPIC -I/home/boris/VOLUME/.env/include -I/usr/include/python3.9 -c bladeWrap
per.cpp -o build/temp.linux-x86_64-3.9/CatStrikeWrapper.o
g++ -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g -Wl,-z,relro -Wl,--as-nee
ded -Wl,-z,now -g build/temp.linux-x86_64-3.9/CatStrike.o build/temp.linux-x86_64-3.9/blade
o
running install_lib
copying build/lib.linux-x86_64-3.9/blade.cpython-39-x86_64-linux-gnu.so -> /home/boris/V
OLUME/.env/lib64/python3.9/site-packages
running install_egg_info
Removing /home/boris/VOLUME/.env/lib64/python3.9/site-packages/bladePkg-1.0-py3.9.egg-in
fo
Writing /home/boris/VOLUME/.env/lib64/python3.9/site-packages/bladePkg-1.0-py3.9.egg-inf
o
(.env) [boris@sever33fedora VOLUME]$ ls -CRl
.:
total 24
drwxrwxr-x. 4 boris boris 63 Aug 1 16:40 build
-rw-rw-r--. 1 boris boris 847 Aug 1 16:39 CatStrike.cpp
-rw-rw-r--. 1 boris boris 298 Aug 1 16:02 CatStrike.h
-rw-rw-r--. 1 boris boris 2346 Aug 1 16:29 CatStrikeWrapper.cpp
-rw-rw-r--. 1 boris boris 674 Jul 31 19:41 log
-rw-rw-r--. 1 boris boris 169 Aug 1 16:06 setup.py
-rw-rw-r--. 1 boris boris 131 Aug 1 16:08 test.py
./build:
total 0
drwxrwxr-x. 2 boris boris 51 Aug 1 16:40 lib.linux-x86_64-3.9
drwxrwxr-x. 2 boris boris 51 Aug 1 16:40 temp.linux-x86_64-3.9
./build/lib.linux-x86_64-3.9:
total 92
-rwxrwxr-x. 1 boris boris 93408 Aug 1 16:40 blade.cpython-39-x86_64-linux-gnu.so
./build/temp.linux-x86_64-3.9:
total 168
-rw-rw-r--. 1 boris boris 82808 Aug 1 16:40 CatStrike.o
-rw-rw-r--. 1 boris boris 85488 Aug 1 16:40 CatStrikeWrapper.o
(.env) [boris@sever33fedora VOLUME]$ cat test.py
import blade
n = int(input("Number of member of Catalan Sequence :"))
v=blade.CatStrike(4096)
result=v.filter(5,n)
print(result)
No comments:
Post a Comment