Using streams to override inputs and outputs

This example shows how to pass data to an input card of a map and how to get data from an output card of a map (the sending of a single buffer to the API).

If the data is received in a number of smaller pieces, it is acceptable, and indeed efficient, to write the data to the stream in small pieces.

#include "dtxpi.h"

#define CHECK_ERR(rc) if(MPIRC_FAILED(rc))  
{ printf("Last error is: %s\n",mpiErrorGetText(rc)); return -1;}

int main()
{
const char             *szMsg;
char szInputBuffer[] = "This is my input data";
int                    iRC;
int                    bIsEnd;
void                   *pData;
size_t                 nSizeOfData;
size_t                 nLen;
HMPIMAP                hMap;
HMPIADAPT              hAdapter;
HMPICARD               hCard;
HMPISTREAM             hStream;
HMPISTREAMPAGE         hPage;
MPIRC                  rc;

rc = mpiInitAPI(NULL);
CHECK_ERR(rc);
rc = mpiMapLoadFile (&hMap, "test5.mmc");
CHECK_ERR(rc);

/* Get the adapter object handle for input card #1*/
rc = mpiMapGetInputCardObject (hMap, 1, &hCard);
CHECK_ERR(rc);

/* Override the adapter in input card #1 to be a stream */
rc = mpiCardOverrideAdapter(hCard,NULL, MPI_ADAPTYPE_STREAM);
CHECK_ERR(rc);

rc = mpiCardGetAdapterObject (hCard, &hAdapter);
CHECK_ERR(rc);
/* Get the handle to the stream object */
rc = mpiPropertyGetObject (hAdapter, MPIP_ADAPTER_DATA_FROM_ADAPT,
 0, &hStream);
CHECK_ERR(rc);

/* Send a single large page */
rc = mpiStreamWrite (hStream, szInputBuffer, strlen(szInputBuffer));
CHECK_ERR(rc);

/* Get the adapter object handle for output card #2*/
rc = mpiMapGetOutputCardObject (hMap, 2, &hCard);
CHECK_ERR(rc);

/* Override the adapter in output card #2 to be a stream */
rc = mpiCardOverrideAdapter(hCard,NULL, MPI_ADAPTYPE_STREAM);
CHECK_ERR(rc);

rc = mpiMapRun (hMap);
CHECK_ERR(rc);

rc = mpiPropertyGetText(hMap, MPIP_OBJECT_ERROR_MSG, 0,
 &szMsg,&nLen);
CHECK_ERR(rc);
rc = mpiPropertyGetInteger (hMap, MPIP_OBJECT_ERROR_CODE, 0, &iRC);
CHECK_ERR(rc);
printf("Map status: %s (%d)\n", szMsg, iRC);

/* Get the adapter object handle for output card #2*/
rc = mpiCardGetAdapterObject (hCard, &hAdapter);
CHECK_ERR(rc);
/* Get the handle to the stream object */
rc = mpiPropertyGetObject (hAdapter, MPIP_ADAPTER_DATA_TO_ADAPT, 0,
 &hStream);
CHECK_ERR(rc);

/* Get the data in pieces from the stream */
mpiStreamSeek(hStream,0,SEEK_SET);
while (1)
{
rc = mpiStreamIsEnd (hStream, &bIsEnd);
CHECK_ERR(rc);
/*Clean and Break*/
if (bIsEnd)
{
rc = mpiStreamSetSize(hStream, 0); 
break;
}
rc = mpiStreamReadPage (hStream, &hPage);
CHECK_ERR(rc);
rc = mpiStreamPageGetInfo (hPage, &pData, &nSizeOfData);
CHECK_ERR(rc);
fwrite(pData,1,nSizeOfData,stdout);
/*                     DoSomethingWithData (pData, nSizeOfData); */
}

rc = mpiMapUnload (hMap);

CHECK_ERR(rc);
rc = mpiTermAPI();
CHECK_ERR(rc);
return 0;