xref: /petsc/src/benchmarks/run_petsc_benchmarks.sh (revision df4cd43f92eaa320656440c40edb1046daee8f75)
1#!/bin/bash
2
3# Before running this script, make sure set up the directory for storing the downloaded matrix files (ARCHIVE_LOCATION)
4ARCHIVE_LOCATION=""
5SSGET=./ssget
6
7if [ ! "${BENCHMARK}" ]; then
8    BENCHMARK="spmv"
9    echo "BENCHMARK   environment variable not set - assuming \"${BENCHMARK}\"" 1>&2
10fi
11
12if [ ! "${DRY_RUN}" ]; then
13    DRY_RUN="false"
14    echo "DRY_RUN     environment variable not set - assuming \"${DRY_RUN}\"" 1>&2
15fi
16
17if [ ! "${EXECUTOR}" ]; then
18    EXECUTOR="cuda"
19    echo "EXECUTOR    environment variable not set - assuming \"${EXECUTOR}\"" 1>&2
20fi
21
22if [ ! "${SEGMENTS}" ]; then
23    echo "SEGMENTS    environment variable not set - running entire suite" 1>&2
24    SEGMENTS=1
25    SEGMENT_ID=1
26elif [ ! "${SEGMENT_ID}" ]; then
27    echo "SEGMENT_ID  environment variable not set - exiting" 1>&2
28    exit 1
29fi
30
31if [ ! "${FORMATS}" ]; then
32    echo "FORMATS    environment variable not set - assuming \"csr\"" 1>&2
33    FORMATS="csr"
34fi
35
36if [ ! "${DEVICE_ID}" ]; then
37    DEVICE_ID="0"
38    echo "DEVICE_ID   environment variable not set - assuming \"${DEVICE_ID}\"" 1>&2
39fi
40
41if [ ! "${SINGLE_JSON}" ]; then
42    SINGLE_JSON="false"
43    echo "SINGLE_JSON environment variable not set - assuming \"${SINGLE_JSONR}\"" 1>&2
44fi
45
46if [ ! "${LAUNCHER}" ]; then
47    LAUNCHER=""
48    echo "LAUNCHER    environment variable not set - assuming \"${LAUNCHER}\"" 1>&2
49fi
50
51# This allows using a matrix list file for benchmarking.
52# The file should contains a suitesparse matrix on each line.
53# The allowed formats to target suitesparse matrix is:
54#   id or group/name or name.
55# Example:
56# 1903
57# Freescale/circuit5M
58# thermal2
59if [ ! "${MATRIX_LIST_FILE}" ]; then
60    use_matrix_list_file=0
61elif [ -f "${MATRIX_LIST_FILE}" ]; then
62    use_matrix_list_file=1
63else
64    echo -e "A matrix list file was set to ${MATRIX_LIST_FILE} but it cannot be found."
65    exit 1
66fi
67
68# Runs the SpMV benchmarks for all SpMV formats by using file $1 as the input,
69# and updating it with the results. Backups are created after each
70# benchmark run, to prevent data loss in case of a crash. Once the benchmarking
71# is completed, the backups and the results are combined, and the newest file is
72# taken as the final result.
73run_spmv_benchmarks() {
74    [ "${DRY_RUN}" == "true" ] && return
75    if [ "${EXECUTOR}" == "cuda" ]; then
76        ${LAUNCHER} ../mat/tests/bench_spmv -formats "${FORMATS}" -repetitions 5 -use_gpu -AJSON "$1"
77    else
78        ${LAUNCHER} ../mat/tests/bench_spmv -formats "${FORMATS}" -repetitions 5 -AJSON "$1"
79    fi
80}
81
82NUM_PROBLEMS="$(${SSGET} -n)"
83
84# Creates an input file for $1-th problem in the SuiteSparse collection
85generate_suite_sparse_input() {
86    INPUT=$(${SSGET} -i "$1" -e)
87    cat << EOT
88[{
89    "filename": "${INPUT}",
90    "problem": $(${SSGET} -i "$1" -j)
91}]
92EOT
93}
94
95# Append an input file for $1-th problem in the SuiteSparse collection
96append_suite_sparse_input() {
97    INPUT=$(${SSGET} -i "$1" -e)
98    cat << EOT
99 {
100    "filename": "${INPUT}",
101    "problem": $(${SSGET} -i "$1" -j)
102 },
103EOT
104}
105
106parse_matrix_list() {
107    local source_list_file=$1
108    local benchmark_list=""
109    local id=0
110    for mtx in $(cat ${source_list_file}); do
111	echo $mtx >&2
112        if [[ ! "$mtx" =~ ^[0-9]+$ ]]; then
113            if [[ "$mtx" =~ ^[a-zA-Z0-9_-]+$ ]]; then
114                id=$(${SSGET} -s "[ @name == $mtx ]")
115            elif [[ "$mtx" =~ ^([a-zA-Z0-9_-]+)\/([a-zA-Z0-9_-]+)$ ]]; then
116                local group="${BASH_REMATCH[1]}"
117                local name="${BASH_REMATCH[2]}"
118                id=$(${SSGET} -s "[ @name == $name ] && [ @group == $group ]")
119            else
120                >&2 echo -e "Could not recognize entry $mtx."
121            fi
122        else
123            id=$mtx
124        fi
125        benchmark_list="$benchmark_list $id"
126    done
127    echo "$benchmark_list"
128}
129
130if [ $use_matrix_list_file -eq 1 ]; then
131    MATRIX_LIST=($(parse_matrix_list $MATRIX_LIST_FILE))
132    NUM_PROBLEMS=${#MATRIX_LIST[@]}
133fi
134
135RESULT_DIR="results/${SYSTEM_NAME}/${EXECUTOR}/SuiteSparse"
136if [ "${SINGLE_JSON}" == "true" ]; then
137    RESULT_FILE="${RESULT_DIR}/SEGMENT${SEGMENT_ID}.json"
138    cat << EOT >"${RESULT_FILE}"
139[
140EOT
141    mkdir -p "$(dirname "${RESULT_FILE}")"
142fi
143LOOP_START=$((1 + (${NUM_PROBLEMS}) * (${SEGMENT_ID} - 1) / ${SEGMENTS}))
144LOOP_END=$((1 + (${NUM_PROBLEMS}) * (${SEGMENT_ID}) / ${SEGMENTS}))
145
146for (( p=${LOOP_START}; p < ${LOOP_END}; ++p )); do
147    if [ $use_matrix_list_file -eq 1 ]; then
148        i=${MATRIX_LIST[$((p-1))]}
149    else
150        i=$p
151    fi
152    if [ "${BENCHMARK}" == "preconditioner" ]; then
153        break
154    fi
155    if [ "$(${SSGET} -i "$i" -preal)" = "0" ] || [ "$(${SSGET} -i "$i" -pbinary)" = "1" ]; then
156        [ "${DRY_RUN}" != "true" ] && ${SSGET} -i "$i" -c >/dev/null
157        continue
158    fi
159    # filter matrices for spmv tests
160    if [ "${BENCHMARK}" == "spmv" ]; then
161	# deselect non-square matrices and matrices with more than 2B non zeros
162        if [ "$(${SSGET} -i "$i" -pcols)" != "$(${SSGET} -i "$i" -prows)" ] || [ "$(${SSGET} -i "$i" -pnonzeros)" -gt 2000000000 ]; then
163            [ "${DRY_RUN}" != "true" ] && ${SSGET} -i "$i" -c >/dev/null
164            continue
165	    fi
166    fi
167    PREFIX="(PROB$p/${NUM_PROBLEMS} ID$i SEG${SEGMENT_ID}):\t"
168    GROUP=$(${SSGET} -i "$i" -pgroup)
169    NAME=$(${SSGET} -i "$i" -pname)
170    if [ "${SINGLE_JSON}" == "false" ]; then
171        RESULT_FILE="${RESULT_DIR}/${GROUP}/${NAME}.json"
172        mkdir -p "$(dirname "${RESULT_FILE}")"
173        echo -e "${PREFIX}Extracting the matrix for ${GROUP}/${NAME}" 1>&2
174        generate_suite_sparse_input "$i" >"${RESULT_FILE}"
175        echo -e "${PREFIX}Running SpMV for ${GROUP}/${NAME}" 1>&2
176        run_spmv_benchmarks "${RESULT_FILE}"
177        echo -e "${PREFIX}Cleaning up problem ${GROUP}/${NAME}" 1>&2
178        [ "${DRY_RUN}" != "true" ] && ${SSGET} -i "$i" -c >/dev/null
179    else
180        append_suite_sparse_input "$i" >>"${RESULT_FILE}"
181    fi
182done
183if [ "${SINGLE_JSON}" == "true" ]; then
184    cat << EOT >"${RESULT_FILE}"
185]
186EOT
187    echo -e "${PREFIX}Running SpMV for SEG${SEGMENT_ID}" 1>&2
188    run_spmv_benchmarks "${RESULT_FILE}"
189    for (( p=${LOOP_START}; p < ${LOOP_END}; ++p )); do
190        if [ $use_matrix_list_file -eq 1 ]; then
191            i=${MATRIX_LIST[$((p-1))]}
192        else
193            i=$p
194	fi
195	echo -e "${PREFIX}Cleaning up problem ${i}" 1>&2
196        [ "${DRY_RUN}" != "true" ] && ${SSGET} -i "$i" -c >/dev/null
197    done
198fi
199