openmp help

Hi

could someone guide me on installing openmp for visual studio 2010 and using it.

would the following work in executing the commands in parallel.
1
2
3
4
5
6
7
8
9
10
11
12
13
for(int i = 0; i < matrx1.rows;i++)
  {
    for(int j=0;j<matrx1.cols;j++)
      {
           #pragma opm parallel
           {
                segmentimage(grayscale);
                calibratecamera(grayscale);          
                   AND SO ON
             }
      }

}


thanks
Hi,

OpenMP should be included in VS2010 (I'm using it in VS2010 Ultimate without having to install anything).
To make it work you have to include <omp.h> and add the option "/openmp" to the compiler configuration ( Project Properties -> Configuration Properties -> C/C++ -> Command Line -> add "/openmp" to Additional Options).

Your code is not going to work. If you want to perform the operations of the inner loop to all elements of the array I would suggest something like this:

1
2
3
4
5
6
7
8
9
10
#pragma omp parallel for
for(int i = 0; i < matrx1.rows;i++)
{
	for(int j=0;j<matrx1.cols;j++)
	{
		segmentimage(grayscale);
		calibratecamera(grayscale);          
		// AND SO ON
	}
}


But you have to check for race conditions and all the other stuff.

For detailed information check out the OpenMP Specifications at http://openmp.org/wp/openmp-specifications/ .
Last edited on
so the code you provided will perform all functions in parallel
inside the loop, what if there was one function that was dependant on calibrate camera.

is there a way to make certain function work in parallel then the result pass it on to the next set of functions that need to executed in parallel.

thanks alot by the way.
i will look into this
closed account (Dy7SLyTq)
is there a way to make certain function work in parallel then the result pass it on to the next set of functions that need to executed in parallel.
i have never heard of openmp let alone use it, but based on your question its like sdl. but anyways thats a multi-threading question. im pretty sure all functions on a thread can still return a value, and you can just pass it by value to the next set when you are putting them on a thread

and look at line 9 of your code daniel.
Here is an example of my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(1)  //this is a loop for videocapture to process all the frames from the camera
{
   webcam >> image;
   Mat silhoette = imagesegmentation(image);
   Vector<Point2f> Corner = Cornerdetection(image);
   for(int i =corners.rows)
   {
     vector<point3f>.pushback( 3d coordinates in the array);
     drawcircle for each coordinate in array
   }
   calibratecamera;
   construct3darray using calibratecamera parameters;
   imshow(image);
   clear arrays;
 }
release webcam;
exit;

I would like to perform image segmentation and corner detection in parallel.
then after perform the loop which only would have 4 corners. then the next part takes a lot of computation.
so i would like to perform calibratecamera followed by construct3darray,
using the multi core processors from openmp to speed up execution time.

would having

#pragma omp parallel for
before the while loop help.

thanks
Last edited on
Hi

i am having problem installing, I did what you said but could not find omp.h file i have visual studio 2010 express.

i found omp.h and placed it in visual studio folder now it says cannot find vcomp100.dll

thanks
OpenMP is not included in the Express Edition of Visual Studio. But here is a workaround which probably works:
http://stackoverflow.com/questions/865686/openmp-in-visual-studio-2005-standard
Although MS VS only supports OpenMP 2.0 which was released in 2002.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
while(1)  //this is a loop for videocapture to process all the frames from the camera
{
   webcam >> image;
  #pragma omp parallel sections
  {
    #pragma omp section
    {   
      Mat silhoette = imagesegmentation(image);
    }
    #pragma omp section
    {
      Vector<Point2f> Corner = Cornerdetection(image);
    }
  }
   for(int i =corners.rows)
   {
     vector<point3f>.pushback( 3d coordinates in the array);
     drawcircle for each coordinate in array
   }
   calibratecamera;
   construct3darray using calibratecamera parameters;
   imshow(image);
   clear arrays;
 }
release webcam;
exit;


I added two sections to your code. Imagesegmentation and Cornerdetections will now run in parallel ( 2 threads ). This will speed up the application a bit. If you want to use OpenMP to really speed up your application you need the source code of all the functions ( imagesegmantation, cornerdetection, ... ). The directives must be added directly before the loops they apply to.

The corner detection function could look like this:

1
2
3
4
5
6
7
8
9
10
11
12
Vector<Point2f> Cornerdetection(CImage image)
{
  #pragma omp parallel for
  for ( int i=0; i < imageHeight; i++ )
  {
    for ( int j=0; j<imageWidth; j++ )
    {
      // Do the work on each pixel
    }
  }
  return edgePoints;
}


Cornerdetection will run in parallel and returns the Vector with the edges you can use in your sequential code ( this is called "fork-join" ).
Here is what my image segmentation function looks like, so i can make it faster by the following code.

1
2
3
4
5

#pragma omp section
    {   
      Mat silhoette = imagesegmentation(image);
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Mat imagesegmentation(Mat image)
{
   #pragma omp parallel for     
    for(int i = 0; i < image.rows;i++)
    {
         for(int j=0; j<image.cols;j++)	//in case it fails changed it from i=1 to i=0
	{
		pixel= image.at<Vec3b>(i,j);	//prints wierd characters
		mdist=mahadistance(icovar,meanmat,pixel);
		if(mdist<distance)
			mask.at<uchar>(i,j)=255;
		else
			mask.at<uchar>(i,j)=0;
	}
     }
     return mask;
}


and for the cornerdetection, its a very straight forward function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector<Point2f> Cornerdetect(Mat image)
{
#pragma omp parallel sections
  {
    #pragma omp section
    {   
   cvtColor(image,grayscale,CV_BGR2GRAY);
		//goodfeatures(grayimage, output to store corners, quality factor, distance factor)
		goodFeaturesToTrack(grayscale,corners,numcorners,0.1,100);   //good so far 0.1 and 100 also 0.01 and 100 a little ok i chose this
		// Mark these corners on the original image
		cornerSubPix(grayscale, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
return corners;
     }
   }
}


would something like the above work. so to make something work in parallel have pragma omp sections and pragma omp around that function and pragma omp parallel for for loops.

nice
thanks a lot.
just trying the openmp solution you posted and will let you know how that goes.

really helps
Last edited on
The imagesegmentation function could work. It seems like you declared the variables 'pixel' and 'mdist' globally. By default variables declared outside of the OpenMP parallel environment are 'shared', that means that all threads access the same memory and that can lead to failures. You could mark the two variables as private or declare them inside the loop. I changed the code to the first solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Mat imagesegmentation(Mat image)
{
   #pragma omp parallel for private( pixel, mdist )
    for(int i = 0; i < image.rows;i++)
    {
         for(int j=0; j<image.cols;j++)	//in case it fails changed it from i=1 to i=0
	{
		pixel= image.at<Vec3b>(i,j);	//prints wierd characters
		mdist=mahadistance(icovar,meanmat,pixel);
		if(mdist<distance)
			mask.at<uchar>(i,j)=255;
		else
			mask.at<uchar>(i,j)=0;
	}
     }
     return mask;
}


Look here ( http://bisqwit.iki.fi/story/howto/openmp/ ) for the basics.

To parallelize the edge detection you can compile OpenCV on your own with support for OpenMP or split the image in several parts which run in parallel. I don't know the best solution because I've never worked with OpenCV.

If OpenMP doesn't work with your VS you could use MinGW to create a DLL with all the functions using OMP. MinGW also supports OMP 3.0 which has some new usefull features (e.g. tasks) you can't use in VS.
Hi

mdist and pixels are variables, which i didn't declare globally just forgot to include it in the function as it is only used by this function.

there are no global variables that are needed by this function, except the image which is passed through.

so would it work if i declare those variables within the pragma omp for braces

i.e.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Mat imagesegmentation(Mat image)
{
   #pragma omp parallel for     
    double mdist=0;
    Vec3b pixel;
    for(int i = 0; i < image.rows;i++)
    {
         for(int j=0; j<image.cols;j++)	//in case it fails changed it from i=1 to i=0
	{
		pixel= image.at<Vec3b>(i,j);	//prints wierd characters
		mdist=mahadistance(icovar,meanmat,pixel);
		if(mdist<distance)
			mask.at<uchar>(i,j)=255;
		else
			mask.at<uchar>(i,j)=0;
	}
     }
     return mask;
}


it is only used in this function.

as for the edge detection, the functions inside don't have to run in parallel just would like it to execute faster. maybe compile with openmp support if that will help execute it faster. by openmp support you mean go to project properties, c/c++, language and yes to openmp support and i can also leave it as execute the entire function itself in parallel with imagesegmentation from the int main, the corner detection speed is good its only the segmentation which takes most time to execute.

i found this site
http://openmp.org/mp-documents/omp-hands-on-SC08.pdf

which i found quite usefull.

thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Mat imagesegmentation(Mat image)
{
   #pragma omp parallel for
    for(int i = 0; i < image.rows;i++)
    {
         for(int j=0; j<image.cols;j++)	//in case it fails changed it from i=1 to i=0
	{
		Vec3b pixel= image.at<Vec3b>(i,j);	//prints wierd characters
		double mdist=mahadistance(icovar,meanmat,pixel);
		if(mdist<distance)
			mask.at<uchar>(i,j)=255;
		else
			mask.at<uchar>(i,j)=0;
	}
     }
     return mask;
}

int main()
{
  // Some Functions

  #pragma omp parallel sections
  {
    #pragma omp section
    { Mat silhoette = imagesegmentation(image); }
    #pragma omp section
    { Vector<Point2f> Corner = Cornerdetection(image); }
  }

  // Some Functions
}


This code is calling both the image segmentation and the edge detection in parallel. The image segmentation function itself will create threads that execute the segmentation. That sould do it.
Nice, thanks

I followed the instructions for installing openmp


After some research I found out that the OpenMP libs and dlls are not included with Visual C++ 2005 or Visual C++ Express Edition 2008. But with a few workarounds you can get it working.

First you need to download the lib files from microsoft which can be found at the Windows SDK for Windows Server 2008 and .NET framework 3.5. After you download it you need to make sure that either vcomp.lib or vcompd.lib is being linked to your program.

Next you need to have the dll which can be found in the Visual C++ Redistributable Packkage. Then make sure that vcomp90.dll is somewhere in your path.

You also need to have the OpenMP compiler option enabled which can be found in the Properties->C/C++/Language menu of Visual C++.

After that you should be able to use OpenMP just fine.


I download SDK, i have already vcomp.lib and vcomd.lib, do i link it through the property sheets debug and release or do i link it another way.

I installed the redistributable package but could not find vcomp90.dll i have already enable openmp support.

The error says can't find vcomp100.dll when i try executing the program.
I will see if updating the direct x run time will work.

any other suggestions?
thanks
Maybe you need the "Visual C++ Redistributable Packkage" for VS 2010.

x86: http://www.microsoft.com/en-us/download/details.aspx?id=5555
x64: http://www.microsoft.com/en-us/download/details.aspx?id=14632

what if i install visual studio 2012 pro version, would that have openmp support.

would it be a long procedure to set it up like visual c++ 2010 like link opencv 2.4, add dependencies and additional libraries and would it be able to run my programs.

if you can get it to work for visual c++ 2010 express it would be ideal or another workaround.
Last edited on
OpenMP is supported in VS2010 Profrssional, so I think that VS2012 Professional also supports OMP.

Normally you can open the solution or project in the newer version without any additional work. So that wold be a solution.
I have downloaded a lot of times, i have 4 copies of it on my computer, it is the one i want, but after installation, it doesn't copy the files it is supposed to copy, vcomp100.dll is in that file, but it does not copy it to my computer.

if i install vs2012 or v c++ 2012 or any other besides express or standard, if i have linked opencv to vc++ would i have to link it again with the new installed software.

i prefer the redist package to getting to work, if not then i will have to install v c++ 2010 pro of 2012 pro i think they have openmp support and then i will have to do the opencv linking.
Ok. Is it possible to extract the DLL from the installation file? Then you could copy it to your workind directory (or the PATH) and try it.

If that doesn't work you can download the dll from http://www.dllme.com/dll/files/vcomp100_dll.html.

If that doesn't work you should try it with VC++ 2010/2012 Pro.
Hi

So copied the dll file from that site and also found redist package sp1 vc++ 2010 which seems to be different to the one i downloaded last time which was not sp1 and i ran the following code for testing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <omp.h>

int main()
{
	#pragma omp parallel
	{
		int ID = 0;
		printf("Hello %d", ID);
		printf("World %d\n", ID);
	}
	system("PAUSE");
	return 0;
}


prints out
Hello0world0
Hello0World0

It works, thanks sooo much for your help

in case someone has the same problem

go to your link for the dll and maybe the link below
http://www.microsoft.com/en-us/download/confirmation.aspx?id=8328

one final comment:
i thought it would output

Hello0World0 only once

thanks again soo much for your help.

when i finish my code could you check it for me if i have used openmp properly final time?

;)
Last edited on
You can use the omp_get_thread_num() function to get the id of the thread. So the output of the following code will be:

Hello from thread 0
Hello from thread 1

(or the other way around)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <omp.h>

int main()
{
	#pragma omp parallel
	{
		int tid = omp_get_thread_num();
		printf("Hello from thread %d\n", tid);
	}
	system("PAUSE");
	return 0;
}


And the printf-function will be called by each thread so if you have a dual-core CPU the output is correct.

I will have a look when you are done.

I'm glad I could help!
Just sent you a pm
code too big to post here

thanks
Topic archived. No new replies allowed.