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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:57:11+00:00 2026-05-17T23:57:11+00:00

I have a function which is passed two structures by reference. These structures are

  • 0

I have a function which is passed two structures by reference. These structures are composed of dynamically allocated arrays. Now when I try to implement OpenMP I’m getting a slowdown not a speedup. I’m thinking this can be attributed to possible sharing issues. Here’s some of the code for your perusal (C):

void    leap(MHD *mhd,GRID *grid,short int gchk)
{
  /*-- V A R I A B L E S --*/
  // Indexes
  int i,j,k,tid;
  double rhoinv[grid->nx][grid->ny][grid->nz];
  double rhoiinv[grid->nx][grid->ny][grid->nz];
  double rhoeinv[grid->nx][grid->ny][grid->nz];
  double rhoninv[grid->nx][grid->ny][grid->nz]; // Rho Inversion
  #pragma omp parallel shared(mhd->rho,mhd->rhoi,mhd->rhoe,mhd->rhon,grid,rhoinv,rhoiinv,rhoeinv,rhoninv) \
                       private(i,j,k,tid,stime)
  {
    tid=omp_get_thread_num();
    printf("-----  Thread %d Checking in!\n",tid);
    #pragma omp barrier
    if (tid == 0)
    {
      stime=clock();
      printf("-----1) Calculating leap helpers");
    }
    #pragma omp for
    for(i=0;i<grid->nx;i++)
    {
      for(j=0;j<grid->ny;j++)
      {
        for(k=0;k<grid->nz;k++)
        {
          //      rho's
          rhoinv[i][j][k]=1./mhd->rho[i][j][k];
          rhoiinv[i][j][k]=1./mhd->rhoi[i][j][k];
          rhoeinv[i][j][k]=1./mhd->rhoe[i][j][k];
          rhoninv[i][j][k]=1./mhd->rhon[i][j][k];
        }
      }
    }
    if (tid == 0)
    {
      printf("........%04.2f [s] -----\n",(clock()-stime)/CLOCKS_PER_SEC);
      stime=clock();
    }
    #pragma omp barrier
  }/*-- End Parallel Region --*/
}

Now I’ve tried default(shared) and shared(mhd) but neither show any signs of improvement. Could it be that since the arrays are allocated

mhd->rho=(double ***)newarray(nx,ny,nz,sizeof(double));

That by declaring the structure or the pointer to the element of the structure that I’m not actually sharing the memory just the pointers to it? Oh and nx=389 ny=7 and nz=739 in this example. Execution time for this section in serial is 0.23 [s] and 0.79 [s] for 8 threads.

  • 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-17T23:57:11+00:00Added an answer on May 17, 2026 at 11:57 pm

    My issue boiled down to a real simple mistake….clock(). While I did protect my timing algorithm by only having a specific thread calculate the time, I forgot one important thing about clock()…it returns wall clock time which is the total processor time (summation over the active threads). What I needed to be calling was omp_get_wtime(). Doing this I suddenly see a speedup for many sections of my code. For the record I’ve modified my code to include

    #ifdef _OPENMP
        #include <omp.h>
        #define TIMESCALE 1
    #else
        #define omp_get_thread_num() 0
        #define omp_get_num_procs() 0
        #define omp_get_num_threads() 1
        #define omp_set_num_threads(bob) 0
        #define omp_get_wtime() clock()
        #define TIMESCALE CLOCKS_PER_SEC
    #endif
    

    And my timing algorithm is now

        #pragma omp barrier
        if (tid == 0)
        {
            stime=omp_get_wtime();
            printf("-----1) Calculating leap helpers");
        }
        #pragma omp for
        for(i=0;i<grid->nx;i++)
        {
            for(j=0;j<grid->ny;j++)
            {
                for(k=0;k<grid->nz;k++)
                {
                    //      rho's
                    rhoinv[i][j][k]=1./mhd->rho[i][j][k];
                    rhoiinv[i][j][k]=1./mhd->rhoi[i][j][k];
                    rhoeinv[i][j][k]=1./mhd->rhoe[i][j][k];
                    rhoninv[i][j][k]=1./mhd->rhon[i][j][k];
                    //  1./(gamma-1.)
                    gaminv[i][j][k]=1./(mhd->gamma[i][j][k]-1.);
                    gamiinv[i][j][k]=1./(mhd->gammai[i][j][k]-1.);
                    gameinv[i][j][k]=1./(mhd->gammae[i][j][k]-1.);
                    gamninv[i][j][k]=1./(mhd->gamman[i][j][k]-1.);
                }
            }
        }
        if (tid == 0)
        {
            printf("........%04.2f [s] -----\n",(omp_get_wtime()-stime)/TIMESCALE);
            stime=omp_get_wtime();
            printf("-----2) Calculating leap helpers");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which parses one string into two strings. In C# I
I have a function which searches an STL container then returns the iterator when
I have an function which decodes the encoded base64 data in binary data but
I have a function which gets a key from the user and generates a
i have a function which retrieves values from a webservice , then loops through
Imagine I have an function which goes through one million/billion strings and checks smth
This is a Windows Forms application. I have a function which captures some mouse
i have a c function which returns a long double . i'd like to
Say I have a C function which takes a variable number of arguments: How
if i have a function A,which can apply a certain rule on a given

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.