Mav
Mav

Reputation: 117

Write string with MPI IO in C

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?

Upvotes: 0

Views: 317

Answers (1)

David Henty
David Henty

Reputation: 1764

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

Upvotes: 2

Related Questions