0

I'm trying to figure out how to write an ASCII string to a file using MPI IO. Its my understanding that data written to a file using MPI IO is written out as byte data, so I'd like to find a method of writing to a file that would allow me to interpret it as a CSV. For example, I have a string named myString that contains "1,1,1,1,['a','b','c']\n". Would I just write to that string as a char array (char* myString) and then use MPI_File_write(file, myString, rank * BUFSIZE * sizeof(char), MPI_CHAR, MPI_STATUS_IGNORE) or would this not output the correct format?

Mav
  • 115
  • 9
  • To elaborate a bit more, I'm interested in doing this: https://stackoverflow.com/questions/15605599/mpi-parallel-io-in-ascii-format-how-do-i-do-it but the c_str() method doesn't seem to exist in C and I'm not sure if the above casting method would work without error. – Mav Jan 10 '22 at 21:41

1 Answers1

1

It seems to work as you expected - I wrote one extra character to ensure I got null termination:

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

int main(void)
{
  int rank;
  MPI_Status status;
  MPI_File fh;

  char *x = "1111abc\n";

  MPI_Init(NULL, NULL);

  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  MPI_File_open(MPI_COMM_WORLD, "mpiio.dat", MPI_MODE_CREATE | MPI_MODE_WRONLY,
                MPI_INFO_NULL, &fh);

  if (rank == 0) MPI_File_write(fh, x, strlen(x)+1, MPI_CHAR, &status);
  
  MPI_File_close(&fh);

  return 0;
}

If I compile and run on my laptop:

me@laptop$ mpicc -o cstring cstring.c
me@laptop$ mpirun -n 2 ./cstring
me@laptop$ cat mpiio.dat 
1111abc
David Henty
  • 1,694
  • 9
  • 11