-
Notifications
You must be signed in to change notification settings - Fork 3
/
omp.pyx
69 lines (54 loc) · 1.65 KB
/
omp.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from cython.parallel import parallel, prange, threadid
from openmp cimport omp_lock_t, \
omp_set_lock, omp_unset_lock, \
omp_init_lock, omp_destroy_lock
cimport openmp
import numpy as np
cimport numpy as np
cpdef parallel_block():
cdef:
omp_lock_t lock
int val
omp_init_lock(&lock)
with nogil, parallel(num_threads=100):
val = threadid()
omp_set_lock(&lock)
with gil:
print("Hello from {}".format(val))
omp_unset_lock(&lock)
omp_destroy_lock(&lock)
cpdef parallel_range(int num_threads):
cdef:
omp_lock_t lock
int idx, val
omp_init_lock(&lock)
for idx in prange(100, nogil=True, num_threads=num_threads, schedule=dynamic):
omp_set_lock(&lock)
val = threadid()
with gil:
print("Idx {} thread {}".format(idx, val))
omp_unset_lock(&lock)
print("Last: Idx {} thread {}".format(idx, val))
omp_destroy_lock(&lock)
cpdef parallel_reduction():
cdef:
int idx, sum = 0
for idx in prange(100, nogil=True, num_threads=10):
sum += idx
print("Sum {}".format(sum))
cpdef nested_parallel_prange():
cdef:
int idx, idx2, val
omp_lock_t lock
omp_init_lock(&lock)
with nogil, parallel(num_threads=10):
with gil:
print ("outer")
for idx in prange(100):
for idx2 in prange(5):
val = threadid()
omp_set_lock(&lock)
with gil:
if idx % 10 == 0:
print("idx {} idx2 {} thread {}".format(idx, idx2, val))
omp_unset_lock(&lock)