<?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 How to allocate memory in MIC correctly? in Software Archive</title>
    <link>https://community.intel.com/t5/Software-Archive/How-to-allocate-memory-in-MIC-correctly/m-p/985860#M27105</link>
    <description>&lt;P&gt;I'm trying MIC and encountered a strange problem about how to allocate memory on MIC.&lt;/P&gt;

&lt;P&gt;I write a example program like THIS:&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

__attribute__ ((target(mic:0))) float *a;
__attribute__ ((target(mic:0))) float *b;
__attribute__ ((target(mic:0))) float *c;

int main() {

#pragma offload target(mic:0) \
	nocopy(a:length(1000) alloc_if(1) free_if(0)) \
	nocopy(b:length(1000) alloc_if(1) free_if(0)) \
	nocopy(c:length(1000) alloc_if(1) free_if(0))
	{
		printf("a: 0x%x b:0x%x, c:0x%x\n", a,b,c);
		for(int i=0; i&amp;lt;1000; i++) {
			a&lt;I&gt; = 1;
			b&lt;I&gt; = 2;
			c&lt;I&gt; = 3;
		}
	}

#pragma offload target(mic:0) \
	nocopy(a: alloc_if(0) free_if(0)) \
	nocopy(b: alloc_if(0) free_if(0)) \
	nocopy(c: alloc_if(0) free_if(0))
	{
		printf("a: 0x%x b:0x%x, c:0x%x\n", a,b,c);
		for(int i=0; i&amp;lt;1000; i++) printf("i: %d a: %f b: %f c: %f\n",i, a&lt;I&gt;,b&lt;I&gt;,c&lt;I&gt;);
	}

	return 0;
}
&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;

&lt;P&gt;The output shows that all of pointers of a,b and c pointed to the same place, so the assignment of ‘c' overwrite the assignments of a and b. Why the program runs like this? How to allocate memory on MIC correctly?&lt;/P&gt;

&lt;P&gt;THX.&lt;/P&gt;</description>
    <pubDate>Sun, 16 Feb 2014 01:53:48 GMT</pubDate>
    <dc:creator>Chang_X_</dc:creator>
    <dc:date>2014-02-16T01:53:48Z</dc:date>
    <item>
      <title>How to allocate memory in MIC correctly?</title>
      <link>https://community.intel.com/t5/Software-Archive/How-to-allocate-memory-in-MIC-correctly/m-p/985860#M27105</link>
      <description>&lt;P&gt;I'm trying MIC and encountered a strange problem about how to allocate memory on MIC.&lt;/P&gt;

&lt;P&gt;I write a example program like THIS:&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

__attribute__ ((target(mic:0))) float *a;
__attribute__ ((target(mic:0))) float *b;
__attribute__ ((target(mic:0))) float *c;

int main() {

#pragma offload target(mic:0) \
	nocopy(a:length(1000) alloc_if(1) free_if(0)) \
	nocopy(b:length(1000) alloc_if(1) free_if(0)) \
	nocopy(c:length(1000) alloc_if(1) free_if(0))
	{
		printf("a: 0x%x b:0x%x, c:0x%x\n", a,b,c);
		for(int i=0; i&amp;lt;1000; i++) {
			a&lt;I&gt; = 1;
			b&lt;I&gt; = 2;
			c&lt;I&gt; = 3;
		}
	}

#pragma offload target(mic:0) \
	nocopy(a: alloc_if(0) free_if(0)) \
	nocopy(b: alloc_if(0) free_if(0)) \
	nocopy(c: alloc_if(0) free_if(0))
	{
		printf("a: 0x%x b:0x%x, c:0x%x\n", a,b,c);
		for(int i=0; i&amp;lt;1000; i++) printf("i: %d a: %f b: %f c: %f\n",i, a&lt;I&gt;,b&lt;I&gt;,c&lt;I&gt;);
	}

	return 0;
}
&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;

&lt;P&gt;The output shows that all of pointers of a,b and c pointed to the same place, so the assignment of ‘c' overwrite the assignments of a and b. Why the program runs like this? How to allocate memory on MIC correctly?&lt;/P&gt;

&lt;P&gt;THX.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2014 01:53:48 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Archive/How-to-allocate-memory-in-MIC-correctly/m-p/985860#M27105</guid>
      <dc:creator>Chang_X_</dc:creator>
      <dc:date>2014-02-16T01:53:48Z</dc:date>
    </item>
    <item>
      <title>Chang,</title>
      <link>https://community.intel.com/t5/Software-Archive/How-to-allocate-memory-in-MIC-correctly/m-p/985861#M27106</link>
      <description>&lt;P&gt;Chang,&lt;/P&gt;

&lt;P&gt;I suggest reading &lt;A href="http://software.intel.com/en-us/blogs/2013/03/27/behind-the-scenes-offload-memory-management-on-the-intel-xeon-phi-coprocessor"&gt;http://software.intel.com/en-us/blogs/2013/03/27/behind-the-scenes-offload-memory-management-on-the-intel-xeon-phi-coprocessor&lt;/A&gt;&amp;nbsp;and looking at the coding examples in your composer installation ( /opt/intel/composer_xe_2013_sp1.2.144/Samples/en_US/C++/mic_samples ).&lt;/P&gt;

&lt;P&gt;If you still have issues, please continue this thread.&lt;/P&gt;

&lt;P&gt;Regards&lt;BR /&gt;
	--&lt;BR /&gt;
	Taylor&lt;BR /&gt;
	&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2014 01:53:41 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Archive/How-to-allocate-memory-in-MIC-correctly/m-p/985861#M27106</guid>
      <dc:creator>TaylorIoTKidd</dc:creator>
      <dc:date>2014-02-19T01:53:41Z</dc:date>
    </item>
    <item>
      <title>I modified your program below</title>
      <link>https://community.intel.com/t5/Software-Archive/How-to-allocate-memory-in-MIC-correctly/m-p/985862#M27107</link>
      <description>&lt;P&gt;I modified your program below adding the corresponding host-side allocations and using IN() vs. NOCOPY() for the first transfer where the allocation on the MIC-side occurs.&lt;/P&gt;

&lt;P&gt;I'm not certain whether it satisfies your interest so in addition to the blog Taylor cited, you can review the details and examples on the &lt;A href="http://software.intel.com/en-us/articles/effective-use-of-the-intel-compilers-offload-features" target="_blank"&gt;Effective Use of Compiler Features for Offloading &lt;/A&gt;article which may offer additional insights.&lt;/P&gt;

&lt;P&gt;#include &amp;lt;stdio.h&amp;gt;&lt;BR /&gt;
	#include &amp;lt;stdlib.h&amp;gt;&lt;BR /&gt;
	__attribute__ ((target(mic:0))) float *a;&lt;BR /&gt;
	__attribute__ ((target(mic:0))) float *b;&lt;BR /&gt;
	__attribute__ ((target(mic:0))) float *c;&lt;/P&gt;

&lt;P&gt;int main()&lt;BR /&gt;
	{&lt;/P&gt;

&lt;P&gt;a = (float *)_mm_malloc(sizeof(float) * 1000,64);&lt;BR /&gt;
	b = (float *)_mm_malloc(sizeof(float) * 1000,64);&lt;BR /&gt;
	c = (float *)_mm_malloc(sizeof(float) * 1000,64);&lt;/P&gt;

&lt;P&gt;#pragma offload target(mic:0) \&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; in(a:length(1000) alloc_if(1) free_if(0)) \&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; in(b:length(1000) alloc_if(1) free_if(0)) \&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; in(c:length(1000) alloc_if(1) free_if(0))&lt;BR /&gt;
	{&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("a: 0x%x b:0x%x, c:0x%x\n", a,b,c);&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=0; i&amp;lt;1000; i++) {&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a&lt;I&gt; = 1;&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&lt;I&gt; = 2;&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&lt;I&gt; = 3;&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;
	}&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;

&lt;P&gt;#pragma offload target(mic:0) \&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nocopy(a: alloc_if(0) free_if(0)) \&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nocopy(b: alloc_if(0) free_if(0)) \&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nocopy(c: alloc_if(0) free_if(0))&lt;BR /&gt;
	{&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("a: 0x%x b:0x%x, c:0x%x\n", a,b,c);&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=0; i&amp;lt;1000; i++)&lt;BR /&gt;
	&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("i: %d a: %f b: %f c: %f\n",i, a&lt;I&gt;,b&lt;I&gt;,c&lt;I&gt;);&lt;BR /&gt;
	}&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;

&lt;P&gt;return 0;&lt;BR /&gt;
	}&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2014 09:47:20 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Archive/How-to-allocate-memory-in-MIC-correctly/m-p/985862#M27107</guid>
      <dc:creator>Kevin_D_Intel</dc:creator>
      <dc:date>2014-02-19T09:47:20Z</dc:date>
    </item>
  </channel>
</rss>

