M
Modular13mo ago
Noe

Term paper about Mojo - Open questions

Hi I am currently completing my Bachelor's degree in Computer Science at the Lucerne University of Applied Sciences and Arts in Switzerland. In my term paper, I am testing Mojo and comparing it with Python. This includes comparing differences, special concepts and performance. In one chapter I would like to explain how the interoperability between Python and Mojo exactly works. I was able to find some information on this in the manual and in the blogs, but not enough in-depth information. Therefore, I still have some open questions: - How can I as a programmer know where parts of my code are compiled? (LLVM or Python interpreter, what does it depend on). - How do the two compilers share resources? And how do the different compiled machine codes work together? (Are they uniform?) - What conversions are required for such shared resources? - What exactly does the process of such interoperability look like? Are there any diagrams or other visualizations? Thank you & kind regards Noe
7 Replies
jordiae
jordiae13mo ago
Everything is compiled except for the statements explicitly offloaded to Python (from Python import … etc )
Noe
NoeOP13mo ago
Hi. Yes, thats how I have also understood it, but how does it look in detail and how if it is AOT compiled? I assume that somehow the memory is shared for the interoperability and that transformations are necessary for the shared data. 🤔
ModularBot
ModularBot13mo ago
Congrats @Noe, you just advanced to level 2!
Arthur Evans
Arthur Evans13mo ago
@Noe your compiled binary is loading the CPython interpreter (from a shared library). Objects constructed by your code are Mojo objects--including literals. If you look at the doc for PythonObject: https://docs.modular.com/mojo/stdlib/python/object.html#pythonobject You'll see that every PythonObject has a py_object field, which is a pointer to a PyObject--that's the representation of a native Python object in Python's C API. When you import a Python module, you get a PythonObject wrapping that module. When you call a function on that object, PythonObject in turn makes a call to the CPython interpreter, passing it the underlying PyObject pointer and Python versons of any (Mojo) arguments you pass in. Whatever Python object that function returns gets wrapped as another PythonObject, and so on. For more information on PyObject and the Python interpreter interface, you might start here: https://docs.python.org/3/c-api/intro.html
Python documentation
Introduction
The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is gener...
Noe
NoeOP13mo ago
Thank you @Arthur Evans , this is exactly what I was looking for.
Arthur Evans
Arthur Evans13mo ago
Excellent. Good luck with the term paper!
Noe
NoeOP13mo ago
Ty
Want results from more Discord servers?
Add your server