I have a python script that takes a day to run. I need to run the same code for different parameters. However, using a for loop to iterate over all the parameters is impossible because it would result in an even longer computational time. This is a simple example to depict the situation:
ValuesParams=[1,2,3,4,5]
for i in ValuesParams:
do something
output file_i.csv
I would like to run 5 different programs (in a computer cluster), with 5 different values, that output 5 different csv files with different names, but doing it at the same time. Because running that for loop in a single program would take 5 days. In reality it's not just 5 parameters, and the program is not a simple loop.
How can I do this? Any piece of advice or information on what to look for would be incredibly useful.
EDIT Thanks to all the replies, specially remram. They put me in the path to find the answer.
Solution 1: I created a script RunPrograms.sh which iterate over the parameters var1, var2, then passed the parameters to my python script test.py and ran all of the jobs at the same time. One limitation persists, I have to be careful with sending for example 1000 jobs at the same time, but this is what I need it.
#!/bin/bash
for var1 in $(seq 1 0.5 3); do
for var2 in $(seq 1 3); do
python3 test.py $var1 $var2 &
done
done
and inside the python script:
import sys
a=float(sys.argv[1])
b=float(sys.argv[2])
def Code(a,b)
do_something
Solution 2: Inside the python script using multiprocessing along with starmap to pass multiple parameters:
def Code(T,K):
do_something
RangeT=np.linspace(0.01,0.05,num=10)
RangeK=[2,3,4]
Z=list(itertools.product(RangeT, RangeK))
if __name__ == '__main__':
pool = Pool(4)
results = pool.starmap(Code, Z)