Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs have moved to the Altera Community. Existing Intel Community members can sign in with their current credentials.

Return pointer from MIC malloc

Guangming_T_
Beginner
870 Views

In my project, I need to pass pointer among different offload functions. However, I do not want to use global variables. For example. 

I want to allocate an array on accelerator in new_array function, and hope that it would return an address on accelerator side so that I could pass the address to the next function exe_array. But, the following codes do not work.

Any solution to this case? Say again, I do not want to use global variables. Thanks!

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

void* new_array();
void exe_array(int *); 
int main()
{
  int i;
  int * p;
  p = new_array();
  exe_array(p);
  for(i=0;i<32;i++)
    printf("p=%p: %d\n",&p, p);

#pragma offload target(mic) nocopy(p)
  free(p);
}

void* new_array()
{
  int i;
  int *p; 
#pragma offload target(mic) nocopy(p)
  {
  p=(int *) malloc(32*sizeof(int));
  for(i=0; i<32; i++)
    p=i;
  }
  return p;
}

void exe_array(int *p) 
{
  int i;
  #pragma offload target(mic) nocopy(p)                                                                                                                           
  {
    for(i=0; i<32;i++)
      p=-1;
  }
}

0 Kudos
1 Solution
Gregg_S_Intel
Employee
870 Views

Maybe you want something like this.

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

void* new_array();
void exe_array(void *);
 int main()
 {
   int i;
   void * p;
   p = new_array();
   exe_array(p);

#pragma offload target(mic)
   free(p);
 }

void* new_array()
 {
   void *p;
 #pragma offload target(mic)
   {
   p=malloc(32*sizeof(int));
   int i;
   int* q=(int*)p;
   for(i=0; i<32; i++)
     q=i;
   }
   return p;
 }

void exe_array(void *p)
 {
   #pragma offload target(mic)
   {
     int i;
     int* q=(int*)p;
     for(i=0; i<32;i++)
       printf("%d ",q);
     printf("\n");
   }
 }

 

View solution in original post

0 Kudos
4 Replies
Gregg_S_Intel
Employee
871 Views

Maybe you want something like this.

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

void* new_array();
void exe_array(void *);
 int main()
 {
   int i;
   void * p;
   p = new_array();
   exe_array(p);

#pragma offload target(mic)
   free(p);
 }

void* new_array()
 {
   void *p;
 #pragma offload target(mic)
   {
   p=malloc(32*sizeof(int));
   int i;
   int* q=(int*)p;
   for(i=0; i<32; i++)
     q=i;
   }
   return p;
 }

void exe_array(void *p)
 {
   #pragma offload target(mic)
   {
     int i;
     int* q=(int*)p;
     for(i=0; i<32;i++)
       printf("%d ",q);
     printf("\n");
   }
 }

 

0 Kudos
Guangming_T_
Beginner
870 Views

Yes, it work! Thanks a lot!

0 Kudos
jimdempseyatthecove
Honored Contributor III
870 Views

Greg,

Is there a requirement that the copy of the pointer on the host be void*?

I would imagine that it is not, however, use of void* will hinder your using the pointer unintentionally and would be good programming practice.

Jim Dempsey

0 Kudos
Gregg_S_Intel
Employee
870 Views

Jim,

I think we do have to use a void type to get a pointer from the coprocessor.

Gregg Skinner

 

0 Kudos
Reply