【问题标题】:How to implement correctly Foreign.Storable to create Vectors?如何正确实现 Foreign.Storable 以创建向量?
【发布时间】:2018-08-22 01:55:51
【问题描述】:

我一直在从事一个使用列表来计算人工神经网络操作的项目。现在,我想将其转换为Data.Vector 以提高其效率。但是,我目前在实现Foreign.Storable 以获得必要的定义时遇到了麻烦。

好的,首先,我使用Data.Array.Repa 进行计算,其类型同义词如下 (CAMTypes.hs):

type NTTVU = Array U VShape NTT

后来我想用Data.Vector如下:

import qualified Data.Vector.Storable as VS
-- ...
trs = createThreshold 3 0 [9,8,7] :: NTTVU

但是,我收到以下错误:

$> VS.singleton trs
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
  error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
  undefined, called at ./Data/Vector/Storable/Mutable.hs:131:22 in vector-0.12.0.1-JlawpRjIcMJIYPJVsWriIA:Data.Vector.Storable.Mutable
CallStack (from -prof):
  Data.Vector.Storable.Mutable.basicUnsafeNew.size (Data/Vector/Storable/Mutable.hs:131:7-36)
  Data.Vector.Storable.Mutable.basicUnsafeNew (Data/Vector/Storable/Mutable.hs:(124,3)-(132,38))
  Data.Vector.Storable.Mutable.CAF:lvl12_r7YZK (<no location info>)

我已经完成了instance Storable NTTVU where 的实现,但仍然抱怨;我启用了分析,但没有告诉我更多信息;尝试按步骤调试,但也没有成功。希望有人知道很多关于它以及如何解决它。

更多上下文,the whole project,以及相关文件:

【问题讨论】:

    标签: haskell vector repa storable


    【解决方案1】:

    我发现了问题:sizeOf

    当我实现它时,它依赖于vec 存在并具有定义的值,但是,事实并非如此。 Vector 可存储实现应该事先知道内存大小,这就是它产生错误的原因。

    所以,目前我已将大小固定为“4”,但如果您想要运行时分配,请不要使用它。

    这是该解决方案的提交:

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      相关资源
      最近更新 更多