/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include "paddle/fluid/platform/device/gpu/gpu_device_function.h" #ifdef __HIPCC__ #define ELEMENTWISE_BLOCK_SIZE 256 #else #define ELEMENTWISE_BLOCK_SIZE 512 #endif namespace pten { namespace funcs { /* * According to NVIDIA, if number of threads per block is 64/128/256/512, * cuda performs better. And number of blocks should be greater (at least * 2x~4x) than number of SMs. Hence, SM count is took into account within * this function to determine the right number of threads per block. */ inline int GetThreadsConfig(const paddle::platform::CUDADeviceContext &ctx, int64_t numel, int vec_size) { int threads = ELEMENTWISE_BLOCK_SIZE; int sm_count = ctx.GetSMCount(); int active_threads_num = numel / vec_size; if (active_threads_num / (sm_count << 1) < ELEMENTWISE_BLOCK_SIZE) { // Round up threads number into an exponential multiple of 2, while number // of acitve blocks is about twice of SM, to acquire better performance. threads = paddle::platform::RoundToPowerOfTwo(active_threads_num / (sm_count << 1)); } else if (active_threads_num / (sm_count << 2) < ELEMENTWISE_BLOCK_SIZE) { // Round up threads number into an exponential multiple of 2, while number // of acitve blocks is about 4 times of SM, to acquire better performance. threads = paddle::platform::RoundToPowerOfTwo(active_threads_num / (sm_count << 2)); } // Number of threads per block shall be larger than 64. return std::max(64, threads); } } // namespace funcs } // namespace pten