0

I'm running my program in a for loop, passing it varying values of num_threads.

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

I understand that bash will store the return value in $? automatically. However, what I am trying to do is get the runtime for a parallel program when passing it arguments of num_threads=1, so that I can use this value to find the speedup when running this program when num_threads>1. I need to return a double for this, and I had tried to return a double from main():

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

return run_time;

}

But it seems that main() can only return an int, and I get a 0 back from my bash script.

time=$?
echo $time

output:
0
AShelly
  • 34,686
  • 15
  • 91
  • 152
marti
  • 69
  • 7
  • 1
    Maybe just `time mpirun ...`? – KamilCuk Nov 15 '21 at 02:30
  • Kind of. That would work for timing the whole program. The issue is that I have to time only specific portions of my program. It uses a Quicksort function and I'm timing just the portions where Quicksort() is being called. So I think I would have to time it within the program itself. – marti Nov 15 '21 at 02:38
  • What programs return is an exit status -- a one-byte unsigned integer. It's conventionally use to indicate whether the program succeeded (zero=success, nonzero=some sort of failure). It cannot pass other types of data, and using it for anything other than success/failure is almost always a bad idea. – Gordon Davisson Nov 15 '21 at 05:37
  • @marti : `double main` is not only illegal according to the C standard, it does not make sense. `main` always returns an integer, and you can rely on it only (i.e. depending on the operating system) if it is an integer less than 256. If the compiler does not complain about the `double`, it will at least silently ignore it. Also, `main` is usually terminated by `exit`, not `return`. My guess is that `return` would also work; it is just somewhat unusual here. – user1934428 Nov 15 '21 at 07:18

1 Answers1

3

Output the value from your program.

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

Then:

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

Generally, the form double main(int, char**) is invalid - main has to have specific forms only, see https://en.cppreference.com/w/cpp/language/main_function .

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Good answer! Why did you choose to print to `stderr` instead of `stdout`? – Jardel Lucca Nov 15 '21 at 02:53
  • 1
    I guess, because stderr is used for logging information that are not output of the program (like `std::clog` in C++), but if the program does not produce relevant output, then `stdout` might be more appropriate, and maybe using a different fd like 3 for example might be even better. – KamilCuk Nov 15 '21 at 02:57
  • Many thanks for all the helpful answers. I was able to just use tail to grab the value I need after outputting it to speed.txt – marti Nov 15 '21 at 03:34