Software Archive
Read-only legacy content
17060 Discussions

Offloading virtual functions?

Sanchit_Misra
Employee
887 Views

Hi, Can we offload virtual functions. I am trying to do that using the following code and I am getting seg fault. #include using namespace std; class Polygon { public: #pragma offload_attribute(push, target(mic)) virtual int area (int width, int height) { return 0; } #pragma offload_attribute(pop) int area_wrapper (int width, int height) { int c; #pragma offload target(mic:0) \ in(width, height) \ out(c) { c = this->area(width, height); } return c; } }; class Rectangle: public Polygon { public: #pragma offload_attribute(push, target(mic)) int area (int width, int height) { return width * height; } #pragma offload_attribute(pop) }; class Triangle: public Polygon { public: #pragma offload_attribute(push, target(mic)) int area (int width, int height) { return (width * height / 2); } #pragma offload_attribute(pop) }; int main () { Rectangle * ppoly1 = new Rectangle(); Triangle * ppoly2 = new Triangle(); Polygon * ppoly3 = new Polygon(); cout << ppoly1->area_wrapper(4, 5) << '\n'; cout << ppoly2->area_wrapper(4, 5) << '\n'; cout << ppoly3->area_wrapper(4, 5) << '\n'; return 0; } When I compile and run it, it results in: $ icpc polygon.cpp $./a.out offload error: process on the device 0 was terminated by signal 11 (SIGSEGV) Thanks in advance, Sanchit

0 Kudos
4 Replies
Kevin_D_Intel
Employee
887 Views

I believe this may require using _Cilk_shared/_Cilk_offload. Let me inquire with others more knowledgeable.

0 Kudos
Kevin_D_Intel
Employee
887 Views

Our C++ Developer confirmed offloading virtual functions requires using _Cilk_shared/_Cilk_for and provided the modified version of your program shown below. The output of the modified program is:

$ icpc t_new.cpp
$ ./a.out
20
10
0

t_new.cpp
==========
#include <iostream>
#include <offload.h>

using namespace std;

#pragma offload_attribute(push, _Cilk_shared)
class Polygon {
public:
   virtual int area (int width, int height)
   {
      return 0;
   }

   int area_wrapper (int width, int height)
   {
      int c;
      {
      c = _Cilk_offload this->area(width, height);
      }
      return c;
   }
};
#pragma offload_attribute(pop)

#pragma offload_attribute(push, _Cilk_shared)
class Rectangle: public Polygon
{
   public:
   int area (int width, int height)
   {
      return width * height;
   }
};
#pragma offload_attribute(pop)

#pragma offload_attribute(push, _Cilk_shared)
class Triangle: public Polygon
{
   public:
   int area (int width, int height)
   {
      return (width * height / 2);
   }
};
#pragma offload_attribute(pop)

int main ()
{
   Rectangle * ppoly1 = new (_Offload_shared_malloc(sizeof(Rectangle))) Rectangle();
   Triangle * ppoly2 = new (_Offload_shared_malloc(sizeof(Triangle))) Triangle();
   Polygon * ppoly3 = new (_Offload_shared_malloc(sizeof(Polygon))) Polygon();
   cout << ppoly1->area_wrapper(4, 5) << '\n';
   cout << ppoly2->area_wrapper(4, 5) << '\n';
   cout << ppoly3->area_wrapper(4, 5) << '\n';
   return 0;
}

0 Kudos
Sanchit_Misra
Employee
887 Views

Thanks Kevin!

Will it hurt the performance to have a class instance in virtual shared memory between CPU and MIC using _Offload_shared_malloc(); especially if that class instance is accessed in critical section?

 

--Sanchit

 

0 Kudos
Kevin_D_Intel
Employee
887 Views

Hi Sanchit - Sorry, we are not clear about your question.

Does "critical section" mean a section of code critical to the code's performance? Or a code section guarded in some fashion for critical or exclusive access?

Or are you asking whether shared memory access is dramatically slower than non-shared memory and will having class objects in shared memory whose access is critical to the code's performance hurt overall performance?

0 Kudos
Reply