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

  • SEARCH
  • Home
  • 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 7368213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:38:14+00:00 2026-05-29T03:38:14+00:00

I’ve a C code which compute the distance between two sets of nodes (three

  • 0

I’ve a C code which compute the distance between two sets of nodes (three coordinate each), even though my code has been fast enough yet, I want to boost it up a bit more using parallel computing. I’ve already found some info about openMP and I’m trying to use it right now, but there’s something a bit weird. Without omp the code cpu time is 20s, adding the two pragma lines it takes 160s! How could it happen?

I append my code down here

float computedist(float **vG1, float **vG2, int ncft, int ntri2, int jump, float *dist){
    int k = 0, i, j;
    float min = 0;
    float max = 0;
    float avg = 0;
    float *d = malloc(3*sizeof(float));
    float diff;

    #pragma omp parallel
    for(i=0;i<ncft;i+=jump){
        #pragma omp parallel
        for(j=0;j<ntri2;j++){
            d[0] = vG1[i][0] - vG2[j][0];
            d[1] = vG1[i][1] - vG2[j][1];
            d[2] = vG1[i][2] - vG2[j][2];
            diff = sqrt(pow(d[0],2) + pow(d[1],2) + pow(d[2],2));
            if(j==0)
                dist[k] = diff;
            else
                if(diff<dist[k])
                    dist[k] = diff;

        }
        avg += dist[k];
        if(dist[k]>max)
            max = dist[k];
        k++;
    }

    printf("max distance: %f\n",max);
    printf("average distance: %f\n",avg/(int)(ncft/jump));

    free(d);

    return max;
}

Thank you so much for any help

  • 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-29T03:38:15+00:00Added an answer on May 29, 2026 at 3:38 am

    (The answer below refers to the initial code in the question, which since then was improved with applying these suggestions)


    You need to read more about how to use OpenMP. The specification is available at http://www.openmp.org; and there are links to tutorials and other resources.

    I will point out some issues in your code and give recommendations how to fix those.

        float *d = malloc(3*sizeof(float));
        float diff;
    

    d is used as a temporary variable, so should be marked as private in #pragma omp parallel for (see below) to avoid data races. Meanwhile, instead of dynamic allocation, I would just use 3 separate floats. diff also holds a temporary value, so should also be private.

        #pragma omp parallel
        for(i=0;i<ncft;i+=jump){
            #pragma omp parallel
            for(j=0;j<ntri2;j++){
    

    You created a parallel region where each thread executes the whole loop (because the region does not contain any work sharing constructs), and inside it you created a nested region with a new(!) set of threads, also each executing the whole inner loop. It adds lots of overhead and unnecessary computation to your program. What you need is #pragma omp parallel for, and only applied to the outer loop.

                d[0] = vG1[i][0] - vG2[j][0];
                d[1] = vG1[i][1] - vG2[j][1];
                d[2] = vG1[i][2] - vG2[j][2];
                diff = sqrt(pow(d[0],2) + pow(d[1],2) + pow(d[2],2));
    

    Not related to parallelism, but why calling pow just to compute squares? A good old multiplication would likely be both simpler to read and faster.

                if(j==0)
                    dist[k] = diff;
                else
                    if(diff<dist[k])
                        dist[k] = diff;
    

    Since the action is the same (dist[k]=diff;), the code can be simplified by combining two conditions with || (logical or).

            }
            avg += dist[k];
            if(dist[k]>max)
                max = dist[k];
    

    Here you compute aggregate values across the outer loop. In OpenMP, this is done with reduction clause of #pragma omp for.

            k++;
        }
    

    Currently, you increment k at each iteration, thus creating an unnecessary dependency between iterations that leads to a data race in parallel code. According to your code, k is just a convenient “alias” for i/jump – so just assign it as such at the beginning of the iteration, and make private.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.