Skip to content

Instantly share code, notes, and snippets.

@hiropppe
Created May 26, 2017 07:00
Show Gist options
  • Save hiropppe/26899f8e5feeef12cf52d4dababa9c09 to your computer and use it in GitHub Desktop.
Save hiropppe/26899f8e5feeef12cf52d4dababa9c09 to your computer and use it in GitHub Desktop.
from cython.parallel import prange
from libc.stdio cimport printf
from libc.stdlib cimport abort, malloc, free
cimport openmp
def get_int_flag():
cdef int flag = 0
cdef int *flag_pointer = &flag
cdef openmp.omp_lock_t mylock
cdef int i
cdef int j[4]
j = [0, 0, 0, 0]
printf('Initialize lock\n')
openmp.omp_init_lock(&mylock) # initialize
for i in prange(4, nogil=True):
while True:
if flag_pointer[0] == 1:
printf('Lose !! t=%d j=%d flag=%d flag_addr=%d\n', openmp.omp_get_thread_num(), j[i], flag_pointer[0], &flag_pointer)
break
if j[i] == 10000:
printf('Get lock %d\n', openmp.omp_get_thread_num())
openmp.omp_set_lock(&mylock)
if flag_pointer[0] == 0:
printf('Win !! t=%d j=%d flag=%d flag_addr=%d\n', openmp.omp_get_thread_num(), j[i], flag_pointer[0], &flag_pointer)
flag_pointer[0] = 1
else:
printf('Shit !! t=%d j=%d flag=%d flag_addr=%d\n', openmp.omp_get_thread_num(), j[i], flag_pointer[0], &flag_pointer)
printf('Release lock %d\n', openmp.omp_get_thread_num())
openmp.omp_unset_lock(&mylock)
break
if j[i] > 1000000:
printf('Lost. t=%d j=%d flag=%d flag_addr=%d\n', openmp.omp_get_thread_num(), j[i], flag_pointer[0], &flag_pointer)
break
j[i] += 1
printf('Destroy lock\n')
openmp.omp_destroy_lock(&mylock) # deallocate the lock
ctypedef struct flag_t:
int flag
openmp.omp_lock_t mylock
def get_struct_flag():
cdef flag_t *flag_data = <flag_t *>malloc(sizeof(flag_t))
cdef int i
cdef int j[4]
j = [0, 0, 0, 0]
flag_data.flag = 0
printf('Initialize lock\n')
openmp.omp_init_lock(&flag_data.mylock) # initialize
for i in prange(4, nogil=True):
while True:
if flag_data.flag == 1:
printf('Lose !! t=%d j=%d flag=%d\n', openmp.omp_get_thread_num(), j[i], flag_data.flag)
break
if j[i] == 10000:
printf('Get lock %d\n', openmp.omp_get_thread_num())
openmp.omp_set_lock(&flag_data.mylock)
if flag_data.flag == 0:
printf('Win !! t=%d j=%d flag=%d\n', openmp.omp_get_thread_num(), j[i], flag_data.flag)
flag_data.flag = 1
else:
printf('Shit !! t=%d j=%d flag=%d\n', openmp.omp_get_thread_num(), j[i], flag_data.flag)
printf('Release lock %d\n', openmp.omp_get_thread_num())
openmp.omp_unset_lock(&flag_data.mylock)
break
if j[i] > 1000000:
printf('%d %d\n', openmp.omp_get_thread_num(), j[i])
printf('Lost. t=%d j=%d flag=%d\n', openmp.omp_get_thread_num(), j[i], flag_data.flag)
break
j[i] += 1
printf('Destroy lock\n')
openmp.omp_destroy_lock(&flag_data.mylock) # deallocate the lock
""" setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
setup(name="cython_omp_lock_sample",
ext_modules=cythonize(
Extension("cython_omp_lock_sample",
sources=["cython_omp_lock_sample.pyx"],
extra_compile_args=["-fopenmp"],
extra_link_args=["-fopenmp"]
)))
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment