Intel® oneAPI DPC++/C++ Compiler Developer Guide and Reference

ID 767253
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Example for aio_suspend Function

The following example illustrates a wait operation execution with the aio_suspend() function. See Example for aio_read and aio_write Functions for a do_compute function definition.

// icx -c do_compute.c
// icx aio_sample3.c do_compute.obj
// aio_sample3.exe

#define DIM_X   123
#define DIM_Y    70
double  aio_dat[DIM_Y] = {0};
static volatile int aio_flg = 1;

#include <aio.h>
#include <stdio.h>
typedef struct aiocb  aiocb_t;
aiocb_t               my_aio;

#define IC_AIO_DATA_INIT(_aio, _fd, _dat, _len, _off)\
  {memset(&_aio, 0, sizeof(_aio)); \
  _aio.aio_fildes = _fd;          \
  _aio.aio_buf    = _dat;         \
  _aio.aio_nbytes = _len;         \
  _aio.aio_offset = _off;}

int main()
{
	double do_compute(double A, double B, int arr_len);
    char    *fname = "aio_sample2.dat";
		
	HANDLE fd = CreateFile("dat",
	  GENERIC_READ | GENERIC_WRITE,
	  FILE_SHARE_READ,
	  NULL,
	  OPEN_ALWAYS,
	  FILE_ATTRIBUTE_NORMAL,
	  NULL);

	static struct aiocb   aio[2]; 
	static struct aiocb  *aio_list[2] = {&aio[0], &aio[1]}; 
	int i, j, ret; 
	my_aio.aio_fildes = fd;
 
	/* Data initialization */ 
	IC_AIO_DATA_INIT(aio[0], fd, "rec#1\n", strlen("rec#1\n"), 0)
	IC_AIO_DATA_INIT(aio[1], fd, "rec#2\n", strlen("rec#2\n"), aio[0].aio_nbytes)

	/* Asynch-write */
	if (aio_write(&aio[0]) == -1) return errno;
	if (aio_write(&aio[1]) == -1) return errno;

	/* Do some complex computation */ 
   for (i = 0; i < DIM_X; i++) {
	   for (j = 0; j < DIM_Y; j++)
		   aio_dat[j] = do_compute(i, j, DIM_X);
   }

	/* do the wait operation using sleep() */ 
	ret = aio_suspend(aio_list, 2, 0); 
	if (ret == -1) return errno; 

	CloseHandle(fd);

	printf("\nDone\n");

	return 0;
}

Execution Result

> aio_sample3.exe
Done
> type dat
rec#1 rec#2_

See Also