具有固定顺序的并行执行

问题描述 投票:1回答:1
#!/bin/bash
doone() {
    tracelength="$1"
    short="$2"
    long="$3"
    ratio="$4"
    echo "$tracelength $short $long $ratio" >> results.csv
    python3 main.py "$tracelength" "$short" "$long" "$ratio" >> file.smt2
    gtime -f "%U" /Users/Desktop/optimathsat-1.5.1-macos-64-bit/bin/optimathsat < file.smt2
}
export -f doone
step=0.1
parallel doone \
         ::: 200 300 \
         :::: <(seq 0 $step 0.2) \
         ::::+ <(seq 1 -$step 0.8) \
         :::: <(seq 0 $step 0.1) \
         ::: {1..2} &> results.csv

我需要在results.csv中给出的数据是有序的。每个作业都打印其输入,这些输入是开头提到的3变量:$ tracelength,$ short,$ long和$ ratio,然后是该作业的相关执行时间;一条线。到目前为止,我的结果看起来像这样:

0.00
0.00
0.00
0.00
200 0 1 0
200 0 1 0.1
200 0.1 0.9 0

我该如何修理订单?为什么执行时间总是0.00? file.smt2是一个大文件,执行时间决不能为0.00。

macos shell time gnu-parallel
1个回答
0
投票

并行附加到同一文件是一个坏主意。你将在整个地方拥有竞争条件。

你是用results.csvfile.smt2做的。

因此,如果您在doone中写入文件,请确保它具有唯一的名称(例如,使用myfile.$$)。

要查看竞争条件是否是您的问题,您可以让GNU Parallel一次运行一个作业:parallel --jobs 1

如果问题消失了,那么你可能会逃脱:

doone() {
    tracelength="$1"
    short="$2"
    long="$3"
    ratio="$4"
    # No >> is needed here, as all output is sent to results.csv
    echo "$tracelength $short $long $ratio"
    tmpfile=file.smt.$$
    cp file.smt2 $tmpfile
    python3 main.py "$tracelength" "$short" "$long" "$ratio" >> $tmpfile
    # Be aware that the output from gtime and optimathsat will be put into results.csv - making results.csv not a CSV-file
    gtime -f "%U" /Users/Desktop/optimathsat-1.5.1-macos-64-bit/bin/optimathsat < $tmpfile
    rm $tmpfile
}

如果results.csv只是一个日志文件,请考虑使用parallel --joblog my.log

如果问题没有消失,那么你的问题就在其他地方。在这种情况下,制作一个MCVE(https://stackoverflow.com/help/mcve):你的例子不完整,因为你提到file.smt2optimathsat没有提供它们,因此我们不能运行你的例子。

© www.soinside.com 2019 - 2024. All rights reserved.