Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the rowwise add function. #7047

Merged
merged 3 commits into from
Dec 27, 2017

Conversation

qingqing01
Copy link
Contributor

Fix #6842

Mainly for the broadcast in Eigen. The time changes after optimization are as follows:

  • Experiments Env:

    • config: 3 stacked LSTM network, the hidden size is 64
    • 2 epoc
  • Total time of 2 epoc:

    • CPU: 345.54137s -> 304.96511s .
    • GPU: 89.72162s vs 89.22058s. This optimization does not change the execution time on GPU.

template <typename T>
__global__ void RowwiseAddKernel(const T* a, const T* b, T* c, int64_t height,
int64_t width) {
int64_t num = height * width;
Copy link
Contributor

Choose a reason for hiding this comment

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

num can be passed in as a parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thank you!

for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < num;
i += blockDim.x * gridDim.x) {
int h = i / width;
int w = i % width;
Copy link
Contributor

@chengduoZH chengduoZH Dec 27, 2017

Choose a reason for hiding this comment

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

The integer modulo (%) and division operations are expensive in GPU hardware.
The division seems can be replaced by the multiplication. And modulo (%) can be replaced by subtraction and multiplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thank you!

__global__ void RowwiseAddKernel(const T* a, const T* b, T* c, int64_t width,
int64_t num) {
T tmp = 1.0 / width;
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < num;
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to change the type of num to int. Otherwise, there is a comparison of int data and int64_t data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks!

Copy link
Contributor

@chengduoZH chengduoZH left a comment

Choose a reason for hiding this comment

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

LGTM

@qingqing01 qingqing01 merged commit 95da78a into PaddlePaddle:develop Dec 27, 2017
@qingqing01 qingqing01 deleted the rowwise_add branch November 14, 2019 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants