On Mon, Nov 5, 2012 at 7:36 AM, stefandw <
stefa...@gmail.com> wrote:
> Thanks for your replies!
> @dag sverre: yes, I suspected something like that might be the case. The
> problem I'm having is that I have to construct / multiply these small
> matrices millions of times, and I'm trying to pull out pieces of pure python
> and rewrite into cython, but I guess the object construction overhead is
> always going to kill me? Do you have any suggestions here?
1) Do you need anew array? or could you alter the input one in place?
2) the fact that you are working with 8X8 arrays, and care about speed
suggests that you have a LOT of these to process -- pehaps you could
put them all in a (N, 8, 8) array, and then process them all in a loop
in Cython. That would allow you to create a single numpy array for the
whole pile -- and object creating performance no longer is an issue.
-CHB
> As another example, I moved the matrix "population" to cython as well: a
> single object for the "unnormalized" matrix is created only once in the
> python code, and it then gets populated in the cython code as below. This is
> the biggest drag in the whole program, so I tried various things, but so far
> the old buffer syntax is the fastest (and about 20% faster than the
> memoryview syntax): (TM stands for transition matrix, by the way, it's a
> piece of code to do with markov chains)
>
> def BTM_populate(np.ndarray[DTYPE_t, ndim=2] TMu, int iReo, DTYPE_t qindex,
> DTYPE_t refi, DTYPE_t sell, parameters):
> '''CAREFUL: operates on its first argument'''
> cdef int i, j, k
> cdef DTYPE_t myval
> #------------------------------current to a1 and back
> TMu[1, 0] = exp(parameters['c_a1_const'] + parameters['c_a1_beta'] *
> qindex)
> TMu[0, 1] = exp(parameters['a1_c_const'] + parameters['a1_c_beta'] *
> qindex)
> #---------------------------------increase in arrears
> myval = exp(parameters['a_a+_const'] + parameters['a_a+_beta'] * qindex)
> for i in range(2, iReo): TMu[i, i - 1] = myval
> #------------------------repossession and sale from repossession
> TMu[iReo, iReo - 1] = exp(parameters['a_reo_const'] +
> parameters['a_reo_beta'] * qindex)
> TMu[iReo + 1, iReo] = exp(parameters['reo_sold_const'])
> #---------------------------------prepayment
> TMu[iReo + 2, 0] = exp(parameters['c_pp_const'] +
> parameters['c_pp_beta'] * qindex +
> parameters['pp_refi'] * refi)
> myval = exp(parameters['a_pp_const'] + parameters['a_pp_beta'] * qindex
> +
> parameters['pp_refi'] * refi +
> parameters['pp_sell_from_arr']*sell)
> for i in range(1, iReo - 1): TMu[iReo + 2, i] = myval
> TMu[iReo + 2, iReo-1] = exp(parameters['aMax_pp_const'] +
> parameters['aMax_pp_beta'] * qindex +
> parameters['pp_refi'] * refi +
> parameters['pp_sell_from_arr']*sell)
> #------------------------------decrease in arrears
> myval = exp(parameters['aMax_a-_const'] + parameters['aMax_a-_beta'] *
> qindex)
> for i in range(iReo-1): TMu[i, iReo - 1] = myval
> myval = exp(parameters['a_a-_const'] + parameters['a_a-_beta'] * qindex)
> for i in range(2, iReo - 1):
> for j in range(i):
> TMu[j, i] = myval / i
> return None
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R
(206) 526-6959 voice
7600 Sand Point Way NE
(206) 526-6329 fax
Seattle, WA 98115
(206) 526-6317 main reception
Chris....@noaa.gov