From 7576c367e56466f5087ade4ec8bd8afa5a05f816 Mon Sep 17 00:00:00 2001 From: maedoc Date: Thu, 28 Jan 2016 16:45:29 +0100 Subject: [PATCH] add bench vs proj & continue python work --- bench/bench_net_exc.c | 4 +- makefile | 2 +- python/sddekit.pyx | 170 ++++++++++++++++++++- .../bench_net_exc.vcxproj} | 94 +++++++----- vs/bench_net_exc/bench_net_exc.vcxproj.filters | 22 +++ vs/libsd/libsd.vcxproj | 4 +- vs/vs.sln | 12 +- 7 files changed, 261 insertions(+), 47 deletions(-) copy vs/{libsd/libsd.vcxproj => bench_net_exc/bench_net_exc.vcxproj} (52%) create mode 100644 vs/bench_net_exc/bench_net_exc.vcxproj.filters diff --git a/bench/bench_net_exc.c b/bench/bench_net_exc.c index 548bed9..b1abd92 100644 --- a/bench/bench_net_exc.c +++ b/bench/bench_net_exc.c @@ -34,8 +34,8 @@ int main() { sd_out *out = sd_out_new_cb(&igcd, &out_igc); /* connectivity, assuming conduction velocity of 1.0 */ - sd_util_read_square_matrix("res/conn76/weights.txt", &n, &w); - sd_util_read_square_matrix("res/conn76/tract_lengths.txt", &n, &d); + sd_util_read_square_matrix("bench/conn76/weights.txt", &n, &w); + sd_util_read_square_matrix("bench/conn76/tract_lengths.txt", &n, &d); sd_sparse_from_dense(n, n, w, d, 0.0, &nnz, &Or, &Ic, &sw, &sd); fprintf(stdout, "[bench_net_exc] nnz=%d\n", nnz); diff --git a/makefile b/makefile index 2406446..dd6ea5b 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-g -std=c99 -Isrc -Wall -Wextra -DSDDEBUG +CFLAGS=-std=c99 -Isrc -Wall -Wextra -Ofast LDFLAGS=-lm # # e.g. -pg -fprofile-arcs -ftest-coverage diff --git a/python/sddekit.pyx b/python/sddekit.pyx index 5cfcd80..49f003f 100644 --- a/python/sddekit.pyx +++ b/python/sddekit.pyx @@ -2,11 +2,12 @@ from libc.stdlib cimport malloc, free from libc.stdint cimport uint32_t -from cpython cimport PyObject, Py_INCREF +from cpython cimport PyObject, Py_INCREF, Py_DECREF cimport numpy as np import numpy as np np.import_array() +# gc-friendly c array wrapper # based on https://gist.github.com/GaelVaroquaux/1249305 BSD-licensed. cdef class CArrayWrapper: "Python wrapper for C array." @@ -44,7 +45,7 @@ cdef np.ndarray np_of_ptr(int size, void *ptr, int typenum, int free_on_gc): Py_INCREF(wrapper) return ndarray - +# hist cdef extern from "sddekit.h": ctypedef enum sd_stat: @@ -58,8 +59,6 @@ cdef extern from "sddekit.h": sd_stat (*apply)(sd_hfill*, uint32_t n, double * t, uint32_t *indices, double * buf) void (*free)(sd_hfill*) - sd_hist * sd_hist_new_default(uint32_t nd, uint32_t *vi, double *vd, double t0, double dt) - ctypedef struct sd_hist: uint32_t(*get_maxvi)(sd_hist*) uint32_t(*get_vi2i)(sd_hist*, uint32_t vi) @@ -81,6 +80,10 @@ cdef extern from "sddekit.h": uint32_t (*get_vi)(sd_hist *h, uint32_t i) double (*get_vd)(sd_hist *h, uint32_t i) + sd_hist * sd_hist_new_default(uint32_t nd, uint32_t *vi, double *vd, double t0, double dt) + + +# hfill ctypedef struct hfill_py: sd_hfill hf void *fn @@ -162,3 +165,162 @@ cdef class Hist(object): return self.h.get_vi(self.h, i) def vd(self, i): return self.h.get_vd(self.h, i) + +# rng +cdef extern from "sddekit.h": + ctypedef struct sd_rng: + void *ptr + void (*seed)(sd_rng*, uint32_t seed) + double (*norm)(sd_rng*) + void (*fill_norm)(sd_rng*, uint32_t n, double *x) + uint32_t (*nbytes)(sd_rng*) + void (*free)(sd_rng*) + sd_rng * sd_rng_new_default() + +cdef class Rng(object): + cdef sd_rng *r + +cdef object rng_of_ptr(sd_rng *r): + rng = Rng() + rng.r = r + return rng + +# sys +cdef extern from "sddekit.h": + ctypedef struct sd_sys: + uint32_t (*ndim)(sd_sys*) + uint32_t (*ndc)(sd_sys*) + uint32_t (*nobs)(sd_sys*) + uint32_t (*nrpar)(sd_sys*) + uint32_t (*nipar)(sd_sys*) + sd_stat (*apply)(sd_sys*, sd_sys_in*, sd_sys_out*) + void (*free)(sd_sys*) + void *ptr + ctypedef struct sd_sys_in: + uint32_t nx, nc, id + double t, *x, *i + sd_hist *hist + sd_rng *rng + ctypedef struct sd_sys_out: + double *f, *g, *o + +cdef uint32_t sys_py_ndim(sd_sys*s): + cdef object sys = s.ptr + cdef uint32_t i = sys.ndim + return i + +cdef uint32_t sys_py_ndc(sd_sys*s): + cdef object sys = s.ptr + cdef uint32_t i = sys.ndc + return i + +cdef uint32_t sys_py_nobs(sd_sys*s): + cdef object sys = s.ptr + cdef uint32_t i = sys.nobs + return i + +cdef uint32_t sys_py_nrpar(sd_sys*s): + cdef object sys = s.ptr + cdef uint32_t i = sys.nrpar + return i + +cdef uint32_t sys_py_nipar(sd_sys*s): + cdef object sys = s.ptr + cdef uint32_t i = sys.nipar + return i + +cdef sd_stat sys_py_apply(sd_sys *s, sd_sys_in *i, sd_sys_out *o): + cdef object sys = s.ptr + # TODO conversions + f, g, o = sys.apply(i.hist, i.rng, i.id, i.t, i.x, i.i) + # TODO conversions + o.f = f + o.g = g + o.o = o + return SD_OK + +cdef void sys_py_free(sd_sys *s): + Py_DECREF( s.ptr) + free(s) + +cdef class Sys(object): + "Base class for system defintions, may be defined in C." + cdef sd_sys *s + + @property + def ndim(self): + return self.s.ndim(self.s) + + @property + def ndc(self): + return self.s.ndc(self.s) + + @property + def nobs(self): + return self.s.nobs(self.s) + + @property + def nrpar(self): + return self.s.nrpar(self.s) + + @property + def nipar(self): + return self.s.nipar(self.s) + + def apply(self, hist, rng, id, t, x, i): + cdef: + sd_sys_in in_ + sd_sys_out out_ + raise NotImplementedError + + def __del__(self): + self.s.free(self.s) + +cdef class PySys(Sys): + "Base class for systems defined in Python." + + def __init__(self): + "Construct system defined by Python subclass." + if self.__class__.__name__ == 'PySys': + raise TypeError('PySys must be subclassed.') + self.s = malloc(sizeof(sd_sys)) + Py_INCREF(self) + self.s.ptr = self + self.s.ndim = &sys_py_ndim + self.s.ndc = &sys_py_ndc + self.s.nobs = &sys_py_nobs + self.s.nrpar = &sys_py_nrpar + self.s.nipar = &sys_py_nipar + self.s.apply = &sys_py_apply + self.s.free = &sys_py_free + + @property + def ndim(self): + raise NotImplementedError + + @property + def ndc(self): + raise NotImplementedError + + @property + def nobs(self): + raise NotImplementedError + + @property + def nrpar(self): + raise NotImplementedError + + @property + def nipar(self): + raise NotImplementedError + + def apply(self, hist, rng, id, t, x, i): + raise NotImplementedError + +cdef object sys_of_ptr(sd_sys *s): + sys = Sys() + sys.s = s + return sys + + +# vim: sw=4 et diff --git a/vs/libsd/libsd.vcxproj b/vs/bench_net_exc/bench_net_exc.vcxproj similarity index 52% copy from vs/libsd/libsd.vcxproj copy to vs/bench_net_exc/bench_net_exc.vcxproj index 1033655..ccd69bd 100644 --- a/vs/libsd/libsd.vcxproj +++ b/vs/bench_net_exc/bench_net_exc.vcxproj @@ -1,6 +1,14 @@  + + Debug + Win32 + + + Release + Win32 + Debug x64 @@ -11,14 +19,26 @@ - {E3918461-6814-4004-B4E4-548778149035} - libsd + {CE002AAC-EA3F-4B63-BB79-F233FBD34658} + bench_net_exc 8.1 - libsd + + Application + true + v140 + MultiByte + + + Application + false + v140 + true + MultiByte + - StaticLibrary + Application true v140 MultiByte @@ -35,6 +55,12 @@ + + + + + + @@ -43,17 +69,36 @@ - $(VC_IncludePath);$(WindowsSDK_IncludePath);$(SolutionDir)..\include; + $(SolutionDir)\.. + + + Level3 + Disabled + true + + Level3 Disabled true - 4996;4146; - SKDEBUG;%(PreprocessorDefinitions) + $(SolutionDir)\..\src + + + Level3 + MaxSpeed + true + true + true + + + true + true + + Level3 @@ -68,39 +113,14 @@ - - - + + {e3918461-6814-4004-b4e4-548778149035} + - - - - - - - - - - - - - - - - - - - - - - - - - - + - + \ No newline at end of file diff --git a/vs/bench_net_exc/bench_net_exc.vcxproj.filters b/vs/bench_net_exc/bench_net_exc.vcxproj.filters new file mode 100644 index 0000000..5b4806c --- /dev/null +++ b/vs/bench_net_exc/bench_net_exc.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/vs/libsd/libsd.vcxproj b/vs/libsd/libsd.vcxproj index 1033655..cd385a9 100644 --- a/vs/libsd/libsd.vcxproj +++ b/vs/libsd/libsd.vcxproj @@ -51,7 +51,7 @@ Disabled true 4996;4146; - SKDEBUG;%(PreprocessorDefinitions) + SDDEBUG;%(PreprocessorDefinitions) @@ -103,4 +103,4 @@ - + \ No newline at end of file diff --git a/vs/vs.sln b/vs/vs.sln index cb27898..4219087 100644 --- a/vs/vs.sln +++ b/vs/vs.sln @@ -5,13 +5,15 @@ VisualStudioVersion = 14.0.24720.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libsd", "libsd\libsd.vcxproj", "{E3918461-6814-4004-B4E4-548778149035}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mex files", "mexfiles\mexfiles.vcxproj", "{38C5244D-05C3-402B-94E0-C528F45DBECE}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mexfiles", "mexfiles\mexfiles.vcxproj", "{38C5244D-05C3-402B-94E0-C528F45DBECE}" ProjectSection(ProjectDependencies) = postProject {E3918461-6814-4004-B4E4-548778149035} = {E3918461-6814-4004-B4E4-548778149035} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests", "run_tests\run_tests.vcxproj", "{A2526CFE-5106-48BA-840E-5BA66F91536C}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bench_net_exc", "bench_net_exc\bench_net_exc.vcxproj", "{CE002AAC-EA3F-4B63-BB79-F233FBD34658}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -40,6 +42,14 @@ Global {A2526CFE-5106-48BA-840E-5BA66F91536C}.Release|x64.Build.0 = Release|x64 {A2526CFE-5106-48BA-840E-5BA66F91536C}.Release|x86.ActiveCfg = Release|Win32 {A2526CFE-5106-48BA-840E-5BA66F91536C}.Release|x86.Build.0 = Release|Win32 + {CE002AAC-EA3F-4B63-BB79-F233FBD34658}.Debug|x64.ActiveCfg = Debug|x64 + {CE002AAC-EA3F-4B63-BB79-F233FBD34658}.Debug|x64.Build.0 = Debug|x64 + {CE002AAC-EA3F-4B63-BB79-F233FBD34658}.Debug|x86.ActiveCfg = Debug|Win32 + {CE002AAC-EA3F-4B63-BB79-F233FBD34658}.Debug|x86.Build.0 = Debug|Win32 + {CE002AAC-EA3F-4B63-BB79-F233FBD34658}.Release|x64.ActiveCfg = Release|x64 + {CE002AAC-EA3F-4B63-BB79-F233FBD34658}.Release|x64.Build.0 = Release|x64 + {CE002AAC-EA3F-4B63-BB79-F233FBD34658}.Release|x86.ActiveCfg = Release|Win32 + {CE002AAC-EA3F-4B63-BB79-F233FBD34658}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- 2.11.4.GIT