Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

How to allocate memory in MIC correctly?

Chang_X_
Beginner
607 Views

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 = 1;
			b = 2;
			c = 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,b,c);
	}

	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.

0 Kudos
2 Replies
TaylorIoTKidd
New Contributor I
607 Views

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
 

0 Kudos
Kevin_D_Intel
Employee
607 Views

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 = 1;
      b = 2;
      c = 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,b,c);
}

return 0;
}

 

0 Kudos
Reply