how to use openmp for making parallel kmeans ?

asked 2016-04-04 02:22:42 -0500

sumit patel gravatar image

I tried with different directives of openmp, but i can't get difference. Or i get errors. (access violation). I am using ms visual studio 2012 (v110) on windows 8.1 . I tried for basic openmp program after changing in configuration of vs 2012. And i got 8 line output. Without enable openmp support, i got one line output. I placed my code here, Please suggest me , where i can use openmp and how.

#include <opencv/highgui.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv\cv.h>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\imgcodecs.hpp>
#include <opencv2\core\core.hpp>
#include <opencv\cvaux.hpp>
#include <fstream>
#include <ctime>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <windows.h>
#include <atlstr.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <omp.h>

using namespace cv;
using namespace cv::ml;
using namespace std;

void printDim(Mat mat,const char *name)
{
    std::cout<<name<<" :"<<mat.rows<<"x"<<mat.cols<<"  depth :"<<mat.depth()<<"  channel :"<<mat.channels()<<std::endl;
}

void readData(cv::Mat& data, int cl)
{
    vector<const char *> filenames;
    filenames.push_back("Data/ik147_1.txt");
    filenames.push_back("Data/ik147_2.txt");
    filenames.push_back("Data/ik147_3.txt");
    vector<Mat> raw_data(cl);//= new std::vector<cv::Mat>(4);
    int row;
    int i;
    double  min,max;

    #pragma omp parallel for 

    for( i =0;i<cl;i++)
    {
        ifstream file( filenames[i] );
        while( file>>row )
        {
            raw_data[i].push_back(row);
        }
        minMaxLoc(raw_data[i],&min,&max);
      cout<<filenames[i]<<" min :"<<min<<", max :"<<max<<std::endl;

    }
    int N=raw_data[0].rows;
   // cv::Mat data(N,3,CV_32FC1);
    cout<<"Number of data (row) : "<< N << endl;
    int columns_to_read=cl;
    data.create(N,columns_to_read,CV_32FC1);

    //#pragma omp parallel for private(i) 
    for( i=0;i<columns_to_read;i++)
    {
        raw_data[i](Rect(0,0,1,N)).copyTo(data(Rect(i,0,1,N)));
    }

}

void resize1(Mat src, string name, string name1)
{
    Size size(512,512);//the dst image size,e.g.100x100
    Mat dst;
    resize(src,dst,size);//resize image
    //cout<<"" <<endl;
    imshow(name,dst);
    imwrite(name1,dst);
}

int main()
{

    int fn;
    int width, height;
    cout << "Enter number of read files :" << endl;
    cin>> fn;
    cout<< "   "<< endl;

    cout << "Enter width and height :" << endl;
    cin>> width >> height;
    cout<< "   "<< endl;
    cout << "Image has width : " <<width <<" and height : " << height << endl;
    cout<< "   "<< endl;


    LARGE_INTEGER frequency;
    LARGE_INTEGER start;
    LARGE_INTEGER end;
    double interval;

    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&start);

    Mat image = Mat::zeros(height, width, CV_8UC3);

    Mat data1; 
    readData(data1, fn);
    printDim(data1,"data1");
    cout << "----------------------- " << endl;

    Mat data=data1.clone();
    printDim(data,"data");
    data1.release();
    printDim(data1,"release data1 ");

    Mat train_data1 = data.reshape(fn, width );      // Mat train_data1 = data.reshape(3, 512 );
    printDim(train_data1, "Dimension of train_data1");

    double Min, Max;
    minMaxLoc(train_data1, &Min, &Max);

    Mat image1;
    train_data1.convertTo(image1,CV_8UC3);

    imwrite("3channel.jpg",image1);
    resize1(image1,"source","source.jpg");

    int nthreads;
    int i;
    int N=data.rows; //number of data points
    cout << "N of vv rows:"<< N << endl;
    cout <<"  "<<endl;
    int K=10;
    int clusterCount=K;
    int sampleCount = N;
    Mat labelsMatKMeans;
    Mat centers;
    /*#pragma omp parallel 
    {
    #pragma omp ...
(more)
edit retag flag offensive close merge delete

Comments

2

can you maybe shorten it to a minimal example, that reproduces the problem ?

berak gravatar imageberak ( 2016-04-04 02:54:07 -0500 )edit

In readData function , i try openmp at first for loop but accees violation error is occured. In main function , I placed kmeans() function in for loop and i try #pragma omp parallel for . It takes less time than kmeans in for loop without openmp.

sumit patel gravatar imagesumit patel ( 2016-04-04 03:59:20 -0500 )edit