SDL and OpenMP

Hello

I get a segfault when running even the most simple SDL program when using
OpenMP.

#include <SDL/SDL.h>

int main()
{
#pragma omp parallel num_threads(2)
{
SDL_Init(SDL_INIT_VIDEO);
}
return(0);
}

I compile with “g++ program.cpp -lSDL -fopenmp -g”, on Debian Sid:
g++ 4:4.3.1-2
libsdl1.2-dev 1.2.13-2

I want to have two threads that plot independently to different surfaces using
OpenGL, and I want to do this by using OpenMP. But even the code program
above segfaults.

Here is the output from valgrind:

==7697== Memcheck, a memory error detector.
==7697== Copyright © 2002-2007, and GNU GPL’d, by Julian Seward et al.
==7697== Using LibVEX rev 1854, a library for dynamic binary translation.
==7697== Copyright © 2004-2007, and GNU GPL’d, by OpenWorks LLP.
==7697== Using valgrind-3.3.1-Debian, a dynamic binary instrumentation
framework.
==7697== Copyright © 2000-2007, and GNU GPL’d, by Julian Seward et al.
==7697== For more details, rerun with: -v
==7697==
==7697== Jump to the invalid address stated on the next line
==7697== at 0x0: ???
==7697== by 0x4166D5B: (within /usr/lib/libSDL-1.2.so.0.11.1)
==7697== by 0x41549F5: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.1)
==7697== by 0x4128722: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.1)
==7697== by 0x4128786: SDL_Init (in /usr/lib/libSDL-1.2.so.0.11.1)
==7697== by 0x8048641: main.omp_fn.0
(in /home/tmac/programming/visualization/sdl/snowman.elf)
==7697== by 0x8048688: main
(in /home/tmac/programming/visualization/sdl/snowman.elf)
==7697== Address 0x0 is not stack’d, malloc’d or (recently) free’d
==7697==
==7697== Process terminating with default action of signal 11 (SIGSEGV)
==7697== Bad permissions for mapped region at address 0x0
==7697== at 0x0: ???
==7697== by 0x4166D5B: (within /usr/lib/libSDL-1.2.so.0.11.1)
==7697== by 0x41549F5: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.1)
==7697== by 0x4128722: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.1)
==7697== by 0x4128786: SDL_Init (in /usr/lib/libSDL-1.2.so.0.11.1)
==7697== by 0x8048641: main.omp_fn.0
(in /home/tmac/programming/visualization/sdl/snowman.elf)
==7697== by 0x8048688: main
(in /home/tmac/programming/visualization/sdl/snowman.elf)
==7697==
==7697== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 65 from 1)
==7697== malloc/free: in use at exit: 416 bytes in 7 blocks.
==7697== malloc/free: 7 allocs, 0 frees, 416 bytes allocated.
==7697== For counts of detected errors, rerun with: -v
==7697== searching for pointers to 7 not-freed blocks.
==7697== checked 9,049,520 bytes.
==7697==
==7697==
==7697== 152 bytes in 1 blocks are possibly lost in loss record 5 of 5
==7697== at 0x4021E22: calloc (vg_replace_malloc.c:397)
==7697== by 0x4010C7B: _dl_allocate_tls (in /lib/ld-2.7.so)
==7697== by 0x42FF672: pthread_create@@GLIBC_2.1
(in /lib/i686/cmov/libpthread-2.7.so)
==7697== by 0x42EAA0B: (within /usr/lib/libgomp.so.1.0.0)
==7697== by 0x42EA2BE: GOMP_parallel_start (in /usr/lib/libgomp.so.1.0.0)
==7697== by 0x804867C: main
(in /home/tmac/programming/visualization/sdl/snowman.elf)
==7697==
==7697== LEAK SUMMARY:
==7697== definitely lost: 0 bytes in 0 blocks.
==7697== possibly lost: 152 bytes in 1 blocks.
==7697== still reachable: 264 bytes in 6 blocks.
==7697== suppressed: 0 bytes in 0 blocks.
==7697== Reachable blocks (those to which a pointer was found) are not shown.
==7697== To see them, rerun with: --leak-check=full --show-reachable=yes
Killed

and the output from gdb after having compiled with -ggdb:

This GDB was configured as “i486-linux-gnu”…
(gdb) run
Starting program: /home/tmac/programming/visualization/sdl/snowman.elf
[Thread debugging using libthread_db enabled]
[New Thread 0xb7866900 (LWP 7693)]
[New Thread 0xb7865b90 (LWP 7696)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7866900 (LWP 7693)]
0x00000000 in ?? ()

Best regards
Torquil S?rensen

Hi!Am Montag, 7. Juli 2008 13:55 schrieb Torquil Macdonald S?rensen:

I get a segfault when running even the most simple SDL program when using
OpenMP.

#include <SDL/SDL.h>

int main()
{
#pragma omp parallel num_threads(2)
{
SDL_Init(SDL_INIT_VIDEO);
}
return(0);
}

AFAIK SDL is not that threadsafe. You shouldn’t initialise a subsystem twice,
especially not from different threads. Use OpenMP where it makes sense, that
is, to speed up time-consuming calculations.

Regards,
Matthias


Matthias Bach

Hi!

I get a segfault when running even the most simple SDL program when using
OpenMP.

#include <SDL/SDL.h>

int main()
{
#pragma omp parallel num_threads(2)
{
SDL_Init(SDL_INIT_VIDEO);
}
return(0);
}

AFAIK SDL is not that threadsafe. You shouldn’t initialise a subsystem
twice, especially not from different threads. Use OpenMP where it makes
sense, that is, to speed up time-consuming calculations.

Regards,
Matthias

Thanks Matthias. What I have is a Monte Carlo simulation that runs
independently on all the cores on a CPU, using OpenMP. But I also need to see
a visualization of the simulation for each thread. So each thread will be
doing some intensive computations, and plot in its own window once in a
while. So I wanted SDL to run independently on each thread. I read somewhere
that SDL can write to a single SDL surface from different threads as long as
one took care of using mutexes in the correct way, so I thought that what I
want should also be possible, since it is much simpler (every thread is
independent).

Best regards,
Torquil S?rensenOn Monday 07 July 2008 14:45:56 Matthias Bach wrote:

Am Montag, 7. Juli 2008 13:55 schrieb Torquil Macdonald S?rensen:

Hi!

I get a segfault when running even the most simple SDL program when using
OpenMP.

#include <SDL/SDL.h>

int main()
{
#pragma omp parallel num_threads(2)
{
SDL_Init(SDL_INIT_VIDEO);
}
return(0);
}

AFAIK SDL is not that threadsafe. You shouldn’t initialise a subsystem
twice, especially not from different threads. Use OpenMP where it makes
sense, that is, to speed up time-consuming calculations.

Regards,
Matthias

I see that SDL has an SDL_thread.h with some thread-related functions, so I
will fiddle around a bit with that. Maybe I can come up with something that
works.

Best regards,
Torquil S?rensenOn Monday 07 July 2008 14:45:56 Matthias Bach wrote:

Am Montag, 7. Juli 2008 13:55 schrieb Torquil Macdonald S?rensen:

Hi,

if I recall correctly, only one thread can safely access the display
surface, even if you protect the accesses to it.

Just check the archives, there are plenty of discussions about it.–
Paulo

Quoting Torquil Macdonald S?rensen :

On Monday 07 July 2008 14:45:56 Matthias Bach wrote:

Hi!

Am Montag, 7. Juli 2008 13:55 schrieb Torquil Macdonald S?rensen:

I get a segfault when running even the most simple SDL program when using
OpenMP.

#include <SDL/SDL.h>

int main()
{
#pragma omp parallel num_threads(2)
{
SDL_Init(SDL_INIT_VIDEO);
}
return(0);
}

AFAIK SDL is not that threadsafe. You shouldn’t initialise a subsystem
twice, especially not from different threads. Use OpenMP where it makes
sense, that is, to speed up time-consuming calculations.

Regards,
Matthias

I see that SDL has an SDL_thread.h with some thread-related functions, so I
will fiddle around a bit with that. Maybe I can come up with something that
works.

Best regards,
Torquil S?rensen


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


This message was sent using IMP, the Internet Messaging Program.

Thanks Matthias. What I have is a Monte Carlo simulation that runs
independently on all the cores on a CPU, using OpenMP. But I also need to see
a visualization of the simulation for each thread. So each thread will be
doing some intensive computations, and plot in its own window once in a
while. So I wanted SDL to run independently on each thread. I read somewhere
that SDL can write to a single SDL surface from different threads as long as
one took care of using mutexes in the correct way, so I thought that what I
want should also be possible, since it is much simpler (every thread is
independent).

since you are not ploting that much, perhaps you could have sdl in the
primary thread only, then have 4 threads with openmp. when the work
thread needs to plot, have it push an event, or copy what needs to be
plotted to the main event loop. the main window would have 4 quadrants.

or lock on of the 4 work threads to update a quadrant. that might work
depending what needs to be updatad on the screen.

this is just a guess as i was reading the email. not sure if this will
work for you. not sure how much detail your plots have.

matt

since you are not ploting that much, perhaps you could have sdl in the
primary thread only, then have 4 threads with openmp. when the work thread
needs to plot, have it push an event, or copy what needs to be plotted to
the main event loop. the main window would have 4 quadrants.

I think that for cache issues it will be better to have 4 stripes than 4
quadrants, you should also be sure to use a software surface.

or lock on of the 4 work threads to update a quadrant. that might work

depending what needs to be updatad on the screen.

I’ve read only a few about openMP but for what I remember the parallelism of
the thread is totally masked so he will not have to care about waiting
threads before doing SDL_Flip/SDL_UpdateRects.

Anyway using multithread to blit a surface it’s usually not so useful except
if you do VERY heavy computations for each pixel (ray tracing for instance).
Usually the bottleneck it’s not the CPU but the memory bandwidth.–
Bye,
Gabry

Hallo, thanks for your suggestions. But since I wanted to have several
independent windows, I chose to implement it using SFML. It was still not
trivial to parallelize the way I wanted, but I managed it in the end. I have
used OpenMP so it reduces to a “one thread” program when compiling without
it. I have also written an almost equivalent version using freeglut, but I
think there is a limitation there in that I can not pass the data I want to
the callback functions. Anyway, I will go for the SFML version in my
simulation program.

Thanks again,
Torquil S?rensenOn Tuesday 08 July 2008 10:23:47 Gabriele Greco wrote:

since you are not ploting that much, perhaps you could have sdl in the
primary thread only, then have 4 threads with openmp. when the work
thread needs to plot, have it push an event, or copy what needs to be
plotted to the main event loop. the main window would have 4 quadrants.

I think that for cache issues it will be better to have 4 stripes than 4
quadrants, you should also be sure to use a software surface.

or lock on of the 4 work threads to update a quadrant. that might work

depending what needs to be updatad on the screen.

I’ve read only a few about openMP but for what I remember the parallelism
of the thread is totally masked so he will not have to care about waiting
threads before doing SDL_Flip/SDL_UpdateRects.

Anyway using multithread to blit a surface it’s usually not so useful
except if you do VERY heavy computations for each pixel (ray tracing for
instance). Usually the bottleneck it’s not the CPU but the memory
bandwidth.