2008-10-23 23:11:05 +04:00
|
|
|
/*
|
|
|
|
* A simple MPI test that reads lines from standard input and writes them
|
|
|
|
* to both standard output and a file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <mpi.h>
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int self;
|
|
|
|
int size;
|
|
|
|
int value;
|
|
|
|
char line[1024];
|
|
|
|
FILE *file;
|
|
|
|
unsigned int bytes = 0;
|
2008-10-25 18:38:06 +04:00
|
|
|
int reader = 0;
|
|
|
|
char *junk;
|
|
|
|
|
|
|
|
if (2 == argc) {
|
|
|
|
/* a reader was specified */
|
|
|
|
reader = strtol(argv[1], NULL, 10);
|
|
|
|
fprintf(stderr, "reading from %d\n", reader);
|
|
|
|
}
|
|
|
|
|
2008-10-23 23:11:05 +04:00
|
|
|
MPI_Init(NULL, NULL);
|
|
|
|
MPI_Comm_rank(MPI_COMM_WORLD, &self);
|
|
|
|
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
|
|
|
printf("Hello from process %d of %d\n", self, size);
|
|
|
|
MPI_Barrier(MPI_COMM_WORLD);
|
2008-10-25 18:38:06 +04:00
|
|
|
if (-1 == reader || reader == self) {
|
|
|
|
asprintf(&junk, "./junk%d", self);
|
|
|
|
unlink(junk);
|
|
|
|
file = fopen(junk, "w+");
|
2008-10-23 23:11:05 +04:00
|
|
|
if (NULL == file) {
|
2008-10-25 18:38:06 +04:00
|
|
|
fprintf(stderr, "Couldn't open %s!", junk);
|
|
|
|
free(junk);
|
2008-10-23 23:11:05 +04:00
|
|
|
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
|
|
}
|
|
|
|
while (NULL != fgets(line, sizeof(line), stdin)) {
|
2012-05-02 23:00:05 +04:00
|
|
|
fprintf(stderr, "%s", line);
|
|
|
|
fprintf(file, "%s", line);
|
2008-10-23 23:11:05 +04:00
|
|
|
bytes += strlen(line) + 1;
|
|
|
|
}
|
|
|
|
fclose(file);
|
2008-10-25 18:38:06 +04:00
|
|
|
fprintf(stderr, "\nWrote %d bytes to %s\n", bytes, junk);
|
|
|
|
free(junk);
|
2008-10-23 23:11:05 +04:00
|
|
|
}
|
|
|
|
MPI_Finalize();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|