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

Jesper Särnesjö sarnesjo at gmail.com
Wed Mar 20 11:19:22 CET 2013


On Tue, Mar 19, 2013 at 8:18 AM, Albert Y. C. Lai <trebla at vex.net> wrote:
> On 13-03-18 09:19 AM, Jesper Särnesjö wrote:
>>
>> Interestingly, running the program in GHCi with the -fno-ghci-sandbox
>> flag, causes it to misbehave in the same way as when compiled:
>
> Then perhaps to mimic default ghci in hope of getting good results:
>
> - compile with -threaded (more candidly, link with -threaded, it does not
> change code generation)
>
> - in the program, deliberately move the work to a forkIO-thread

I did this [1], and sure enough - it did result in a GHC-compiled
program behaving like one run in GHCi:

    $ ghc -threaded -lglfw -framework OpenGL -fforce-recomp
glfw_test_threaded.hs && ./glfw_test_threaded
    [...]
    NVIDIA GeForce GT 330M OpenGL Engine

I should mention that I tried both forkIO and forkOS, as well as
marking the foreign imports as safe and unsafe, and, for my simple
test program, the result was the same every way. This is certainly not
the case in general, though, as the forkOS documentation explains [2].

I haven't yet worked out why simply moving all the work from the main
thread to another (OS) thread fixes this problem. Based on what I've
learned so far, that should, if anything, *prevent* things from
working correctly.

On that note, I did try to apply the same solution to a larger program
[3], and while it did allow the GPU switch to happen properly, it also
caused the program's window to become unresponsive. Like I mentioned
in my earlier email, Mac OS X (or rather Cocoa) is fussy about GUI
actions being run on the main thread.

So, in summary, using GLFW to initialize an OpenGL context fails to
trigger a GPU switch if done on the main thread, but works as expected
if done on a different thread, in a Haskell program. I need to figure
out *why* that is, so that I can find a way to simultaneously appease
Haskell and Cocoa.

Thanks for all your suggestions!

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

[1] https://gist.github.com/sarnesjo/5151894#file-glfw_test_threaded-hs
[2] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Concurrent.html#v:forkOS
[3] https://gist.github.com/sarnesjo/5116084#file-test-hs



More information about the Haskell-Cafe mailing list