[Magick++] Multi-threading in OS X: bus error

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
DrLex

[Magick++] Multi-threading in OS X: bus error

Post by DrLex »

I use Magick++ to load images in a C++ program. The usage of Magick++ is limited to the following chunk of code:

Code: Select all

try {
  Image image( sFileName.c_str() );
  image.type( GrayscaleType );
  int imgWidth = image.columns();
  int imgHeight = image.rows();
  unsigned char *imageData = new unsigned char[imgWidth*imgHeight];
  image.write(0,0, imgWidth, imgHeight, "I", MagickCore::CharPixel, imageData);
}
catch( Exception e ) { ...
Of course this piece of code is simplified and would be useless as-is. In reality imgWidth, imgHeight and imageData are members of a class.

The global structure of the program is as follows:
1. Load a reference image and store its pixels in a buffer.
2. Start multiple independent threads (through pthreads) that each do the following:
a. Load an image (using the above chunk of code) from a semaphore-protected queue.
b. Do some operations on the image image pixels and compare them with the reference buffer.
c. Report the comparison score in a semaphore-protected score array.
d. Back to a. or exit if the queue is empty.

This program has been running perfectly for years in both Linux and Mac OS X. It still runs perfectly in Linux, but since I moved from ImageMagick 6.4.1 to 6.5.8 in OS X (installed through Fink), it crashes with a bus error whenever I try to run the program with more than one thread. The crash occurs when the second thread tries to execute the Image() call in the chunk of code given above:

Code: Select all

(gdb) backtrace
#0  gomp_resolve_num_threads (specified=0) at /var/tmp/gcc/gcc-5646.1~2/src/libgomp/parallel.c:48
#1  0x00276c22 in omp_get_max_threads () at /var/tmp/gcc/gcc-5646.1~2/src/libgomp/parallel.c:96
#2  0x000f1c15 in AcquirePixelCache ()
#3  0x001aec2c in AcquireImage ()
#4  0x0005adc6 in Magick::ImageRef::ImageRef ()
#5  0x00056219 in Magick::Image::Image ()
Now, I haven't looked at Magick++ in years and I have noticed that a lot has changed. I found that I'm supposed to call InitializeMagick() before doing anything, but the bus error still occurs when I do that (and in Linux the program happily runs without InitializeMagick). Do I need to do some other kind of initialization or is this 'simply' a bug somewhere?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: [Magick++] Multi-threading in OS X: bus error

Post by magick »

Try setting the OMP_NUM_THREADS environment variable to 1. If that fails, you can try building ImageMagick without OpenMP support (add --disable-openmp to the configure script command line). Also try getting ImageMagick 6.6.1-0 from Mac Ports. It has a number of patches concerning OpenMP which may fix the problem.
DrLex

Re: [Magick++] Multi-threading in OS X: bus error

Post by DrLex »

magick wrote:Try setting the OMP_NUM_THREADS environment variable to 1. If that fails, you can try building ImageMagick without OpenMP support (add --disable-openmp to the configure script command line). Also try getting ImageMagick 6.6.1-0 from Mac Ports. It has a number of patches concerning OpenMP which may fix the problem.
I have tried the OMP_NUM_THREADS=1, but it has no effect.
Building a custom ImageMagick outside Fink, or installing MacPorts to get IM 6.6.1 is a bit too much hassle for what it's worth (after all, the Mac has only two cores). I'll see if it's easy to hack the Fink package to rebuild without OpenMP. Otherwise I guess I'll run the program single-threaded until Fink's ImageMagick package is updated. At any rate, thanks for the suggestions.

Edit: I managed to add --disable-openmp to the configure options in the Fink package, and this did the trick. Thanks!
Post Reply