-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathch_vdif_assembler_cython.hpp
164 lines (124 loc) · 4.35 KB
/
ch_vdif_assembler_cython.hpp
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
//
// This file defines some extra helper classes and routines which are
// useful for the python interface, but aren't used in the C++ interface.
//
#ifndef _CH_VDIF_ASSEMBLER_CYTHON_HPP
#define _CH_VDIF_ASSEMBLER_CYTHON_HPP
#include "ch_vdif_assembler_internals.hpp"
namespace ch_vdif_assembler {
#if 0
}; // pacify emacs c-mode
#endif
// -------------------------------------------------------------------------------------------------
//
// cython_stream: just a wrapper around shared_ptr<vdif_stream>, to avoid confusing cython
//
struct cython_stream {
std::shared_ptr<vdif_stream> p;
cython_stream(const std::shared_ptr<vdif_stream> &p_) : p(p_) { xassert(p); }
};
inline cython_stream *cython_file_stream(const std::vector<std::string> &filename_list)
{
std::shared_ptr<vdif_stream> p = make_file_stream(filename_list);
return new cython_stream(p);
}
inline cython_stream *cython_simulated_stream(double gbps, double nsec)
{
std::shared_ptr<vdif_stream> p = make_simulated_stream(gbps, nsec);
return new cython_stream(p);
}
inline cython_stream *cython_network_stream()
{
std::shared_ptr<vdif_stream> p = make_network_stream();
return new cython_stream(p);
}
// -------------------------------------------------------------------------------------------------
//
// cpp_processor: wrapper around shared_ptr<vdif_processor>, to avoid confusing cython
//
struct cpp_processor {
std::shared_ptr<vdif_processor> p;
cpp_processor(const std::shared_ptr<vdif_processor> &p_) : p(p_) { xassert(p); }
};
inline cpp_processor *cpp_waterfall_plotter(const std::string &outdir, bool is_critical)
{
std::shared_ptr<vdif_processor> p = make_waterfall_plotter(outdir, is_critical);
return new cpp_processor(p);
}
// -------------------------------------------------------------------------------------------------
//
// cython_assembled_chunk: wrapper around shared_ptr<assembled_chunk>, to avoid confusing cython
//
struct cython_assembled_chunk {
std::shared_ptr<assembled_chunk> p;
int64_t t0;
int nt;
cython_assembled_chunk(const std::shared_ptr<assembled_chunk> &p_)
: p(p_)
{
xassert(p);
this->t0 = p->t0;
this->nt = p->nt;
}
// FIXME how to get a "float complex" pointer from cython?
inline void fill_efield(void *efield_hack, int32_t *mask)
{
if (!efield_hack || !mask)
throw std::runtime_error("NULL pointer passed to fill_efield()");
std::complex<float> *efield = reinterpret_cast<std::complex<float> *> (efield_hack);
p->fill_efield_array_reference(efield, mask);
}
};
// -------------------------------------------------------------------------------------------------
//
// cython_assembler
struct cython_assembler {
vdif_assembler a;
std::shared_ptr<processor_handle> python_processor;
cython_assembler(bool write_to_disk, int rbuf_size, int abuf_size, int assembler_nt)
: a(write_to_disk, rbuf_size, abuf_size, assembler_nt)
{ }
void register_cpp_processor(cpp_processor *processor)
{
xassert(processor);
xassert(processor->p);
a.register_processor(processor->p);
}
void register_python_processor()
{
if (python_processor)
throw std::runtime_error("double call to cython_assembler::register_python_processor");
python_processor = std::make_shared<processor_handle> ("python processor", a.nc);
}
// can return NULL
cython_assembled_chunk *get_next_python_chunk()
{
if (!python_processor)
throw std::runtime_error("cython_assembler::get_next_python_chunk() called, but no python processor was registered");
// We don't bother collecting timing statistics for the python processor
thread_timer timer_unused;
std::shared_ptr<assembled_chunk> chunk = this->python_processor->get_next_chunk(timer_unused);
if (!chunk)
return NULL;
return new cython_assembled_chunk(chunk);
}
// this seemed like a good idea, before calling wait_until_end()
void unregister_python_processor()
{
python_processor = std::shared_ptr<processor_handle> ();
}
void start_async(cython_stream *s)
{
xassert(s);
xassert(s->p);
a.start_async(s->p);
}
void wait_until_end()
{
if (python_processor)
throw std::runtime_error("cython_assembler::wait_until_end() called with python processor registered");
a.wait_until_end();
}
};
} // namespace ch_vdif_assembler
#endif // _CH_VDIF_ASSEMBLER_CYTHON_HPP