Gaia@home

A simple Python code for a single CPU

In order to use Python script in Gaia@Home it is required to transform the script into executable file. There are several methods to do so and we present two of them. First one is using PyInstaller to create executable that contains interpreter, script and all its dependencies, the other uses Cython to transform the script into C code that is compiled into binary code.

Example Code

To show how to prepare the software for Gaia@Home we will use the simple exemplary script:

#EXAMPLE 1: simple file input and output
with open('in0') as f:
    with open('out','a') as g:
        g.write(str(len(f.readlines()))+'\n')

This script reads the input file and prints the number of lines in input file to the output file.

We can use any example script to produce an executable file for Gaia@home in the same way. In the next part of this tutorial we assume that the name of the script is example.py.

Method 1 - PyInstaller

The easiest way to install PyInstalle is to use pip:

pip install pyinstaller

After PyInstaller is installed you can create executable file by typing

pyinstaller --onefile example.py

The PyInstaller will creae an executable file in ./dist folder. This file can be used in Gaia@Home.

Method 2 - Cython

This method uses Cython, the language which is superset of Python to create C code from Python script and then to compile it using C compiler. It results smaller executable file size and the python source code is not distributed to users, however this method is more difficult.

First you need to install the current version of Cython and the simplest way to do so is to use pip:

pip install cython

You will also need to have any C compiler installed. We will be using gcc compiler in this tutorial.

After Cython is installed you can create C source code from your Python script using following command (for python 3)

cython -3 example.py --embed

or (for python 2)

cython -2 example.py --embed

This command generates a C source file example.c from Python example.py script. After that you need to compile this file. If you are using gcc compiler use following command (change 3.8 to your Python version):

gcc -Os -I /usr/include/python3.8 example.c -lpython3.8 -o example

The compilation creates the executable file named example, which can be used in the Gaia@Home.

 

Article Details

Article ID:
18
Rating :