Crash on the playground

I just discovered that my algos can’t play anymore in the playground:
They all crash on turn 0 because they can’t find the C++ module I wrote. But they still run fine for ranked matches (as in season 1)

It’s same with my algos.

I had the opposite problem (working in the playground, but crashing in ranked matches) when I first uploaded an algo using a compiled c++ subprogram.

I solved that problem by including this line in the run.sh file:
chmod u+x $DIR/Aelgoo_cpp_linux

It is apparently still working.

1 Like

I didn’t realize that you could use C++ subprograms. Is there any trickery that needs to be done to get C++ modules to work in Python on the terminal server? And if so, would you like to provide a snippet of code where a C++ subprogram is called to construct a “Hello World!” string that is then published in the debug log by Python or something similar?

Thanks in advance :slight_smile:

Also, does anybody know what range of speedup would that could provide? I think that an optimized C++ program should generally be faster by about a factor of 5X w.r.t Python. [Edit] Nevermind, if implemented properly the speedup can go up to 100X. Wow

yes you can
it is not specific to Terminal: you can always build C++ extensions for python. I can’t now but maybe I will publish an example this WE.

I don’t know about x100 but x10 is achievable (my python simulator was barely under 1ms, the C++ one is at about 0.08ms)

1 Like

Since we don’t officially support running code in other languages, we won’t be spending dev time trying to ensure that this works. Note that our policy on not officially supported features like this is that they could fail at any point

that’s scary haha :scream:
but understandable

We will try to be reasonable about it. For example, I think the only change to the game/environment we put out during the month preceding the S1Global comp were a handful of important engine bug-fixes. No promises though

If there’s no “supported” way to run C++ modules, is there a general way to do this (other than what’s written above) that would “probably” work?

I would enjoy creating a post like this, giving a brief example code and explanation.

@Ryan_Draves, c++ modules are generally supported by Python, and so the issues people are facing have nothing to do with terminal supporting these modules (mine are still working fine, both locally and remotely). Rather, to me, it sounds like these issues are problems with compiling for the correct system. If a compiled module is on a different OS than what it was compiled on/for, it will not be found. I personally have a Linux OS and Windows OS and I compile for both separately using the same code.

2 Likes

@kkroep Here is a (modified) part of my python code to use the compiled c++ subprogram.
I removed a few lines and made some other parts more readable

    current_os = platform.system()
    executable_file = 'Aelgoo_cpp_win.exe' if current_os == 'Windows' else 'Aelgoo_cpp_linux'
    sub_process_path = os.path.join(os.path.dirname(__file__), executable_file)

    if not os.path.exists(sub_process_path):
        gamelib.debug_write("Compiled c++ Aelgoo not found")
        self.cpp_working = False
        return

    arg0 = self.game_state.build_cpp_arg_string()
    argv = [arg0, arg1, arg2, ...]
    process = Popen(argv, stdout=PIPE, stderr=PIPE)
    cout, cerr = process.communicate()
    exit_code = process.returncode

    for line in cerr.decode("utf-8").split("\n"):
        gamelib.debug_write(line)

    if exit_code != 0:
        gamelib.debug_write("{} crashed with exit code: {}".format(executable_file, hex(exit_code)))
        self.cpp_working = False
        return

    for line in cout.decode("utf-8").split("\n"):
         # parse the cout to send moves.

As you can see you will probably need a different executable for each os. However only the linux one is really indispensable, since the terminal servers are (for the moment) only linux. So if, like me, you are working on Windows, you will either need to compile on a linux VM, or to have fun with cross-compilation.

1 Like

Interesting, so you are compiling to a .exe file? I have been compiling everything through Python to create .pyd/.so files. Then I can simply do import my_module instead of running a process.

1 Like

Note that Rust provides the same speedups, is memory safe, is officially supported, and already has a starter kit, so if you’re willing to learn a new language that’s a great option.

2 Likes

that’s also what I do, so I find it strange that my algos can’t load the module anymore on the playground…

That’s very strange… I can’t think off of the top of my head what it could be but if I think of anything I’ll be sure to let you know :wink:.

1 Like