/*---------- main energy computation, weights, etc. ----------*/
typedef double CostComputation(const Vertices vertices, int section);
+typedef void PreComputation(const Vertices vertices, int section);
typedef struct {
double weight;
CostComputation *fn;
} CostContribution;
-static const CostContribution costs[]= {
-#define PRECOMP(compute) { 0,(compute) },
+#define NPRECOMPS ((sizeof(precomps)/sizeof(precomps[0])))
+#define NCOSTS ((sizeof(costs)/sizeof(costs[0])))
#define COST(weight, compute) { (weight),(compute) },
- PRECOMP(compute_edge_lengths)
- PRECOMP(compute_vertex_areas)
+static PreComputation *const precomps[]= {
+ compute_edge_lengths,
+ compute_vertex_areas
+};
+
+static const CostContribution costs[]= {
#if XBITS==3
#define STOP_EPSILON 1e-6
#define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3)
COST( 1e18, noncircular_rim_cost)
#endif
-};
-#define NCOSTS ((sizeof(costs)/sizeof(costs[0])))
+};
const double edge_angle_cost_circcircrat= EDGE_ANGLE_COST_CIRCCIRCRAT;
/*---------- energy computation machinery ----------*/
-typedef struct {
- double total;
- const CostContribution *cc;
-} CostComputationData;
-
void compute_energy_separately(const struct Vertices *vs,
- int section, void *energy_v, void *ccd_v) {
- CostComputationData *ccd= ccd_v;
- double *energy= energy_v;
- *energy= ccd->cc->fn(vs->a, section);
+ int section, void *energies_v, void *totals_v) {
+ double *energies= energies_v;
+ int ci;
+
+ for (ci=0; ci<NPRECOMPS; ci++) {
+ costs[ci].fn(vs->a, section);
+ inparallel_barrier();
+ }
+ for (ci=0; ci<NCOSTS; ci++)
+ energies[ci]= costs[ci].fn(vs->a, section);
}
void compute_energy_combine(const struct Vertices *vertices,
- int section, void *energy_v, void *ccd_v) {
- CostComputationData *ccd= ccd_v;
- double *energy= energy_v;
- ccd->total += *energy;
+ int section, void *energies_v, void *totals_v) {
+ int ci;
+ double *energies= energies_v;
+ double *totals= totals_v;
+
+ for (ci=0; ci<NCOSTS; ci++)
+ totals[ci] += energies[ci];
}
double compute_energy(const struct Vertices *vs) {
static int bests_unprinted;
- double energy;
+ double totals[NCOSTS], energy;
int ci, printing;
- CostComputationData ccd;
printing= printing_check(pr_cost,0);
if (printing) printf("%15lld c>e |", evaluations);
- energy= 0;
+ for (ci=0; ci<NCOSTS; ci++)
+ totals[ci]= 0;
- for (ci=0; ci<NCOSTS; ci++) {
- ccd.total= 0;
- ccd.cc= &costs[ci];
-
- inparallel(vs,
- compute_energy_separately,
- compute_energy_combine,
- sizeof(energy),
- &ccd);
-
- if (ccd.cc->weight != 0)
- addcost(&energy, costs[ci].weight, ccd.total, printing);
- }
+ inparallel(vs,
+ compute_energy_separately,
+ compute_energy_combine,
+ sizeof(totals) /* really, size of energies */,
+ totals);
+
+ energy= 0;
+ for (ci=0; ci<NCOSTS; ci++)
+ addcost(&energy, costs[ci].weight, totals[ci], printing);
if (printing) printf("| total %# e |", energy);
/*---------- Precomputations ----------*/
-double compute_edge_lengths(const Vertices vertices, int section) {
+void compute_edge_lengths(const Vertices vertices, int section) {
int v1,e,v2;
FOR_EDGE(v1,e,v2, OUTER)
edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
-
- return 0;
}
-double compute_vertex_areas(const Vertices vertices, int section) {
+void compute_vertex_areas(const Vertices vertices, int section) {
int v0,v1,v2, e1,e2;
// int k;
vertex_areas[v0]= total / count;
vertex_mean_edge_lengths[v0]= edges_total / count;
}
-
- return 0;
}
/*---------- Edgewise vertex displacement ----------*/
* Parallel processing
*/
+#include "common.h"
+
#include <pthread.h>
#include "mgraph.h"
pthread_t thread;
} PerThread;
-#if NPROCESSORS != 1
static void *routine(void *thread_v) {
PerThread *t= thread_v;
- ForAllThreads *a= t->allthreads;
- a->separately(a->vertices, t->section, t->secdata, a->gendata);
+ for (;;) {
+ inparallel_barrier(); /* wait for work to do */
+ ForAllThreads *a= t->allthreads;
+ a->separately(a->vertices, t->section, t->secdata, a->gendata);
+ inparallel_barrier(); /* synchronise for completion */
+ }
return 0;
}
-#endif
+
+static int threads_started;
+static pthread_barrier_t threads_barrier;
+static PerThread threads[NSECTIONS-1];
void inparallel(const struct Vertices *vertices,
Computation *separately,
allthreads.separately= separately;
allthreads.gendata= gendata;
-#if NPROCESSORS != 1
- PerThread threads[nsections];
int s, r;
- for (s=0; s<nsections; s++) {
+ if (!threads_started) {
+ r= pthread_barrier_init(&threads_barrier, 0, NSECTIONS);
+ if (r) { errno=r; diee("pthread_barrier_init"); }
+
+ for (s=0; s<NSECTIONS-1; s++) {
+ r= pthread_create(&threads[s].thread,0,routine,&threads[s]);
+ if (r) { errno=r; diee("pthread_create"); }
+ }
+ }
+
+ for (s=0; s<NSECTIONS-1; s++) {
threads[s].allthreads= &allthreads;
threads[s].section= s;
threads[s].secdata= secdatas[s].secdata;
- r= pthread_create(&threads[s].thread,0,routine,&threads[s]);
- if (r) diee("pthread_create");
}
- for (s=0; s<nsections; s++) {
- r= pthread_join(threads[s].thread, 0);
- if (r) diee("pthread_join");
- combine(vertices, s, threads[s].secdata, gendata);
- }
-#else
- separately(vertices, 0, &secdatas[0], gendata);
- combine(vertices, 0, &secdatas[0], gendata);
-#endif
+ inparallel_barrier(); /* announce more work to do */
+
+ separately(vertices, NSECTIONS-1, &secdatas[NSECTIONS-1], gendata);
+
+ inparallel_barrier(); /* synchronise for completion */
+
+ for (s=0; s<nsections; s++)
+ combine(vertices, s, &secdatas[s].secdata, gendata);
+}
+
+void inparallel_barrier(void) {
+ int r;
+ r= pthread_barrier_wait(&threads_barrier);
+ if (r && r!=PTHREAD_BARRIER_SERIAL_THREAD)
+ { errno=r; diee("pthread_barrier_wait"); }
}