- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am trying to explore the code offloading construct .In the following program the offloaded region fetches the architecture of MIC card.
#include<stdio.h> #include<omp.h> void main() { FILE *fp,*fp1; char data[100],data1[100],final[100]; #pragma offload target(mic: 0) inout(data , fp) { fp=popen("uname -m","r"); fread(data, sizeof(char),100 , fp); fclose(fp); } puts(data); }
Here are three sample runs of this program:
- The first run succeeds ,
- Second run hangs(manually killing program using ^C )
- Third run gives buffer write fail error
[puneet@paramshavak NWCHEM_INTEL_MICOFFLOAD]$ ./a.out
[Offload] [MIC 0] [File] omp.c [Offload] [MIC 0] [Line] 8 [Offload] [MIC 0] [Tag] Tag 0 HOST: Offload function __offload_entry_omp_c_8mainicc161915962IRh4TM, is_empty=0, #varDescs=4, #waits=0, signal=none HOST: Total pointer data sent to target: [316] bytes HOST: Total copyin data sent to target: [16] bytes HOST: Total pointer data received from target: [316] bytes MIC0: Calling COIBufferAddRef 0x7f4cbd802000 MIC0: AddRef count = 1 MIC0: Calling COIBufferAddRef 0x7f4cbd7ff000 MIC0: AddRef count = 1 MIC0: Total copyin data received from host: [16] bytes MIC0: Total copyout data sent to host: [0] bytes HOST: Total copyout data received from target: [0] bytes [Offload] [HOST] [Tag 0] [CPU Time] 0.265805(seconds) [Offload] [MIC 0] [Tag 0] [MIC Time] 0.036923(seconds) k1om ************************************************************** timer data (sec) **************************************************************
[puneet@paramshavak NWCHEM_INTEL_MICOFFLOAD]$ ./a.out
[Offload] [MIC 0] [File] omp.c [Offload] [MIC 0] [Line] 8 [Offload] [MIC 0] [Tag] Tag 0 HOST: Offload function __offload_entry_omp_c_8mainicc161915962IRh4TM, is_empty=0, #varDescs=4, #waits=0, signal=none HOST: Total pointer data sent to target: [316] bytes HOST: Total copyin data sent to target: [16] bytes HOST: Total pointer data received from target: [316] bytes MIC0: Calling COIBufferAddRef 0x7f8eeedd3000 MIC0: AddRef count = 1 MIC0: Calling COIBufferAddRef 0x7f8eeedd1000 MIC0: AddRef count = 1 MIC0: Total copyin data received from host: [16] bytes MIC0: Total copyout data sent to host: [0] bytes HOST: Total copyout data received from target: [0] bytes ^C
[puneet@paramshavak NWCHEM_INTEL_MICOFFLOAD]$ ./a.out
[Offload] [MIC 0] [File] omp.c [Offload] [MIC 0] [Line] 8 [Offload] [MIC 0] [Tag] Tag 0 HOST: Offload function __offload_entry_omp_c_8mainicc161915962IRh4TM, is_empty=0, #varDescs=4, #waits=0, signal=none offload error: buffer write failed (error code 6) ************************************************************** timer data (sec) ************************************************************** Offload from file omp.cLine 8 host: total offload time 0.00000 host: initialize target 0.20055 host: acquire target 0.00014 host: wait dependencies 0.00000 host: setup buffers 0.00383 host: allocate buffers 0.00159 host: setup misc_data 0.00000 host: allocate buffer 0.00000 host: send pointers 0.00000 host: gather inputs 0.00000 host: map IN data buffer 0.00000 host: unmap IN data buffer 0.00000 host: initiate compute 0.00000 host: wait compute 0.00000 host: initiate pointer reads 0.00000 host: scatter outputs 0.00000 host: map OUT data buffer 0.00000 host: unmap OUT data buffer 0.00000 host: wait pointer reads 0.00000 host: destroy buffers 0.00000 target: total time 0.00000javascript:void(0) target: setup offload descriptor 0.00000 target: entry lookup 0.00000 target: entry time 0.00000 target: scatter inputs 0.00000 target: add buffer reference 0.00000 target: compute 0.00000 target: gather outputs 0.00000 target: remove buffer reference 0.00000
Am i missing out on some essentials here ?(buffer cleanup etc.) .Some insight into the reason & solution will be very useful.
Eagerly Awaiting your replies,
Regards,
Puneet Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Puneet,
Can you please verify if you get the expected results with the following update to your code:
#include<stdio.h> #include<omp.h> void main() { FILE *fp,*fp1; //dummy initialization of fp //otherwise you pass null pointer fp = (FILE *)malloc(sizeof(FILE *)); char data[100],data1[100],final[100]; #pragma offload target(mic: 0) inout(data, fp) { fp=popen("uname -m","r"); if(NULL != fp){ fread(data, sizeof(char),100 , fp); pclose(fp); puts(data); } } if(NULL != fp) free(fp); }
Thanks,
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Puneet,
What version of the Intel compiler are you using? You should have received a warning about fp being a pointer with no data associated with it.
Why are you passing the pointer fp to the offload section?
Regards
--
Taylor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Puneet,
Can you please verify if you get the expected results with the following update to your code:
#include<stdio.h> #include<omp.h> void main() { FILE *fp,*fp1; //dummy initialization of fp //otherwise you pass null pointer fp = (FILE *)malloc(sizeof(FILE *)); char data[100],data1[100],final[100]; #pragma offload target(mic: 0) inout(data, fp) { fp=popen("uname -m","r"); if(NULL != fp){ fread(data, sizeof(char),100 , fp); pclose(fp); puts(data); } } if(NULL != fp) free(fp); }
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thankyou very much Sunny, Your previous reply resolved my issue. It would be very kind of you if you can explain the reason that why
fp = (FILE *)malloc(sizeof(FILE *));
makes my program work correctly ?
- Follow up question:
AFAIK the best way of getting private pointer in omp is to get dynamic memory within parallel region!!
#pragma omp parallel { fp=malloc(sizeof(FILE*)); }
Approach 1:
Sharing a pointer seems no use here !!
#include<stdio.h> #include<omp.h> void main() { char *p; p=(char*)malloc(sizeof(char)*1); #pragma omp parallel private(p) { printf("%p\n",p); } }
Output: (nil)
(nil)
(nil)
(nil)
(nil)
Approach 2:
and here clearly, i am getting private pointers, but this approach makes my offload program wait!!!
#include<stdio.h> #include<omp.h> void main() { char *p; #pragma omp parallel private(p) { p=(char*)malloc(sizeof(char)*1); printf("%p\n",p); } }
Output:0xae49b0
0x2b787c000950
0x2b7880000950
0x2b7874000950
0x2b7870000950
So as per approach 2 ,I modified my offload code slightly for getting more threads(initializing fp within parallel region).This code simply hangs up (seems like it waits for something!!) and does not produce any error:
FILE *fp; char data[100]; #pragma omp parallel default(none) private(fp,data) #pragma offload target(mic:0) inout(fp,data) { fp = (FILE *)malloc(sizeof(FILE *)); fp=popen("uname -p","r"); fread(data,sizeof(char),100,fp); pclose(fp); puts(data); } if(fp!=NULL) free(fp); return 0;
Though ,the following is not the best way of getting private pointer ; approach 1 sets my program work fine(with the fp initiallized before omp parallel region)
FILE *fp; char data[100]; fp = (FILE *)malloc(sizeof(FILE *)); #pragma omp parallel default(none) private(fp,data) #pragma offload target(mic:0) inout(fp,data) { fp=popen("uname -p","r"); fread(data,sizeof(char),100,fp); pclose(fp); puts(data); } if(fp!=NULL) free(fp); return 0;
What might be the reason behind this behaviour ?any hints/suggestions will be very useful !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Puneet,
Please find some of the possible reasons you are seeing the above behavior.
1. Why allocating fp works?
This is because when a program runs in a heterogeneous environment (with host and coprocessors), program variables are copied back and forth between CPU and the target. So if you don't initialize fp and try to offload, you basically sign up for copy from a uninitialized variable (null pointer - in our case).
2. Also as per my knowledge when you use openMP parallel with private clause: A new variable of the same type is declared once for each thread and that variable should be assumed to be uninitialized. You will still be able to use same variable name, however all your references will be replaced by new per thread variables.
Considering the above 2 conditions, you might have to do both to get expected result: Initialize fp outside and then initialized per thread fp inside openMP region as follows:
#include <stdio.h> #include <omp.h> int main(){ FILE *fp; fp = (FILE *)malloc(sizeof(FILE *)); char data[100]; #pragma omp parallel default(none) private(fp,data) #pragma offload target(mic:0) inout(fp,data) { fp = (FILE *)malloc(sizeof(FILE *)); fp=popen("uname -m","r"); fread(data,sizeof(char),100,fp); pclose(fp); puts(data); } if(fp!=NULL) free(fp); return 0; }
Also if you don't mind, can you please explain me what are you trying to achieve here?
Thanks,

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page