# Loop to run md on gpu

**URL:** <https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587>\
**Category:** User discussions\
**Created:** [March 12, 2024, 10:17am UTC](https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587 "2024-03-12T10:17:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AF1](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AF1](https://gromacs.bioexcel.eu/u/AF1)\
**Post date:** [March 12, 2024, 10:17am UTC](https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587/1 "2024-03-12T10:17:48Z")

</div>

Hi is it possible to create a loop for the last step in md simulation such that when one ends the next starts.

gmx mdrun -deffnm md -nb gpu  
for md, md1, md2, md3 etc?

Thank you.

---

<div class="post-metadata">

**Author:** ![obZehn](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/obzehn/32/2629_2.png) [@obZehn](https://gromacs.bioexcel.eu/u/obZehn)\
**Post date:** [March 12, 2024, 10:53am UTC](https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587/2 "2024-03-12T10:53:35Z")

</div>

Hello,

Yes. Usually if you are running simulations in a Unix environment you can write a simple bash script and prepare a loop to loop over the simulations you want to run. The programming language will depend on your environment and which one do you know.

---

<div class="post-metadata">

**Author:** ![AF1](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AF1](https://gromacs.bioexcel.eu/u/AF1)\
**Post date:** [March 12, 2024, 11:01am UTC](https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587/3 "2024-03-12T11:01:56Z")

</div>

Hello

Thank you for your response. I use slurm and #!/bin/bash as the first line then I have the other lines used in slurm jobs and I add the module before giving the command gmx mdrun -deffnm md -nb gpu. Please let me know if it’s still possible to do so in a slurm job.

Thank you.

---

<div class="post-metadata">

**Author:** ![obZehn](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/obzehn/32/2629_2.png) [@obZehn](https://gromacs.bioexcel.eu/u/obZehn)\
**Post date:** [March 12, 2024, 3:59pm UTC](https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587/4 "2024-03-12T15:59:53Z")

</div>

There is no general solution, it largely depends on the names of the files that you are using. For example, if you run md\_1 and you have md\_1.gro, md\_1.xtc, md\_1.tpr files etc. (that is, you run with the -deffnm flag and giving the md\_1 name), and now you want to run md\_2.mdp, md\_3.mdp, …, md\_10.mdp, then in slurm you could do a simple bash loop like this

```auto
#!/bin/bash

#### SLURM STUFF
#
#
#
#### SLURM STUFF

for ((i =2;i<11;i++))
do
j=$((${i}-1))
gmx grompp -f md_${i}.mdp -c md_${j}.gro -r md_${j}.gro -t md_${j}.cpt -p topol.top -n index.ndx -o md_${i}.tpr 
gmx mdrun -v -deffnm md_${i}
done

```

Specifically, here the grompp pre-processing step needs to be fine-tuned by you (pointing to the right topology, the right index if you need it, the right constraint file if you need it etc.) and similarly the mdrun step may need further details depending on the amount of resources you are allocating for the job on the cluster with slurm.

However, this is a sample idea. In the future you may want to write something different and/or process things in a different way. I think - if you are not familiar with bash - that you should take some time to learn the basics of it so that you will have maximum freedom in writing your scripts.

---

<div class="post-metadata">

**Author:** ![AF1](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AF1](https://gromacs.bioexcel.eu/u/AF1)\
**Post date:** [March 13, 2024, 9:04am UTC](https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587/5 "2024-03-13T09:04:53Z")

</div>

> [@obZehn](#):
>
> ```auto
> for ((i =2;i<11;i++))
> do
> j=$((${i}-1))
> gmx grompp -f md_${i}.mdp -c md_${j}.gro -r md_${j}.gro -t md_${j}.cpt -p topol.top -n index.ndx -o md_${i}.tpr 
> gmx mdrun -v -deffnm md_${i}
> done
> 
> ```

Thank you so much I will try this.

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 13, 2024, 9:09am UTC](https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587/6 "2024-03-13T09:09:29Z")

</div>

Note that, depending on the CPU and GPU usage, it can be advantageous for throughput to run multiple simulations simultaneously using an MPI build and the mdrun -multidir option.

---

<div class="post-metadata">

**Author:** ![AF1](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AF1](https://gromacs.bioexcel.eu/u/AF1)\
**Post date:** [March 13, 2024, 11:21am UTC](https://gromacs.bioexcel.eu/t/loop-to-run-md-on-gpu/8587/7 "2024-03-13T11:21:41Z")

</div>

Hi

Thank you for your response.

The platform that I’m using do not have gromacs with MPI on the GPU. I wanted to only make a loop so that when one ends the other starts just after.
