From 53ec4a1276a501186697bcdb4483be776dcac18c Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sat, 24 Feb 2024 18:51:53 +0100 Subject: [PATCH 1/2] adapt setup.py --- .gitignore | 1 + setup.py | 71 ++++++++++++++++++++++++------------------------------ 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 02b2268..205bc5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .eggs/ build/ +dist/ cnorxz.egg-info/ *.so diff --git a/setup.py b/setup.py index 6bd7f85..7af21d2 100644 --- a/setup.py +++ b/setup.py @@ -1,47 +1,56 @@ #!/usr/bin/env python -#import numpy +# imports: + import setuptools from distutils.core import setup, Extension +import os import sysconfig import subprocess +# basic variables: + libname = "cnorxz" author = "Christian Zimmermann" author_email = "chizeta@f3l.de" -path_to_cnorxz = "/home/chizeta/repos/cnorxz/install" -expected_cnorxz_version = "1.0.0" # dummy for now; in the future this is supposed to be the c++ version which is guaranteed to be compatible with py cnorxz +path_to_cnorxz = "" +if 'CNORXZ' in os.environ: + path_to_cnorxz = os.environ['CNORXZ'] +version = "0.0.0" +have_numpy = True -git_hash = subprocess.run(['git','rev-parse','HEAD'],stdout=subprocess.PIPE).stdout.decode('ascii')[:-1] -git_tags = subprocess.run(['git', 'tag', '-l', '--sort=refname', 'v*'],stdout=subprocess.PIPE).stdout.decode('ascii').split() +# flags: + +extra_compile_args = sysconfig.get_config_var('CFLAGS').split() cnorxz_version = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--version'],stdout=subprocess.PIPE).stdout.decode('ascii')[:-1] +cnorxz_flags = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split() +cnorxz_definitions = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--definitions'],stdout=subprocess.PIPE).stdout.decode('ascii').split() +cnorxz_flags.remove("-Werror") +cnorxz_flags.append("-Wno-write-strings") +extra_compile_args += cnorxz_flags + +# includes: -assert len(git_tags) != 0, "no version tags found" -git_tag = git_tags[-1] -version = git_tag[1:] + 'dev' -git_hash_tag = subprocess.run(['git','rev-list','-n','1',git_tag],stdout=subprocess.PIPE).stdout.decode('ascii')[:-1] -if git_hash_tag == git_hash and expected_cnorxz_version != cnorxz_version: - version = git_tag[1:] - inc_dirs = list() if path_to_cnorxz != "": inc_dirs.append( path_to_cnorxz + "/include/cnorxz" ) inc_dirs.append( "cnorxz/core/include" ) -#inc_dirs.append( numpy.get_include() ) -inc_dirs.append( "/home/chizeta/my_env/lib/python3.11/site-packages/numpy/core/include" ) - + +# library dirs: + lib_dirs = list() lib_dirs.append( "/usr/lib" ) lib_dirs.append( "/usr/local/lib" ) if path_to_cnorxz != "": - inc_dirs.append( path_to_cnorxz + "/lib" ) -lib_dirs.append( "/home/chizeta/repos/cnorxz/install/lib" ) + lib_dirs.append( path_to_cnorxz + "/lib" ) -extra_compile_args = sysconfig.get_config_var('CFLAGS').split() -cnorxz_flags = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split() -cnorxz_flags.remove("-Werror") -cnorxz_flags.append("-Wno-write-strings") -extra_compile_args += cnorxz_flags +# optional dependencies: + +if have_numpy: + import numpy + inc_dirs.append( numpy.get_include() ) + +# main default_extension_args = dict( language = "c++", @@ -49,6 +58,7 @@ default_extension_args = dict( libraries = [libname], library_dirs = lib_dirs, extra_compile_args = extra_compile_args, + define_macros = [(mac,None) for mac in cnorxz_definitions] ) if __name__ == "__main__": @@ -59,21 +69,6 @@ if __name__ == "__main__": for d in lib_dirs: print("\t",d) - #extensions = list() - #extensions.append( - # setuptools.Extension( - # #name = libname + ".core", - # name = libname, - # sources = [ - # "cnorxz/core/core.cpp", - # "cnorxz/core/lib/array_wrapper.cpp" - # ], - # define_macros=[('HAVE_CEREAL',None)], - # **default_extension_args - # ) - #) - # append further extensions (cereal, hdf5, mpi ...) here - setup( name = libname, packages = setuptools.find_packages(), @@ -81,10 +76,8 @@ if __name__ == "__main__": include_dirs = inc_dirs, ext_modules = [Extension( 'cnorxz', ['cnorxz/core/core.cpp','cnorxz/core/lib/array_wrapper.cpp','cnorxz/core/lib/range_wrapper.cpp'], - define_macros=[('HAVE_CEREAL',None)], **default_extension_args )], - #ext_modules = Cython.Build.cythonize(extensions), author = author, author_email = author_email, ) From bd7607094031631481885764fc9309f601e54aa8 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 25 Feb 2024 01:38:01 +0100 Subject: [PATCH 2/2] add more functions --- cnorxz/core/include/array_wrapper.h | 15 ++++++-- cnorxz/core/include/range_wrapper.h | 3 ++ cnorxz/core/lib/range_wrapper.cpp | 60 ++++++++++++++++++++++++++--- setup.py | 7 ++-- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/cnorxz/core/include/array_wrapper.h b/cnorxz/core/include/array_wrapper.h index 58052df..4eecc79 100644 --- a/cnorxz/core/include/array_wrapper.h +++ b/cnorxz/core/include/array_wrapper.h @@ -24,10 +24,16 @@ namespace CNORXZ virtual void readFile(cer::Format f, const String& fname) const = 0; #endif virtual int typenum() const = 0; - virtual void* data() const = 0; + virtual const void* data() const = 0; virtual SizeT datasize() const = 0; }; + class ArrayWrapperBase : public CArrayWrapperBase + { + public: + virtual void* data() = 0; + }; + template struct Typenum { @@ -59,7 +65,7 @@ namespace CNORXZ }; template - class ArrayWrapper : public CArrayWrapperBase + class ArrayWrapper : public ArrayWrapperBase { private: Sptr> mArr; @@ -67,6 +73,7 @@ namespace CNORXZ public: ArrayWrapper() : mArr( std::make_shared>() ) {} ArrayWrapper(const RangePtr& r) : mArr( std::make_shared>(r) ) {} + ArrayWrapper(const Sptr>& arr) : mArr(arr) {} virtual RangePtr range() const override final { return mArr->range(); } virtual SizeT size() const override final { return mArr->size(); } @@ -92,7 +99,9 @@ namespace CNORXZ } #endif virtual int typenum() const override final { return Typenum::value; } - virtual void* data() const override final + virtual const void* data() const override final + { return reinterpret_cast( mArr->data() ); } + virtual void* data() override final { return reinterpret_cast( mArr->data() ); } virtual SizeT datasize() const override final { return size() * sizeof(T); } }; diff --git a/cnorxz/core/include/range_wrapper.h b/cnorxz/core/include/range_wrapper.h index 2714da3..fabcd74 100644 --- a/cnorxz/core/include/range_wrapper.h +++ b/cnorxz/core/include/range_wrapper.h @@ -16,6 +16,8 @@ namespace CNORXZ inline SizeT size() const { return mR->size(); } inline SizeT dim() const { return mR->dim(); } inline String stringMeta(SizeT pos) const { return mR->stringMeta(pos); } + inline MArray sub() const { return mR->sub(); } + inline RangePtr sub(SizeT pos) const { return mR->sub(pos); } private: RangePtr mR; @@ -33,6 +35,7 @@ void PyCxRange_dealloc(PyCxRange* self); PyObject* PyCxRange_size(PyCxRange* self); PyObject* PyCxRange_dim(PyCxRange* self); PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds); +PyObject* PyCxRange_sub(PyCxRange* self, PyObject* args, PyObject* kwds); PyTypeObject* PyCxRangeType_init(); extern PyMethodDef PyCxRange_methods[]; diff --git a/cnorxz/core/lib/range_wrapper.cpp b/cnorxz/core/lib/range_wrapper.cpp index f8b290e..405bd6e 100644 --- a/cnorxz/core/lib/range_wrapper.cpp +++ b/cnorxz/core/lib/range_wrapper.cpp @@ -1,4 +1,5 @@ +#include "array_wrapper.h" #include "range_wrapper.h" using namespace CNORXZ; @@ -6,6 +7,7 @@ using namespace CNORXZ; int PyCxRange_init(PyCxRange* self, PyObject* args, PyObject* kwds) { //!!! + return 0; } void PyCxRange_dealloc(PyCxRange* self) @@ -14,6 +16,9 @@ void PyCxRange_dealloc(PyCxRange* self) Py_TYPE(self)->tp_free(self); } +PyDoc_STRVAR(cxz_PyCxRange_size_doc, + "size(self)\n--\n\n" + "return range size"); PyObject* PyCxRange_size(PyCxRange* self) { if(not self->ptrObj->_init()){ @@ -24,6 +29,9 @@ PyObject* PyCxRange_size(PyCxRange* self) return Py_BuildValue("k", retval); } +PyDoc_STRVAR(cxz_PyCxRange_dim_doc, + "dim(self)\n--\n\n" + "return range dimension"); PyObject* PyCxRange_dim(PyCxRange* self) { if(not self->ptrObj->_init()){ @@ -34,16 +42,58 @@ PyObject* PyCxRange_dim(PyCxRange* self) return Py_BuildValue("k", retval); } +PyDoc_STRVAR(cxz_PyCxRange_stringMeta_doc, + "stringMeta(self, pos)\n--\n\n" + "return string meta data for given range position"); PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds) { - //!!! + static char* kwlist[] = { "pos", NULL }; + SizeT pos = 0; + if(not PyArg_ParseTupleAndKeywords(args, kwds, "k", kwlist, &pos)){ + return NULL; + } + const String s = self->ptrObj->stringMeta(pos); + return Py_BuildValue("s", s.c_str()); } +PyDoc_STRVAR(cxz_PyCxRange_sub_doc, + "sub(self, pos)\n--\n\n" + "get sub-ranges for given dimension; if pos >= dim, all sub-ranges are returned"); +PyObject* PyCxRange_sub(PyCxRange* self, PyObject* args, PyObject* kwds) +{ + static char* kwlist[] = { "pos", NULL }; + const SizeT max = self->ptrObj->dim(); + SizeT pos = max; + if(not PyArg_ParseTupleAndKeywords(args, kwds, "|k", kwlist, &pos)){ + return NULL; + } + if(pos < max) { + const RangePtr sub = self->ptrObj->sub(pos); + if(sub){ + PyCxRange* o; + o = PyObject_New(PyCxRange, &PyCxRangeType); + o->ptrObj = new RangeWrapper( sub ); + return Py_BuildValue("O", o); + } + else { + Py_RETURN_NONE; + } + } + else { + PyCxArrayB* o; + o = PyObject_New(PyCxArrayB, &PyCxArrayBType); + o->ptrObj = new ArrayWrapper( std::make_shared>(self->ptrObj->sub()) ); + return Py_BuildValue("O", o); + } +} + + PyMethodDef PyCxRange_methods[] = { - { "size", (PyCFunction) PyCxRange_size, METH_VARARGS, "return range size" }, - { "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, "return range dimension" }, - { "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS, - "return string meta data for given range position" }, + { "size", (PyCFunction) PyCxRange_size, METH_VARARGS, cxz_PyCxRange_size_doc }, + { "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, cxz_PyCxRange_dim_doc }, + { "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS| METH_KEYWORDS, + cxz_PyCxRange_stringMeta_doc }, + { "sub", (PyCFunction) PyCxRange_sub, METH_VARARGS| METH_KEYWORDS, cxz_PyCxRange_sub_doc }, { NULL } }; diff --git a/setup.py b/setup.py index 7af21d2..fbdeb42 100644 --- a/setup.py +++ b/setup.py @@ -21,10 +21,11 @@ have_numpy = True # flags: +config_bin = path_to_cnorxz+"/bin/cnorxz-config" extra_compile_args = sysconfig.get_config_var('CFLAGS').split() -cnorxz_version = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--version'],stdout=subprocess.PIPE).stdout.decode('ascii')[:-1] -cnorxz_flags = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split() -cnorxz_definitions = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--definitions'],stdout=subprocess.PIPE).stdout.decode('ascii').split() +cnorxz_version = subprocess.run([config_bin,'--version'],stdout=subprocess.PIPE).stdout.decode('ascii')[:-1] +cnorxz_flags = subprocess.run([config_bin,'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split() +cnorxz_definitions = subprocess.run([config_bin,'--definitions'],stdout=subprocess.PIPE).stdout.decode('ascii').split() cnorxz_flags.remove("-Werror") cnorxz_flags.append("-Wno-write-strings") extra_compile_args += cnorxz_flags