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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:38:49+00:00 2026-06-18T15:38:49+00:00

Simple parallel adder. You pass it the pointer to the first element in the

  • 0

Simple parallel adder. You pass it the pointer to the first element in the array, a pointer to the last element in the array, the element # of the first pointer, and the number of elements in this portion of the array being operated on.

    double my_func (double *x, double *x_last, int first_pos, int n)
    {
      if (n ==1) {
        return x[first_pos];
      }
      else if (n == 2) {
        return x[first_pos] + x[first_pos+1];
      }
      else {
        double x1,x2;

        x1 = _Cilk_spawn my_func (&x[first_pos], &x[n/2+first_pos-1],first_pos, n/2);
        x2 = my_func (&x[n/2+first_pos],&x[first_pos+n-1],n/2+first_pos, n-n/2);
        _Cilk_sync;

        return x1 + x2;
      }
    }

Let’s say we start with an array of size 80. This works perfectly for element #0-19 (the first quarter), then returns a bunch of 0’s/garbage for elements #20-39 and all elements after. Clearly both the x1 and x2 lines are working to some degree, but the function breaks down and I don’t know why. Any ideas?

  • 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-06-18T15:38:50+00:00Added an answer on June 18, 2026 at 3:38 pm

    You’re slicing the array wrong:

    x1 = my_func (&x[n/2+first_pos],&x[first_pos+n-1],n/2+first_pos, n-n/2);
                        ^                                 ^
                new "base" pointer                first pos in slice?!
    

    You can’t pass both an adjusted pointer to the slice’s first element, and that element’s global index. You end up indexing more than once, so to speak, leading to invalid addresses being dereferenced.

    For the case of n = 80, the second recursive call will pass x pointing at element &x[80/2+0], i.e. &x[40], but it will also set first_pos to 40 for that call. Eventually you’ll reach down to n < 3, and index using accumulated first_pos.

    Also, the input data should be const if it’s read-only, for clarity’s sake.

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

Sidebar

Related Questions

I developed this simple function to perform the sum of n number in parallel
I have implemented a simple parallel merge sort algorithm in Java. This cuts the
I have a simple question, i have following simple Parallel for loop. this for
I am writing simple parallel program in C++ using OpenMP. I am working on
I have a simple Parallel.Foreach loop that has about 1000 rows in the DataTable,
Simple question :) How can I reduce build time using a parallel build /
use LWP::Simple; use Parallel::ForkManager; @links=( [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe,SweetHome3D-2.1-windows.exe], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg,SweetHome3D-2.1-macosx.dmg], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip,SweetHome3DViewer-2.1.zip], ); # Max 30 processes for
I'm writing a simple script to run bunch of tasks in parallel using the
I wanted to implement a simple python program using parallel execution. It's I/O bound,
I need to write a simple program that records all the input from parallel

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.