Vectors in NMODL

NMODL and the Channel Builder.
Post Reply
mctavish
Posts: 74
Joined: Tue Mar 14, 2006 6:10 pm
Location: New Haven, CT

Vectors in NMODL

Post by mctavish »

I am trying to pass a Vector to a NMODL object.

I can do the following in hoc:

Code: Select all

objref vec
vec=new Vector(490776) // Too big?
mynmodlobj.setVec(vec)
in my NMODL file I have:

Code: Select all

NEURON {
  POINTER vecP
}

PARAMETER {
  vecP=0
}

PROCEDURE setVec() {
VERBATIM
  if (ifarg(1)) {
    Object *o = *hoc_objgetarg(1);
    check_obj_type(o, "Vector");
    printf("setVec.size=%d\n",vector_capacity(o->u.this_pointer));
    _p_vecP=vector_vec(o->u.this_pointer);
    printf("_p_vecP=%f\n",*_p_vecP);
  }
ENDVERBATIM
}
Now, setVec() works just fine except for when I have a lot of other processing/memory being used. In this case, I will get the first printf statement and then a either a segmentation violation before the second printf, or the values will be null. I have also attempted to assign the vector_vec to a local double pointer and get the same result. Am I doing something wrong, is my vector too big, or is the memory moving out from underneath me, in which case, how do I handle handles in NMODL?

Thanks,
Tom
hines
Site Admin
Posts: 1710
Joined: Wed May 18, 2005 3:32 pm

Re: Vectors in NMODL

Post by hines »

vector size is limited by the amount of available memory so space is not a problem.
It's a good idea to declare the types of the functions you are using. eg.

Code: Select all

VERBATIM
extern int ifarg(int iarg);
extern double* vector_vec(void* vv);
extern int vector_capacity(void* vv);
extern void* vector_arg(int iarg);
ENDVERBATIM
Since _p_vecP is a pointer to a double, it is best to cast with
void** vv = (void**)_p_vecP;
then fill _p_vecP with the pointer to the vector with
*vv = vector_arg(1);
and get vector info with
int size = vector_capacity(*vv);
double* px = vector_vec(*vv);
mctavish
Posts: 74
Joined: Tue Mar 14, 2006 6:10 pm
Location: New Haven, CT

Re: Vectors in NMODL

Post by mctavish »

Thanks, Michael. That works.

Regarding the last line,

Code: Select all

double* px = vector_vec(*vv);
is this necessary, fast, or otherwise a safe way to read from the pointer? Right now I read from _p_vecP directly....
hines
Site Admin
Posts: 1710
Joined: Wed May 18, 2005 3:32 pm

Re: Vectors in NMODL

Post by hines »

A Vector contains a pointer to a double * array.
_p_vecP is the pointer to the Vector.
vector_vec(_p_vecP) is the pointer to the double* that the Vector points to.
mctavish
Posts: 74
Joined: Tue Mar 14, 2006 6:10 pm
Location: New Haven, CT

Re: Vectors in NMODL

Post by mctavish »

What threw me was that if I called

Code: Select all

double *dP = _p_vecP;
and used dP, then it would operate correctly. I suppose the reason this did not crash or give erroneous output is because the double* array is the first part of the Vector object(?). In any case, I'll use vector_vec(_p_vecP).

Thanks,
Tom
Post Reply