How to parallelize this nested loop in Python that calls Abaqus

2024/10/12 17:26:33

I have the nested loops below. How can i parallelize the outside loop so i can distribute the outside loop into 4 simultaneous runs and wait for all 4 runs to complete before moving on with the rest of the script?

    for r in range(4):for k in range( r*nAnalysis/4, (r+1)*nAnalysis/4 ):# - Write Abaqus INP file - #writeABQfile(ppos,props,totalTime[k],recInt[k],inpFiles[k],i,lineNum[k],aPath[k])# - Delete LCK file to Enable Another Analysis - #delFile(aPath[k]+"/"+inpFiles[k]+".lck")# - Run Analysis - #runABQfile(inpFiles[k],aPath[k])

I tried using multiprocess.pool as but it never gets in:

            def parRunABQfiles(nA,nP,r,ppos,prop0,prop1,totalTime2Run_,recIntervals_,inpFiles_,i,lineNumbers_,aPath_):from os import path from auxFunctions import writeABQfile, runABQfile print("I am Here")for k in range( r*nA/nP, (r+1)*nA/nP ):# - Write Abaqus INP file - #writeABQfile(ppos,prop0,prop1,totalTime2Run_,recIntervals_,inpFiles_,i,lineNumbers_,aPath_)# - Delete LCK file to Enable Another Analysis - #delFile(aPath_+"/"+inpFiles[k]+".lck")# - Run Analysis - #runABQfile(inpFiles_,aPath_)# - Make Sure Analysis is not Bypassed - #while os.path.isfile(aPath_+"/"+inpFiles[k]+".lck") == True:sleep(0.1)return kresults = zip(*pool.map(parRunABQfiles, range(0, 4, 1)))

The runABQfile is just a subprocess.call to a sh script that runs abaqus

     def runABQfile(inpFile,path):    import subprocessimport osprcStr1 = ('sbatch '+path+'/runJob.sh')process = subprocess.call(prcStr1, stdin=None, stdout=None, stderr=None, shell=True )return

I have no errors showing up so I am not sure why is not getting in there. I know because the writeABQfile does not write the input file. The question again is:

How can i parallelize the outside loop so i can distribute the outside loop into 4 simultaneous runs and wait for all 4 runs to complete before moving on with the rest of the script?

Answer

Use concurrent.futures module if multiprocessing is what you want.

from concurrent.futures import ProcessPoolExecutordef each(r):for k in range( r*nAnalysis/4, (r+1)*nAnalysis/4 ):writeABQfile(ppos,props,totalTime[k],recInt[k],inpFiles[k],i,lineNum[k],aPath[k])delFile(aPath[k]+"/"+inpFiles[k]+".lck")runABQfile(inpFiles[k],aPath[k])with ProcessPoolExecutor(max_workers=4) as executor:output = executor.map(each, range(4)) # returns an iterable

If you just want to "do" stuff rather than "produce", check out as_completed function from the same module. There are direct examples in the doc.

https://en.xdnf.cn/q/118176.html

Related Q&A

Comparing one column value to all columns in linux enviroment

So I have two files , one VCF that looks like88 Chr1 25 C - 3 2 1 1 88 Chr1 88 A T 7 2 1 1 88 Chr1 92 A C 16 4 1 1and another with genes that looks likeGENEI…

Can be saved into a variable one condition?

It would be possible to store the condition itself in the variable, rather than the immediate return it, when to declare it?Example:a = 3 b = 5x = (a == b) print(x)a = 5 print(x)The return isFalse Fal…

Pycharm, can not find Python version 2.7.11

I installed Python 2.7.11 on this Mac, and from terminal Python 2.7.11 can be started. However, From the interpreter of Pycharm (2016.1 version) , there is no Python 2.7.11.Any suggestions ? ThanksPS…

Python Convert Binary String to IP Address in Dotted Notation

So Im trying to read in a file with some binary strings, i.e: 10000010 00000000 0000**** ********. The script will convert the *s to both 0 and 1, so there will be two binary strings that look like thi…

Scrapy: Extracting data from source and its links

Edited question to link to original:Scrapy getting data from links within tableFrom the link https://www.tdcj.state.tx.us/death_row/dr_info/trottiewillielast.htmlI am trying to get info from the main t…

Rename file on upload to admin using Django

I have used a function in Django 1.6 to rename my files when they are uploaded through admin, but this does not work in Django 1.8. Anyone know if it is still possible to do this in 1.8?class Entry(mo…

Ignore newline character in binary file with Python?

I open my file like so :f = open("filename.ext", "rb") # ensure binary reading with bMy first line of data looks like this (when using f.readline()):\x04\x00\x00\x00\x12\x00\x00\x00…

RegEx Parse Error by Parsley Python

I have made a simple parser for simple queries, to fetch data from a datastore. The operands I have used are <,<=,>,>=,==,!= The Parser works fine for every operand except for < I am a b…

Accessing Bangla (UTF-8) string by index in Python

I have a string in Bangla and Im trying to access characters by index.# -*- coding: utf-8 -*- bstr = "তরদজ" print bstr # This line is working fine for i in bstr:print i, # question marks …

Computing KL divergence for many distributions

I have a matrix of test probability distributions:qs = np.array([[0.1, 0.6], [0.9, 0.4] ])(sums up to 1 in each column) and "true" distribution:p = np.array([0.5, 0.5])I would like to calcula…