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 6920423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:08:42+00:00 2026-05-27T10:08:42+00:00

I’m having an interesting problem with seg faults when I attempt to move a

  • 0

I’m having an interesting problem with seg faults when I attempt to move a malloc call inside an openmp for loop. Each thread must calculate it’s own copy of the distances vector in order to compute the classification correctly so the vector must be private…however when I attempt to call it with more than 1 thread it seg faults. This does not occur if the p_distances vector is declared as shared although of course this results in inaccurate distance calculations since the threads overwrite each other. Is there some very obvious rule I’m violating here…also, I’m aware that there are other bad coding practices evident in my code; I’m always open to suggestion regarding style but please help me focus on what’s actually causing the issue.

int *labels_train;
float *data_train;
int *labels_test;
float *data_test;
float *s_distances;
int *s_results, *p_results;
int i, j, k, h;
int N, D, K, M, thread_count;

void sort(float *_distances, int *_labels_train,  int _N);

void computeParallelKNN()
{
// this is the target loop for multi-point parallelization
// seg fault here whenever p_distances malloc is moved inside parallel for loop and declared private
#pragma omp parallel for num_threads(thread_count) private(h, j, i)
for (i = 0; i < M; i++)
{
    float *p_distances = (float*)malloc(N * sizeof(float));
    k = 0;

    // This is the target loop for single point parallelization
    // No dependencies on outer loop (each thread can calculate distance for current point with some
    // different training point)
    for (h = 0; h < N*D; h+=D)
    {
        float dTmp = 0;
        // Reduction operation..no dependencies here either (I don't think?)
        // dTmp is critical variable for parallel operations
        for (j = 0; j < D; j++)
        {
            dTmp += pow(data_test[i*D+j] - data_train[h+j],2);
        }
        p_distances[k] = (float)sqrt((double)dTmp);
        k++;
    }

    // Make a copy of labels (since sort will invalidate original data/labels correlation)
    int *temp_labels;
    temp_labels = (int*)malloc(N * sizeof(int));
    for (h = 0; h < N; h++)
        temp_labels[h] = labels_train[h];

    // Sort distances/labels_train vector
    sort(p_distances, temp_labels, N);

    // Calculate/print KNN classification
    int neg = 0;
    int pos = 0;
    for (h = 0; h < K; h++)
    {
        if(temp_labels[h] == -1) neg++;
        else pos++;
    }
    if (pos > neg) p_results[i] = 1;
    else p_results[i] = -1;

    free(p_distances);
      }
}

// Selection sort algorithm modified to sort labels according to distance data
void sort(float *_distances, int *_labels_train,  int _N) 
{
  int k;
  for (k = 1; k < _N; ++k) 
  {
    float dist_key = _distances[k];
    int label_key = _labels_train[k];
    int i = k - 1;
    while ((i >= 0) && (dist_key < _distances[i])) 
    {
        _distances[i + 1] = _distances[i];
        _labels_train[i + 1] = _labels_train[i];
         --i;
    }
    _distances[i + 1] = dist_key;
   _labels_train[i + 1] = label_key;
}

}

I can post the complete code but this is definitely the area where the fault is occurring. Thanks in advance, hopefully it’s just a stupid mistake I’m making.

  • 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-27T10:08:42+00:00Added an answer on May 27, 2026 at 10:08 am

    First of all, there is k which is shared across all threads; there are no declarations to inform about a critical section around it or that it should be done atomically.

    Rewrite your code in a cleaner way and avoid global variables as much as possible – you can define your variables when you have just entered a new scope.

    For example,

    int i;
    void foo() {
    #pragma parallel private(i)
    {
      // ...
    }
    

    is the same as:

    void foo() {
    #pragma parallel
    {
      int i;
      // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put

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.