Return pointer from MIC malloc

Return pointer from MIC malloc

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[i], p[i]);

#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]=i;
  }
  return p;
}

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

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

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]=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[i]);
     printf("\n");
   }
 }

 

Yes, it work! Thanks a lot!

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

www.quickthreadprogramming.com

Jim,

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

Gregg Skinner

 

Leave a Comment

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