免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2638 | 回复: 2
打印 上一主题 下一主题

[C] 有人玩过grand central dispatch么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-10 19:58 |只看该作者 |倒序浏览
好像文档太少了,除了deverloper和xcode自带的之外,似乎就没有了。
mac tech测试的好像比openmp略快一点。

论坛徽章:
0
2 [报告]
发表于 2010-03-10 20:29 |只看该作者
有openmp那么简单不?

论坛徽章:
0
3 [报告]
发表于 2010-03-10 20:41 |只看该作者
本帖最后由 cobranail 于 2010-03-10 20:52 编辑

OpenMP

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int main (int argc, const char * argv[]) {
    const int N = 1000;
    const int BLURRADIUS = 7;
    float originalImage[N*N], blurredImage[N*N];
    int row, col, i, j;

    #pragma omp parallel for private (col, i, j)
    for ( row = 0; row < N; ++row ) {
        for ( col = 0; col < N; ++col ) {
            int pixIndex = row*N + col;
            blurredImage[pixIndex] = 0.0f;
            for ( i = MAX(0, row-BLURRADIUS); i < MIN(row+BLURRADIUS, N); ++i ) {
                for ( j = MAX(0, col-BLURRADIUS); j < MIN(col+BLURRADIUS, N); ++j ) {            
                    int blurPixIndex = i*N + j;
                    float distance = (i-row)*(i-row) + (j-col)*(j-col);
                    distance = sqrt(distance);
                    blurredImage[pixIndex] += exp(-distance*distance) * originalImage[blurPixIndex];
                }
            }
        }
    }

    return 0;
}



grand central

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <dispatch/dispatch.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int main (int argc, const char * argv[]) {
    const int N = 1000;
    const int BLURRADIUS = 7;
    float originalImage[N*N], blurredImage[N*N];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply(N, queue,
        ^(size_t row) {
            int col, i, j;
            for ( col = 0; col < N; ++col ) {
                int pixIndex = row*N + col;
                blurredImage[pixIndex] = 0.0f;
                for ( i = MAX(0, row-BLURRADIUS); i < MIN(row+BLURRADIUS, N); ++i ) {
                    for ( j = MAX(0, col-BLURRADIUS); j < MIN(col+BLURRADIUS, N); ++j ) {            
                        int blurPixIndex = i*N + j;
                        float distance = (i-row)*(i-row) + (j-col)*(j-col);
                        distance = sqrt(distance);
                        blurredImage[pixIndex] += exp(-distance*distance) * originalImage[blurPixIndex];
                    }
                }
            }
        }
    );

    return 0;
}

好像差不多,这个应该只是grand central的副业,主业应该还是想取代pthread。我比较喜欢grand central,因为不用锁。

貌似grand central还可以把任务发送到opencl设备上。
freebsd 8.1将支持block和grand central dispatch,不知道性能怎么样。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP