Created
June 30, 2015 21:09
-
-
Save vighneshbirodkar/7f52f5c0a8e13b8d4822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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