Skip to content

Instantly share code, notes, and snippets.

@vighneshbirodkar
Created June 30, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vighneshbirodkar/7f52f5c0a8e13b8d4822 to your computer and use it in GitHub Desktop.
Save vighneshbirodkar/7f52f5c0a8e13b8d4822 to your computer and use it in GitHub Desktop.
# distutils: language = c++
# https://groups.google.com/forum/#!topic/cython-users/Ady-DdWu6rE
from cython.parallel import parallel, prange, threadid
from libcpp.vector cimport vector
from libc.stdlib cimport malloc, free
cimport openmp
import time
print("Threads = ", openmp.omp_get_max_threads())
cdef int work(int x) nogil:
cdef int i = 0
while x > 0:
i += x
x -= 1
return i
cdef int num_scales = 100000
cdef int scale
cdef vector[int] int_vector = vector[int]()
cdef int* tmp = <int*>malloc(sizeof(int)*num_scales)
cdef openmp.omp_lock_t mylock
openmp.omp_init_lock(&mylock)
T = time.time()
with nogil:
for scale in range(num_scales):
tmp[scale] = work(scale)
openmp.omp_set_lock(&mylock) # acquire
int_vector.push_back(tmp[scale])
openmp.omp_unset_lock(&mylock) # release
#for idx in range(int_vector.size()):
# print(int_vector[idx])
print("Time taken = ", time.time() - T)
#print("Size = ", int_vector.size())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment