0

I have this code:

Matrix mat;

for (int y=0; y<n; ++y)
{
    for (int x=0; x<m; ++x)
    {
        // do some small operation on mat(y,x)
    }
};

The serial computation is very slow (this double loop is called 500-1000 times), so as a first step I want to parallelize it with dispatch_apply.

Matrix mat;

dispatch_apply(PATCH_SIZE, _queue, ^(size_t y)
{
    for (int x=0; x<m; ++x)
    {
        // do some small operation on mat(y,x)
    }
});

The problem is with the variable mat, it's define as read-only inside the block. Is there a way to workaround this?

jscs
  • 63,694
  • 13
  • 151
  • 195
aledalgrande
  • 5,167
  • 3
  • 37
  • 65

2 Answers2

2

If you want a variable (ref) to be writable from within a block, you can prefix it with __block.

For example, __block Matrix mat;

David Xu
  • 5,555
  • 3
  • 28
  • 50
0

You could try declaring mat as __block Matrix mat;

Casey Fleser
  • 5,707
  • 1
  • 32
  • 43