bsp_ex.cpp 4.03 KB
Newer Older
Davis King's avatar
Davis King committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

    This is an example illustrating the use of the Bulk Synchronous Parallel 
    processing tools from the dlib C++ Library.


*/





#include "dlib/cmd_line_parser.h"
#include "dlib/bsp.h"
#include "dlib/matrix.h"

#include <iostream>

using namespace std;
using namespace dlib;

// ----------------------------------------------------------------------------------------

Davis King's avatar
Davis King committed
25
double f (double x)
Davis King's avatar
Davis King committed
26
27
28
29
30
31
32
{
    return std::pow(x-2.0, 2.0);
}

// ----------------------------------------------------------------------------------------

void bsp_job_node_0 (
Davis King's avatar
Davis King committed
33
    bsp_context& bsp,
Davis King's avatar
Davis King committed
34
35
36
37
38
39
40
41
42
43
    double& min_value,
    double& optimal_x
)
{
    double left = -100;
    double right = 100;

    min_value = std::numeric_limits<double>::infinity();
    double interval_width = std::abs(right-left);

Davis King's avatar
Davis King committed
44
    for (int i = 0; i < 100; ++i)
Davis King's avatar
Davis King committed
45
    {
Davis King's avatar
Davis King committed
46
47
        bsp.broadcast(left);
        bsp.broadcast(right);
Davis King's avatar
Davis King committed
48

Davis King's avatar
Davis King committed
49
        for (unsigned int k = 1; k < bsp.number_of_nodes(); ++k)
Davis King's avatar
Davis King committed
50
51
        {
            std::pair<double,double> val;
Davis King's avatar
Davis King committed
52
            bsp.receive(val);
Davis King's avatar
Davis King committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
            if (val.second < min_value)
            {
                min_value = val.second;
                optimal_x = val.first;
            }
        }

        interval_width *= 0.5;
        left  = optimal_x - interval_width/2;
        right = optimal_x + interval_width/2;
    }
}

// ----------------------------------------------------------------------------------------

void bsp_job_other_nodes (
Davis King's avatar
Davis King committed
69
    bsp_context& bsp,
Davis King's avatar
Davis King committed
70
71
72
73
    long grid_resolution
)
{
    double left, right;
Davis King's avatar
Davis King committed
74
    while (bsp.try_receive(left))
Davis King's avatar
Davis King committed
75
    {
Davis King's avatar
Davis King committed
76
        bsp.receive(right);
Davis King's avatar
Davis King committed
77

Davis King's avatar
Davis King committed
78
79
        const double l = (bsp.node_id()-1)/(bsp.number_of_nodes()-1.0);
        const double r = bsp.node_id()    /(bsp.number_of_nodes()-1.0);
Davis King's avatar
Davis King committed
80
81
82
83

        const double width = right-left;
        const matrix<double> values_to_check = linspace(left+l*width, left+r*width, grid_resolution);

Davis King's avatar
Davis King committed
84
        double best_x = 0;
Davis King's avatar
Davis King committed
85
86
87
88
89
90
91
92
93
94
95
        double best_val = std::numeric_limits<double>::infinity();
        for (long j = 0; j < values_to_check.size(); ++j)
        {
            double temp = f(values_to_check(j));
            if (temp < best_val)
            {
                best_val = temp;
                best_x = values_to_check(j);
            }
        }

Davis King's avatar
Davis King committed
96
        bsp.send(make_pair(best_x, best_val), 0);
Davis King's avatar
Davis King committed
97
98
99
100
101
102
103
104
105
    }
}

// ----------------------------------------------------------------------------------------

int main(int argc, char** argv)
{
    try
    {
Davis King's avatar
Davis King committed
106
        command_line_parser parser;
Davis King's avatar
Davis King committed
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
        parser.add_option("h","Display this help message.");
        parser.add_option("l","Run as a listening BSP node.",1);

        parser.parse(argc, argv);
        parser.check_option_arg_range("l", 1, 65535);

        if (parser.option("h"))
        {
            // display all the command line options
            cout << "Usage: bsp_ex (-l port | <list of hosts>)\n";
            parser.print_options(cout); 
            cout << endl;
            return 0;
        }


        if (parser.option("l"))
        {
            const unsigned short listening_port = get_option(parser, "l", 0);
            cout << "Listening in port " << listening_port << endl;
            const long grid_resolution = 100;
            bsp_listen(listening_port, bsp_job_other_nodes, grid_resolution);
        }
        else
        {
            if (parser.number_of_arguments() == 0)
            {
                cout << "You must give some listening BSP nodes as arguments to this program!" << endl;
                return 0;
            }

            std::vector<network_address> hosts;
            for (unsigned long i = 0; i < parser.number_of_arguments(); ++i)
                hosts.push_back(parser[i]);

            double min_value, optimal_x;
            bsp_connect(hosts, bsp_job_node_0, dlib::ref(min_value), dlib::ref(optimal_x));

            cout << "optimal_x: "<< optimal_x << endl;
            cout << "min_value: "<< min_value << endl;
        }

    }
    catch (std::exception& e)
    {
        cout << "error in main(): " << e.what() << endl;
    }
}