Simple code example
In this guide you will be shown how to run a simple Hello-word program in Python and C. It demonstrates the different output methods there are. Each code will have two possible options of running. You can either manually set some necessary parameters in the slurm submit-script and structure the folders yourself (recommended) or run a single bash-script and look at the code afterwards. (Use the provided bash-scripts at your own risk)
Python
Run the code manually
Start by downloading the three files “Hello_world_script.py.txt”, “hello_world_input_python.txt” and “run_hello_world_python.sh”. Then rename “Hello_world_script.py.txt” to “Hello_world_script.py” and finally
upload the files to Holodeck.
Alternatively you can download them directly to Holodeck and rename the file by running
wget https://wiki.atlas.aei.uni-hannover.de/foswiki/pub/NumRel/HoloHelloWorld/Hello_world_script.py.txt
mv Hello_world_script.py.txt Hello_world_script.py
wget https://wiki.atlas.aei.uni-hannover.de/foswiki/pub/NumRel/HoloHelloWorld/hello_world_input_python.txt
wget https://wiki.atlas.aei.uni-hannover.de/foswiki/pub/NumRel/HoloHelloWorld/run_hello_world_python.sh
Once the files are on Holodeck you can in principle run them (with some minor modifications) It is however recommended to put them into a folder. This way your /holohome will stay clean.
Therefore run
mkdir Hello_world_python
mv -t Hello_world_python Hello_world_script.py hello_world_input_python.txt run_hello_world_python.sh
cd Hello_world_python
to create a directory called “Hello_world_python” and move the files into it.
The next step is to fix the paths in the slurm submission script (for details on the submission file see
here. The submission file in this tutorial is based on it.). For this run
pwd
and copy the output (mark it with the mouse and press Ctrl + Shift + C). Afterwards run
nano run_hello_world_python.sh
which will open up the nano texteditor. The first two lines beginning with “#SBATCH” specify the input- and the output-paths. In this environment the “~” statement does not work. Therefore you have to exchange <current_path> with the string you have just copied. To insert it press Ctrl + Shift + V. Save the changes (Ctrl + O → Enter) and exit the editor (Ctrl + X).
Now you are ready to run the script. Type
python Hello_world_script.py
to run it using the default Python interpreter. The behavior and output of the script is
described below.
To see what differences there are between running the script “normally” and through slurm, run
sbatch $(pwd)/run_hello_world_python.sh
and look at the output it produces. This output too is described
below.
Run the code through the setup file
If you don’t want to manually set everything up and just have the code run via one simple command, download the “setup_python.sh” script and
upload it to Holodeck. Alternatively download it directly to Holodeck using
wget https://wiki.atlas.aei.uni-hannover.de/foswiki/pub/NumRel/HoloHelloWorld/setup_python.sh
Afterwards change to the directory the script is stored at and type
bash setup_python.sh
to execute is. The script will ask you for a file name. Choose one that you like. (The file shouldn’t exist beforehand) The bash-script will create the three files you would have downloaded if you were to execute the manual setup and executes them afterwards once through the Python interpreter and once through
slurm. To understand what the scripts it executes do, see the
section below. The code will be in a folder called Hello_world_python, which is a child-directory of the directory from which you executed the setup-script.
Output of the script and what it means
Whichever way of running the code you chose, you should now have a directory called “Hello_world_python”. This directory should contain 6 files.
- Hello_world_script.py: The source code that was executed.
- hello_world_input_python.txt: The input that needs to be provided to the script when using slurm
- run_hello_world_python.sh: The submit script for slurm.
- Holodeck_Hello_world_python.txt: The output-file generated by the code when executed through slurm.
- SlurmHelloWorld_python.txt: The output-file slurm created.
- <your_chosen_name>.txt: The file you named yourself when asked for input. It contains the same output as Holodeck_Hello_world_python.txt.
When run from the terminal, the script will print multiple lines to the terminal. The first one comes from a usual “print” statement, the second is directly piped to stdout and the third asks you for input.
The string you provide to the input is then used as a file name and echoed back to you. Also a file path will be printed to the screen. This path is the path the script writes a file to. The file contains some simple text, showing you how to store at specified paths.
The reason for all these prints is the following. When you invoke the script through slurm. stdout gets redirected to a output-file and thus there will not be any output visible on the terminal. The output-file will be called “SlurmHelloWorld_python.txt”, if you haven’t changed the name in the submission script.
The only thing that doesn’t get redirected will be the written file. This will (by default) be named “Holodeck_Hello_world_python.txt”. Note that you are also not asked for input when slurm is running the script. This is because an input-file named “hello_world_input_python.txt” was provided. This shows that also stdin is redirected. The input-file needs to also be provided in the submission file.
C
Run the code manually
Start by downloading the three files “Hello_world_code.c”, “hello_world_input_c.txt” and “run_hello_world_c.sh”. Afterwards
upload them to Holodeck.
Alternatively you can download them directly to Holodeck by running
wget https://wiki.atlas.aei.uni-hannover.de/foswiki/pub/NumRel/HoloHelloWorld/Hello_world_code.c
wget https://wiki.atlas.aei.uni-hannover.de/foswiki/pub/NumRel/HoloHelloWorld/hello_world_input_c.txt
wget https://wiki.atlas.aei.uni-hannover.de/foswiki/pub/NumRel/HoloHelloWorld/run_hello_world_c.sh
Once the files are on Holodeck you can in principle run them (with some minor modifications) It is however recommended to put them into a folder. This way your /holohome will stay clean.
Therefore run
mkdir Hello_world_c
mv -t Hello_world_c Hello_world_code.c hello_world_input_c.txt run_hello_world_c.sh
cd Hello_world_c
to create a directory called “Hello_world_c” and move the files into it.
The next step is to fix the paths in the slurm submission script (for details on the submission file see
here. The submission file in this tutorial is based on it.). For this run
pwd
and copy the output (mark it with the mouse and press Ctrl + Shift + C). Afterwards run
nano run_hello_world_c.sh
which will open up the nano texteditor. The first two lines beginning with “#SBATCH” specify the input- and the output-paths. In this environment the “~” statement does not work. Therefore you have to exchange <current_path> with the string you have just copied. To insert it press Ctrl + Shift + V. Save the changes (Ctrl + O → Enter) and exit the editor (Ctrl + X).
Now all preparations are made and you can compile the c-code. To do so type
gcc Hello_world_code.c -std=c11 -o Hello_world_executable
Note the flag “-std=c11”. If you are usually writing code that uses the c11 or c99 standard you will have to set this flag, as it is not applied automatically. It has to be the first option you pass to gcc.
This creates an executable called “Hello_world_executable” which you can then finally run
./Hello_world_executable
You will be asked for input. The script will create a file with the name you provide it.
The output is discussed
below.
To see what differences there are between running the code “normally” and through slurm, run
sbatch $(pwd)/run_hello_world_c.sh
and look at the output it produces. This output too is described
below.
Run the code through the setup file
If you don’t want to manually set everything up and just have the code run via one simple command, download the “setup_c.sh” script and
upload it to Holodeck. Alternatively download it directly to Holodeck using
wget https://wiki.atlas.aei.uni-hannover.de/foswiki/pub/NumRel/HoloHelloWorld/setup_c.sh
Afterwards change to the directory the script is stored at and type
bash setup_c.sh
to execute is. The script will ask you for a file name. Choose one that you like. (The file shouldn’t exist beforehand) The bash-script will create the three files you would have downloaded if you were to execute the manual setup and executes them afterwards, once by compiling and running the executable, and once through
slurm. To understand what the scripts it executes do,
see the section below. The code will be in a folder called Hello_world_c, which is a child-directory of the directory from which you executed the setup-script.
Output of the code and what it means
Whichever way of running the code you chose, you should now have a directory called “Hello_world_c”. This directory should contain 7 of the 8 files listed below. (All 8 if you ran both ways)
- Hello_world_code.c: The source code that was compiled and executed.
- hello_world_input_c.txt: The input that needs to be provided when using slurm
- run_hello_world_c.sh: The submit script for slurm.
- Holodeck_Hello_world_c.txt: The output-file generated by the code when executed through slurm.
- SlurmHelloWorld_c.txt: The output-file slurm created.
- <your_chosen_name>.txt: The file you named yourself when asked for input. It contains the same output as Holodeck_Hello_world_c.txt
- Hello_world_executable: The executable that the compiler created on the manual invocation.
- Hello_world_executable_slurm: The executable that the compiler created when slurm compiled and executed the code.
When run from the terminal, the code will print multiple lines to the terminal. The first one comes from a usual “print” statement and the second asks you for input.
The string you provide to the input is then used as a file name and echoed back to you. The file contains some simple text, showing you how to store at specified paths.
The reason for all these prints is the following. When you invoke the code through slurm. stdout gets redirected to a output-file and thus there will not be any output visible on the terminal. The output-file will be called “SlurmHelloWorld_c.txt”, if you haven’t changed the name in the submission script.
The only thing that doesn’t get redirected will be the written file. This will (by default) be named “Holodeck_Hello_world_c.txt”. Note that you are also not asked for input when slurm is running the code. This is because an input-file named “hello_world_input_c.txt” was provided. This shows that also stdin is redirected. The input-file needs to also be provided in the submission file.
See also
Guide to data storage ·
Useful commands ·
List of installed software ·
List of available nodes ·
Upload files to Holodeck
--
MarlinSchFer - 23 May 2019