Tory
Tory

Reputation: 109

Using Python multiprocessing to generate points for Monte Carlo algorithm

I have a pool of 150 processes:

import multiprocessing as mp
pool = mp.Pool(processes=150)

For each process, I need to generate 10000 random 3D points inside the bounding box (min_X, max_X, etc.. defines the bounding box). I need these points to determine if they are within a shape that is within the bounding box (Monte Carlo type algorithm). To generate the points, I have the code:

# generates intervals within the bounding box
# supposed to generate these points for each process
xPoints = [round(random.uniform(min_X, max_X), 2) for _ in range(10000)]
yPoints = [round(random.uniform(min_Y, max_Y), 2) for _ in range(10000)]
zPoints = [round(random.uniform(min_Z, max_Z), 2) for _ in range(10000)]

I assume I need to create a function that does the above code for calculating points, then call it with pool.apply similar to this:

pool = mp.Pool(processes=4)
results = [pool.apply(square, args=(x,)) for x in range(1,7)]
print(results) 

I just can't seem to figure out how to do it though.

Upvotes: 1

Views: 541

Answers (1)

Minh Dao
Minh Dao

Reputation: 837

How about using Joblib? Embarrassingly parallel for loops

import random
from joblib import Parallel, delayed


def random_point(min_v, max_v):
    return round(random.uniform(min_v, max_v), 2)


min_X = 10
max_X = 20
xPoints = Parallel(n_jobs=4)(delayed(random_point)(min_X, max_X) for _ in range(10000))

print(xPoints)
# [19.81, 12.37, 17.4, 12.35, 16.5, 10.04, 18.03, 16.25, 12.52, 10.35]

Upvotes: 1

Related Questions