如何从dispatch_apply(GCD)循环写入数组? [英] How to write into an array from a dispatch_apply (GCD) loop?

查看:771
本文介绍了如何从dispatch_apply(GCD)循环写入数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了代码来使用Runge-Kutta方法来计算大量耦合主方程的动力学。代码包含大量的for循环,其中每个步骤是独立的。我打算使用大中央调度来加快程序。我根据我在 http:// www。 .macresearch.org / cocoa-scientists-xxxi-all-aboard-grand-central 。我的代码和Macresearch上的示例在我的机器上编译(MacOSX 10.6.8 Xcode 4.0.2)。所以这里是我的代码:

I have written code to calculate the dynamics of a large set of coupled master equation using the Runge-Kutta-method. The code contains a lot of for-loops, where each step is independent. I intend to use Grand Central Dispatch to speed up the program. I based my attempt on an example I found at http://www.macresearch.org/cocoa-scientists-xxxi-all-aboard-grand-central . Neither my code nor the example on macresearch compile on my machine (MacOSX 10.6.8 Xcode 4.0.2). So here is my code:

...
    double values[SpaceSize], k1[SpaceSize];    

        for ( int t=1 ; t<Time ; t++ ) {

            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

            //k1
            for (int k=0 ; k<SpaceSize ; k++ ) values[k]=Concentration[k][t-1];

            dispatch_apply(SpaceSize, queue,
                       ^(size_t k)  {
                           k1[k]=h * derives(values, SpaceSize, k); //<--error      
                                    }
                        );
...

它会错误:


语义问题:不能引用块中可变修改的
类型的
声明

Semantic Issue: Cannot refer to declaration with a variably modified type inside block

我尝试用一​​个向量替换数组(值,k1),但是我得到另一个错误消息:

I tried replacing the arrays (values, k1) with a vectors, but then I get another error message instead:


语义问题:只读变量是
不可分配

Semantic Issue: Read-only variable is not assignable

真的知道这些错误消息试图告诉我。我花了相当多的时间搜索和询问是否有人可以帮助。我将非常感谢提示或更好的方法来解决这个问题。

That is where I'm stuck, not really knowing what those error messages are trying to tell me. I spend quite some time searching and asking around if anyone could help. I would be very grateful for tips or better ways to solve this.

推荐答案


错误:到块中的数组类型的声明

error: cannot refer to declaration with an array type inside block

在块实现下,不允许从块访问C数组。 (我找不到有关它的文档...)

Under the blocks implementation, it is not allowed to access to an C array from blocks. (I can't find the documentation about it...)

有一个简单的修复: - )

There is an easy fix :-)

double valuesArray[SpaceSize], k1Array[SpaceSize];    
double *values = valuesArray, *k1 = k1Array;

这篇关于如何从dispatch_apply(GCD)循环写入数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆