[Haskell-cafe] Compiled program using OpenGL fails to trigger GPU switch on Mac, but works in GHCi

Jesper Särnesjö sarnesjo at gmail.com
Sun Mar 17 02:53:59 CET 2013


On Thu, Mar 14, 2013 at 12:51 AM, Jesper Särnesjö <sarnesjo at gmail.com> wrote:
> In short, I have two programs, one written in Haskell [1] and one
> written in C [2], that consist of calls to the same functions, in the
> same order, to the same C library, but which do not exhibit the same
> behavior. Further, the Haskell program behaves differently when
> compiled using GHC, and when run in GHCi. I would like to know why
> this is, and how to fix it.

To be clear, I think this isn't really an OpenGL problem, but rather
one related to FFI or event handling. If anyone could explain to me,
in general, how and why a call to a foreign function returning IO ()
might cause different behavior in Haskell than in C, that might help
me track down the problem.

I've updated my test programs to use glGetString [3] to check which
renderer is active. On my machine, it should return "NVIDIA GeForce GT
330M OpenGL Engine" if rendering happens on the discrete GPU, and
"Intel HD Graphics OpenGL Engine" or "Apple Software Renderer"
otherwise. These are the results of running the C and Haskell programs
in various ways:

    $ gcc -lglfw -framework OpenGL glfw_test.c && ./a.out
    NVIDIA GeForce GT 330M OpenGL Engine

    $ ghc -lglfw -framework OpenGL -fforce-recomp glfw_test.hs && ./glfw_test
    [...]
    Apple Software Renderer

    $ runhaskell -lglfw glfw_test.hs
    NVIDIA GeForce GT 330M OpenGL Engine

    $ ghci -lglfw glfw_test.hs
    [...]
    Prelude Main> main
    NVIDIA GeForce GT 330M OpenGL Engine

The C program behaves as expected, as does the Haskell one when run
using runhaskell or GHCi. Only the Haskell program compiled using GHC
behaves incorrectly. Again, the OS event that signifies that a GPU
switch has occurred fires either way, but for the compiled Haskell
program, it fires with roughly a second's delay. Why would that be?

-- 
Jesper Särnesjö
http://jesper.sarnesjo.org/

[1] https://gist.github.com/sarnesjo/5151894#file-glfw_test-hs
[2] https://gist.github.com/sarnesjo/5151894#file-glfw_test-c
[3] http://www.opengl.org/sdk/docs/man3/xhtml/glGetString.xml



More information about the Haskell-Cafe mailing list