multiprocessing, - , OpenMP prange. , .
x*x x**2, pow(x, 2)):double- ,
size % num_threads != 0
:
#cython: wraparound=False
#cython: boundscheck=False
#cython: cdivision=True
#cython: nonecheck=False
#cython: profile=False
import numpy as np
cimport numpy as np
from cython.parallel import prange
cdef void cpow2(int size, double *inp, double *out) nogil:
cdef int i
for i in range(size):
out[i] = inp[i]*inp[i]
def pow2(np.ndarray[np.float64_t, ndim=1] inp,
np.ndarray[np.float64_t, ndim=1] out,
int num_threads=4):
cdef int thread
cdef np.ndarray[np.int32_t, ndim=1] sub_sizes, pos
size = np.shape(inp)[0]
sub_sizes = np.zeros(num_threads, np.int32) + size
pos = np.zeros(num_threads, np.int32)
sub_sizes[num_threads-1] += size % num_threads
pos[1:] = np.cumsum(sub_sizes)[:num_threads-1]
for thread in prange(num_threads, nogil=True, chunksize=1,
num_threads=num_threads, schedule='static'):
cpow2(sub_sizes[thread], &inp[pos[thread]], &out[pos[thread]])
def main():
a = np.arange(642312323).astype(np.float64)
pow2(a, out=a, num_threads=4)