Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7610495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:25:53+00:00 2026-05-31T01:25:53+00:00

I implemented the following code that use the data points in dat to calculate

  • 0

I implemented the following code that use the data points in “dat” to calculate the distance matrix between each point and all the other points “dist”. Then I use this distance matrix to find the K closest points to each point in the data “smallest”, then use this to find the sum of the K nearest neighbor.

The following algorithm is a parallel algorithm using OpenMP and it’s working very fine. I just need suggestions to make it run faster. Any suggestion is highly appreciated.

vector<vector<double> > dist(dat.size(), vector<double>(dat.size()));
size_t p,j;
ptrdiff_t i;
double* sumKnn = new double[dat.size()];
vector<vector<int > > smallest(dat.size(), vector<int>(k));
#pragma omp parallel for private(p,j,i) default(shared)
for(p=0;p<dat.size();++p)
{
    int mycont=0;
    for (j = 0; j < dat.size(); ++j)
    {
        double ecl = 0.0;
        for (i = 0; i < c; ++i)
        {
            ecl += (dat[p][i] - dat[j][i]) * (dat[p][i] - dat[j][i]);
        }
        ecl = sqrt(ecl);
        dist[p][j] = ecl;
        //dist[j][p] = ecl;
        int index=0; 
        if(mycont<k && j!=p)
        {
            smallest[p][mycont]=j;
            mycont++;
        }
        else if(j!=p)
        {
            double max=0.0;
            int index=0;
            for(int i=0;i<smallest[p].size();i++)
            {
                if(max < dist[p][smallest[p][i]])
                {
                    index=i;
                    max=dist[p][smallest[p][i]];
                } 
            }
            if(max>dist[p][j])
            {
                smallest[p].erase(smallest[p].begin()+index);
                smallest[p].push_back(j);
            }
        }        
    }
    double sum=0.0;
    for(int r=0;r<k;r++)
        sum+= dist[p][smallest[p][r]];
    sumKnn[p]=sum; 
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:25:55+00:00Added an answer on May 31, 2026 at 1:25 am

    This is more of a comment than an answer, but the comment box is too small, …

    One of the useful aspects of OpenMP is that you can parallelise a serial program in steps. So your first step should be to write a serial code which solves your problem. When you’ve done that you could post again and ask for help on parallelising it.

    To parallelise your program, find the outermost loop statement and think how distributing the loop iterations across threads will affect the calculations. I suspect that you’ll want to create a shared vector of close points as the loops go round, then sort it at the end on one thread only. Or perhaps not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How is the following code implemented in Perl? sub add_item : Local { my
The following code is implemented in Page_Load event to show SaveFileDialog to the user
I've implemented url routing under asp.net 3.5 with the following code: public IHttpHandler GetHttpHandler(RequestContext
Usage scenario We have implemented a webservice that our web frontend developers use (via
I have to work with webservices in Actionscript. I found the following code that
I've got the following piece of code that implements a monad. I'm trying to
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
consider the following code, which represents an attempt to implement partial matching. the intended
I have the following code: public class Test extends JFrame implements ActionListener{ private static
How can I implement cross browser opacity gradient (not color gradient)? See following code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.