Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> if (conn_ctr&lt;sum*2){ *(state_out-&gt;data + i*state_out-&gt;strides[0]) = true; } else { *(state_out-&gt;data + i*state_out-&gt;strides[0]) = false; } </code></pre> <p>Here, I naively make a pointer arithmetic, state_out->data is a pointer to the beginning of data, it is defined to be a pointer of char:<a href="http://scipy.org/doc/numpy/reference/c-api.types-and-structures.html" rel="nofollow">SciPy Doc - Python Types and C-Structures</a></p> <pre><code>typedef struct PyArrayObject { PyObject_HEAD char *data; int nd; npy_intp *dimensions; npy_intp *strides; ... } PyArrayObject; </code></pre> <p>Which a portion of I copied here. state_out->strides is a pointer to an array of length of the dimension of the array we have. This is a 1d array in this case. So when I make the pointer arithmetic <code>(state_out-&gt;data + i*state_out-&gt;strides[0])</code> I certainly aim to calculate the pointer that points the ith value of the array, but I failed to give the type of the pointer, so the </p> <p>I had tried :</p> <pre><code>NPY_BOOL *adj_value_ptr, *mask_value_ptr, *state_value_ptr, *state_out_ptr; </code></pre> <p>which the variables are pointing towards the values that I am interested in my for loop, and state_out_ptr is the one that I am writing to. I had thought that since I state that the constituents of these arrays are of type <code>NPY_BOOL</code>, the pointers that point to the data within the array would be of type <code>NPY_BOOL</code> also. <strong>This fails with a SegFault when one is working with data directly manipulating the memory. This is from the fact that NPY_BOOL is an <code>enum</code> for an integer (as pv kindly stated in the comments.) for NumPy to use internally,.There is a C <code>typedef</code> <code>npy_bool</code> in order to use within the code for boolean values.</strong> <a href="http://docs.scipy.org/doc/numpy/reference/c-api.dtype.html" rel="nofollow">Scipy Docs</a>. When I introduced my pointers with the type </p> <pre><code>npy_bool *adj_value_ptr, *mask_value_ptr, *state_value_ptr, *state_out_ptr; </code></pre> <p>Segmentation fault disappeared, and I succeeded in manipulating and returning a Numpy Array.</p> <p>I'm not an expert, but this solved my issue, point out if I'm wrong.</p> <p>The part that has changed in the source code is:</p> <pre><code>state_out = (PyArrayObject *) PyArray_FromDims(1,dims_new,NPY_BOOL); npy_bool *adj_value_ptr, *mask_value_ptr, *state_value_ptr, *state_out_ptr; npy_intp i,j; for(i=0;i&lt;dims[0];i++){ npy_int sum = 0; npy_int conn_ctr = 0; for(j=0;j&lt;dims[1];j++){ adj_value_ptr = (adjacency_arr-&gt;data + i*adjacency_arr-&gt;strides[0] +j*adjacency_arr-&gt;strides[1]); if (*adj_value_ptr == true){ mask_value_ptr = (mask_arr-&gt;data + i*mask_arr-&gt;strides[0] +j*mask_arr-&gt;strides[1]); state_value_ptr = (state_arr-&gt;data + j*state_arr-&gt;strides[0]); if ( (*(bool *) mask_value_ptr ^ *(bool *)state_value_ptr) == true){ sum++; } conn_ctr++; } } state_out_ptr = (state_out-&gt;data + i*state_out-&gt;strides[0]); if (conn_ctr &lt; sum*2){ *state_out_ptr = true; } else { *state_out_ptr = false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload