Intel® C++ Compiler XE 13.1 User and Reference Guides

Logging Stdout and Stderr from Offloaded Code

This topic only applies to Intel® Many Integrated Core Architecture (Intel® MIC Architecture).

Capturing C/C++ program output to stdout and stderr from writes performed inside offloaded code may require calling fflush(). This function is under the control of the coprocessor offload interface (COI) layer, not the compiler or compiler run-time libraries.

Writes performed in offloaded code may be buffered when they are directed to a file, while writes to the console happen immediately. If the application exits prior to the buffer threshold being reached then the output data may be lost. Therefore, I/O to a file requires an additional explicit fflush() on the coprocessor to capture this output data, as shown in the sample.c example below.

Example

sample.c:

#pragma offload_attribute(push,target(mic))
#include <stdio.h>

void sub()
{
  printf("hello from MIC\n");
  fflush(0);
}

#pragma offload_attribute(pop)

int main(int argc, char* argv[])
{
   printf("hello from main\n");
   #pragma offload target(mic)
   sub();
}

Compile and execute the above example as follows:

$ icc -offload-build –o sample sample.c
$ ./sample > log.txt
$ cat log.txt hello from MIC hello from main

When calling fflush(), you can control directing stdout and stderr to the same or separate output files.

For example, to send the stdout (1) and stderr (2) to log.txt, enter:

$ ./sample > log.txt 2>&1

To redirect stdout to one file and stderr to another, use:

$ ./sample > log.txt 2> log_err.txt


Submit feedback on this help topic