3

I know MPI IO writes to files in binary format, however this time I need it to be in ASCII. I need this because I am using the Netpbm for images, and the header must be ASCII (so the program can read P1, P2, P3, P4, P5 or P6 and determine if the content of the image is in ASCII or binary among other things).

The code I am using is the one provided in (simple example) MPI parallel io writing garbage and I wonder if there is a small moification in the code that allows me to output the content of the integer buffer in ASCII instead of binary.

Additionally, having a way to output this in ASCII will also allow for easier debugging.

Community
  • 1
  • 1
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • 3
    Netpbm of course also allows binary format for the body of the data (eg, P4-P6 in the link you provided). However, the answer is yes, even for ASCII format: just consider the file to be a big 2d array of characters, and write to subarrays by formatting the character strings yourself [as in this answer](http://stackoverflow.com/questions/9777828/writing-a-matrix-into-a-single-txt-file-with-mpi/9810006#9810006). – Jonathan Dursi Mar 25 '13 at 00:45

2 Answers2

2

You can use strlen for the buffer size

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(int argc,char *argv[])
{

    int  namelen, numprocs, rank = 7;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);    
    MPI_Get_processor_name(processor_name,&namelen);
    MPI_Status status;
    MPI_File fh;
    string test;
    const char* buf[100]; 
    string g = "test" + to_string(rank) + ".txt";
    MPI_File_open(MPI_COMM_SELF, g.c_str(), MPI_MODE_CREATE | MPI_MODE_WRONLY,MPI_INFO_NULL,&fh);
    for (int i=0; i < 100; i++){
        test = to_string(i) + "\n";
        buf[i] = test.c_str();
        MPI_File_write(fh, buf[i], strlen(buf[i]), MPI_CHAR, &status);
    }
    MPI_File_close(&fh);
    MPI_Finalize();
    return 0;
}

Notice the MPI_File_write(fh, buf[i], strlen(buf[i]), MPI_CHAR, &status);

Hope that helps.

Nikki Mino
  • 309
  • 1
  • 2
  • 12
0

sprint your data as char array and write using MPI-IO with Datatype MPI_CHAR

Mohsin
  • 1