Friday, 27 July 2018

Accelerate: Finding pointer address for vDSP_maxviD

I am working on a machine learning project in objective-c. Which is using the following code:

unsigned cStride = multiArray.strides[2].intValue;
unsigned hStride = multiArray.strides[3].intValue;
unsigned wStride = multiArray.strides[4].intValue;

for (unsigned h = 0; h < height; h++) {
    for (unsigned w = 0; w < width; w++) {
        unsigned highestClass = 0;
        double highest = -DBL_MAX;
        for (unsigned c = 0; c < channels; c++) {
            unsigned offset = c * cStride + h * hStride + w * wStride;
            double score = pointer[offset];
            if (score > highest) {
                highestClass = c;
                highest = score;
            }
        }
        // futher code
    }
}

So far so good, it is working well. However I came across Accelerate which I want to implement for this class, since it could be faster then the current implementation.

I decided to implement vDSP_maxviD. https://developer.apple.com/documentation/accelerate/1449682-vdsp_maxvid?language=objc

Their implementation is written as follows:

__A Single-precision real input vector.

__I Stride for A

__C Output scalar

__IC Output scalar index

__N The number of elements to process

And their under the hood Accelerate code:

*C = -INFINITY;
for (n = 0; n < N; ++n)
{
    if (*C < A[n * I])
    {
        *C = A[n * I];
        *IC = n * I;
    }
}

Now I started to think what the parameters in this case need to become:

__A will become pointer

__C will become highest

__IC will become highestClass

__N will become channels

However, I left __I blank. This is because I don't know how to find the right pointer address. I my previous code I use:

unsigned offset = c * cStride + h * hStride + w * wStride;

However, we miss c now, and it's replaced by n inside the Accelerate framework.

What will be the correct calculation for my pointer index?



from Accelerate: Finding pointer address for vDSP_maxviD

No comments:

Post a Comment