Skip to content

[flang][cuda] Add entry point to launch cuda fortran kernel #113490

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

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flang/include/flang/Runtime/CUDA/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static constexpr unsigned kDeviceToDevice = 2;
const char *name = cudaGetErrorName(err); \
if (!name) \
name = "<unknown>"; \
Terminator terminator{__FILE__, __LINE__}; \
Fortran::runtime::Terminator terminator{__FILE__, __LINE__}; \
terminator.Crash("'%s' failed with '%s'", #expr, name); \
}(expr)

Expand Down
27 changes: 27 additions & 0 deletions flang/include/flang/Runtime/CUDA/kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- include/flang/Runtime/CUDA/kernel.h ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef FORTRAN_RUNTIME_CUDA_KERNEL_H_
#define FORTRAN_RUNTIME_CUDA_KERNEL_H_

#include "flang/Runtime/entry-names.h"
#include <cstddef>
#include <stdint.h>

extern "C" {

// This function uses intptr_t instead of CUDA's unsigned int to match
// the type of MLIR's index type. This avoids the need for casts in the
// generated MLIR code.
void RTDEF(CUFLaunchKernel)(const void *kernelName, intptr_t gridX,
intptr_t gridY, intptr_t gridZ, intptr_t blockX, intptr_t blockY,
intptr_t blockZ, int32_t smem, void **params, void **extra);

} // extern "C"

#endif // FORTRAN_RUNTIME_CUDA_KERNEL_H_
1 change: 1 addition & 0 deletions flang/runtime/CUDA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_flang_library(${CUFRT_LIBNAME}
allocator.cpp
allocatable.cpp
descriptor.cpp
kernel.cpp
memory.cpp
registration.cpp
)
Expand Down
33 changes: 33 additions & 0 deletions flang/runtime/CUDA/kernel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===-- runtime/CUDA/kernel.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "flang/Runtime/CUDA/kernel.h"
#include "../terminator.h"
#include "flang/Runtime/CUDA/common.h"

#include "cuda_runtime.h"

extern "C" {

void RTDEF(CUFLaunchKernel)(const void *kernel, intptr_t gridX, intptr_t gridY,
intptr_t gridZ, intptr_t blockX, intptr_t blockY, intptr_t blockZ,
int32_t smem, void **params, void **extra) {
dim3 gridDim;
gridDim.x = gridX;
gridDim.y = gridY;
gridDim.z = gridZ;
dim3 blockDim;
blockDim.x = blockX;
blockDim.y = blockY;
blockDim.z = blockZ;
cudaStream_t stream = 0;
CUDA_REPORT_IF_ERROR(
cudaLaunchKernel(kernel, gridDim, blockDim, params, smem, stream));
}

} // extern "C"
Loading