Skip to content

Add check of c_contiguous when indexing CArray - #3191

Merged
asi1024 merged 3 commits into
cupy:masterfrom
emcastillo:fast_indexing
Mar 17, 2020
Merged

asi1024 merged 3 commits into
cupy:masterfrom
emcastillo:fast_indexing

Conversation

@emcastillo

@emcastillo emcastillo commented Mar 13, 2020 •

Copy link
Copy Markdown
Member

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 ndim Indexer.

Results on convolve function for x_size = (4000, 4000) and w_size of (3, 3) in a P100 are:

Master before #3179

convolve            :    6640.630 us

This PR
Before template

convolve            :    2222.367 us

After template

convolve            :    1570.488

Master after #3179

convolve            :    1571.202 us

#3179 addresses this issue directly in the convolve function. 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.

@emcastillo emcastillo added the cat:performance Performance in terms of speed or memory consumption label Mar 13, 2020
Comment thread cupy/core/_carray.pxd Resolved
Comment thread cupy/core/include/cupy/carray.cuh Outdated
static const int ndim = _ndim;
private:
T* data_;
bool c_contiguous;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me give it a try again but I had problems with it before

@emcastillo emcastillo Mar 16, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not catching the boolean value in the carray.cuh ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😅

@emcastillo
emcastillo force-pushed the fast_indexing branch 2 times, most recently from 4dfcfb8 to 8ef2113 Compare March 16, 2020 01:36
Comment thread cupy/core/_carray.pxd Outdated
cdef struct _CArray:
void* data
Py_ssize_t size
Py_ssize_t c_contiguous

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using ssize_t to enforce correct alignment.
Maybe add a comment?

@asi1024 asi1024 self-assigned this Mar 16, 2020
Comment thread cupy/core/include/cupy/carray.cuh Outdated
private:
T* data_;
ptrdiff_t size_;
ptrdiff_t c_contiguous;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about passing c_contiguous as a template parameter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops...we replied in sync 😛

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the condition in L219 can be decided at compile-time, so it is good for compile-time optimization.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that the total increase in the number of compiled kernels can be a burden?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed the changes, PTAL

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the checks, Emilio!

@leofang

leofang commented Mar 16, 2020

Copy link
Copy Markdown
Member

One more stupid question: I suppose the name "CArray" implies that F-contiguous arrays are discriminated?

@emcastillo

Copy link
Copy Markdown
Member Author

yay, no one likes fortran arrays

@asi1024

asi1024 commented Mar 17, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit 0f55f11:

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 0f55f11, target branch master) failed with status FAILURE.

@emcastillo

emcastillo commented Mar 17, 2020 •

Copy link
Copy Markdown
Member Author

After fixing the issues, there are 2 tests that keep failing.
However, the failure is a bit tricky.

tests.cupy_tests.core_tests.test_ndarray_adv_indexing.TestArrayAdvancedIndexingGetitemPerm_param_43_{indexes=([1, 2], [1, 0], slice(0, 3, 2)), shape=(4, 4, 4)}::test_adv_getitem
tests.cupy_tests.core_tests.test_ndarray_adv_indexing.TestArrayAdvancedIndexingGetitemPerm_param_45_{indexes=([1, 0], [1, 2], slice(0, 3, 2)), shape=(4, 4, 4)}::test_adv_getitem

If we only run these two tests, they never fail, but if they are run with other tests, they do.
If we change the order of the parameters and make this test to execute in the first place, then everything works. There seems to be some kind of issue in the advanced indexing kernels maybe. I am not sure if it's related to my PR or not ...

TestArgpartition_param_0_{external=False}.test_argpartition_non_contiguous

Also fails.

Fixed!
It was an error memoizing the c_contiguous flag when getting elementwise kernels

Comment thread cupy/core/_routines_sorting.pyx Resolved
@asi1024

asi1024 commented Mar 17, 2020

Copy link
Copy Markdown
Member

Jenkins, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit 99e9286:

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 99e9286, target branch master) succeeded!

@asi1024

asi1024 commented Mar 17, 2020

Copy link
Copy Markdown
Member

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:performance Performance in terms of speed or memory consumption

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants