| | 162 | === Use Array Task ID to identify the data file === |
| | 163 | |
| | 164 | Get into '''JobArray3''' directory under '''hpc-workshop''', |
| | 165 | {{{ |
| | 166 | [fuji@cypress1 ~]$ cd hpc-workshop/JobArray3/ |
| | 167 | [fuji@cypress1 JobArray3]$ ls |
| | 168 | data slurmscript |
| | 169 | }}} |
| | 170 | |
| | 171 | In '''data''' directory, |
| | 172 | {{{ |
| | 173 | [fuji@cypress1 JobArray3]$ ls data |
| | 174 | data_file_10.txt data_file_2.txt data_file_4.txt data_file_6.txt data_file_8.txt |
| | 175 | data_file_1.txt data_file_3.txt data_file_5.txt data_file_7.txt data_file_9.txt |
| | 176 | }}} |
| | 177 | '''slurmscript''' |
| | 178 | {{{#!bash |
| | 179 | #!/bin/bash |
| | 180 | #SBATCH --qos=workshop # Quality of Service |
| | 181 | #SBATCH --partition=workshop # partition |
| | 182 | #SBATCH --job-name=job_array # Job Name |
| | 183 | #SBATCH --time=00:01:00 # WallTime |
| | 184 | #SBATCH --nodes=1 # Number of Nodes |
| | 185 | #SBATCH --ntasks-per-node=1 # Number of tasks (MPI processes) |
| | 186 | #SBATCH --cpus-per-task=1 # Number of threads per task (OMP threads) |
| | 187 | #SBATCH --array=1-10 # Array of IDs=1,2,...10 |
| | 188 | |
| | 189 | # list all (10) files in a data directory and use a job array to process each file. |
| | 190 | # define the data directory |
| | 191 | DATA_DIRECTORY=./data |
| | 192 | echo Using DATA_DIRECTORY=$DATA_DIRECTORY |
| | 193 | echo Using SLURM_ARRAY_TASK_ID=$SLURM_ARRAY_TASK_ID |
| | 194 | # select the data file from the data directory using the SLURM task ID |
| | 195 | DATA_FILE=$(find $DATA_DIRECTORY -type f | sort -V | sed -n "$SLURM_ARRAY_TASK_ID p") |
| | 196 | echo Using DATA_FILE=$DATA_FILE. |
| | 197 | # define the output directory |
| | 198 | OUTPUT_DIRECTORY=./output |
| | 199 | mkdir -p $OUTPUT_DIRECTORY |
| | 200 | echo Using OUTPUT_DIRECTORY=$OUTPUT_DIRECTORY |
| | 201 | OUTPUT_FILE=$OUTPUT_DIRECTORY/$(basename $DATA_FILE).out |
| | 202 | # if the output file already exists, then bypass and exit |
| | 203 | echo Checking for OUTPUT_FILE=$OUTPUT_FILE... |
| | 204 | if [ -f $OUTPUT_FILE ]; then |
| | 205 | echo Found. Bypassing processing. |
| | 206 | else |
| | 207 | echo Not found. Processing. |
| | 208 | sed -r 's/(.*)/\1 output/' $DATA_FILE >> $OUTPUT_FILE |
| | 209 | echo Done. |
| | 210 | fi |
| | 211 | }}} |