Skip to content

Conversation

@mahf708
Copy link
Collaborator

@mahf708 mahf708 commented Nov 6, 2025

the idea here is:

Since AI models are usually written in Python, we can easily bridge our coupler infrastructure in F90 via a lightweight C++ bridge to call these models. This example illustrates the basic idea from a fortran_main through python_bridge to a python_main that calls python_torch. We hide the python_torch deps in main so that the C++ bridge doesn't need to know anything about them. We pass around pointers so that we don't copy data. Pytorch (not Python) can transfer the data (usually arrays) to GPU if it wants --- not shown here (yet)

todo:

  • add docs (something to try at home as well as something to try on pm-gpu)
  • add optional explicit gpu sync via pytorch device stuff
  • explore dlpack for seamlessly wrapping of device/host data (say if we want ace on gpu to interface with omega on gpu, etc.)

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new prototype demonstrating interoperability between Fortran, C++, and Python with PyTorch integration. The implementation establishes a complete pipeline where Fortran initiates computation, C++ provides a bridge layer using pybind11, and Python performs PyTorch tensor operations.

Key changes:

  • Creates a multi-language bridge architecture enabling Fortran → C++ → Python communication
  • Implements PyTorch tensor operations callable from Fortran via C++ bridge
  • Configures CMake build system with pybind11 integration for the prototype

Reviewed Changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
prototypes/fortran-cpp-python/src/python/python_torch.py Implements PyTorch tensor sum operation
prototypes/fortran-cpp-python/src/python/python_main.py Provides Python dispatcher entry point with lazy import
prototypes/fortran-cpp-python/src/fortran/fortran_main.f90 Fortran main program that initializes and calls Python operations
prototypes/fortran-cpp-python/src/cpp/python_bridge.hpp C API header for Python bridge functions
prototypes/fortran-cpp-python/src/cpp/python_bridge.cpp C++ implementation of Python bridge using pybind11
prototypes/fortran-cpp-python/CMakeLists.txt CMake configuration for building the prototype
.gitignore Ignores build artifacts and Python cache files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +20 to +21
# we are importing here so we don't need to make the
# cpp bridge aware of this stuff and such
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

[nitpick] Comment text should be capitalized and properly formatted. 'we' should be 'We' and 'cpp' should be 'C++'.

Suggested change
# we are importing here so we don't need to make the
# cpp bridge aware of this stuff and such
# We are importing here so we don't need to make the
# C++ bridge aware of this stuff and such

Copilot uses AI. Check for mistakes.
Comment on lines +47 to +52
py::array_t<float> array(
{size}, // shape
{sizeof(float)}, // strides
data, // data pointer
py::cast(nullptr) // owner (nullptr = not owned by Python)
);
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

No validation is performed on the 'size' parameter before creating the array. If a negative or excessively large size is passed, it could lead to undefined behavior or security issues. Add validation to ensure size is positive and within reasonable bounds.

Copilot uses AI. Check for mistakes.

int call_main_python(const float* data, int size, float* result) {
std::cout << "[C++] Calling call_main_python..." << std::endl;

Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

The 'data' pointer is not validated for null before being passed to py::array_t constructor. Add a null check to prevent potential crashes or undefined behavior.

Suggested change
if (data == nullptr) {
std::cerr << "[C++] Error: data pointer is null." << std::endl;
return -1;
}

Copilot uses AI. Check for mistakes.

int call_main_python(const float* data, int size, float* result) {
std::cout << "[C++] Calling call_main_python..." << std::endl;

Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

The 'result' pointer is not validated for null before dereferencing at line 56. Add a null check to prevent potential crashes.

Suggested change
if (result == nullptr) {
std::cerr << "[C++] Error: result pointer is null." << std::endl;
return -2;
}

Copilot uses AI. Check for mistakes.
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