From b881e5465df969e8dabd4c4ffba3316a1f09aa21 Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Sat, 26 Jun 2021 19:12:06 +0300 Subject: [PATCH 1/2] ENH: try OpenMP in SetEffectiveMigration --- VGsim/_BirthDeath.pyx | 7 +++++-- VGsim/setup.py | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/VGsim/_BirthDeath.pyx b/VGsim/_BirthDeath.pyx index f5b2477..586d77d 100644 --- a/VGsim/_BirthDeath.pyx +++ b/VGsim/_BirthDeath.pyx @@ -3,6 +3,7 @@ # distutils: language = c++ cimport cython +from cython.parallel import prange from libc.math cimport log, floor, abs from libcpp.vector cimport vector @@ -185,9 +186,11 @@ cdef class BirthDeathModel: @cython.wraparound(False) @cython.cdivision(True) cdef void SetEffectiveMigration(self): + cdef Py_ssize_t i, j + for i in range(self.popNum): self.pm_maxEffectiveMigration[i] = 0.0 - for i in range(self.popNum): + for i in prange(self.popNum, nogil=True): for j in range(self.popNum): self.pm_effectiveMigration[i,j] = self.pm_migrationRates[i,j]*self.pm.contactDensity[j]/self.pm.sizes[j]+self.pm_migrationRates[j,i]*self.pm.contactDensity[i]/self.pm.sizes[i] if self.pm_effectiveMigration[i,j] > self.pm_maxEffectiveMigration[j]: @@ -497,7 +500,7 @@ cdef class BirthDeathModel: for j in range(0, iterations): self.SampleTime() popId = self.GenerateEvent() - if self.totalRate == 0.0 or self.pm.globalInfectious == 0: + if self.totalRate == 0.0 or self.pm.globalInfectious == 0 or self.events.ptr == 20000000: break self.CheckLockdown(popId) print("Total number of iterations: ", self.events.ptr) diff --git a/VGsim/setup.py b/VGsim/setup.py index 284b475..6fce369 100644 --- a/VGsim/setup.py +++ b/VGsim/setup.py @@ -20,6 +20,8 @@ def configuration(parent_package='', top_path=None): 'models.pxi', 'fast_choose.pxi'], language='c++', include_dirs=[numpy.get_include()], + extra_compile_args=['-fopenmp'], + extra_link_args=['-fopenmp'], ) return config From c59fc5f90b8491a4341cef2439e4c12f6c144e7b Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Sat, 26 Jun 2021 19:48:24 +0300 Subject: [PATCH 2/2] BLD: guard OpenMP on MacOS --- VGsim/setup.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/VGsim/setup.py b/VGsim/setup.py index 6fce369..cb5c683 100644 --- a/VGsim/setup.py +++ b/VGsim/setup.py @@ -2,12 +2,19 @@ from Cython.Compiler import Options +import sys import numpy Options.annotate = True PACKAGE_NAME = 'VGsim' +# enable openMP on linux, disable on MacOS +if sys.platform == "darwin": + openmp_args = [] +else: + openmp_args = ['-fopenmp'] + def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration @@ -20,8 +27,8 @@ def configuration(parent_package='', top_path=None): 'models.pxi', 'fast_choose.pxi'], language='c++', include_dirs=[numpy.get_include()], - extra_compile_args=['-fopenmp'], - extra_link_args=['-fopenmp'], + extra_compile_args=openmp_args, + extra_link_args=openmp_args, ) return config