-
Notifications
You must be signed in to change notification settings - Fork 0
simple fortran-cpp-python prototype #1
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
| # we are importing here so we don't need to make the | ||
| # cpp bridge aware of this stuff and such |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
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++'.
| # 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 |
| py::array_t<float> array( | ||
| {size}, // shape | ||
| {sizeof(float)}, // strides | ||
| data, // data pointer | ||
| py::cast(nullptr) // owner (nullptr = not owned by Python) | ||
| ); |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
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.
|
|
||
| int call_main_python(const float* data, int size, float* result) { | ||
| std::cout << "[C++] Calling call_main_python..." << std::endl; | ||
|
|
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
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.
| if (data == nullptr) { | |
| std::cerr << "[C++] Error: data pointer is null." << std::endl; | |
| return -1; | |
| } |
|
|
||
| int call_main_python(const float* data, int size, float* result) { | ||
| std::cout << "[C++] Calling call_main_python..." << std::endl; | ||
|
|
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
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.
| if (result == nullptr) { | |
| std::cerr << "[C++] Error: result pointer is null." << std::endl; | |
| return -2; | |
| } |
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: