| 158 | // ---------------------------------------------------------------------------------------- |
| 159 | |
| 160 | void bsp_job_node_0 (bsp_context& bsp, double& min_value, double& optimal_x) |
| 161 | { |
| 162 | // This function is called by bsp_connect(). In general, any BSP node can do anything |
| 163 | // you want. However, in this example we use this node as a kind of controller for the |
| 164 | // other nodes. In particular, since we are doing a nested grid search, this node's |
| 165 | // job will be to collect results from other nodes and then decide which part of the |
| 166 | // number line subsequent iterations should focus on. |
| 167 | // |
| 168 | // Also, each BSP node has a node ID number. You can determine it by calling |
| 169 | // bsp.node_id(). However, the node spawned by a call to bsp_connect() always has a |
| 170 | // node ID of 0 (hence the name of this function). Additionally, all functions |
| 171 | // executing a BSP task always take a bsp_context as their first argument. This object |
| 172 | // is the interface that allows BSP jobs to communicate with each other. |
| 173 | |
| 174 | |
| 175 | // Now let's get down to work. Recall that we are trying to find the x value that |
| 176 | // minimizes the f(x) defined above. The grid search will start out by considering the |
| 177 | // range [-1e100, 1e100] on the number line. It will progressively narrow this window |
| 178 | // until it has located the minimizer of f(x) to within 1e-15 of its true value. |
| 179 | double left = -1e100; |
| 180 | double right = 1e100; |
| 181 | |
| 182 | min_value = std::numeric_limits<double>::infinity(); |
| 183 | double interval_width = std::abs(right-left); |
| 184 | |
| 185 | // keep going until the window is smaller than 1e-15. |
| 186 | while (right-left > 1e-15) |
| 187 | { |
| 188 | // At the start of each loop, we broadcast the current window to all the other BSP |
| 189 | // nodes. They will each search a separate part of the window and then report back |
| 190 | // the smallest values they found in their respective sub-windows. |
| 191 | // |
| 192 | // Also, you can send/broadcast/receive anything that has global serialize() and |
| 193 | // deserialize() routines defined for it. Dlib comes with serialization functions |
| 194 | // for a lot of types by default, so we don't have to define anything for this |
| 195 | // example program. However, if you want to send an object you defined then you |
| 196 | // will need to write your own serialization functions. See the documentation for |
| 197 | // dlib's serialize() routine or the bridge_ex.cpp example program for an example. |
| 198 | bsp.broadcast(left); |
| 199 | bsp.broadcast(right); |
| 200 | |
| 201 | // Receive the smallest values found from the other BSP nodes. |
| 202 | for (unsigned int k = 1; k < bsp.number_of_nodes(); ++k) |
| 203 | { |
| 204 | // The other nodes will send std::pairs of x/f(x) values. So that is what we |
| 205 | // receive. |
| 206 | std::pair<double,double> val; |
| 207 | bsp.receive(val); |
| 208 | // save the smallest result. |
| 209 | if (val.second < min_value) |
| 210 | { |
| 211 | min_value = val.second; |
| 212 | optimal_x = val.first; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Now narrow the search window by half. |
| 217 | interval_width *= 0.5; |
nothing calls this directly
no test coverage detected