root/orte/test/mpi/read_write.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  * A simple MPI test that reads lines from standard input and writes them
   3  * to both standard output and a file
   4  */
   5 
   6 #include <stdio.h>
   7 #include <stdlib.h>
   8 #include <string.h>
   9 #include <unistd.h>
  10 
  11 #include <mpi.h>
  12 
  13 int main(int argc, char *argv[])
  14 {
  15     int self;
  16     int size;
  17     int value;
  18     char line[1024];
  19     FILE *file;
  20     unsigned int bytes = 0;
  21     int reader = 0;
  22     char *junk;
  23 
  24     if (2 == argc) {
  25         /* a reader was specified */
  26         reader = strtol(argv[1], NULL, 10);
  27         fprintf(stderr, "reading from %d\n", reader);
  28     }
  29 
  30     MPI_Init(NULL, NULL);
  31     MPI_Comm_rank(MPI_COMM_WORLD, &self);
  32     MPI_Comm_size(MPI_COMM_WORLD, &size);
  33     printf("Hello from process %d of %d\n", self, size);
  34     MPI_Barrier(MPI_COMM_WORLD);
  35     if (-1 == reader || reader == self) {
  36         opal_asprintf(&junk, "./junk%d", self);
  37         unlink(junk);
  38         file = fopen(junk, "w+");
  39         if (NULL == file) {
  40             fprintf(stderr, "Couldn't open %s!", junk);
  41             free(junk);
  42             MPI_Abort(MPI_COMM_WORLD, 1);
  43         }
  44         while (NULL != fgets(line, sizeof(line), stdin)) {
  45             fprintf(stderr, "%s", line);
  46             fprintf(file, "%s", line);
  47             bytes += strlen(line) + 1;
  48         }
  49         fclose(file);
  50         fprintf(stderr, "\nWrote %d bytes to %s\n", bytes, junk);
  51         free(junk);
  52     }
  53     MPI_Finalize();
  54 
  55     return 0;
  56 }

/* [<][>][^][v][top][bottom][index][help] */