gpu opencl 向量加_如何在OpenCL中累积向量?

bd96500e110b49cbb3cd949968f18be7.png

I have a set of operations running in a loop.

for(int i = 0; i < row; i++)

{

sum += arr1[0] - arr2[0]

sum += arr1[0] - arr2[0]

sum += arr1[0] - arr2[0]

sum += arr1[0] - arr2[0]

arr1 += offset1;

arr2 += offset2;

}

Now I'm trying to vectorize the operations like this

for(int i = 0; i < row; i++)

{

convert_int4(vload4(0, arr1) - vload4(0, arr2));

arr1 += offset1;

arr2 += offset2;

}

But how do I accumulate the resulting vector in the scalar sum without using a loop?

I'm using OpenCL 2.0.

解决方案

I have found a solution which seems to be the closest way I could have expected to solve my problem.

uint sum = 0;

uint4 S;

for(int i = 0; i < row; i++)

{

S += convert_uint4(vload4(0, arr1) - vload4(0, arr2));

arr1 += offset1;

arr2 += offset2;

}

S.s01 = S.s01 + S.s23;

sum = S.s0 + S.s1;

OpenCL 2.0 provides this functionality with vectors where the elements of the vectors can successively be replaced with the addition operation as shown above. This can support up to a vector of size 16. Larger operations can be split into factors of smaller operations. For example, for adding the absolute values of differences between two vectors of size 32, we can do the following:

uint sum = 0;

uint16 S0, S1;

for(int i = 0; i < row; i++)

{

S0 += convert_uint16(abs(vload16(0, arr1) - vload16(0, arr2)));

S1 += convert_uint16(abs(vload16(1, arr1) - vload16(1, arr2)));

arr1 += offset1;

arr2 += offset2;

}

S0 = S0 + S1;

S0.s01234567 = S0.s01234567 + S0.s89abcdef;

S0.s0123 = S0.s0123 + S0.s4567;

S0.s01 = S0.s01 + S0.s23;

sum = S0.s0 + S0.s1;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值