xref: /aosp_15_r20/external/tensorflow/tensorflow/stream_executor/rocm/rocm_helpers.cu.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <hip/hip_runtime.h>
17 
18 #include <limits>
19 namespace stream_executor {
20 namespace gpu {
21 
22 // GPU kernel to populate an array of pointers:
23 //
24 //   [base + stride * i for i in range(n)].
25 //
26 
__xla_MakeBatchPointers(char * base,int stride,int n,void ** ptrs_out)27 __global__ void __xla_MakeBatchPointers(char* base, int stride, int n,
28                                         void** ptrs_out) {
29   int idx = threadIdx.x + blockIdx.x * blockDim.x;
30   if (idx >= n) return;
31   ptrs_out[idx] = base + idx * stride;
32 }
33 
rocm_MakeBatchPointers(void * stream,char * base,int stride,int n,void ** ptrs_out)34 void rocm_MakeBatchPointers(void* stream, char* base, int stride, int n,
35                             void** ptrs_out) {
36   const int threads_per_block = 256;
37   hipLaunchKernelGGL(
38       __xla_MakeBatchPointers,
39       dim3((n + threads_per_block - 1) / threads_per_block, 1, 1),
40       dim3(threads_per_block, 1, 1), 0, (hipStream_t)stream, base, stride, n,
41       ptrs_out);
42 }
43 
44 };  // namespace gpu
45 };  // namespace stream_executor
46