C ++ amp运算符/数据类型 [英] C++ amp operator/ datatype

查看:73
本文介绍了C ++ amp运算符/数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算1 / i,i = 1 ... 10.

所以我生成以下代码:

I want to calculate the 1/i,i=1...10.
So i generate the code below:

double A[10]={1,1,1,1,1,1,1,1,1,1};
double B[10]={1,2,3,4,5,6,7,8,9,10};
double C[10];
array_view<double 1=""> Ag(10,A);
array_view<double 1=""> Bg(10,B);
array_view<double 1=""> Cg(10,C);
parallel_for_each(
    Cg.extent,
    [=](index<1> idx)(restrict amp)
    {
        Cg[idx]=Ag[idx]/Bg[idx]
    }
    );



此代码将返回有关读取的内存异常,然后中断。

我认为这本书是由凯特和阿德写的。我发现double是lambda表达式中的兼容类型。有谁知道为什么?


This code will return a memory exception about read, and then interrupted.
I view the book writed by Kate and Ade. I find that double is compatible type in lambda expression. Anybody knows why?

推荐答案

下面的代码,稍微修改一下这个 MSDN示例 [ ^ ],在我的系统上运行良好

The following code, slight modification of this MSDN sample[^], works well on my system
#include <amp.h>
#include <iostream>
using namespace concurrency;

const int size = 10;

void CppAmpMethod() {
    double aCPP[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    double bCPP[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    double sumCPP[size];

    // Create C++ AMP objects.
    array_view<const double, 1> a(size, aCPP);
    array_view<const double, 1> b(size, bCPP);
    array_view<double, 1> sum(size, sumCPP);
    //sum.discard_data();

    parallel_for_each(
        // Define the compute domain, which is the set of threads that are created.
        sum.extent,
        // Define the code to run on each thread on the accelerator.
        [=](index<1> idx) restrict(amp)
    {
        sum[idx] = a[idx]/ b[idx];
    }
    );

    // Print the results. The expected output is "7, 9, 11, 13, 15".
    for (int i = 0; i < size; i++) {
        std::cout << sum[i] << "\n";
    }
}

int main()
{
    CppAmpMethod();
}





产生以下输出



producing the following output

1
0.5
0.333333
0.25
0.2
0.166667
0.142857
0.125
0.111111
0.1


在我引用Kate Gregory和Ade Miller编写的C ++ AMP加速大规模并行与Microsoft Visual C ++之后。 Windows7不支持Full Double Precision,但Windows8可以支持。
After i refer to "C++ AMP Accelerated Massive Parallelism with Microsoft Visual C++" writed by Kate Gregory and Ade Miller pp.299-300. Windows7 cannot supports Full Double Precision, but Windows8 can supports.


这篇关于C ++ amp运算符/数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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