Sign In

Don't have a Xilinx account yet?

  • Choose to receive important news and product information
  • Gain access to special content
  • Personalize your web experience on Xilinx.com

Create Account

Username

Password

Forgot your password?
XClose Panel
Xilinx Home
Reply
bv
Visitor
bv
Posts: 12
Registered: ‎11-24-2011
0
Accepted Solution

Why don't the For Loops cause a delay?

Seems like I should see some sort of noticible delay but it zips through this code like the for loops aren't even there...

 

int main()
{
    init_platform();

     print("...Hello World...\n\r\n");

	int i, j, tmp;

	print ("Start  Delay\r\n");
	for (i=0; i<1000000000; i++) {
		tmp=i+1;
		for (j=0; j<1000000000; j++) {
			tmp=j+1;
		}
	}
	xil_printf ("Done w/Delay - i=(%d) , j=(%d) , tmp=((%d))\r\n\n", i, j, tmp);

    print("!!! Goodbye World !!!\n\r");

    cleanup_platform();

    return 0;
}

 

 Output:

...Hello World...

 

Start  Delay

Done w/Delay - i=(1000000000) , j=(1000000000) , tmp=((1000000000))

 

!!! Goodbye World !!!

Expert Contributor
gszakacs
Posts: 5,251
Registered: ‎08-14-2007

Re: Why don't the For Loops cause a delay?

Looks like optimization works :smileyhappy:

 

There may be some other optimization settings available that won't cut out

the loops.  Otherwise you could use a variable in external memory instead of tmp

to make sure the loop needs to execute every iteration.  I would expect this

loop to take a very long time indeed...

 

-- Gabor

-- Gabor
bv
Visitor
bv
Posts: 12
Registered: ‎11-24-2011
0

Re: Why don't the For Loops cause a delay?

Thanks Gabor, that's interesting!  When you say external memory what do you mean?  I moved tmp outside the main () { code } to make it a global variable but it still behaves the same.  Is it easy to turn off optimization?

bv
Visitor
bv
Posts: 12
Registered: ‎11-24-2011
0

Re: Why don't the For Loops cause a delay?

Turned optimization off and it now delays as expected.  Still curious what's meant by external memory.  Explanation would be most appreciated.

 

Regards,

Bob

 

Expert Contributor
bassman59
Posts: 4,653
Registered: ‎02-25-2008
0

Re: Why don't the For Loops cause a delay?


bv wrote:

Thanks Gabor, that's interesting!  When you say external memory what do you mean?  I moved tmp outside the main () { code } to make it a global variable but it still behaves the same.  Is it easy to turn off optimization?



"External memory" as opposed to a register. Most counter variables get put into registers for immediate access. Putting it in the memory space occupied by your SDRAM may force the tools to not optimize the access.


----------------------------------------------------------------
Yes, I do this for a living.
bv
Visitor
bv
Posts: 12
Registered: ‎11-24-2011
0

Re: Why don't the For Loops cause a delay?

Doesn't declaring a variable cause, esp outside the subroutine, force this?

 

Expert Contributor
gszakacs
Posts: 5,251
Registered: ‎08-14-2007
0

Re: Why don't the For Loops cause a delay?


bv wrote:

Doesn't declaring a variable cause, esp outside the subroutine, force this?

 


That really depends on the optimizer.  Global optimization allows the compiler to

assume that even global variables do not have side-effects on write as long as

they don't go off-chip (outside the MicroBlaze and its attached BRAM).

 

It is possible that declaring your tmp variable "volatile" would prevent the loop

from being optimized out.

 

-- Gabor

-- Gabor