<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Hi Petr, in OpenCL* for CPU</title>
    <link>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144376#M5943</link>
    <description>&lt;P&gt;Hi Petr,&lt;/P&gt;

&lt;P&gt;Could you fix the second issue mentioned by Ben and check if it fixes you problem, please?&lt;/P&gt;

&lt;P&gt;You can do this by placing additional barrier in the while loop before the if statement.&lt;/P&gt;

&lt;P&gt;I would avoid putting any expectations on compiler optimizations results for invalid code.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;

&lt;P&gt;Alexey&lt;/P&gt;</description>
    <pubDate>Mon, 26 Feb 2018 20:04:52 GMT</pubDate>
    <dc:creator>Alexey_B_Intel1</dc:creator>
    <dc:date>2018-02-26T20:04:52Z</dc:date>
    <item>
      <title>code optimization + while-loop + local memory counter + CPU = infinite run of the kernel, demo inside</title>
      <link>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144373#M5940</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;encountered some strange bug and prepared the demonstration.&lt;/P&gt;

&lt;P&gt;1. Simple kernel:&lt;BR /&gt;
	--------------------------&lt;/P&gt;

&lt;PRE class="brush:;"&gt;#pragma OPENCL EXTENSION cl_intel_printf : enable

__kernel void glitch( 
	__local uint *scratch
) {

	uint k,v;
	k=0;
	v=0; 

	printf(" START\n");
	uint id = (uint)get_global_id(0);
	if (id==0) {
		scratch&lt;K&gt; = v;
	}
	barrier( CLK_LOCAL_MEM_FENCE );
	while ( scratch&lt;K&gt;&amp;lt;2 ) {
		if (id==0) {
			scratch&lt;K&gt;++;
		}
		barrier( CLK_LOCAL_MEM_FENCE );
	}
	printf(" FINISH\n");
}&lt;/K&gt;&lt;/K&gt;&lt;/K&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;BR /&gt;
	---------------------------&lt;/P&gt;

&lt;P&gt;2. Run with natural grid dimensions: single group 16x1x1 , or 256x1x1&lt;BR /&gt;
	and local memory size, for example, 1024&lt;/P&gt;

&lt;P&gt;3. what expected:&lt;BR /&gt;
	the items are started (you see 16 lines of START)&lt;BR /&gt;
	the first of them increments the counter in local memory&lt;BR /&gt;
	and all items are finished (you see 16 lines of FINISH)&lt;/P&gt;

&lt;P&gt;4. but FINISH repeat forever!&lt;BR /&gt;
	maybe some stack corruption?&lt;BR /&gt;
	When you switch off the printf and remove its pragma,&lt;BR /&gt;
	the kernel simply does not return which is the same.&lt;/P&gt;

&lt;P&gt;---------------------------&lt;BR /&gt;
	cases:&lt;/P&gt;

&lt;P&gt;Intel CPU with "-cl-opt-disable" runs fine&lt;/P&gt;

&lt;P&gt;Nvidia GPU - no problem with or without optimization.&lt;/P&gt;

&lt;P&gt;Intel CPU with optimization and workgroup sizes 8 or 1 runs fine. 2, 4, 16, 64, 256 are bad.&lt;BR /&gt;
	(8 may be the special case: the number of threads in my CPU, i7-4790)&lt;/P&gt;

&lt;P&gt;Intel OpenCL SDK and CPU driver are re-installed today.&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;---------------------------&lt;/P&gt;

&lt;P&gt;additional demonstration:&lt;BR /&gt;
	I added two #define's (see 1st and 2nd lines of the code)&lt;/P&gt;

&lt;P&gt;GLITCH=0 whows how to overcome with nonzero index for local memory array&lt;BR /&gt;
	and initial value of counter from get_global_id(1)&lt;/P&gt;

&lt;P&gt;zero index is bad.&lt;/P&gt;

&lt;P&gt;initial value of counter = explicitly written 0 is bad.&lt;/P&gt;

&lt;P&gt;GLITCH2 shows that the item counter does not go after 8:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;&amp;nbsp;FINISH: z=0 y=0 x=0&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=1&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=2&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=3&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=4&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=5&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=6&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=7&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=0&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=1&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=2&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=3&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=4&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=5&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=6&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=7&lt;BR /&gt;
		&amp;nbsp;FINISH: z=0 y=0 x=0&lt;BR /&gt;
		...etc.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;but if you don't call printf with x,y,z, then again everything is OK.&lt;/P&gt;

&lt;P&gt;-----&lt;BR /&gt;
	Rather strange.&lt;BR /&gt;
	Would like to know if it works for you?&lt;BR /&gt;
	I don't see any rough errors in my code...&lt;/P&gt;

&lt;P&gt;Regards, Petr&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Feb 2018 14:59:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144373#M5940</guid>
      <dc:creator>Petr_F__Kartsev</dc:creator>
      <dc:date>2018-02-24T14:59:11Z</dc:date>
    </item>
    <item>
      <title>Hello,</title>
      <link>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144374#M5941</link>
      <description>Hello,

I'd need to see both your global work size and your local work size to be sure, but since scratch&lt;K&gt; is only incremented by work item global id zero, won't there be an infinite loop any time there is more than one work group?

I think there's also a chance that the loop could execute a varying number of times for work items in a work group, and hence could violate the spec restriction that "If work_group_barrier is inside a conditional statement, then all work-items must enter the conditional if any work-item enters the conditional statement and executes the work_group_barrier."  It's a little tricky to see this one, but consider that one of the work items in the work group enters the while loop, increments the counter, and hits the barrier before another work item even evaluates the while loop condition.  Barriers in control flow are tricky!

Good luck, hope this helps.
  -- Ben&lt;/K&gt;</description>
      <pubDate>Mon, 26 Feb 2018 17:29:29 GMT</pubDate>
      <guid>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144374#M5941</guid>
      <dc:creator>Ben_A_Intel</dc:creator>
      <dc:date>2018-02-26T17:29:29Z</dc:date>
    </item>
    <item>
      <title>Hello,</title>
      <link>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144375#M5942</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;Yes, I agree that there should be get_local_id(0). This demo is made to run in single group. Now replaced with get_local_id(0), nothing changed.&lt;/P&gt;

&lt;P&gt;Global and local sizes are 16, full demonstration is attached to first message (UPD: updated ZIP file, corrected mistyping). Does it show the same behaviour for You?&lt;/P&gt;

&lt;P&gt;Yes, looks like first item goes too fast and switches everything off without respect to barrier.&lt;/P&gt;

&lt;P&gt;But I can't understand why it prints 'FINISH' more times than 'START'.&lt;/P&gt;

&lt;P&gt;UPD: It is really executed more times! I added local counter:&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;    printf(" FINISH\n");
    atomic_inc(&amp;amp;w);
//    barrier( CLK_LOCAL_MEM_FENCE );
    printf(" FINISH-1: %d\n", w);&lt;/PRE&gt;

&lt;P&gt;(the barrier is commented, as it stops the kernel completely)&lt;/P&gt;

&lt;P&gt;Now the program prints&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt; FINISH-1: 1312
 FINISH-1: 1312
 FINISH-1: 1312
 FINISH-1: 1312
 FINISH-1: 1312
 FINISH-1: 1312
 FINISH-1: 1312
 FINISH-1: 1312
 FINISH
 FINISH
 FINISH
 FINISH
 FINISH
 FINISH
 FINISH
 FINISH
 FINISH-1: 1320
 FINISH-1: 1320
 FINISH-1: 1320
 FINISH-1: 1320
 FINISH-1: 1320
 FINISH-1: 1320
 FINISH-1: 1320
 FINISH-1: 1320
... etc.&lt;/PRE&gt;

&lt;P&gt;Cool picture, looks like the system starts two bunches of 8 items,&lt;BR /&gt;
	but cannot get the second right, and starts it again and again.&lt;/P&gt;

&lt;P&gt;Is not it a bug? I started only 16 work-items...&lt;/P&gt;

&lt;P&gt;Thanks for your interest!&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 18:24:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144375#M5942</guid>
      <dc:creator>Petr_F__Kartsev</dc:creator>
      <dc:date>2018-02-26T18:24:00Z</dc:date>
    </item>
    <item>
      <title>Hi Petr,</title>
      <link>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144376#M5943</link>
      <description>&lt;P&gt;Hi Petr,&lt;/P&gt;

&lt;P&gt;Could you fix the second issue mentioned by Ben and check if it fixes you problem, please?&lt;/P&gt;

&lt;P&gt;You can do this by placing additional barrier in the while loop before the if statement.&lt;/P&gt;

&lt;P&gt;I would avoid putting any expectations on compiler optimizations results for invalid code.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;

&lt;P&gt;Alexey&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 20:04:52 GMT</pubDate>
      <guid>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144376#M5943</guid>
      <dc:creator>Alexey_B_Intel1</dc:creator>
      <dc:date>2018-02-26T20:04:52Z</dc:date>
    </item>
    <item>
      <title>Thanks!</title>
      <link>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144377#M5944</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;

&lt;P&gt;Half of the problem gone :) two of three demonstration cases are fixed now.&lt;/P&gt;

&lt;P&gt;This one persists:&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;__kernel void glitch( 
	__local uint *scratch
) {

	printf(" START\n"); 
	uint id = (uint)get_local_id(0);
	if (id==0) {
		scratch[1] = 0;
	}
	barrier( CLK_LOCAL_MEM_FENCE );
	while ( scratch[1]&amp;lt;1 ) {
		barrier( CLK_LOCAL_MEM_FENCE );
		if (id==0) {
			scratch[1]++;
		}
		barrier( CLK_LOCAL_MEM_FENCE );
	}
	printf(" FINISH\n");
}
&lt;/PRE&gt;

&lt;P&gt;If I replace the index occurencies (all three) from [1] to [0] then the problem disappears. All other nonzero values are bad, too.&lt;/P&gt;

&lt;P&gt;Do I set wrong local argument size? This is how I do it:&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;    ret = clSetKernelArg( kernel, 0, 1024, NULL);
   


&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 20:28:40 GMT</pubDate>
      <guid>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144377#M5944</guid>
      <dc:creator>Petr_F__Kartsev</dc:creator>
      <dc:date>2018-02-26T20:28:40Z</dc:date>
    </item>
    <item>
      <title>Alexey, I am sorry if looked</title>
      <link>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144378#M5945</link>
      <description>&lt;P&gt;Alexey, I am sorry if looked arrogant.&lt;/P&gt;

&lt;P&gt;P.S. Now I understood the logic of the second issue. Yes, tricky...&lt;BR /&gt;
	Any loop should not contain barriers, then.&lt;/P&gt;

&lt;P&gt;Moreover, the loop is not guaranteed to run in the whole width?&lt;/P&gt;

&lt;P&gt;This one freezes completely and does not print 'FINISH':&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;	uint id = (uint)get_local_id(0);
	printf(" Hello! my id is %d\n", id);
	if (id==0) {
		scratch[0] = 0;
	}
	barrier( CLK_LOCAL_MEM_FENCE );
	while ( scratch[0]&amp;lt;1 ) {
		if (id==0) {
			atomic_inc( &amp;amp;scratch[0] );
		}
	}
	printf(" FINISH\n");
&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2018 21:39:46 GMT</pubDate>
      <guid>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144378#M5945</guid>
      <dc:creator>Petr_F__Kartsev</dc:creator>
      <dc:date>2018-02-26T21:39:46Z</dc:date>
    </item>
    <item>
      <title>Hi PetrF,</title>
      <link>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144379#M5946</link>
      <description>&lt;P&gt;Hi PetrF,&lt;/P&gt;

&lt;P&gt;Devs have suggested the spinning and atomic behavior in the example exhibits behavior outside of whats defined in OpenCL 2.0 Section 3.2.2 standard.&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;"In the absence of work-group functions (e.g. a barrier), work-items within a workgroup may be serialized. In the presence of work-group functions, work-items within a workgroup may be serialized before any given work-group function, between dynamically encountered pairs of work-group functions and between a work-group function and the end of the kernel."&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&lt;BR /&gt;
	So, if the order of work-items execution is, for example, 3, 2, 1, 0 and the were serialized (this is correct by the spec), first executed work-item with id = 3 will never exit the loop.&lt;/P&gt;

&lt;P&gt;Secondly, this section from 3.2.2:&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;"The work-items within a single work-group execute concurrently but not necessarily in parallel (i.e. they are not guaranteed to make independent forward progress). "&lt;/P&gt;

&lt;P&gt;"The potential of the work-items within a workgroup to be serialized means that independent forward progress of the work-items cannot be assumed; therefore, synchronization between subsets of work-items within a work-group (e.g. using spin-locks) cannot portably be supported in OpenCL. "&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;-MichaelC&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 01:03:14 GMT</pubDate>
      <guid>https://community.intel.com/t5/OpenCL-for-CPU/code-optimization-while-loop-local-memory-counter-CPU-infinite/m-p/1144379#M5946</guid>
      <dc:creator>Michael_C_Intel1</dc:creator>
      <dc:date>2018-08-14T01:03:14Z</dc:date>
    </item>
  </channel>
</rss>

