-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMPITest.c
69 lines (58 loc) · 1.46 KB
/
MPITest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#define MASTER 0
static const long LIMIT=1024000;
int main(int argc, char * argv[]) {
int size, rank;
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Offset file_size;
MPI_File in, out;
int err;
char buf[4096];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(processor_name, &namelen);
fprintf(stdout, "Process %d of %d is on %s\n",
rank, size, processor_name);
fflush(stdout);
err = MPI_File_open(MPI_COMM_WORLD, argv[1], MPI_MODE_RDONLY, MPI_INFO_NULL, &in);
if (err != MPI_SUCCESS)
{
printf("Error: file open. code: %d. \n", err);
MPI_Finalize();
exit(-1);
}
err = MPI_File_open(MPI_COMM_WORLD, argv[2], MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &out);
if (err != MPI_SUCCESS)
{
printf("Error: file open write. code: %d. \n", err);
MPI_Finalize();
exit(-1);
}
MPI_File_get_size(in, &file_size);
if (file_size < sizeof(buf)) {
err = MPI_File_read(in, buf, file_size, MPI_CHAR, MPI_STATUS_IGNORE);
if (err == -1) {
printf("Error: file read. code: %d. \n", err);
MPI_Finalize();
exit(-1);
}
buf[file_size] = 0;
puts(buf);
}
err = MPI_File_close(&in);
if (err != MPI_SUCCESS)
{
printf("Error: file close. code: %d. \n", err);
}
err = MPI_File_close(&out);
if (err != MPI_SUCCESS)
{
printf("Error: file close. code: %d. \n", err);
}
MPI_Finalize();
return 0;
}