File: forking_range_solver.c

package info (click to toggle)
freecell-solver 5.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,256 kB
  • sloc: ansic: 28,700; perl: 10,050; xml: 5,600; python: 1,339; sh: 533; cpp: 275; makefile: 20; javascript: 8
file content (299 lines) | stat: -rw-r--r-- 8,757 bytes parent folder | download | duplicates (4)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * This file is part of Freecell Solver. It is subject to the license terms in
 * the COPYING.txt file found in the top-level directory of this distribution
 * and at http://fc-solve.shlomifish.org/docs/distro/COPYING.html . No part of
 * Freecell Solver, including this file, may be copied, modified, propagated,
 * or distributed except according to the terms contained in the COPYING file.
 *
 * Copyright (c) 2000 Shlomi Fish
 */
//  forking_range_solver.c - a range solver that solves different boards in
//  several UNIX processes.
//
//  See also:
//      - fc_pro_range_solver.c
//      - serial_range_solver.c
//      - threaded_range_solver.c
#ifdef __linux__
#define USE_EPOLL
#include <sys/epoll.h>
#endif
#include <sys/wait.h>
#include "range_solvers.h"
#include "try_param.h"

#ifndef FCS_WITHOUT_CMD_LINE_HELP
static void print_help(void)
{
    printf("\n%s",
        "freecell-solver-fork-solve start end print_step\n"
        "    [--num-workers n] [--worker-step step] [fc-solve Arguments...]\n"
        "\n"
        "Solves a sequence of boards from the Microsoft/Freecell Pro Deals\n"
        "\n"
        "start - the first board in the sequence\n"
        "end - the last board in the sequence (inclusive)\n"
        "print_step - at which division to print a status line\n");
}
#endif

static long long total_num_iters = 0, total_num_finished_boards = 0;

#define READ_FD 0
#define WRITE_FD 1
typedef struct
{
    int child_to_parent_pipe[2], parent_to_child_pipe[2];
} fcs_worker;

typedef struct
{
    long long board_num, quota_end;
} request_type;

typedef struct
{
    long long num_iters, num_finished_boards;
} response_type;

static inline void write_request(const long long end_board,
    const long long board_num_step, long long *const next_board_num_ptr,
    const fcs_worker *const worker)
{
    request_type req;
    if ((*next_board_num_ptr) > end_board)
    {
        /* We only absolutely need to initialize .board_num here, but the
         * Coverity Scan scanner complains about quota_end being uninitialized
         * when passed to write() so we initialize it here as well.
         * */
        req = (request_type){.board_num = -1, .quota_end = -1};
    }
    else
    {
        req.board_num = *(next_board_num_ptr);
        if (((*next_board_num_ptr) += board_num_step) > end_board)
        {
            (*next_board_num_ptr) = end_board + 1;
        }
        req.quota_end = (*next_board_num_ptr) - 1;
    }

    write(worker->parent_to_child_pipe[WRITE_FD], &req, sizeof(req));
}

static inline void transaction(const fcs_worker *const worker,
    const int read_fd, const long long end_board,
    const long long board_num_step, long long *const next_board_num_ptr)
{
    response_type response;
    if (read(read_fd, &response, sizeof(response)) <
        (ssize_t)(sizeof(response)))
    {
        return;
    }
    total_num_iters += response.num_iters;
    total_num_finished_boards += response.num_finished_boards;

    write_request(end_board, board_num_step, next_board_num_ptr, worker);
}

static inline int read_fd(const fcs_worker *const worker)
{
    return worker->child_to_parent_pipe[READ_FD];
}

static inline int range_solvers_main(int argc, char *argv[], int arg,
    long long next_board_num, const long long end_board,
    const long long stop_at)
{
    size_t num_workers = 3;
    long long board_num_step = 1;
    for (; arg < argc; ++arg)
    {
        const char *param;
        if ((param = TRY_P("--num-workers")))
        {
            num_workers = atoi(param);
        }
        else if ((param = TRY_P("--worker-step")))
        {
            board_num_step = atoll(param);
        }
        else
        {
            break;
        }
    }

    fc_solve_print_started_at();
    void *const instance = simple_alloc_and_parse(argc, argv, arg);
    fcs_worker workers[num_workers];

    for (size_t idx = 0; idx < num_workers; ++idx)
    {
        if (pipe(workers[idx].child_to_parent_pipe))
        {
            fc_solve_err(
                "C->P Pipe for worker No. %zu failed! Exiting.\n", idx);
        }
        if (pipe(workers[idx].parent_to_child_pipe))
        {
            fc_solve_err(
                "P->C Pipe for worker No. %zu failed! Exiting.\n", idx);
        }

        switch (fork())
        {
        case -1:
            fc_solve_err("Fork for worker No. %zu failed! Exiting.\n", idx);

        case 0:
        {
            /* I'm the child. */
            const_AUTO(w, workers[idx]);
            close(w.parent_to_child_pipe[WRITE_FD]);
            close(w.child_to_parent_pipe[READ_FD]);
            /* I'm one of the slaves */
            request_type req;
            while (read(w.parent_to_child_pipe[READ_FD], &req, sizeof(req)),
                req.board_num != -1)
            {
                response_type response = {
                    .num_iters = 0,
                    .num_finished_boards = req.quota_end - req.board_num + 1,
                };
                for (; req.board_num <= req.quota_end; ++req.board_num)
                {
                    range_solvers__solve(
                        instance, req.board_num, &response.num_iters);
                    freecell_solver_user_recycle(instance);
                }
                write(w.child_to_parent_pipe[WRITE_FD], &response,
                    sizeof(response));
            }
            /* Cleanup */
            freecell_solver_user_free(instance);
            close(w.child_to_parent_pipe[WRITE_FD]);
            close(w.parent_to_child_pipe[READ_FD]);
            return 0;
        }

        default:
            /* I'm the parent. */
            close(workers[idx].parent_to_child_pipe[READ_FD]);
            close(workers[idx].child_to_parent_pipe[WRITE_FD]);
            break;
        }
    }

    freecell_solver_user_free(instance);

/* I'm the master. */
#ifdef USE_EPOLL
    const int epollfd = epoll_create1(0);
    if (epollfd == -1)
    {
        perror("epoll_create1");
        exit(EXIT_FAILURE);
    }
#else
    fd_set initial_readers;
    FD_ZERO(&initial_readers);
    int mymax = -1;
#endif

    for (size_t idx = 0; idx < num_workers; ++idx)
    {
        const int fd = read_fd(&workers[idx]);
#ifdef USE_EPOLL
        struct epoll_event ev = {
            .events = EPOLLIN,
            .data.ptr = &(workers[idx]),
        };
        if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1)
        {
            perror("epoll_ctl: listen_sock");
            exit(EXIT_FAILURE);
        }
#else
        FD_SET(fd, &initial_readers);
        if (fd > mymax)
        {
            mymax = fd;
        }
#endif
    }
#ifndef USE_EPOLL
    ++mymax;
#endif
    const long long total_num_boards_to_check = end_board - next_board_num + 1;

    long long next_milestone = stop_at;
    for (size_t idx = 0; idx < num_workers; ++idx)
    {
        write_request(
            end_board, board_num_step, &next_board_num, &(workers[idx]));
    }

    while (total_num_finished_boards < total_num_boards_to_check)
    {
        while (total_num_finished_boards >= next_milestone)
        {
            fc_solve_print_reached(next_milestone, total_num_iters);
            next_milestone += stop_at;
        }
#ifdef USE_EPOLL
        struct epoll_event events[4];
        const int nfds = epoll_wait(epollfd, events, COUNT(events), -1);
        if (nfds == -1)
        {
            perror("epoll_pwait");
            exit(EXIT_FAILURE);
        }

        for (int i = 0; i < nfds; ++i)
        {
            const fcs_worker *const worker = events[i].data.ptr;
            transaction(worker, read_fd(worker), end_board, board_num_step,
                &next_board_num);
        }
#else
        fd_set readers = initial_readers;
        /* I'm the master. */
        const int select_ret = select(mymax, &readers, NULL, NULL, NULL);

        if (select_ret == -1)
        {
            perror("select()");
        }
        else if (select_ret)
        {
            for (size_t idx = 0; idx < num_workers; ++idx)
            {
                const int fd = read_fd(&workers[idx]);

                if (FD_ISSET(fd, &readers))
                {
                    /* FD_ISSET can be set on EOF, so we check if
                     * read failed. */
                    transaction(&(workers[idx]), fd, end_board, board_num_step,
                        &next_board_num);
                }
            }
        }
#endif
    }
    while (total_num_finished_boards >= next_milestone)
    {
        fc_solve_print_reached(next_milestone, total_num_iters);
        next_milestone += stop_at;
    }

    for (size_t idx = 0; idx < num_workers; ++idx)
    {
        wait(NULL);
    }
    fc_solve_print_finished(total_num_iters);
    return 0;
}