From 03ff113ac48f21956247b164a83000b5f6ab311d Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 25 Sep 2018 18:24:57 +0200 Subject: Add support for checking features at run-time --- astra_vc14.vcxproj | 2 ++ astra_vc14.vcxproj.filters | 6 ++++++ build/linux/Makefile.in | 1 + build/msvc/gen.py | 2 ++ include/astra/Features.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++ matlab/mex/astra_mex_c.cpp | 38 ++++++++++++++++++++++++++++++++- python/astra/__init__.py | 2 +- python/astra/astra.py | 14 +++++++++++++ python/astra/astra_c.pyx | 8 ++++++- src/Features.cpp | 41 ++++++++++++++++++++++++++++++++++++ 10 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 include/astra/Features.h create mode 100644 src/Features.cpp diff --git a/astra_vc14.vcxproj b/astra_vc14.vcxproj index 6c19c41..d281179 100644 --- a/astra_vc14.vcxproj +++ b/astra_vc14.vcxproj @@ -508,6 +508,7 @@ + @@ -596,6 +597,7 @@ + diff --git a/astra_vc14.vcxproj.filters b/astra_vc14.vcxproj.filters index 226068e..cb3622d 100644 --- a/astra_vc14.vcxproj.filters +++ b/astra_vc14.vcxproj.filters @@ -168,6 +168,9 @@ Global & Other\source + + Global & Other\source + Global & Other\source @@ -437,6 +440,9 @@ Global & Other\headers + + Global & Other\headers + Global & Other\headers diff --git a/build/linux/Makefile.in b/build/linux/Makefile.in index 2ec3851..f4c5c20 100644 --- a/build/linux/Makefile.in +++ b/build/linux/Makefile.in @@ -141,6 +141,7 @@ BASE_OBJECTS=\ src/FanFlatBeamStripKernelProjector2D.lo \ src/FanFlatProjectionGeometry2D.lo \ src/FanFlatVecProjectionGeometry2D.lo \ + src/Features.lo \ src/FilteredBackProjectionAlgorithm.lo \ src/Filters.lo \ src/Float32Data2D.lo \ diff --git a/build/msvc/gen.py b/build/msvc/gen.py index a42d8e5..42d0e0e 100644 --- a/build/msvc/gen.py +++ b/build/msvc/gen.py @@ -214,6 +214,7 @@ P_astra["filters"]["Global & Other\\source"] = [ "src\\AstraObjectManager.cpp", "src\\CompositeGeometryManager.cpp", "src\\Config.cpp", +"src\\Features.cpp", "src\\Filters.cpp", "src\\Fourier.cpp", "src\\Globals.cpp", @@ -354,6 +355,7 @@ P_astra["filters"]["Global & Other\\headers"] = [ "include\\astra\\clog.h", "include\\astra\\CompositeGeometryManager.h", "include\\astra\\Config.h", +"include\\astra\\Features.h", "include\\astra\\Filters.h", "include\\astra\\Fourier.h", "include\\astra\\Globals.h", diff --git a/include/astra/Features.h b/include/astra/Features.h new file mode 100644 index 0000000..d88ae71 --- /dev/null +++ b/include/astra/Features.h @@ -0,0 +1,52 @@ +/* +----------------------------------------------------------------------- +Copyright: 2010-2018, imec Vision Lab, University of Antwerp + 2014-2018, CWI, Amsterdam + +Contact: astra@astra-toolbox.com +Website: http://www.astra-toolbox.com/ + +This file is part of the ASTRA Toolbox. + + +The ASTRA Toolbox is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The ASTRA Toolbox is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with the ASTRA Toolbox. If not, see . + +----------------------------------------------------------------------- +*/ + +#ifndef _INC_ASTRA_FEATURES +#define _INC_ASTRA_FEATURES + +#include "astra/Globals.h" + +namespace astra { +_AstraExport bool hasFeature(const std::string &feature); +} + +/* + +FEATURES: + +cuda: is cuda support compiled in? + NB: To check if there is also actually a usable GPU, use cudaAvailable() + +mex_link: is there support for the matlab command astra_mex_data3d('link')? + +For future backward-incompatible changes, extra features will be added here + + +*/ + + +#endif diff --git a/matlab/mex/astra_mex_c.cpp b/matlab/mex/astra_mex_c.cpp index f494ce6..43c438e 100644 --- a/matlab/mex/astra_mex_c.cpp +++ b/matlab/mex/astra_mex_c.cpp @@ -35,6 +35,7 @@ along with the ASTRA Toolbox. If not, see . #include "mexInitFunctions.h" #include "astra/Globals.h" +#include "astra/Features.h" #include "astra/AstraObjectManager.h" #ifdef ASTRA_CUDA @@ -132,6 +133,7 @@ void astra_mex_set_gpu_index(int nlhs, mxArray* plhs[], int nrhs, const mxArray* #endif } +//----------------------------------------------------------------------------------------- /** get_gpu_info = astra_mex('get_gpu_info'); * * Get GPU info @@ -148,6 +150,38 @@ void astra_mex_get_gpu_info(int nlhs, mxArray* plhs[], int nrhs, const mxArray* } +//----------------------------------------------------------------------------------------- +/** has_feature = astra_mex('has_feature'); + * + * Check a feature flag. See include/astra/Features.h. + */ +void astra_mex_has_feature(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) +{ + if (2 > nrhs) { + mexErrMsgTxt("Usage: astra_mex('has_feature', feature);\n"); + return; + } + + string sMode = mexToString(prhs[0]); + bool ret = false; + + // NB: When adding features here, also document them centrally in + // include/astra/Features.h + if (sMode == "mex_link") { +#ifdef USE_MATLAB_UNDOCUMENTED + ret = true; +#else + ret = false; +#endif + } else { + ret = astra::hasFeature(sMode); + } + + plhs[0] = mxCreateDoubleScalar(ret ? 1 : 0); +} + + + //----------------------------------------------------------------------------------------- /** version_number = astra_mex('version'); * @@ -208,7 +242,7 @@ void astra_mex_delete(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] static void printHelp() { mexPrintf("Please specify a mode of operation.\n"); - mexPrintf(" Valid modes: version, use_cuda, credits, set_gpu_index, info, delete\n"); + mexPrintf(" Valid modes: version, use_cuda, credits, set_gpu_index, has_feature, info, delete\n"); } //----------------------------------------------------------------------------------------- @@ -241,6 +275,8 @@ void mexFunction(int nlhs, mxArray* plhs[], astra_mex_set_gpu_index(nlhs, plhs, nrhs, prhs); } else if (sMode == std::string("get_gpu_info")) { astra_mex_get_gpu_info(nlhs, plhs, nrhs, prhs); + } else if (sMode == std::string("has_feature")) { + astra_mex_has_feature(nlhs, plhs, nrhs, prhs); } else if (sMode == std::string("info")) { astra_mex_info(nlhs, plhs, nrhs, prhs); } else if (sMode == std::string("delete")) { diff --git a/python/astra/__init__.py b/python/astra/__init__.py index 1a7f444..ae35316 100644 --- a/python/astra/__init__.py +++ b/python/astra/__init__.py @@ -27,7 +27,7 @@ from . import matlab as m from .creators import astra_dict,create_vol_geom, create_proj_geom, create_backprojection, create_sino, create_reconstruction, create_projector,create_sino3d_gpu, create_backprojection3d_gpu from .functions import data_op, add_noise_to_sino, clear, move_vol_geom, geom_size, geom_2vec, geom_postalignment from .extrautils import clipCircle -from .astra import set_gpu_index, get_gpu_info, use_cuda +from .astra import set_gpu_index, get_gpu_info, use_cuda, has_feature from . import data2d from . import astra from . import data3d diff --git a/python/astra/astra.py b/python/astra/astra.py index ef46db1..e74d29a 100644 --- a/python/astra/astra.py +++ b/python/astra/astra.py @@ -54,6 +54,20 @@ def get_gpu_info(idx=-1): """ return a.get_gpu_info(idx) +def has_feature(feature): + """Check a feature flag. + + These are used to check if certain functionality has been + enabled at compile time, if new functionality is present, or if + a backward-incompatible change is present. + + See include/astra/Features.h for a list. + + :param feature: The name of the feature + :type feature: :class:`str` + :returns: :class:`bool` -- The presence of the feature + """ + return a.has_feature(feature) def delete(ids): """Delete an astra object. diff --git a/python/astra/astra_c.pyx b/python/astra/astra_c.pyx index 65dbf28..69909b5 100644 --- a/python/astra/astra_c.pyx +++ b/python/astra/astra_c.pyx @@ -28,7 +28,7 @@ include "config.pxi" import six -from .utils import wrap_from_bytes +from .utils import wrap_from_bytes, wrap_to_bytes from libcpp.string cimport string from libcpp.vector cimport vector @@ -40,6 +40,9 @@ cdef extern from "astra/Globals.h" namespace "astra": bool cudaEnabled() bool cudaAvailable() +cdef extern from "astra/Features.h" namespace "astra": + bool hasFeature(string) + IF HAVE_CUDA==True: cdef extern from "astra/cuda/2d/astra.h" namespace "astraCUDA": bool setGPUIndex(int) @@ -120,3 +123,6 @@ def info(ids): if ptr: s = ptr.getType() + six.b("\t") + ptr.getInfo(i) six.print_(wrap_from_bytes(s)) + +def has_feature(feature): + return hasFeature(wrap_to_bytes(feature)) diff --git a/src/Features.cpp b/src/Features.cpp new file mode 100644 index 0000000..9114131 --- /dev/null +++ b/src/Features.cpp @@ -0,0 +1,41 @@ +/* +----------------------------------------------------------------------- +Copyright: 2010-2018, imec Vision Lab, University of Antwerp + 2014-2018, CWI, Amsterdam + +Contact: astra@astra-toolbox.com +Website: http://www.astra-toolbox.com/ + +This file is part of the ASTRA Toolbox. + + +The ASTRA Toolbox is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The ASTRA Toolbox is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with the ASTRA Toolbox. If not, see . + +----------------------------------------------------------------------- +*/ + +#include "astra/Features.h" +#include "astra/Globals.h" + +namespace astra { + +_AstraExport bool hasFeature(const std::string &flag) { + if (flag == "cuda") { + return cudaEnabled(); + } + + return false; +} + +} -- cgit v1.2.3