Array Indices: Integers and Functions of Integers

I'm trying to compute log-likelihoods using the logit formula (familiarity is irrelevant to my question, but see i.e. discrete choice for reference). In the specific case here, I do so many times using a cuda mex file in matlab.
THE PROBLEM: I keep getting the return vector (peps) all zeros (that is, the initialized values). The kernel function and further explanation are below.
void __global__ sublik(double * const peps , int const * const Y, double const * const X, double const * const price,
double const * const beta, int const * const ids,
int const * const N, int const * const K, int const * const J, int const * const D, int const * const T, int const * const yr){
/* Calculates logit log-likelihood, price is a vector with an entry for each alternative (including base) */
int tid = blockDim.x * blockIdx.x + threadIdx.x;
int tmpdx1 ; int tmpdx2 ; int tmpdx3; int tmpdx4; int tmpdx5;
if (tid < *D) {
double ll=0;
for (int i=ids[*yr]; i<ids[*yr+1]; i++){
double chutil = 0;
double d = 0;
for (int j=0; j<*J; j++){
double util = 0;
if (j > 0){
tmpdx1 = *yr * ( 1 + *K * ( *J - 1 ) ) + tid * ( ( 1 + *K * ( *J - 1 ) ) * *T ) ; // *T,tid is causing me problems here
tmpdx2 = *yr + j * *T; // *T is causing me problems here
tmpdx3 = *yr;
util += beta[tmpdx1] * ( price[ tmpdx2 ] - price[ tmpdx3 ]); // price relative to alternative 0
for (int k=0; k<*K; k++){
tmpdx4 = ( k + ( j - 1 ) * *K + 1 ) + *yr * ( 1 + *K * ( *J - 1 ) ) + tid * ( ( 1 + *K * ( *J - 1 ) ) * *T ); // *T,tid,k,j is causing me problems here
tmpdx5 = i + k * *N; // k is causing me problems here
util += beta[tmpdx4] * X[ tmpdx5 ];
}
}
if (Y[i]==j+1){
chutil=util;
}
d += exp(util);
}
ll += chutil - log(d);
}
peps[tid] = ll;
}
}
If I write in numeric values in place of some of the variables in the indices, it produces reasonable output. For instance, if I replace `tid' with one of the possible values of `tid', say 9999, and do so for some select others, then it "works". I note problematic variables above and below.
Specifically, if I replace:
if (j > 0){
tmpdx1 = *yr * ( 1 + *K * ( *J - 1 ) ) + tid * ( ( 1 + *K * ( *J - 1 ) ) * *T ) ; // *T,tid is causing me problems here
tmpdx2 = *yr + j * *T; // *T is causing me problems here
tmpdx3 = *yr;
util += beta[tmpdx1] * ( price[ tmpdx2 ] - price[ tmpdx3 ]); // price relative to alternative 0
for (int k=0; k<*K; k++){
tmpdx4 = ( k + ( j - 1 ) * *K + 1 ) + *yr * ( 1 + *K * ( *J - 1 ) ) + tid * ( ( 1 + *K * ( *J - 1 ) ) * *T ); // *T,tid,k,j is causing me problems here
tmpdx5 = i + k * *N; // k is causing me problems here
util += beta[tmpdx4] * X[ tmpdx5 ];
}
}
with the following, it produces reasonable output, but still not correct for the actual program: (I've replace T=19,tid=9999,k=0,j=3 where each must be replaced to produce reasonable output)
if (j > 0){
tmpdx1 = *yr * ( 1 + *K * ( *J - 1 ) ) + 9999 * ( ( 1 + *K * ( *J - 1 ) ) * 19 ) ; // *T,tid is causing me problems here
tmpdx2 = *yr + j * 19; // *T is causing me problems here
tmpdx3 = *yr;
util += beta[tmpdx1] * ( price[ tmpdx2 ] - price[ tmpdx3 ]);
for (int k=0; k<*K; k++){
tmpdx4 = ( 0 + ( 3 - 1 ) * *K + 1 ) + *yr * ( 1 + *K * ( *J - 1 ) ) + 9999 * ( ( 1 + *K * ( *J - 1 ) ) * 19 ); // *T,tid,k,j is causing me problems here
tmpdx5 = i + 0 * *N; // k is causing me problems here
util += beta[tmpdx4] * X[ tmpdx5 ];
}
}
I thought it might be how the individual variables are defined, but the use of `j' makes me think it is something more subtle. In the first use of `j', for tmpdx2, it works fine. But, in the second use of `j', for tmpdx4, it will not work.

Réponses (0)

Cette question est clôturée.

Clôturé :

le 2 Sep 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by