title: How to parallelize this for loop using slurm?
body: <p>I have a large number of different bam files to process and here is my sbatch file:</p>
  <pre><code>#! /bin/bash
  #
  #SBATCH --job-name=test
  #SBATCH --ntasks=1
  #SBATCH --cpus-per-task=2
  #SBATCH --mem-per-cpu=4000
  #SBATCH --output=output.%j.out
  #SBATCH --error=output.%j.err
  
  srun picard.sh
  </code></pre>
  <p>By this I meant to run using threads=2</p>
  <p>And my <code>picard.sh</code> file:</p>
  <pre><code>#!/bin/bash
  
  module load picard-tools/2.4.1-gcb01
  module load java/1.8.0_45-fasrc01
  
  picard=./picard-tools/2.4.1-gcb01/picard.jar
  outdir=./bam2fastq/fastq
  tmpdir=./tmp/
  
  for bam in $(find . -type f -name \*.bam);
  do
      echo &quot;processing ${bam}&quot;
      s=${bam##*/}
      r1=${s%.bam}_R1.fastq
      r2=${s%.bam}_R2.fastq
      echo $r1
      echo $r2
      java -Djava.io.tmpdir=${tmpdir} -Xmx8G -jar ${picard} SamToFastq \
          I=${bam} \
          FASTQ=${outdir}/${r1} \
          SECOND_END_FASTQ=${outdir}/${r2}
  done
  </code></pre>
  <p>While this will process each bam with thread=2 but it will be one by one. How could I run this parallelized such like 6 bam files being processed simultaneously with thread=2?</p>