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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:22:44+00:00 2026-05-31T00:22:44+00:00

I would like to split a range of numbers into roughly equal size in

  • 0

I would like to split a range of numbers into roughly equal size in C using OpenMP. For example, if I have a range from 7 through 24 and number of threads is 8. I would like the first thread to begin from 7 and end at 9. Second thread begin from 10 and end at 12. Third thread begin from 13 and end at 14. Fourth thread begin at 15 and end at 16 and so forth… Until the last thread begins at 23 and ends at 24. The code that I wrote is as follows, but it is not getting the previously explained results. I am wondering if there is something that I missed that I can do or is there a more efficient way of doing this? Your help is greatly appreciated.

Note of predefined declaration of variables based on the example given above:

first = 7
last = 24
size = 2 (which signifies the amount of numbers per thread)
r = 2    (r signifies remainder)
nthreads = 8
myid = is the thread ID in a range of 0 to 7

    if (r > 0)
    {
        if (myid == 0)
        {
            start = first + myid*size;
            end = start + size;
        }
        else if (myid == nthreads - 1)
        {
            start = first + myid*size + myid;
            end = last;
        }
        else 
        {
            start = first + myid*size + myid;
            end = start + size;
        }
    }
    else
    {
        start = first + myid*size;
        if (myid == nthreads - 1) end = last;
        else end = start + size - 1;
    }
  • 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-31T00:22:45+00:00Added an answer on May 31, 2026 at 12:22 am

    As far as I remember, #pragma omp parallel for automatically divides work between threads in equal chunks, and is ok in most situations.

    However, if you want to do this manually, here is a piece of code which does what you want:

    int len = last - first + 1;
    int chunk = len / nthreads;
    int r = len % nthreads;
    if (myid < r) {
        start = first + (chunk + 1) * myid;
        end = start + chunk;
    } else {
        start = first + (chunk + 1) * r + chunk * (myid - r);
        end = start + chunk - 1;
    }
    

    If there are no additional restrictions, such distribution is indeed optimal.

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

Sidebar

Related Questions

I would like to split the date range into respective months. For example -
I would like to split a text into single words using PHP. Do you
I have an IEnumerable<string > which I would like to split into groups of
I would like to split a string into a String[] using a String as
I have a web app that I would like to split into three pieces.
I have a container std::vector and I would like to efficiently split it into
I have a large source repository split across multiple projects. I would like to
I have a file with data in it that I would like to split
I would like to split the example string: ~Peter~Lois~Chris~Meg~Stewie on the character ~ and
I would like to split a custom url for app opening in iPhone into

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.