1 year ago

#345775

test-img

Ayo Alagba

Monte Carlo using pool in python

how do I change the Monte Carlo section of the code below into a multiprocessing pool with the arrays ARR1 and ARR2 as the outputs of the pool process?

import numpy as np

def HS():
    arr1=np.zeros((1,2))
    arr2=np.zeros((1,2))
    arr1 =np.random.random((1,2))
    
    arr2= arr1**4
    return arr1,arr2

length=10000
ARR1 = np.zeros((length,2)) 
ARR2 = np.zeros((length,2)) 

# Monte Carlo   
for i in range(0,10000):
    arr1, arr2 = HS()
    ARR1[i,:]=arr1
    ARR2[i,:]=arr2

I have tried several things, but the last I played is below:

import numpy as np
from multiprocessing import Pool

def HS():
    arr1=np.zeros((1,2))
    arr2=np.zeros((1,2))
    arr1 =np.random.random((1,2))
    
    arr2= arr1**4
    return arr1,arr2

length=10000
ARR1 = np.zeros((length,2)) 
ARR2 = np.zeros((length,2))    
# for i in range(0,10000):
#     arr1, arr2 = HS()
#     ARR1[i,:]=arr1
#     ARR2[i,:]=arr2

if __name__=="__main__":
    p=Pool(4)
    arr1, arr2 =p.map(HS,range(length))
    #ARR1[i,:]=arr1
    #ARR2[i,:]=arr2
    p.close()
    p.join
    

python

for-loop

parallel-processing

multiprocessing

montecarlo

0 Answers

Your Answer

Accepted video resources