0

我在for循环中运行我的程序,传递给它不同的num_threads值。

for (( j=1; j<9; j++ ))
    do
        mpirun -n $j ./my_program
    done

我知道 bash 会将返回值存储在 $? 自动地。但是,我要做的是在传递 num_threads=1 的参数时获取并行程序的运行时,以便在 num_threads>1 时运行该程序时可以使用此值来查找加速。我需要为此返回一个双精度数,并且我曾尝试从 main() 返回一个双精度数:

double main(int argc, char** argv) {
double run_time;
...

return run_time;

}

但似乎 main() 只能返回一个 int,而我从我的 bash 脚本中得到一个 0。

time=$?
echo $time

output:
0
4

1 回答 1

2

从您的程序中输出值。

int main(int argc, char** argv) {
    double run_time;
    ...
    
    fprintf(stderr, "%f\n", run_time);
}

然后:

for (( j=1; j<9; j++ )); do
    echo -n "$j " >> speed.txt
    mpirun -n $j ./my_program 2>>speed.txt
    # or you want to see it
    # mpirun -n $j ./my_program 2> >(tee -a speed.txt >&2)
done
sort -k2g speed.txt   # -g is from GNU sort

通常,表格double main(int, char**)无效 -main必须只有特定表格,请参阅https://en.cppreference.com/w/cpp/language/main_function

于 2021-11-15T02:47:39.560 回答