Can the Accelerate Framework aggregate array values based on a separate index array?

115 views Asked by At

I am trying to determine if the Accelerate Framework can help speed up a calculation my app has to perform. Let's say I have the following arrays:

  • invoice[0..n], an array of doubles containing invoice values

  • week[0..n], an array of integers, where week[x] holds the week-of-year invoice[x] was billed.

The number of invoices per week is variable. I have tens of thousands of invoices. Both arrays are ordered by week.

Is there any way the Accelerate framework can help me calculate how much was billed per week?

So, for example, if:

invoice = [10.0, 15.0, 10.0, 25.0, 40.0,   x, ...]
week    = [   0,    0,    0,    1,    1,   2, ...]

the result should be: [35.0, 65.0, ...]

I have browsed the documentation, and it does not seem to have this capability but, since I am completely new to it, there may be some trick I am missing.

1

There are 1 answers

1
Khaled Barazi On

Have you tried something similar:

Float32 invoices[6] = {10.0, 15.0, 10.0, 25.0, 40.0, 80.0};
UInt8   weeks[6] = {0, 0, 0, 1, 1, 2};
Float32 *weeklyInvoicesAmount = calloc(52,sizeof(Float32));

int weekCounter = 0;
int invoiceCounter = 0;
int weeklyInvoice = 0;


while (weekCounter < 52)
{
    weeklyInvoice = 0;

    while (weeks[invoiceCounter] == weekCounter)
        weeklyInvoice += invoices [invoiceCounter++];

    weeklyInvoicesAmount[weekCounter] = weeklyInvoice;
    printf("Week :%i invoice: %f\n", weekCounter, weeklyInvoicesAmount[weekCounter]);
    ++weekCounter;
}

free(weeklyInvoicesAmount);

Now obviously, there is a bit of work you need to do on the array setup.

The reason I am asking if you tried C logic is because I usually find that logical C + compiler optimization usually gives better results than C alone - since this is what the compiler is doing in the background anyway...And probably optimizing better than we can....

Compiler optimization: project -> build settings -> optimization level -> change Debug and release to "-Os".

I would start with that and measure the different mach times using different optimizations.

If it is still slow, I would try vDSP in Accelerate framework:

Accelerate Framework API

I think the problem you will have with any matrix calculations is that you have a variable stride so you can't use something like vDSP_sve.

Accelerate framework and vector math optimization is based on being able to use a static number of elements to fill the registers and to compute at the same time. A variable stride negates that.

Hope some of that helps.