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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:25:17+00:00 2026-06-11T14:25:17+00:00

I have a loop of this structure Reference : Maxwell Code Example do z=1,zend

  • 0

I have a loop of this structure

Reference : Maxwell Code Example

do z=1,zend
    do y=1,yend
        do x=1,xend
            k=arr(x,y,z)
            do while(k.ne.0)
                ix=fooX(k)
                iy=fooY(k)
                iz=fooZ(k)
                x1=x(ix  ,iy  ,iz)
                x2=x(ix+1,iy  ,iz)
                x3=x(ix  ,iy+1,iz)
                x4=x(ix+1,iy+1,iz)
                x5=x(ix  ,iy  ,iz+1)
                x6=x(ix+1,iy  ,iz+1)
                x7=x(ix  ,iy+1,iz+1)
                x8=x(ix+1,iy+1,iz+1)

                y1=y(ix  ,iy  ,iz)
                y2=y(ix+1,iy  ,iz)
                y3=y(ix  ,iy+1,iz)
                y4=y(ix+1,iy+1,iz)
                y5=y(ix  ,iy  ,iz+1)
                y6=y(ix+1,iy  ,iz+1)
                y7=y(ix  ,iy+1,iz+1)
                y8=y(ix+1,iy+1,iz+1)

                z1=z(ix  ,iy  ,iz)
                z2=z(ix+1,iy  ,iz)
                z3=z(ix  ,iy+1,iz)
                z4=z(ix+1,iy+1,iz)
                z5=z(ix  ,iy  ,iz+1)
                z6=z(ix+1,iy  ,iz+1)
                z7=z(ix  ,iy+1,iz+1)
                z8=z(ix+1,iy+1,iz+1)
                sumX+=x1+x2+..x8
                sumY+=y1+y2+..y8
                sumZ+=z1+z2+..z8

                k=linkArr(k)
            enddo
        enddo
    enddo
enddo

x1 through x8 are the 8 corners of a rectangular cuboid. There are three challenges to vectorize this code. One is that the 8 array elements are not contiguous in memory. Second is the inherent while loop structure along with linked List access. Third the values of ix, iy, iz returned from from fooX, fooY, fooZ are not not contiguous. So each iteration of the loop has a completely different set of ix, iy, iz. So the even across the iterations the memory access is scattered.
I tried the following approaches:
1. unrolled the 3-level DO loops as :

do z=1,zend
    do y=1,yend
        do x=1,xend  
           if(arr(x,y,z).NE.0) then
                kArr(indx)=arr(x,y,z)
                DO WHILE (kArr(indx).NE.0)
                  indx = indx + 1
                  kArr(indx)=linkArr(kArr(indx-1))
                ENDDO
            endif
        enddo
    enddo
enddo

With this i have got rid of the while loop structure and now I’m able to run one big loop on kArr inside which i group 8 elements (say my VPU can accomodate 8 sets of data at a time). It did not give a performance improvement. I can post the details of these if anyone is interested. I need suggestions on how to optimize this code. Another option i tried was to combine x,y,z data in a single array so that when i compute x1, y1 & z1 also will be in adjacent memory locations.

  • 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-11T14:25:18+00:00Added an answer on June 11, 2026 at 2:25 pm

    That while loop is killing you. In a similar situation a few years back, I got a modest improvement in performance doing something like this:

    ! at top of your code, introduce:
    integer :: special_index
    integer :: ix(1000), iy(1000), iz(1000)  !promoting scalars to arrays.
                                             ! make as big as possibly needed.
    
    ! code as usual until you get to your loops, then
    
    ! first, make lookup table
    special_index=0
    do z=1,zend
      do y=1,yend
        do x=1,xend
          k=arr(x,y,z)
          do while(k.ne.0)
            special_index=special_index+1
            ix(special_index)=fooX(k)
            iy(special_index)=fooY(k)
            iz(special_index)=fooZ(k)
            k=linkArr(k)
          enddo
        enddo
      enddo
    endoo
    ! and now we do the calculation, loop over lookup table:
    do n=1,special_index
      x1=x(ix(n)  ,iy(n)  ,iz(n))
      x2=x(ix(n)+1,iy(n)  ,iz(n))
      x3=x(ix(n)  ,iy(n)+1,iz(n))
      etc.
    enddo
    

    Like I said, this helped me a few years back. Your mileage may vary. The first loop still won’t vectorize, but the second one might, and it might give better performance.

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

Sidebar

Related Questions

I have this loop for (it= someCollection.iterator; it.hasNext(); ) { //some code here }
In my code, I have this loop, which brings up various 2-hour time blocks.
I have the code below and I want to loop this every 5 seconds.
I have this loop method to display a ViewPager and I set an OnClickListener
I have a loop like this: for i=1:no %some calculations fid = fopen('c:\\out.txt','wt'); %write
So I do have this loop, but it stops when it come to function
I have loop and each iteration of Hash looks like this: [1, {:clid=>1, :nvz=>4,
I have a loop which basically calls this every few seconds (after the timeout):
I have a for each loop like this $sn_count = 1; $prodfilter = ;
I have a loop that can look like this: For Each article In artAll

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.