Skip to content

Instantly share code, notes, and snippets.

@cwsmith
Created July 10, 2014 12:15
Show Gist options
  • Save cwsmith/166d5beb400f3a8136f7 to your computer and use it in GitHub Desktop.
Save cwsmith/166d5beb400f3a8136f7 to your computer and use it in GitHub Desktop.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
int rank, nprocs;
MPI_File file;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_File_open(MPI_COMM_WORLD, "test.txt", MPI_MODE_CREATE|MPI_MODE_WRONLY,
MPI_INFO_NULL, &file);
char str[32];
int idx[2][2] = {1,3,0,2};
int val[2][2] = {10,2,1,4};
for (int i=0; i< 2; i++) {
sprintf(str, "%-15d\n", val[rank][i]);
MPI_Offset offset = idx[rank][i]*16;
MPI_File_seek(file, offset, MPI_SEEK_SET);
MPI_File_get_position(file, &offset);
MPI_Status status;
printf("%d len %lu val %d offset %lld idx %d str \"%s\"\n", rank, strlen(str), val[rank][i], offset, idx[rank][i], str);
MPI_File_write(file, str, strlen(str), MPI_CHAR, &status);
}
MPI_File_close(&file);
MPI_Finalize();
return 0;
}
~
@ibaned
Copy link

ibaned commented Jul 10, 2014

that should work, depending on how fast it is for all processes to seek once per element.
I vaguely remember an MPI File set size command, which might make things faster.

@cwsmith
Copy link
Author

cwsmith commented Jul 10, 2014

@cwsmith
Copy link
Author

cwsmith commented Jul 10, 2014

note, this is setup for scattered indices, we will be collecting a range of contiguous indices on each part so only a single write is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment