【问题标题】:Integer division using gpuR使用 gpuR 进行整数除法
【发布时间】:2017-10-22 15:39:06
【问题描述】:

我正在尝试使用自定义函数计算大距离矩阵。为了加快计算速度,我试图将所有计算都放在 GPU 上。我正在执行的计算的一部分遇到了可以重现如下的错误:

require('gpuR')
a=gpuVector(c(5,4,3,6,7),type='integer')
d=a/2
Error in gpuVecScalarDiv(e1, e2, 0) : integer not currently implemented

有人有解决办法吗?

【问题讨论】:

  • 欢迎在githubhere提出请求。如果你这样做,我当然可以做到这一点。请注意,R 中的整数除法也是通过 %/% 运算符完成的。

标签: r parallel-processing gpu


【解决方案1】:

下面是一个示例,应该指出一个可能的解决方案。
当前未实现两个整数向量的乘积:

library(gpuR)
A <- seq.int(from=0, to=999)
B <- seq.int(from=1000, to=1)
gpuA <- gpuVector(A)
gpuB <- gpuVector(B)   
gpuC <- gpuA %*% gpuB

gpuVecInnerProd(x, y) 中的错误:当前未实现整数

但是我们可以将AB 从整数转换为数字,并且内积在gpuR 下工作得很好:

A <- as.numeric(seq.int(from=0, to=999))
B <- as.numeric(seq.int(from=1000, to=1))
gpuA <- gpuVector(A)
gpuB <- gpuVector(B)    
gpuC <- gpuA %*% gpuB

all(A%*%B == gpuC)
[1] TRUE

在你的例子中:

a <- gpuVector(c(5,4,3,6,7)*1.0)
d <- a/2
d[,]
[1] 2.5 2.0 1.5 3.0 3.5

一个有趣的笔记。如果我们在gpuMatrix 对象中转换AB 整数矩阵,我们不会收到来自gpuA %*% gpuB 的错误消息,但结果是错误的:

A <- matrix(seq.int(from=0, to=999),nrow=1)
B <- matrix(seq.int(from=1000, to=1),ncol=1) 
gpuA <- gpuMatrix(A)
gpuB <- gpuMatrix(B)
print(gpuA)

Source: gpuR Matrix [1 x 1,000]
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    1    2    3    4

print(gpuB)

Source: gpuR Matrix [1,000 x 1]
     [,1]
[1,] 1000
[2,]  999
[3,]  998
[4,]  997
[5,]  996

gpuC <- gpuA %*% gpuB
print(gpuA %*% gpuB)

Source: gpuR Matrix [1 x 1]   
     [,1]
[1,]    0

print(A%*%B)

          [,1]
[1,] 166666500

希望对你有帮助。

【讨论】:

  • 对当前版本的良好解释。我最近才注意到你在这里的回答和注意到的乘法错误。该问题已记录在 here 中,现在已在 github 上的“开发”版本中修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 2020-06-16
  • 1970-01-01
  • 2014-01-10
  • 2012-10-09
  • 2015-08-27
相关资源
最近更新 更多