Chris Mutel avatar Chris Mutel committed a1b93e6

Calculate chunk_size in Monte Carlo automatically

Comments (0)

Files changed (1)

bw2calc/monte_carlo.py

 
 class ParallelMonteCarlo(object):
     """Split a Monte Carlo calculation into parallel jobs"""
-    def __init__(self, demand, method, iterations=1000, chunk_size=100):
+    def __init__(self, demand, method, iterations=1000, chunk_size=None):
         self.demand = demand
         self.method = method
-        self.chunk_size = chunk_size
-        self.num_jobs = iterations // chunk_size
-        if iterations % self.chunk_size:
-            self.num_jobs += 1
+        if chunk_size:
+            self.chunk_size = chunk_size
+            self.num_jobs = iterations // chunk_size
+            if iterations % self.chunk_size:
+                self.num_jobs += 1
+        else:
+            self.num_jobs = max(multiprocessing.cpu_count() - 1, 1)
+            self.chunk_size = (iterations // self.num_jobs) + 1
 
     def calculate(self):
         pool = multiprocessing.Pool(processes=max(multiprocessing.cpu_count() - 1, 1))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.