Repository navigation
Add check of c_contiguous when indexing CArray - #3191
Conversation
| static const int ndim = _ndim; | ||
| private: | ||
| T* data_; | ||
| bool c_contiguous; |
There was a problem hiding this comment.
I wonder if data alignment in CArray matters or not? Could you try moving this flag to the end of private data members and see if timing changes?
There was a problem hiding this comment.
let me give it a try again but I had problems with it before
There was a problem hiding this comment.
It is not catching the boolean value in the carray.cuh ...
There was a problem hiding this comment.
changed types to enforce alignment, if defined after the shape & strides array probably the value is not written to the same memory location due to some memory layout differences between nvrtc & gcc. I am not 100% sure of this but I can't find another explanation.
There was a problem hiding this comment.
I have no idea why we have the alignment issue here...Looks like the struct should be 8 byte aligned (due to ptrdiff_t)? Sorry I didn't help 😅
4dfcfb8 to
8ef2113
Compare
8ef2113 to
9f9ae2a
Compare
| cdef struct _CArray: | ||
| void* data | ||
| Py_ssize_t size | ||
| Py_ssize_t c_contiguous |
There was a problem hiding this comment.
using ssize_t to enforce correct alignment.
Maybe add a comment?
| private: | ||
| T* data_; | ||
| ptrdiff_t size_; | ||
| ptrdiff_t c_contiguous; |
There was a problem hiding this comment.
How about passing c_contiguous as a template parameter?
There was a problem hiding this comment.
Oh, I happen to be looking into the internals, so while waiting for thoughts from @emcastillo, I can share my objection: If c_contiguous is a template parameter instead of a class member, you'd be re-compiling kernels for C-contiguous inputs, reducing the chances of reusing existing kernels. This is a pure waste, since all the kernels do not have any specialization for such cases.
There was a problem hiding this comment.
I don't think it will create that much additional kernels.
I guess I can try to run the test-suite and measure the number of created kernels in both cases. something like an extra 15~20% should be admissible I guess
There was a problem hiding this comment.
Yes, the condition in L219 can be decided at compile-time, so it is good for compile-time optimization.
There was a problem hiding this comment.
Do you think that the total increase in the number of compiled kernels can be a burden?
There was a problem hiding this comment.
Currently, CArray takes ndim as a template parameter, but I feel it is not a burden. So I guess that c_contiguous will not be so too.
There was a problem hiding this comment.
pushed the changes, PTAL
|
One more stupid question: I suppose the name "CArray" implies that F-contiguous arrays are discriminated? |
|
yay, no one likes fortran arrays |
|
Jenkins, test this please. |
|
Successfully created a job for commit 0f55f11: |
|
Jenkins CI test (for commit 0f55f11, target branch master) failed with status FAILURE. |
|
Fixed! |
b5f9d00 to
40531fa
Compare
40531fa to
99e9286
Compare
|
Jenkins, test this please. |
|
Successfully created a job for commit 99e9286: |
|
Jenkins CI test (for commit 99e9286, target branch master) succeeded! |
|
LGTM! |
Originated at #3179
When using raw arrays in a Elementwise kernel, their dimensions are not reduced even though they are contiguous. This is because the user may expect to address them using their real shape.
However, if these arrays are c_contiguous they can be indexed linearly, avoiding several expensive operations in 64-bit to transform a single integer index to a
ndimIndexer.Results on convolve function for x_size = (4000, 4000) and w_size of (3, 3) in a P100 are:
Master before #3179
This PR
Before template
After template
Master after #3179
#3179 addresses this issue directly in the
convolvefunction. So results are expected to be better, as there is still some overhead on the indexing of this PR (A function call and a comparison). But this PR is applied generally so other routines can see performance benefits from it. If we templatize the parameter the performance gains are exactly in the range of #3179.But this will increase the number of compiled kernels as a drawback.
Thanks to @anaruse and @coderforlife for their ideas.