📄 pvmfitness.c
字号:
#include <stdlib.h>#include <pvm3.h>#include "petrol.h"#include "utils.h"static int started = FALSE;static int nhosts;static struct pvmhostinfo *hinfo;static int *children;static void SendAllMessage(int *hosts, int nhosts, int message);static void SendMessage(int tid, int message);static int GetMessage(int *tid);static void SendIndividuals(Population *p, int tid, int n, int howmany);static void GetIndividualsFitness(Population *p);void PVMFitness(Settings *s, Population *p) { int n, keepgoing, workcount, tid; if (!started) { /* Start by getting the configuration of the current PVM environment */ if (pvm_config(&nhosts, NULL, &hinfo) == PvmSysErr) { pvmerror("PVMFitness"); } if (!(children = (int *)malloc(sizeof(int) * nhosts))) { syserror("PVMFitness"); } /* For each host, spawn a new petrolslave */ for (n = 0; n < nhosts; n++) { if (pvm_spawn("petrolslave", NULL, PvmTaskHost, (hinfo + n)->hi_name, 1, &children[n]) != 1) { pvmerror("PVMFitness"); } } started = TRUE; } /* Notify all the slaves about available work */ SendAllMessage(children, nhosts, PETROL_WORK); n = 0; keepgoing = TRUE; workcount = 0; while (keepgoing) { switch(GetMessage(&tid)) { case PETROL_WORK_REQUEST: { int howmany; /* Send work out to the slave */ if (n + s->packetSize >= p->popSize) howmany = p->popSize - n; else howmany = s->packetSize; SendIndividuals(p, tid, n, howmany); n += howmany; workcount++; } break; case PETROL_WORK_DONE: /* Update fitness values */ GetIndividualsFitness(p); workcount--; /* Send more work out if any */ if (n < p->popSize) SendMessage(tid, PETROL_WORK); break; default: error("PVMFitness", "Unkown message received"); } if (n == p->popSize && workcount == 0) keepgoing = FALSE; } }void PVMFitnessExit(void) { int n; for (n = 0; n < nhosts; n++) { pvm_kill(children[n]); } started = FALSE; pvm_exit();} void SendAllMessage(int *hosts, int nhosts, int message) { if (pvm_initsend(PvmDataDefault) < 0) { pvmerror("SendAllMessage"); } if (pvm_mcast(hosts, nhosts, message) < 0) { pvmerror("SendAllMessage"); }}void SendMessage(int tid, int message) { if (pvm_initsend(PvmDataDefault) < 0) { pvmerror("SendAllMessage"); } if (pvm_send(tid, message) < 0) { pvmerror("SendAllMessage"); }}int GetMessage(int *tid) { int message, buf; if ((buf = pvm_recv(-1, -1)) < 0) { pvmerror("GetMessage"); } if (pvm_bufinfo(buf, NULL, &message, tid) < 0) { pvmerror("GetMessage"); } return message;}void SendIndividuals(Population *p, int tid, int n, int howmany) { int x; Individual *i; if (pvm_initsend(PvmDataDefault) < 0) { pvmerror("SendIndividuals"); } /* First send the location of this packet (so we know where to put it when the slave is done */ x = n; if (pvm_pkint(&x, 1, 1)) { pvmerror("SendIndividuals"); } /* Next send how many individuals to be evaluated */ x = howmany; if (pvm_pkint(&x, 1, 1)) { pvmerror("SendIndividuals"); } /* Finally, loop through all the Individuals and send them */ for (x = 0; x < howmany; x++) { i = p->pop + n + x; /* Pack the chromosome length for the Individual */ if (pvm_pkint(&(i->chromLen), 1, 1) < 0) { pvmerror("SendIndividuals"); } /* Pack the chromosome for the Individual */ if (pvm_pkint(i->chrom, i->chromLen, 1) < 0) { pvmerror("SendIndividuals"); } } /* Send the bundle out */ if (pvm_send(tid, PETROL_WORK_INDIVIDUALS) < 0) { pvmerror("SendIndividuals"); }}void GetIndividualsFitness(Population *p) { int location, howmany, n; Individual *i; /* First, get the starting location of the first Individual */ if (pvm_upkint(&location, 1, 1)) { pvmerror("GetIndividualsFitness"); } /* Get how many Individuals' fitnesses are packed in this message */ if (pvm_upkint(&howmany, 1, 1)) { pvmerror("GetIndividualsFitness"); } /* Cycle through and save the fitness in each Individual */ for (n = 0; n < howmany; n++) { i = p->pop + location + n; if (pvm_upkdouble(&(i->fitness), 1, 1)) { pvmerror("GetIndividualsFitness"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -