How to allocate memory in MIC correctly?

How to allocate memory in MIC correctly?

I'm trying MIC and encountered a strange problem about how to allocate memory on MIC.

I write a example program like THIS:

#include <stdio.h>
#include <stdlib.h>

__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<1000; i++) {
			a[i] = 1;
			b[i] = 2;
			c[i] = 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<1000; i++) printf("i: %d a: %f b: %f c: %f\n",i, a[i],b[i],c[i]);
	}

	return 0;
}

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?

THX.

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Chang,

I suggest reading http://software.intel.com/en-us/blogs/2013/03/27/behind-the-scenes-offload-memory-management-on-the-intel-xeon-phi-coprocessor and looking at the coding examples in your composer installation ( /opt/intel/composer_xe_2013_sp1.2.144/Samples/en_US/C++/mic_samples ).

If you still have issues, please continue this thread.

Regards
--
Taylor
 

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.

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 Effective Use of Compiler Features for Offloading article which may offer additional insights.

#include <stdio.h>
#include <stdlib.h>
__attribute__ ((target(mic:0))) float *a;
__attribute__ ((target(mic:0))) float *b;
__attribute__ ((target(mic:0))) float *c;

int main()
{

a = (float *)_mm_malloc(sizeof(float) * 1000,64);
b = (float *)_mm_malloc(sizeof(float) * 1000,64);
c = (float *)_mm_malloc(sizeof(float) * 1000,64);

#pragma offload target(mic:0) \
        in(a:length(1000) alloc_if(1) free_if(0)) \
        in(b:length(1000) alloc_if(1) free_if(0)) \
        in(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<1000; i++) {
      a[i] = 1;
      b[i] = 2;
      c[i] = 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<1000; i++)
        printf("i: %d a: %f b: %f c: %f\n",i, a[i],b[i],c[i]);
}

return 0;
}

 

Leave a Comment

Please sign in to add a comment. Not a member? Join today