<div dir="ltr"><font face="arial, helvetica, sans-serif">HI Eleanore,</font><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Thanks for the kind words, you are very welcome!</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">As for your issues, I think they are coming from the handling of the strides you are doing in the </font><font face="monospace, monospace">slow_dtw_dist</font><font face="arial, helvetica, sans-serif"> function.  The strides are the number of bytes you have to advance your pointer to get to the next item.  In your code, you end up doing something akin to:</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="monospace, monospace"><span style="color:rgb(0,0,0);font-size:12px;line-height:21px">dtype </span><span class="" style="color:rgb(0,0,64);font-size:12px;line-height:21px">*</span><span style="color:rgb(0,0,0);font-size:12px;line-height:21px">v_i = v0;</span><br></font></div><div><span style="color:rgb(0,0,0);font-size:12px;line-height:21px"><font face="monospace, monospace">...</font></span></div><div><span style="color:rgb(0,0,0);font-size:12px;line-height:21px"><font face="monospace, monospace">for (...) {</font></span></div><div><span style="color:rgb(0,0,0);font-size:12px;line-height:21px"><font face="monospace, monospace">    ...</font></span></div><div><font face="monospace, monospace"><span style="color:rgb(0,0,0);font-size:12px;line-height:21px">    v_i += stri</span><span style="color:rgb(0,0,0);font-size:12px;line-height:21px">de_v;</span></font></div><div><span style="color:rgb(0,0,0);font-size:12px;line-height:21px"><font face="monospace, monospace">}</font></span></div><div><span style="color:rgb(0,0,0);font-size:12px;line-height:21px"><font face="arial, helvetica, sans-serif"><br></font></span></div><div><span style="color:rgb(0,0,0);font-size:12px;line-height:21px"><font face="arial, helvetica, sans-serif">This, rather than increase the </font><font face="monospace, monospace">v_i</font><font face="arial, helvetica, sans-serif"> pointer by </font><font face="monospace, monospace">stride_v</font><font face="arial, helvetica, sans-serif"> bytes, increases it by </font><font face="monospace, monospace">stride_v * sizeof(dtype)</font><font face="arial, helvetica, sans-serif">, and with the </font><font face="monospace, monospace">npy_double</font><font face="arial, helvetica, sans-serif"> you seem to be using as </font><font face="monospace, monospace">dtype</font><font face="arial, helvetica, sans-serif">, sends you out of your allocated memory at a rate 8x too fast.</font></span></div><div><span style="color:rgb(0,0,0);font-size:12px;line-height:21px"><font face="arial, helvetica, sans-serif"><br></font></span></div><div><span style="color:rgb(0,0,0);font-size:12px;line-height:21px"><font face="arial, helvetica, sans-serif">What you increase by </font><font face="monospace, monospace">stride_v</font><font face="arial, helvetica, sans-serif"> has to be of </font><font face="monospace, monospace">char*</font><font face="arial, helvetica, sans-serif"> type, so one simple solution would be to do something like:</font></span></div><div><span style="color:rgb(0,0,0);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono',monospace,serif;font-size:12px;line-height:21px"><br></span></div><div><span style="color:rgb(0,0,0);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono',monospace,serif;font-size:12px;line-height:21px">char *v_ptr = (char *)v0;</span></div><div><span style="color:rgb(0,0,0);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono',monospace,serif;font-size:12px;line-height:21px">...</span></div><div><span style="color:rgb(0,0,0);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono',monospace,serif;font-size:12px;line-height:21px">for (...) {</span></div><div><span style="color:rgb(0,0,0);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono',monospace,serif;font-size:12px;line-height:21px">    dtype v_val = *(dtype *)v_ptr;</span></div><div><span style="color:rgb(0,0,0);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono',monospace,serif;font-size:12px;line-height:21px">    ...</span></div><div><span style="color:rgb(0,0,0);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono',monospace,serif;font-size:12px;line-height:21px">    v_ptr += stride_v;</span></div><div><span style="color:rgb(0,0,0);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono',monospace,serif;font-size:12px;line-height:21px">}</span></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font color="#000000"><span style="font-size:12px;line-height:21px"><font face="arial, helvetica, sans-serif">And use </font><font face="monospace, monospace">v_val</font><font face="arial, helvetica, sans-serif"> directly wherever you were dereferencing </font><font face="monospace, monospace">v_i</font><font face="arial, helvetica, sans-serif"> before.</font></span></font></div><div><font color="#000000"><span style="font-size:12px;line-height:21px"><font face="arial, helvetica, sans-serif"><br></font></span></font></div><div><font color="#000000"><span style="font-size:12px;line-height:21px"><font face="arial, helvetica, sans-serif">Jaime</font></span></font></div><div> </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Oct 25, 2015 at 5:06 AM,  <span dir="ltr"><<a href="mailto:eleanore.young@artorg.unibe.ch" target="_blank">eleanore.young@artorg.unibe.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div style="word-wrap:break-word">
Dear Numpy maintainers and developers,<br>
<br>
Thanks for providing such a great numerical library!<br>
<br>
I’m currently trying to implement the Dynamic Time Warping metric as a set of generalised numpy ufuncs, but unfortunately, I have lasting issues with pointer arithmetic and segmentation faults. Is there any way that I can<br>
use GDB or some such to debug a python/numpy extension? Furthermore: is it necessary to use pointer arithmetic to access the function arguments (as seen on <a href="http://docs.scipy.org/doc/numpy/user/c-info.ufunc-tutorial.html" target="_blank">http://docs.scipy.org/doc/numpy/user/c-info.ufunc-tutorial.html</a>)<br>
or is element access (operator[]) also permissible?<br>
<br>
To break it down quickly, I need to have a fast DTW distance function dist_dtw() with two vector inputs (broadcasting should be possible), two scalar parameters and one scalar output (signature: (i), (j), (), () -> ()) usable in python for a 1-Nearest Neighbor
 classification algorithm. The extension also implements two functions compute_envelope() and piecewise_mean_reduction() which are used for lower-bounding based on Keogh and Ratanamahatana, 2005. The source code is available at <a href="http://pastebin.com/MunNaP7V" target="_blank">http://pastebin.com/MunNaP7V</a> and
 the prominent segmentation fault happens somewhere in the chain dist_dtw() —> meta_dtw_dist() —> slow_dtw_dist(), but I fail to pin it down.<br>
<br>
Aside from my primary questions, I wonder how to approach errors/exceptions and unit testing when developing numpy ufuncs. Are there any examples apart from the numpy manual that I could use as reference implementations of generalised numpy ufuncs?<br>
<br>
I would greatly appreciate some insight into properly developing generalised ufuncs.<br>
<br>
Best,<br>
Eleanore<br>
<br>
</div>

<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="https://mail.scipy.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.</div>
</div>