> For the complete documentation index, see [llms.txt](https://esciencecu-twiki.sc.chula.ac.th/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://esciencecu-twiki.sc.chula.ac.th/slurm/slurm-examples/slurm-simple-c++-python-program.md).

# Simple C, C++, Python program

Start with a C++ program, we can start with a simple C++ program, to print out a text to a file, i.e. example2.cpp

```cpp
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example2.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
```

You can choose to compile your program first, or you can compile it on a worker node. You may want to compile on the worker node, in case your program is sensitive to the hardware, e.g. can run with CPU-only, CPU+GPU and/or GPU-only depend on the availability of the worker node.&#x20;

We now compile the `example2` first

```
make example2
```

Then you prepare the Slurm submission script, e.g.

```bash
#!/bin/bash
#
#SBATCH --qos=cu_hpc
#SBATCH --partition=cpu
#SBATCH --job-name=example2
#SBATCH --output=example2_log.txt
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=00:10:00
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=1G

module purge

#To handle PATHs
export MYCODEDIR=`pwd`
echo "MYCODEDIR = "$MYCODEDIR
echo "TMPDIR = "$TMPDIR

#To run C++ program
cd $TMPDIR
cp $MYCODEDIR/example2 .
chmod a+x example2
rm -rf example2.txt
./example2
cp -rf example2.txt $MYCODEDIR
```

You should get `example2.txt` as an output from your program, and `example2_log.txt` as the log from the Slurm.

For C and Python program, user can follow the same way:

1. Create and test your program
2. Write the submission script
3. Submit jobs to Slurm clusters, and get the output back to your working directory

However, for Python, a user may need specific packages or specific versions which are not installed centrally. User can also set the virtual environment, install on what are needed. You can see example in [Python with VirtualEnv](/slurm/slurm-examples/slurm-python-with-virtualenv.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://esciencecu-twiki.sc.chula.ac.th/slurm/slurm-examples/slurm-simple-c++-python-program.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
