Skip to content

Commit a7f34fb

Browse files
committed
[flang][cuda] Add entry point to launch cuda fortran kernel
1 parent 60105ac commit a7f34fb

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- include/flang/Runtime/CUDA/kernel.h ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef FORTRAN_RUNTIME_CUDA_KERNEL_H_
10+
#define FORTRAN_RUNTIME_CUDA_KERNEL_H_
11+
12+
#include "flang/Runtime/entry-names.h"
13+
#include <cstddef>
14+
#include <stdint.h>
15+
16+
namespace Fortran::runtime::cuda {
17+
18+
extern "C" {
19+
20+
// This function uses intptr_t instead of CUDA's unsigned int to match
21+
// the type of MLIR's index type. This avoids the need for casts in the
22+
// generated MLIR code.
23+
void RTDEF(CUFLaunchKernel)(const void *kernelName, intptr_t gridX,
24+
intptr_t gridY, intptr_t gridZ, intptr_t blockX, intptr_t blockY,
25+
intptr_t blockZ, int32_t smem, void **params, void **extra);
26+
27+
} // extern "C"
28+
29+
} // namespace Fortran::runtime::cuda

flang/runtime/CUDA/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_flang_library(${CUFRT_LIBNAME}
1717
allocator.cpp
1818
allocatable.cpp
1919
descriptor.cpp
20+
kernel.cpp
2021
memory.cpp
2122
registration.cpp
2223
)

flang/runtime/CUDA/kernel.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- runtime/CUDA/kernel.cpp -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "flang/Runtime/CUDA/kernel.h"
10+
#include "../terminator.h"
11+
#include "flang/Runtime/CUDA/common.h"
12+
13+
#include "cuda_runtime.h"
14+
15+
namespace Fortran::runtime::cuda {
16+
17+
extern "C" {
18+
19+
void RTDEF(CUFLaunchKernel)(const void *kernel, intptr_t gridX, intptr_t gridY,
20+
intptr_t gridZ, intptr_t blockX, intptr_t blockY, intptr_t blockZ,
21+
int32_t smem, void **params, void **extra) {
22+
dim3 gridDim;
23+
gridDim.x = gridX;
24+
gridDim.y = gridY;
25+
gridDim.z = gridZ;
26+
dim3 blockDim;
27+
blockDim.x = blockX;
28+
blockDim.y = blockY;
29+
blockDim.z = blockZ;
30+
cudaStream_t stream = 0;
31+
CUDA_REPORT_IF_ERROR(
32+
cudaLaunchKernel(kernel, gridDim, blockDim, params, smem, stream));
33+
}
34+
35+
} // extern "C"
36+
37+
} // namespace Fortran::runtime::cuda

0 commit comments

Comments
 (0)