Implementing k-means in OpenMP

General OpenMP discussion

Implementing k-means in OpenMP

Postby waqasaminawan » Tue Mar 19, 2013 12:19 am

I want to implement the following k-means algorithm in C++ OpenMP. It's C++ implementation is as follows;
Code: Select all
#include <iostream>
//#include <conio.h>
#include <math.h>
#define row 10
#define col 2
using namespace std;
// int row=10,col=2;
int d [row][col];
int dataset[row][col]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},g[row][col],g1[row][col],centroid1[1][2],centroid2[1][2];
//***************Method for calculating Eucledian distances***************//
void dist()
{
  int x,y,i;
  int v;
  for (i=0;i<row;i++)
  {
   x=dataset[i][0]-centroid1[0][0];//Eucledian distance at x from centroid1
   y=dataset[i][1]-centroid1[0][1];//Eucledian distance at y from centroid1
   x=x*x;//squaring Eucledian distance at x
   y=y*y;//squaring Eucledian distance at y
   v=(int)(x+y);//sum of squared distance
   d[i][0]=(int)sqrt(v);//mean squaring of distance

   x=dataset[i][0]-centroid2[0][0];//Eucledian distance at x from centroid2
   y=dataset[i][1]-centroid2[0][1];//Eucledian distance at y from centroid2
   x=x*x;//squaring Eucledian distance at x
   y=y*y;//squaring Eucledian distance at y
   v=(int)(x+y);//sum of squared distance
      d[i][1]=(int)sqrt(v);//mean squaring of distance
  }
}
//***********************************************************************//

//*****************Method to perform k-means clustering*****************//
void clust()
{
   int i;
   for(i=0;i<row;i++)
      {
     g[i][0]=g[i][1]=0;
     if(d[i][0]<=d[i][1])
        g[i][0]=1;
     else
        g[i][1]=1;
      }
}
void copy()
{
    for(int i=0;i<row;i++)
       for(int j=0;j<col;j++)
      g1[i][j]=g[i][j];
}
int main(void)
{
   int x,y,s,ch,i,j;
   ch=x=y=s=0;
//   clrscr();
   std::cout<<"\n****## Program for K-Means Clustering ##****";
  // cout<<"\n\nEnter values : ";
   //********************Reading dataset********************//
   for(i=0;i<row;i++)
       for(j=0;j<col;j++)
      {
    //cin>>dataset[i][j];
    g[i][j]=g1[i][j]=0;
      }
    //******************************************************//

    //********Randomly assigning cluster centres************//
    centroid1[0][0]=dataset[0][0];
    centroid1[0][1]=dataset[0][1];
    centroid2[0][0]=dataset[1][0];
    centroid2[0][1]=dataset[1][1];
    //******************************************************//

    dist();
    clust();
    copy();

    while(ch==0)
    {
   x=y=s=0;
   for(i=0;i<row;i++)
   {
         if(g[i][0]==1)
          {
        x=x+dataset[i][0];
        y=y+dataset[i][1];
        s=s+1;
          }
    }
    centroid1[0][0]=x/s;
    centroid1[0][1]=y/s;

   x=y=s=0;
   for(i=0;i<row;i++)
   {
         if(g[i][1]==1)
          {
        x=x+dataset[i][0];
        y=y+dataset[i][1];
        s=s+1;
          }
    }
    centroid2[0][0]=x/s;
    centroid2[0][1]=y/s;
    dist();
    clust();

    for(i=0;i<row;i++)
       for(j=0;j<col;j++)
          if(g[i][j]!=g1[i][j])
         goto xyz;
     ch=1;
     xyz:copy();
      }

    cout<<"\n---We get the final grouping as the results---";
    cout<<"\n\n\nObject\t  Value1  Value2   Cluster"<<endl;
    for(i=0;i<row;i++)
     {
   if(g[i][0]==1)
      cout<<"\n\nmedicine"<<i<<"   "<<dataset[i][0]<<"\t\t"<<dataset[i][1]<<"\t1";
   if(g[i][1]==1)
      cout<<"\n\nmedicine"<<i<<"   "<<dataset[i][0]<<"\t\t"<<dataset[i][1]<<"\t2";
      }
//    getch();
    return 0;
}


I want OpenMP version of this algo... Any help plzzz
waqasaminawan
 
Posts: 2
Joined: Tue Dec 11, 2012 8:00 am

Re: Implementing k-means in OpenMP

Postby MarkB » Tue Mar 19, 2013 5:31 am

Hi there,

This looks very much like a homework assignment! If so, please at least make some effort to solve it before asking for assistance on here......

Mark.
MarkB
 
Posts: 408
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 11 guests

cron