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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:11:39+00:00 2026-05-22T12:11:39+00:00

How can I parallelize an array shift with OpenMP? I’ve tryed a few things

  • 0

How can I parallelize an array shift with OpenMP?

I’ve tryed a few things but didn’t get any accurate results for the following example (which rotates the elements of an array of Carteira objects, for a permutation algorithm):

void rotaciona(int i)
{
    Carteira aux = this->carteira[i];
    for(int c = i; c < this->size - 1; c++)
    {
        this->carteira[c] = this->carteira[c+1];
    }
    this->carteira[this->size-1] = aux;
}

Thank you very much!

  • 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-22T12:11:40+00:00Added an answer on May 22, 2026 at 12:11 pm

    This is an example of a loop with loop-carried dependencies, and so can’t be easily parallelized as written because the tasks (each iteration of the loop) aren’t independent. Breaking the dependency can vary from a trivial modification to the completely impossible
    (eg, an iteration loop).

    Here, the case is somewhat in between. The issue with doing this in parallel is that you need to find out what your rightmost value is going to be before your neighbour changes the value. The OMP for construct doesn’t expose to you which loop iterations values will be “yours”, so I don’t think you can use the OpenMP for worksharing construct to break up the loop. However, you can do it yourself; but it requires a lot more code, and it won’t nicely reduce to the serial case any more.

    But still, an example of how to do this is shown below. You have to break the loop up yourself, and then get your rightmost value. An OpenMP barrier ensures that no one starts modifying values until all the threads have cached their new rightmost value.

    #include <stdio.h>
    #include <stdlib.h>
    #include <omp.h>
    
    int main(int argc, char **argv) {
        int i;
        char *array;
        const int n=27;
    
        array = malloc(n * sizeof(char) );
        for (i=0; i<n-1; i++)
            array[i] = 'A'+i;
    
        array[n-1] = '\0';
    
        printf("Array pre-shift  = <%s>\n",array);
    
        #pragma omp parallel default(none) shared(array) private(i)
        {
            int nthreads = omp_get_num_threads();
            int tid = omp_get_thread_num();
    
            int blocksize = (n-2)/nthreads;
            int start = tid*blocksize;
            int end = start + blocksize - 1;
            if (tid == nthreads-1) end = n-2;
    
            /* we are responsible for values start...end */
    
            char rightval = array[end+1];
            #pragma omp barrier 
    
            for (i=start; i<end; i++)
                array[i] = array[i+1];
    
            array[end] = rightval;
        }
        printf("Array post-shift = <%s>\n",array);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can i get the source code for a WAMP stack installer somewhere? Any help
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can I get a 'when to use' for these and others? <% %> <%#
In a member function, I can parallelize using the shared member variable int *x
I'm trying to parallelize a ray tracer in C, but the execution time is
I'm trying to parallelise the Floyd-Warshall algorithm using OpenMP (basically editing a 2D array
I have to parallelize some c++ application for my college project. But, I could
I've got a collection of records to process, and the processing can be parallelized,
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through

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.