File: DIRparallel.c

package info (click to toggle)
nlopt 2.4.2%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,368 kB
  • sloc: ansic: 22,387; cpp: 20,233; sh: 11,475; python: 323; makefile: 244; fortran: 108
file content (381 lines) | stat: -rw-r--r-- 19,477 bytes parent folder | download | duplicates (11)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* DIRparallel.f -- translated by f2c (version 20050501).

   f2c output hand-cleaned by SGJ (August 2007).
*/

#include "direct-internal.h"

/* Table of constant values */

static integer c__0 = 0;
static integer c_n1 = -1;

/* +-----------------------------------------------------------------------+ */
/* | Program       : Direct.f (subfile DIRseriell.f)                       | */
/* | Last modified : 02-22-01                                              | */
/* | Written by    : Joerg Gablonsky                                       | */
/* | Subroutines, which differ depending on the serial or parallel version.| */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | Parallel Direct. This routine replaces the normal main routine DIRect.| */
/* | In it, we find out if this pe is the master or slave. If it is the    | */
/* | master, it calls the serial DIRect main routine. The only routine that| */
/* | has to change for parallel Direct is DIRSamplef, where the actual     | */
/* | sampling of the function is done. If we are on the slave, wait for    | */
/* | either the coordinates of a point to sample the function or the       | */
/* | termination signal.                                                   | */
/* +-----------------------------------------------------------------------+ */
/* Subroutine */ int direct_pardirect_(fp fcn, doublereal *x, integer *n, 
	doublereal *eps, integer *maxf, integer *maxt, doublereal *minf, 
	doublereal *l, doublereal *u, integer *algmethod, integer *ierror, 
	FILE *logfile, doublereal *fglobal, doublereal *fglper, doublereal 
	*volper, doublereal *sigmaper, void *fcn_data)
{
    /* System generated locals */
    integer i__1;

    /* Local variables */
    integer i__, k;
    integer tid;
    integer flag__;
    doublereal fval;
    integer tids[360], kret;
    integer mytid;
    doublereal fscale;
    integer nprocs;

/* +-----------------------------------------------------------------------+ */
/* | Parameters                                                            | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | The maximum of function evaluations allowed.                          | */
/* | The maximum dept of the algorithm.                                    | */
/* | The maximum number of divisions allowed.                              | */
/* | The maximal dimension of the problem.                                 | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | Global Variables.                                                     | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | External Variables.                                                   | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | User Variables.                                                       | */
/* | These can be used to pass user defined data to the function to be     | */
/* | optimized.                                                            | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | Parallel programming variables                                        | */
/* +-----------------------------------------------------------------------+ */
/*       maxprocs should be >= the number of processes used for DIRECT */
/* +-----------------------------------------------------------------------+ */
/* | End of parallel programming variables                                 | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | Internal variables                                                    | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | JG 02/28/01 Begin of parallel additions                               | */
/* | DETERMINE MASTER PROCESSOR. GET TIDS OF ALL PROCESSORS.               | */
/* +-----------------------------------------------------------------------+ */
    /* Parameter adjustments */
    --u;
    --l;
    --x;

    /* Function Body */
    getmytidif_(&mytid);
    getnprocsif_(&nprocs);
    gettidif_(&c__0, tids);
/* +-----------------------------------------------------------------------+ */
/* | If I am the master get the other tids and start running DIRECT.       | */
/* | Otherwise, branch off to do function evaluations.                     | */
/* +-----------------------------------------------------------------------+ */
    if (mytid == tids[0]) {
	i__1 = nprocs - 1;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    gettidif_(&i__, &tids[i__]);
/* L46: */
	}
/* +-----------------------------------------------------------------------+ */
/* | Call Direct main routine. This routine calls DIRSamplef for the       | */
/* | function evaluations, which are then done in parallel.                | */
/* +-----------------------------------------------------------------------+ */
	direct_direct_(fcn, &x[1], n, eps, maxf, maxt, minf, &l[1], &u[1], 
		algmethod, ierror, logfile, fglobal, fglper, volper, sigmaper,
		fcn_data);
/* +-----------------------------------------------------------------------+ */
/* | Send exit message to rest of pe's.                                    | */
/* +-----------------------------------------------------------------------+ */
	flag__ = 0;
	i__1 = nprocs;
	for (tid = 2; tid <= i__1; ++tid) {
	    mastersendif_(&tids[tid - 1], &tids[tid - 1], n, &flag__, &flag__,
		     &x[1], &u[1], &l[1], &x[1]);
/* L200: */
	}
    } else {
/* +-----------------------------------------------------------------------+ */
/* | This is what the slaves do!!                                          | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* |   Receive the first point from the master processor.                  | */
/* +-----------------------------------------------------------------------+ */
	slaverecvif_(tids, &c_n1, n, &flag__, &k, &fscale, &u[1], &l[1], &x[1]
		);
/* +-----------------------------------------------------------------------+ */
/* | Repeat until master signals to stop.                                  | */
/* +-----------------------------------------------------------------------+ */
	while(flag__ > 0) {
/* +-----------------------------------------------------------------------+ */
/* | Evaluate f(x).                                                        | */
/* +-----------------------------------------------------------------------+ */
	     direct_dirinfcn_(fcn, &x[1], &l[1], &u[1], n, &fval, &kret, &
		      fcn_data);
/* +-----------------------------------------------------------------------+ */
/* | Send result and wait for next point / message with signal to stop.    | */
/* +-----------------------------------------------------------------------+ */
	    slavesendif_(tids, &mytid, &k, &mytid, &fval, &kret);
	    slaverecvif_(tids, &c_n1, n, &flag__, &k, &fscale, &u[1], &l[1], &
		    x[1]);
	}
    }
    return 0;
} /* pardirect_ */

/* +-----------------------------------------------------------------------+ */
/* | Subroutine for sampling. This sampling is done in parallel, the master| */
/* | prozessor is also evaluating the function sometimes.                  | */
/* +-----------------------------------------------------------------------+ */
/* Subroutine */ void direct_dirsamplef_(doublereal *c__, integer *arrayi, doublereal 
	*delta, integer *sample, integer *new__, integer *length, 
	FILE *logfile, doublereal *f, integer *free, integer *maxi, 
	integer *point, fp fcn, doublereal *x, doublereal *l, doublereal *
	minf, integer *minpos, doublereal *u, integer *n, integer *maxfunc, 
	integer *maxdeep, integer *oops, doublereal *fmax, integer *
	ifeasiblef, integer *iinfesiblef, void *fcn_data)
{
    /* System generated locals */
    integer length_dim1, length_offset, c_dim1, c_offset, f_dim1, f_offset, 
	    i__1;
    doublereal d__1;

    /* Local variables */
    integer i__, j, k, helppoint, tid, pos;
    integer flag__, tids[360], kret, npts;
    doublereal fhelp;
    integer oldpos, nprocs, datarec;

/* +-----------------------------------------------------------------------+ */
/* | JG 07/16/01 fcn must be declared external.                            | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | JG 07/16/01 Removed fcn.                                              | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | JG 01/22/01 Added variable to keep track of the maximum value found.  | */
/* |             Added variable to keep track if feasible point was found. | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | Variables to pass user defined data to the function to be optimized.  | */
/* +-----------------------------------------------------------------------+ */
/* +-----------------------------------------------------------------------+ */
/* | Parallel programming variables.                                       | */
/* +-----------------------------------------------------------------------+ */
/* JG 09/05/00 Increase this if more processors are used. */
/* +-----------------------------------------------------------------------+ */
/* | Find out the id's of all processors.                                  | */
/* +-----------------------------------------------------------------------+ */
    /* Parameter adjustments */
    --u;
    --l;
    --x;
    --arrayi;
    --point;
    f_dim1 = *maxfunc;
    f_offset = 1 + f_dim1;
    f -= f_offset;
    length_dim1 = *maxfunc;
    length_offset = 1 + length_dim1;
    length -= length_offset;
    c_dim1 = *maxfunc;
    c_offset = 1 + c_dim1;
    c__ -= c_offset;

    /* Function Body */
    getnprocsif_(&nprocs);
    i__1 = nprocs - 1;
    for (i__ = 0; i__ <= i__1; ++i__) {
	gettidif_(&i__, &tids[i__]);
/* L46: */
    }
/* +-----------------------------------------------------------------------+ */
/* | Set the pointer to the first function to be evaluated,                | */
/* | store this position also in helppoint.                                | */
/* +-----------------------------------------------------------------------+ */
    pos = *new__;
    helppoint = pos;
/* +-----------------------------------------------------------------------+ */
/* | Iterate over all points, where the function should be                 | */
/* | evaluated.                                                            | */
/* +-----------------------------------------------------------------------+ */
    flag__ = 1;
    npts = *maxi + *maxi;
    k = 1;
    while(k <= npts && k < nprocs) {
/* +-----------------------------------------------------------------------+ */
/* | tid is the id of the prozessor the next points is send to.            | */
/* +-----------------------------------------------------------------------+ */
	tid = k + 1;
/* +-----------------------------------------------------------------------+ */
/* | Copy the position into the helparray x.                               | */
/* +-----------------------------------------------------------------------+ */
	i__1 = *n;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    x[i__] = c__[pos + i__ * c_dim1];
/* L60: */
	}
/* +-----------------------------------------------------------------------+ */
/* | Send the point.                                                       | */
/* +-----------------------------------------------------------------------+ */
	mastersendif_(&tids[tid - 1], &tids[tid - 1], n, &flag__, &pos, &x[1],
		 &u[1], &l[1], &x[1]);
	++k;
	pos = point[pos];
/* +-----------------------------------------------------------------------+ */
/* | Get the next point.                                                   | */
/* +-----------------------------------------------------------------------+ */
    }
/* +-----------------------------------------------------------------------+ */
/* |  Get data until it is all received.                                   | */
/* +-----------------------------------------------------------------------+ */
    datarec = 0;
    while(datarec < npts) {
	if ((doublereal) datarec / (doublereal) nprocs - datarec / nprocs < 
		1e-5 && k <= npts) {
	    i__1 = *n;
	    for (i__ = 1; i__ <= i__1; ++i__) {
		x[i__] = c__[pos + i__ * c_dim1];
/* L165: */
	    }
	    direct_dirinfcn_(fcn, &x[1], &l[1], &u[1], n, &fhelp, &kret,
		      fcn_data);
	    oldpos = pos;
	    f[oldpos + f_dim1] = fhelp;
	    ++datarec;
/* +-----------------------------------------------------------------------+ */
/* | Remember if an infeasible point has been found.                       | */
/* +-----------------------------------------------------------------------+ */
	    *iinfesiblef = MAX(*iinfesiblef,kret);
	    if (kret == 0) {
/* +-----------------------------------------------------------------------+ */
/* | if the function evaluation was O.K., set the flag in                  | */
/* | f(pos,2).                                                             | */
/* +-----------------------------------------------------------------------+ */
		f[oldpos + (f_dim1 << 1)] = 0.;
		*ifeasiblef = 0;
/* +-----------------------------------------------------------------------+ */
/* | JG 01/22/01 Added variable to keep track of the maximum value found.  | */
/* +-----------------------------------------------------------------------+ */
/* Computing MAX */
		d__1 = f[pos + f_dim1];
		*fmax = MAX(d__1,*fmax);
	    }
/* +-----------------------------------------------------------------------+ */
/* | Remember if an infeasible point has been found.                       | */
/* +-----------------------------------------------------------------------+ */
	    *iinfesiblef = MAX(*iinfesiblef,kret);
	    if (kret == 1) {
/* +-----------------------------------------------------------------------+ */
/* | If the function could not be evaluated at the given point,            | */
/* | set flag to mark this (f(pos,2) and store the maximum                 | */
/* | box-sidelength in f(pos,1).                                           | */
/* +-----------------------------------------------------------------------+ */
		f[oldpos + (f_dim1 << 1)] = 2.;
		f[oldpos + f_dim1] = *fmax;
	    }
/* +-----------------------------------------------------------------------+ */
/* | If the function could not be evaluated due to a failure in            | */
/* | the setup, mark this.                                                 | */
/* +-----------------------------------------------------------------------+ */
	    if (kret == -1) {
		f[oldpos + (f_dim1 << 1)] = -1.;
	    }
	    ++k;
	    pos = point[pos];
	}
/* +-----------------------------------------------------------------------+ */
/* | Recover where to store the value.                                     | */
/* +-----------------------------------------------------------------------+ */
	masterrecvif_(&c_n1, &c_n1, &oldpos, &tid, &fhelp, &kret);
	f[oldpos + f_dim1] = fhelp;
	++datarec;
/* +-----------------------------------------------------------------------+ */
/* | Remember if an infeasible point has been found.                       | */
/* +-----------------------------------------------------------------------+ */
	*iinfesiblef = MAX(*iinfesiblef,kret);
	if (kret == 0) {
/* +-----------------------------------------------------------------------+ */
/* | if the function evaluation was O.K., set the flag in                  | */
/* | f(pos,2).                                                             | */
/* +-----------------------------------------------------------------------+ */
	    f[oldpos + (f_dim1 << 1)] = 0.;
	    *ifeasiblef = 0;
/* +-----------------------------------------------------------------------+ */
/* | JG 01/22/01 Added variable to keep track of the maximum value found.  | */
/* +-----------------------------------------------------------------------+ */
/* Computing MAX */
	    d__1 = f[oldpos + f_dim1];
	    *fmax = MAX(d__1,*fmax);
	}
	if (kret == 1) {
/* +-----------------------------------------------------------------------+ */
/* | If the function could not be evaluated at the given point,            | */
/* | set flag to mark this (f(pos,2) and store the maximum                 | */
/* | box-sidelength in f(pos,1).                                           | */
/* +-----------------------------------------------------------------------+ */
	    f[oldpos + (f_dim1 << 1)] = 2.;
	    f[oldpos + f_dim1] = *fmax;
	}
/* +-----------------------------------------------------------------------+ */
/* | If the function could not be evaluated due to a failure in            | */
/* | the setup, mark this.                                                 | */
/* +-----------------------------------------------------------------------+ */
	if (kret == -1) {
	    f[oldpos + (f_dim1 << 1)] = -1.;
	}
/* +-----------------------------------------------------------------------+ */
/* |         Send data until it is all sent.                               | */
/* +-----------------------------------------------------------------------+ */
	if (k <= npts) {
/* +-----------------------------------------------------------------------+ */
/* | Copy the position into the helparray x.                               | */
/* +-----------------------------------------------------------------------+ */
	    i__1 = *n;
	    for (i__ = 1; i__ <= i__1; ++i__) {
		x[i__] = c__[pos + i__ * c_dim1];
/* L160: */
	    }
	    mastersendif_(&tid, &tid, n, &flag__, &pos, &x[1], &u[1], &l[1], &
		    x[1]);
	    ++k;
	    pos = point[pos];
	}
    }
    pos = helppoint;
/* +-----------------------------------------------------------------------+ */
/* | Iterate over all evaluated points and see, if the minimal             | */
/* | value of the function has changed. If this has happend,               | */
/* | store the minimal value and its position in the array.                | */
/* | Attention: Only valied values are checked!!                           | */
/* +-----------------------------------------------------------------------+ */
    i__1 = *maxi + *maxi;
    for (j = 1; j <= i__1; ++j) {
	if (f[pos + f_dim1] < *minf && f[pos + (f_dim1 << 1)] == 0.) {
	    *minf = f[pos + f_dim1];
	    *minpos = pos;
	}
	pos = point[pos];
/* L50: */
    }
} /* dirsamplef_ */