Software Archive
Read-only legacy content
17061 讨论

__MIC__ macro does not work

wang_p_1
初学者
1,038 次查看

Hello everyone,

           To solve problems like this:   undefined reference to `_mm512_xxx_xxx(), I have added __MIC__ macro into my code.(https://software.intel.com/en-us/comment/1726413#comment-1726413)

          But, these code between the

#ifdef __MIC__   

xxxx

xxxx

#endif

         never won't be executed.   

         _________________________________________________________________________________

        as the picture showing , the printf() fucntion never won't be executed.

QQ图片20150202112142.jpg

             

              Thank you!

0 项奖励
1 解答
Kevin_D_Intel
员工
1,038 次查看

Use of __MIC__ inside the pragma offload is not supported. The compiler does not enforce this restriction so the programmer needs to be aware of and avoid this,  otherwise it can lead to problems with what the compiler sees for the host and target compilations. For more info see: Restrictions on Offloaded Code Using a Pragma

In this case, there are some missing/mis-placed curly-braces within the #ifdef/else/endif construction that lead to the unexpected results. As written the #ifdef/endif is not completely enclosed inside the pragma offload construct which leads the unexpected results.

The function f1 should be modified as shown below.

void point::f1()
{
#pragma offload target(mic:0)
{
#ifdef __MIC__
        for(int i = 0; i < 20; i++)
        {                                                 // Add this missing curly brace to for loop
             printf("I'm mic\n");
             fflush(0);
        }
#else
//}                                                       // this curly brace is mis-placed - it should appear after the #endif below
        printf("I'm CPU\n");
#endif
}                                                         // place curly brace here to enclose ifdef block in offload construct
}

With those changes the program runs as expected. Also, just a note, the __declspec(target(mic:0)) syntax does not include the target id, so this only needs to be: __declspec(target(mic))  For more info, see: Placing Variables and Functions on the Coprocessor

Hope that helps.

在原帖中查看解决方案

0 项奖励
5 回复数
wang_p_1
初学者
1,038 次查看

In order to simplify this problem, I have simulated the runtime environment.

#head.h

class point{
	public:
	point(){}
	void f1();
	void f2();
		
};

#head.cpp

#include"head.h"
#include"stdio.h"
__declspec(target(mic:0)) __forceinline void print()
{
#ifdef __MIC__
	printf("I'm monkey\n");
	fflush(0);
#else
	printf("I'm dog\n");
	fflush(0);
#endif
}

void point::f1()
{
#pragma offload target(mic:0)
{
#ifdef __MIC__
	for(int i = 0; i < 20; i++)
	printf("I'm mic\n");
	fflush(0);
}
#else
}
	printf("I'm CPU\n");
#endif
}

void point::f2()
{
#pragma offload target(mic:0)
{
	print();
}
}

#main.cpp

#include"head.h"
int main()
{
	point p;
	p.f1();//output "I'm CPU". but without "I'm mic"
	p.f2();//output "I'm monkey".It's ok,mic print this.But why??
return 0 ;
}

$icpc -V 

//14.0

$icpc head.cpp main.cpp -o main

$./main

//I'm cpu

//I'm monkey

__________________________________________________________________________________

but why class point member fucntion f1 can not print out "I'm mic" ?? 

this is an interesting topic, isn't it?  ^.^

thank u !

0 项奖励
Kevin_D_Intel
员工
1,039 次查看

Use of __MIC__ inside the pragma offload is not supported. The compiler does not enforce this restriction so the programmer needs to be aware of and avoid this,  otherwise it can lead to problems with what the compiler sees for the host and target compilations. For more info see: Restrictions on Offloaded Code Using a Pragma

In this case, there are some missing/mis-placed curly-braces within the #ifdef/else/endif construction that lead to the unexpected results. As written the #ifdef/endif is not completely enclosed inside the pragma offload construct which leads the unexpected results.

The function f1 should be modified as shown below.

void point::f1()
{
#pragma offload target(mic:0)
{
#ifdef __MIC__
        for(int i = 0; i < 20; i++)
        {                                                 // Add this missing curly brace to for loop
             printf("I'm mic\n");
             fflush(0);
        }
#else
//}                                                       // this curly brace is mis-placed - it should appear after the #endif below
        printf("I'm CPU\n");
#endif
}                                                         // place curly brace here to enclose ifdef block in offload construct
}

With those changes the program runs as expected. Also, just a note, the __declspec(target(mic:0)) syntax does not include the target id, so this only needs to be: __declspec(target(mic))  For more info, see: Placing Variables and Functions on the Coprocessor

Hope that helps.

0 项奖励
wang_p_1
初学者
1,038 次查看

It does work well. 

 thanks very much.

Kevin Davis (Intel) wrote:

Use of __MIC__ inside the pragma offload is not supported. The compiler does not enforce this restriction so the programmer needs to be aware of and avoid this,  otherwise it can lead to problems with what the compiler sees for the host and target compilations. For more info see: Restrictions on Offloaded Code Using a Pragma

In this case, there are some missing/mis-placed curly-braces within the #ifdef/else/endif construction that lead to the unexpected results. As written the #ifdef/endif is not completely enclosed inside the pragma offload construct which leads the unexpected results.

The function f1 should be modified as shown below.

void point::f1()
{
#pragma offload target(mic:0)
{
#ifdef __MIC__
        for(int i = 0; i < 20; i++)
        {                                                 // Add this missing curly brace to for loop
             printf("I'm mic\n");
             fflush(0);
        }
#else
//}                                                       // this curly brace is mis-placed - it should appear after the #endif below
        printf("I'm CPU\n");
#endif
}                                                         // place curly brace here to enclose ifdef block in offload construct
}

With those changes the program runs as expected. Also, just a note, the __declspec(target(mic:0)) syntax does not include the target id, so this only needs to be: __declspec(target(mic))  For more info, see: Placing Variables and Functions on the Coprocessor

Hope that helps.

0 项奖励
Kevin_D_Intel
员工
1,038 次查看

Great. You're welcome.

0 项奖励
wang_p_1
初学者
1,038 次查看

after reading these information, we get this conclusion:

"Do not use the __MIC__ macro inside the statement following a #pragma offload statement."

thanks your link, it's really useful !

Kevin Davis (Intel) wrote:

Use of __MIC__ inside the pragma offload is not supported. The compiler does not enforce this restriction so the programmer needs to be aware of and avoid this,  otherwise it can lead to problems with what the compiler sees for the host and target compilations. For more info see: Restrictions on Offloaded Code Using a Pragma

In this case, there are some missing/mis-placed curly-braces within the #ifdef/else/endif construction that lead to the unexpected results. As written the #ifdef/endif is not completely enclosed inside the pragma offload construct which leads the unexpected results.

The function f1 should be modified as shown below.

void point::f1()
{
#pragma offload target(mic:0)
{
#ifdef __MIC__
        for(int i = 0; i < 20; i++)
        {                                                 // Add this missing curly brace to for loop
             printf("I'm mic\n");
             fflush(0);
        }
#else
//}                                                       // this curly brace is mis-placed - it should appear after the #endif below
        printf("I'm CPU\n");
#endif
}                                                         // place curly brace here to enclose ifdef block in offload construct
}

With those changes the program runs as expected. Also, just a note, the __declspec(target(mic:0)) syntax does not include the target id, so this only needs to be: __declspec(target(mic))  For more info, see: Placing Variables and Functions on the Coprocessor

Hope that helps.

0 项奖励
回复