summaryrefslogtreecommitdiff
path: root/python/gras/GRAS_Block.i
blob: 9503af812ed813dcec666a4757c020a7f00f0618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// Copyright 2012 Josh Blum
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#define GRAS_API

////////////////////////////////////////////////////////////////////////
// SWIG director shit - be explicit with all virtual methods
////////////////////////////////////////////////////////////////////////
%module(directors="1") GRAS_Block
%feature("director") gras::BlockPython;
%feature("nodirector") gras::BlockPython::input_buffer_allocator;
%feature("nodirector") gras::BlockPython::output_buffer_allocator;
%feature("nodirector") gras::BlockPython::propagate_tags;
%feature("nodirector") gras::BlockPython::start;
%feature("nodirector") gras::BlockPython::stop;
%feature("nodirector") gras::BlockPython::notify_topology;
%feature("nodirector") gras::BlockPython::work;


////////////////////////////////////////////////////////////////////////
// http://www.swig.org/Doc2.0/Library.html#Library_stl_exceptions
////////////////////////////////////////////////////////////////////////
%include "exception.i"

%exception
{
    try
    {
        $action
    }
    catch (const Swig::DirectorException &e)
    {
        SWIG_fail;
    }
    catch (const std::exception& e)
    {
        SWIG_exception(SWIG_RuntimeError, e.what());
    }
}

%feature("director:except")
{
    if ($error != NULL)
    {
        throw Swig::DirectorMethodException();
    }
}

%{
#include <gras/block.hpp>
#include <iostream>
%}

////////////////////////////////////////////////////////////////////////
// Simple class to deal with smart locking/unlocking of python GIL
////////////////////////////////////////////////////////////////////////
%{

struct PyGILPhondler
{
    PyGILPhondler(void):
        s(PyGILState_Ensure())
    {
        //NOP
    }
    ~PyGILPhondler(void)
    {
        PyGILState_Release(s);
    }
    PyGILState_STATE s;
};

%}

////////////////////////////////////////////////////////////////////////
// SWIG up the representation for IO work arrays
////////////////////////////////////////////////////////////////////////
%include <std_vector.i>
%template (IntVec) std::vector<size_t>;
%template (VoidStarVec) std::vector<void *>;

////////////////////////////////////////////////////////////////////////
// Pull in the implementation goodies
////////////////////////////////////////////////////////////////////////
%include <gras/element.i>
%include <gras/io_signature.i>
%include <gras/sbuffer.hpp>
%include <gras/tags.hpp>
%include <gras/block.hpp>
%include <PMC/PMC.i>

////////////////////////////////////////////////////////////////////////
// Make a special block with safe overloads
////////////////////////////////////////////////////////////////////////

%inline %{

namespace gras
{

struct BlockPython : Block
{
    BlockPython(const std::string &name):
        Block(name)
    {
        //NOP
    }

    virtual ~BlockPython(void)
    {
        //NOP
    }

    bool start(void)
    {
        PyGILPhondler phil;
        return this->_Py_start();
    }

    virtual bool _Py_start(void) = 0;

    bool stop(void)
    {
        PyGILPhondler phil;
        return this->_Py_stop();
    }

    virtual bool _Py_stop(void) = 0;

    void notify_topology(const size_t num_inputs, const size_t num_outputs)
    {
        _input_addrs.resize(num_inputs);
        _input_sizes.resize(num_inputs);
        _output_addrs.resize(num_outputs);
        _output_sizes.resize(num_outputs);

        PyGILPhondler phil;
        return this->_Py_notify_topology(num_inputs, num_outputs);
    }

    virtual void _Py_notify_topology(const size_t, const size_t) = 0;

    void work
    (
        const InputItems &input_items,
        const OutputItems &output_items
    )
    {
        for (size_t i = 0; i < input_items.size(); i++)
        {
            _input_addrs[i] = (void *)(input_items[i].get());
            _input_sizes[i] = input_items[i].size();
        }

        for (size_t i = 0; i < output_items.size(); i++)
        {
            _output_addrs[i] = (void *)(output_items[i].get());
            _output_sizes[i] = output_items[i].size();
        }

        PyGILPhondler phil;
        return this->_Py_work(_input_addrs, _input_sizes, _output_addrs, _output_sizes);
    }

    std::vector<void *> _input_addrs;
    std::vector<size_t> _input_sizes;
    std::vector<void *> _output_addrs;
    std::vector<size_t> _output_sizes;

    virtual void _Py_work
    (
        const std::vector<void *> &,
        const std::vector<size_t> &,
        const std::vector<void *> &,
        const std::vector<size_t> &
    ) = 0;
};

}

%}

////////////////////////////////////////////////////////////////////////
// Conversion to the numpy array
////////////////////////////////////////////////////////////////////////
%pythoncode %{

import numpy

def pointer_to_ndarray(addr, dtype, nitems, readonly=False):
    class array_like:
        __array_interface__ = {
            'data' : (addr, readonly),
            'typestr' : dtype.base.str,
            'descr' : dtype.base.descr,
            'shape' : (nitems,) + dtype.shape,
            'strides' : None,
            'version' : 3,
        }
    return numpy.asarray(array_like()).view(dtype.base)
%}

////////////////////////////////////////////////////////////////////////
// Python overload for adding pythonic interfaces
////////////////////////////////////////////////////////////////////////
%pythoncode %{

import numpy
import traceback

def sig_to_dtype_sig(sig):
    if sig is None: sig = ()
    return map(numpy.dtype, sig)

class Block(BlockPython):
    def __init__(self, name='Block', in_sig=None, out_sig=None):
        BlockPython.__init__(self, name)
        self.set_input_signature(in_sig)
        self.set_output_signature(out_sig)

    def set_input_signature(self, sig):
        self.__in_sig = sig_to_dtype_sig(sig)
        BlockPython.set_input_signature(self, IOSignature([s.itemsize for s in self.__in_sig]))

    def set_output_signature(self, sig):
        self.__out_sig = sig_to_dtype_sig(sig)
        BlockPython.set_output_signature(self, IOSignature([s.itemsize for s in self.__out_sig]))

    def input_signature(self): return self.__in_sig
    def output_signature(self): return self.__out_sig

    def _Py_work(self, input_addrs, input_sizes, output_addrs, output_sizes):

        try:

            input_arrays = list()
            for i in self.__in_indexes:
                addr = long(input_addrs[i])
                nitems = input_sizes[i]
                ndarray = pointer_to_ndarray(addr=addr, dtype=self.__in_sig[i], nitems=nitems, readonly=True)
                input_arrays.append(ndarray)

            output_arrays = list()
            for i in self.__out_indexes:
                addr = long(output_addrs[i])
                nitems = output_sizes[i]
                ndarray = pointer_to_ndarray(addr=addr, dtype=self.__out_sig[i], nitems=nitems, readonly=False)
                output_arrays.append(ndarray)

            ret = self.work(input_arrays, output_arrays)
            if ret is not None:
                raise Exception, 'work return != None, did you call consume/produce?'
        except: traceback.print_exc(); raise

    def work(self, *args):
        print 'Implement Work!'

    def _Py_notify_topology(self, num_inputs, num_outputs):
        self.__in_indexes = range(num_inputs)
        self.__out_indexes = range(num_outputs)
        try: return self.notify_topology(num_inputs, num_outputs)
        except: traceback.print_exc(); raise

    def notify_topology(self, *args): return

    def _Py_start(self):
        try: return self.start()
        except: traceback.print_exc(); raise

    def start(self): return True

    def _Py_stop(self):
        try: return self.stop()
        except: traceback.print_exc(); raise

    def stop(self): return True

%}