我有一些代码在没有并行化的情况下运行没有问题。但是,如果我尝试使用PSeq
而不是Seq
运行异常,则相同的代码会生成异常。我得到的消息看起来有点随机,很难完全复制。
这是代码。发生异常时,突出显示以let tmp2
开头的三行。
let frameToRMatrix (df: Frame<'R,string>) =
let foo k df : float list =
df
|> Frame.getCol k
|> Series.values
|> List.ofSeq
let folder acc k = (k, foo k df |> box) :: acc
let tmp =
List.fold folder [] (df.ColumnKeys |> List.ofSeq)
|> namedParams
let sd = df |> Frame.getCol "Vol0" |> Series.lastValue
let sd = sd * 1000.0 |> int
printfn "%s" "I was here"
let rand = System.Random(sd)
let rms = rand.Next(500)
System.Threading.Thread.Sleep rms
let tmp2 =
tmp
|> R.cbind // This line prints something on the console the first time it is executed
printfn "%s" "And here too"
tmp2
上面的代码包括随机数生成和对System.Threading.Thread.Sleep
的调用。如果我不包含在顺序执行中不需要的代码,我会收到一条消息:
System.ArgumentException: 'An item with the same key has already been added.'
以及控制台上的以下内容:
I was here
I was here
[1] 4095
因此执行永远不会进入And here too
行。
当我包含随机数生成器和对sleep
的调用时,我会得到不同的结果,这似乎取决于构建选项。
以下是四个示例,包含构建选项,错误消息以及我在控制台上看到的内容。请注意,在所有示例中,有I was here
的四个实例,但只有And here too
的三个实例。
---------------------------------------
Any CPU with Prefer 32-bit checked
System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'
I was here
I was here
[1] 4095
And here too
I was here
And here too
I was here
And here too
Warning: stack imbalance in 'lazyLoadDBfetch', 66 then 65
Error in value[[3L]](cond) : unprotect_ptr: pointer not found
---------------------------------------
Any CPU with Prefer 32-bit unchecked
System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'
I was here
I was here
[1] 1.759219e+13
And here too
I was here
And here too
I was here
And here too
Error: cons memory exhausted (limit reached?)
Error: cons memory exhausted (limit reached?)
---------------------------------------
x86
System.AccessViolationException" 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
I was here
I was here
[1] 4095
And here too
I was here
And here too
I was here
And here too
---------------------------------------
x64
Exception thrown: 'System.AccessViolationException' in Unknown Module. Attermpted to read or write protected memory.
$$$ - MachineLearning.signal: Calculating signal for ticker AAPL
$$$ - MachineLearning.signal: Calculating signal for ticker AAPL
I was here
I was here
[1] 1.759219e+13
And here too
I was here
And here too
I was here
And here too
Error in loadNamespace(name) :
no function to return from, jumping to top level
答案 0 :(得分:1)
根据我在R类型提供程序中调试线程的微妙问题的经验,我认为答案是否定的 - 遗憾的是,R本机互操作层不是线程安全的,因此您无法从F#中的多个线程调用它应用
我认为并行运行R的标准方法是生成多个R.exe
进程来完成工作。我不认为您可以轻松地从F#初始化多个独立的R进程,所以最好的办法是创建多个.NET进程,每个进程控制一个R引擎。