Anonymous Anonymous - 1 month ago
119 0

Not sure what the problem is!

Python

Cython nogil not working?

import numpy as np
cimport numpy as np
cimport cython
from cython.parallel import *

ctypedef np.float64_t DTYPE

cdef DTYPE evaluate_objective(np.ndarray[DTYPE, ndim=2] A,np.ndarray[DTYPE, ndim=1] b,np.ndarray[DTYPE, ndim=1] x):
    # TODO: Can be made even faster by using for loop can do it later though
    return x.dot(A.dot(x) - b.dot(x))

cpdef DTYPE coordinate_descent(np.ndarray[DTYPE, ndim=2] A,np.ndarray[DTYPE, ndim=1] b,np.int nDim, np.int nIter=1000 ):
    cdef Py_ssize_t i
    cdef Py_ssize_t iter
    cdef np.ndarray[DTYPE,
                    ndim=1] x = \
                    np.zeros(nDim, )
    cdef DTYPE temp
    # Note sure if it is the right way to do?
    for iter in prange(nIter,nogil=True):
        i = (iter%nDim)
        temp = (b[i]-( A[:,i].dot(x)*x[i] ) + (A[i,i]*x[i]*x[i]))/A[i,i]
        x[i] = np.max([0,np.min([temp,1])])
    return evaluate_objective(A,b,x)