简体   繁体   English

如何正确实现 Foreign.Storable 来创建 Vectors?

[英]How to implement correctly Foreign.Storable to create Vectors?

I have been working in a project that has been using lists to calculate artificial neural network operations.我一直在从事一个项目,该项目一直使用列表来计算人工神经网络操作。 Now, I would like to transform that to Data.Vector to improve its efficiency.现在,我想将其转换为Data.Vector以提高其效率。 However, I have been having a trouble at the moment to implement Foreign.Storable for a definition that is essential.但是,目前我在为一个必不可少的定义实现Foreign.Storable遇到了麻烦。

Ok, so first, I have used Data.Array.Repa for the calculations, with a type synonym like the following ( CAMTypes.hs ):好的,首先,我使用Data.Array.Repa进行计算,其类型同义词如下( CAMTypes.hs ):

type NTTVU = Array U VShape NTT

Then later I wanted to use Data.Vector as follows:后来我想使用Data.Vector如下:

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

However, I get the following error:但是,我收到以下错误:

$> 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>)

I have already done the implementation for instance Storable NTTVU where , but still complains;我已经完成了instance Storable NTTVU where的实现,但仍然抱怨; I enabled profiling, but does not tell me more about it;我启用了分析,但没有告诉我更多关于它的信息; tried debugging by steps but no success as well.尝试按步骤调试,但也没有成功。 Hopefully someone knows a lot about it and how to solve it.希望有人知道很多关于它以及如何解决它。

For more context, the whole project , and the related files:有关更多上下文, 整个项目和相关文件:

I found the problem: sizeOf .我发现了问题: sizeOf

As I was implement it, it relied on vec to exist and have defined values, however, that's not the case.当我实现它时,它依赖于vec存在并定义了值,但是,情况并非如此。 The memory size should be known to the Vector storable implementation before hand, which is why it was creating the error. Vector 可存储实现应该事先知道内存大小,这就是它产生错误的原因。

So, for now I have fixed the size to "4", but do not use that if what you want is to have a runtime allocation.所以,现在我已将大小固定为“4”,但如果您想要的是运行时分配,请不要使用它。

Here is the commit for that solution:这是该解决方案的提交:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM