Seg fault (Android NDK) using OpenMP with nested loops

General OpenMP discussion

Seg fault (Android NDK) using OpenMP with nested loops

Postby may » Tue Mar 04, 2014 3:36 pm

I'm trying to use OpenMP in my Android NDK project. I have a program where I receive frames from the camera - and carry out processing on 2D matrixes. I would like to parallelize the accessing and updating of each pixel value. I have the following code, where mat is the 2D matrix local to a processFrame function, however this causes a segmentation fault:

Code: Select all
unsigned y,x;
    #pragma omp parallel shared(mat) private(y,x)
            {
    #pragma omp for schedule(dynamic,1) collapse(2)
                //For each row
                for (y = 0U; y < _height; ++y){
                    // For each column
                    for ( x = 0U; x < _width; ++x){
                        mat.at<Vec3b>(y, x) = Vec3b(0, 0, 0);           // Background
                    }
                }
            }


At the start of the program, I am able to initialize the global matrix using OpenMP, as such:

Code: Select all
#pragma omp parallel for schedule(dynamic,1) collapse(2)
for (unsigned y = 0U; y < height; ++y)
{
    // For each column
    for (unsigned x = 0U; x < width; ++x)
    {
        // For each channel
        #pragma omp parallel for
        for (unsigned i = 0U; i < 3U; ++i)
            pixels[x][y].bgr[i] = pixels[x][y].mean_bgr[i] = 0.0F;

        pixels[x][y].scalar = pixels[x][y].pot = pixels[x][y].mean_pot = pixels[x][y].std_pot = 0.0F;
    }
}


The above code seems to work fine.

Note: I make a call to
Code: Select all
omp_set_nested(1);
in my main method. The variable
Code: Select all
mat
is an OpenCV
Code: Select all
Mat
object, and the method
Code: Select all
mat.at(x,y)
returns a pixel at the specified location. My OpenMP code is taken from samples, but doesn't seem to work. Is there a problem with calling
Code: Select all
mat.at(x,y)
within each thread? I'm not really sure what's wrong? Thanks for any help.
may
 
Posts: 3
Joined: Tue Mar 04, 2014 12:55 pm

Re: Seg fault (Android NDK) using OpenMP with nested loops

Postby MarkB » Wed Mar 05, 2014 5:52 am

I see nothing wrong with the OpenMP: does it work OK

  • without the OpenMP directives
  • with the OpenMP directives but on one thread only
  • without the collapse clause
MarkB
 
Posts: 432
Joined: Thu Jan 08, 2009 10:12 am

Re: Seg fault (Android NDK) using OpenMP with nested loops

Postby may » Wed Mar 05, 2014 7:37 am

1) It works fine without the OpenMP directives.

2) If I try to use one thread, using num_threas(1), it fails.

3) It fails if I don't use the collapse clause.

There seems to be something very strange. Any OpenMP directives fail within the
Code: Select all
processFrame()
function that I have. I tried something simple like this:

Code: Select all
   int p,i;
#pragma omp parallel num_threads(1)
   {
      p=5*5;
      i++;
      LOGD(TAG,"processFrame i:%d result:%d",i,p);

   }


but it fails. However, that same code works in my
Code: Select all
initialiseMatrix()
function. Called only once when the program starts. Is it failing because of the nature of processFrame() ? I.e. gets call whenever a frame is received.
may
 
Posts: 3
Joined: Tue Mar 04, 2014 12:55 pm

Re: Seg fault (Android NDK) using OpenMP with nested loops

Postby may » Wed Mar 05, 2014 12:46 pm

I think the issue lies with the Java Native Interface. Since my processFrame() function is a JNI function call, the JVM limits all processing within that thread to the main thread only (I think). I tried wrapping the processing - i.e. have a native function called by the JNI function, which uses a clone of the Mat image frame variable, but this also fails. I think it may not be possible to use OpenMP in this context. So my new strategy is multi-thread from the Java side - split the Mat image frame into blocks, and process each in a separate thread. I'm not sure if there is an easy-to-use OpenMP equivalent library in Java, but I'll look into Java concurrency. Thanks for your input!
may
 
Posts: 3
Joined: Tue Mar 04, 2014 12:55 pm

Re: Seg fault (Android NDK) using OpenMP with nested loops

Postby MarkB » Wed Mar 05, 2014 2:14 pm

may wrote:I think the issue lies with the Java Native Interface. Since my processFrame() function is a JNI function call, the JVM limits all processing within that thread to the main thread only (I think). I tried wrapping the processing - i.e. have a native function called by the JNI function, which uses a clone of the Mat image frame variable, but this also fails. I think it may not be possible to use OpenMP in this context. So my new strategy is multi-thread from the Java side - split the Mat image frame into blocks, and process each in a separate thread. I'm not sure if there is an easy-to-use OpenMP equivalent library in Java, but I'll look into Java concurrency. Thanks for your input!


You're welcome! I agree that using OpenMP inside a JNI call isn't guaranteed to work: it would require the JVM and the OpenMP runtime to play nicely together. There isn't an OpenMP equivalent for Java outside of research projects, but using Java threads (e.g. via an ExecutorService ) isn't too difficult!
MarkB
 
Posts: 432
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 6 guests

cron