-
Notifications
You must be signed in to change notification settings - Fork 1
/
writebin.mpi.c
102 lines (93 loc) · 2.17 KB
/
writebin.mpi.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <mpi.h>
static FILE* fp = NULL;
static size_t slice_number = 0;
static int inotify = -1;
static int cfgwatchdes = -1;
static const char* cfgfile = "psiphi.cfg";
static size_t
rank()
{
int rnk;
MPI_Comm_rank(MPI_COMM_WORLD, &rnk);
return (size_t)rnk;
}
static size_t
read_slicenum()
{
FILE* cfg = fopen(cfgfile, "r");
if(!cfg) {
fprintf(stderr, "%s: no '%s'; using slice 0.\n", __FILE__, cfgfile);
return 0;
}
size_t snum;
if(fscanf(cfg, "%zu", &snum) != 1) {
fprintf(stderr, "error reading slice number from %s!\n", cfgfile);
}
fclose(cfg);
return snum;
}
static void
myfilename(char fname[256])
{
snprintf(fname, 256, "tjfbin.%zu", rank());
}
static bool
slice_num_changed()
{
/* FIXME implement this to select/read the cfgwatchdes */
return false;
}
static void
start()
{
assert(fp == NULL);
char fname[256] = {0};
myfilename(fname);
fp = fopen(fname, "wb");
assert(fp);
slice_number = read_slicenum();
if((inotify = inotify_init1(IN_CLOEXEC)) == -1) {
fprintf(stderr, "%s error initializing inotify\n", __FILE__);
return;
}
const int wflags = IN_CLOSE_WRITE;
if((cfgwatchdes = inotify_add_watch(inotify, cfgfile, wflags)) == -1) {
fprintf(stderr, "%s error adding watch for config file...\n", __FILE__);
close(inotify); inotify = -1;
}
}
void
exec(unsigned dtype, const size_t dims[3], const void* buf, size_t n)
{
/* we don't need data type args, for now.. we just copy the binary data
* somewhere else. */
(void)dtype; (void)dims;
if(NULL == fp) {
start();
}
if(slice_num_changed()) {
slice_number = read_slicenum();
}
if(fwrite(buf, 1, n, fp) != n) {
fprintf(stderr, "%s: short write?\n", __FILE__);
}
}
void
finish(unsigned dtype, const size_t dims[3])
{
(void)dtype; (void)dims;
if(fclose(fp) != 0) {
fprintf(stderr, "%s: error closing file!\n", __FILE__);
}
if(close(cfgwatchdes) != 0) {
fprintf(stderr, "%s: error closing watch descriptor\n", __FILE__);
}
if(close(inotify) != 0) {
fprintf(stderr, "%s: error closing inotify descriptor\n", __FILE__);
}
}