Skip to content

Non-deterministic behavior with FFI

I have a simple perceptron learning algorithm in C++ which I am calling from Haskell via FFI. Most of the time it works perfectly, but occasionally the results it produces are not the expected. It seems to happen more often if there is another memory/cpu intensive process running on the machine.

The same never happens when I call the same function from C++, which seems to indicate the problem is related to GHC.

I attach a simplified extract of the code which will show the problem.

I included:

  • c_perceptronmodel.cpp, c_perceptronmodel.h : C++ implementation
  • test.hs : Haskell program which calls the C++ function via FFI
  • test.cpp : Equivalent C++ program which calls the same C++ function
  • run.sh : shell script which will repeatedly run a program and check if any consecutive two runs produce different results
  • train : data file which the programs process

I compiled using GHC 6.10.4 like this:

ghc-6.10.4  --make -O2 -o test-ghc-6.10.4 test.hs c_perceptronmodel.cpp -lstdc++

To see the problem execute:

./run.sh ./test-ghc-6.10.4
Trac metadata
Trac field Value
Version 6.10.4
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (FFI)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture Unknown/Multiple
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information