• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Chess Algorithm behaving rather oddly

Locutus4657

Senior member
I wrote a chess algorithm for my MPI program, and it's behaving rather strangly... For some reason the only processor that actually get's to talk to every other processor is the very last one... And if you run the program with more that 2 CPU's it will crash while copying the transmition... This one really has me stumped... I've analized it to death, it should work as far as I can tell. I'm sorry for the poor indenting in the file... I've uploaded the original .c file to http://euclid.nmu.edu/~crazzeto/sga-c.d/transmit, the whole project is avaiable at http://euclid.nmu.edu/~crazzeto/sga-c.d

#include "external.h"

/* Transmit() makes use of Dr. Poe's "Chess Algorithm" too propagate the chromosomes
of every worker too every other worker */
Transmit()
{
int i, j, k, a, counter, ulen, c;
int other, n_odd, nbytes, numRandoms;
int *randoms;
struct individual *temp;
struct individual *my_transmition;
int id;

/* Temporary variables for revicing a worker */
unsigned *tchrom;
double tfit;
int txsite;
int tparent[2];
int *tutility;
double t_totalcost, tpipecost, tpenaltycost, tmaxpressdef, tavgpressdef, tmcount;
double tparm[21];

num_transmit = (int) (invaid_size * popsize);
ulen = (utility_size / sizeof(int));
nbytes = chromsize * sizeof(unsigned);
c = 0;

tchrom = malloc(nbytes);
if (utility_size)
{
tutility = malloc(utility_size);
}

counter = 0;

complete_number = (num_transmit * (now_size - 1));
size_of_transmit = (num_transmit * sizeof(struct individual));
compleated_size = (complete_number * sizeof(struct individual));

/* Initialize my two temps and my gobal list */
if((completed_list = (struct individual *) malloc(compleated_size)) == NULL)
{
nomemory(stderr,"completed_list");
}
if((temp = (struct individual *) malloc(size_of_transmit)) == NULL)
{
nomemory(stderr,"temp");
}
if((my_transmition = (struct individual *) malloc(size_of_transmit)) == NULL)
{
nomemory(stderr,"my_transmition");
}
for (i = 0; i < num_transmit; i++)
{
if((temp.chrom = (unsigned *) malloc(nbytes)) == NULL)
{
nomemory(stderr,"temp.chrom");
}
if((my_transmition.chrom = (unsigned *) malloc(nbytes)) == NULL)
{
nomemory(stderr,"my_transmition.chrom");
}
if (utility_size)
{
if((temp.utility = (int *) malloc(utility_size)) == NULL)
{
nomemory(stderr,"temp.chrom");
}
if((my_transmition.utility = (int *) malloc(utility_size)) == NULL)
{
nomemory(stderr,"my_transmition.chrom");
}
}
}

for (i = 0; i < complete_number; i++)
{
if((completed_list.chrom = (unsigned *) malloc(nbytes)) == NULL)
{
nomemory(stderr,"completed_list");
}
if (utility_size)
{
if((completed_list.utility = (unsigned *) malloc(utility_size)) == NULL)
{
nomemory(stderr,"completed_list");
}
}
}

/* This is currently a fairly simple way too fill up my transmition array,
I hope to have a better way of doing it soon */
for (i = 0; i < anum_best; i++)
{
for (j = 0; j < chromsize; j++)
{
my_transmition.chrom[j] = best_indi.chrom[j];
}
my_transmition.fitness = best_indi.fitness;
my_transmition.xsite = best_indi.xsite;
my_transmition.parent[0] = best_indi.parent[0];
my_transmition.parent[1] = best_indi.parent[1];
if(utility_size)
{
for (j = 0; j < ulen; j++)
{
my_transmition.utility[j] = best_indi.utility[j];
}
}
else
{
my_transmition.utility = NULL;
}
my_transmition.avgpressdef = best_indi.avgpressdef;
my_transmition.maxpressdef = best_indi.maxpressdef;
my_transmition.mcount = best_indi.mcount;
my_transmition.penaltycost = best_indi.penaltycost;
my_transmition.pipecost = best_indi.pipecost;
my_transmition.totalcost = best_indi.totalcost;
for (j = 0; j < 21; j++)
{
my_transmition.parms[j] = best_indi.parms[j];
}
}
numRandoms = ((num_transmit - anum_best) * sizeof(int));
randoms = malloc(numRandoms);
for(i = 0; i < (num_transmit - anum_best); i++)
{
randoms = rand() % popsize;
}
for (i = 0; i < (num_transmit - anum_best); i++)
{
for (j = 0; j < chromsize; j++)
{
my_transmition[i+anum_best].chrom[j] = oldpop[randoms].chrom[j];
}
my_transmition[i+anum_best].fitness = oldpop[randoms].fitness;
my_transmition[i+anum_best].xsite = oldpop[randoms].xsite;
my_transmition[i+anum_best].parent[0] = oldpop[randoms].parent[0];
my_transmition[i+anum_best].parent[1] = oldpop[randoms].parent[1];
if(utility_size)
{
for (j = 0; j < ulen; j++)
{
my_transmition[i+anum_best].utility[j] = oldpop[randoms].utility[j];
}
}
else
{
my_transmition[i+anum_best].utility = NULL;
}
my_transmition[i+anum_best].avgpressdef = oldpop[randoms].avgpressdef;
my_transmition[i+anum_best].maxpressdef = oldpop[randoms].maxpressdef;
my_transmition[i+anum_best].mcount = oldpop[randoms].mcount;
my_transmition[i+anum_best].penaltycost = oldpop[randoms].penaltycost;
my_transmition[i+anum_best].pipecost = oldpop[randoms].pipecost;
my_transmition[i+anum_best].totalcost = oldpop[randoms].totalcost;
for (j = 0; j < 21; j++)
{
my_transmition[i+anum_best].parms[j] = oldpop[randoms].parms[j];
}
}

if ((now_size % 2) == 0)
{
n_odd = now_size - 1;
}
else
{
n_odd = now_size;
}
for (i = 0; i < (now_size - 1); i++)
{
if ((!(now_size % 2)) || workstation_id != i)
{
if (workstation_id == (now_size - 1))
{
other = i;
}
else if (workstation_id == i)
{
other = (now_size - 1);
}
else
{
other = (n_odd + 2 * i - workstation_id) % n_odd;
}
if (workstation_id < other)
{
for (j = 0; j < num_transmit; j++)
{
if((my_transmition[j].chrom != NULL) && (my_transmition[j].parent != NULL))
{
if (!j)
{
(void) printf("Workstation: %d sending\n", workstation_id);
fflush(stdout);
}
MPI_Send(my_transmition[j].chrom, chromsize, MPI_UNSIGNED, other, chromsize, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].fitness, 1, MPI_DOUBLE, other, 1, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].xsite, 1, MPI_INT, other, 2, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].parent, 2, MPI_INT, other, 3, MPI_COMM_WORLD);
if (utility_size)
{
MPI_Send(my_transmition[j].utility, ulen, MPI_INT, other, ulen, MPI_COMM_WORLD);
}
MPI_Send(&my_transmition[j].avgpressdef, 1, MPI_DOUBLE, other, 4, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].maxpressdef, 1, MPI_DOUBLE, other, 5, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].mcount, 1, MPI_DOUBLE, other, 6, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].penaltycost, 1, MPI_DOUBLE, other, 7, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].pipecost, 1, MPI_DOUBLE, other, 8, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].totalcost, 1, MPI_DOUBLE, other, 9, MPI_COMM_WORLD);
MPI_Send(my_transmition[j].parms, 21, MPI_DOUBLE, other, 21, MPI_COMM_WORLD);
}
else
{
MPI_Finalize();
exit(-1);
}
}
for (j = 0; j < num_transmit; j++)
{
if (!j)
{
(void) printf("Workstation: %d reciving\n", workstation_id);
(void) fflush(stdout);
}
MPI_Recv(tchrom, chromsize, MPI_UNSIGNED, other, chromsize, MPI_COMM_WORLD, &status);
// _heapchk();
MPI_Recv(&tfit, 1, MPI_DOUBLE, other, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&txsite, 1, MPI_UNSIGNED, other, 2, MPI_COMM_WORLD, &status);
MPI_Recv(&tparent, 2, MPI_INT, other, 3, MPI_COMM_WORLD, &status);
if (utility_size)
{
MPI_Recv(tutility, ulen, MPI_UNSIGNED, other, ulen, MPI_COMM_WORLD, &status);
}
MPI_Recv(&tavgpressdef, 1, MPI_DOUBLE, other, 4, MPI_COMM_WORLD, &status);
MPI_Recv(&tmaxpressdef, 1, MPI_DOUBLE, other, 5, MPI_COMM_WORLD, &status);
MPI_Recv(&tmcount, 1, MPI_DOUBLE, other, 6, MPI_COMM_WORLD, &status);
MPI_Recv(&tpenaltycost, 1, MPI_DOUBLE, other, 7, MPI_COMM_WORLD, &status);
MPI_Recv(&tpipecost, 1, MPI_DOUBLE, other, 8, MPI_COMM_WORLD, &status);
MPI_Recv(&t_totalcost, 1, MPI_DOUBLE, other, 9, MPI_COMM_WORLD, &status);
MPI_Recv(tparm, 21, MPI_DOUBLE, other, 21, MPI_COMM_WORLD, &status);

for (k = 0; k < chromsize; k++)
{
temp[j].chrom[k] = tchrom[k];
}
temp[j].fitness = tfit;
temp[j].xsite = txsite;
temp[j].parent[0] = tparent[0];
temp[j].parent[1] = tparent[1];
if (utility_size)
{
for (k = 0; k < ulen; k++)
{
temp[j].utility[k] = tutility[k];
}
}
else
{
temp[j].utility = NULL;
}
temp[j].avgpressdef = tavgpressdef;
temp[j].maxpressdef = tmaxpressdef;
temp[j].mcount = tmcount;
temp[j].penaltycost = tpenaltycost;
temp[j].pipecost = tpipecost;
temp[j].totalcost = t_totalcost;
for (k = 0; k < 21; k++)
{
temp[j].parms[k] = tparm[k];
}
}
}
else
{
for (j = 0; j < num_transmit; j++)
{
if (!j)
{
(void) printf("Workstation: %d reciving\n", workstation_id);
(void) fflush(stdout);
}
MPI_Recv(tchrom, chromsize, MPI_UNSIGNED, other, chromsize, MPI_COMM_WORLD, &status);
// _heapchk();
MPI_Recv(&tfit, 1, MPI_DOUBLE, other, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&txsite, 1, MPI_UNSIGNED, other, 2, MPI_COMM_WORLD, &status);
MPI_Recv(&tparent, 2, MPI_INT, other, 3, MPI_COMM_WORLD, &status);
if (utility_size)
{
MPI_Recv(tchrom, utility_size, ulen, other, ulen, MPI_COMM_WORLD, &status);
}
MPI_Recv(&tavgpressdef, 1, MPI_DOUBLE, other, 4, MPI_COMM_WORLD, &status);
MPI_Recv(&tmaxpressdef, 1, MPI_DOUBLE, other, 5, MPI_COMM_WORLD, &status);
MPI_Recv(&tmcount, 1, MPI_DOUBLE, other, 6, MPI_COMM_WORLD, &status);
MPI_Recv(&tpenaltycost, 1, MPI_DOUBLE, other, 7, MPI_COMM_WORLD, &status);
MPI_Recv(&tpipecost, 1, MPI_DOUBLE, other, 8, MPI_COMM_WORLD, &status);
MPI_Recv(&t_totalcost, 1, MPI_DOUBLE, other, 9, MPI_COMM_WORLD, &status);
MPI_Recv(tparm, 21, MPI_DOUBLE, other, 21, MPI_COMM_WORLD, &status);
// _heapchk();
for (k = 0; k < chromsize; k++)
{
temp[j].chrom[k] = tchrom[k];
}
temp[j].fitness = tfit;
temp[j].xsite = txsite;
temp[j].parent[0] = tparent[0];
temp[j].parent[1] = tparent[1];
if (utility_size)
{
for (k = 0; k < ulen; k++)
{
temp[j].utility[k] = tutility[k];
}
}
else
{
temp[j].utility = NULL;
}
temp[j].avgpressdef = tavgpressdef;
temp[j].maxpressdef = tmaxpressdef;
temp[j].mcount = tmcount;
temp[j].penaltycost = tpenaltycost;
temp[j].pipecost = tpipecost;
temp[j].totalcost = t_totalcost;
for (k = 0; k < 21; k++)
{
temp[j].parms[k] = tparm[k];
}
// _heapchk();
}
for (j = 0; j < num_transmit; j++)
{
if ((my_transmition[j].chrom != NULL) && (my_transmition[j].parent != NULL))
{
if (!j)
{
(void) printf("Workstation: %d sending\n", workstation_id);
(void) fflush(stdout);
}
MPI_Send(my_transmition[j].chrom, chromsize, MPI_UNSIGNED, other, chromsize, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].fitness, 1, MPI_DOUBLE, other, 1, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].xsite, 1, MPI_INT, other, 2, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].parent, 2, MPI_INT, other, 3, MPI_COMM_WORLD);
if (utility_size)
{
MPI_Send(my_transmition[j].utility, ulen, MPI_INT, other, ulen, MPI_COMM_WORLD);
}
MPI_Send(&my_transmition[j].avgpressdef, 1, MPI_DOUBLE, other, 4, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].maxpressdef, 1, MPI_DOUBLE, other, 5, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].mcount, 1, MPI_DOUBLE, other, 6, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].penaltycost, 1, MPI_DOUBLE, other, 7, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].pipecost, 1, MPI_DOUBLE, other, 8, MPI_COMM_WORLD);
MPI_Send(&my_transmition[j].totalcost, 1, MPI_DOUBLE, other, 9, MPI_COMM_WORLD);
MPI_Send(my_transmition[j].parms, 21, MPI_DOUBLE, other, 21, MPI_COMM_WORLD);
}
else
{
MPI_Finalize();
exit(-1);
}
}
}
// _heapchk();
for (j = 0; j < complete_number; j++)
{
for (k = 0; k < chromsize; k++)
{
completed_list[counter].chrom[k] = temp[j].chrom[k];
}
completed_list[counter].fitness = temp[j].fitness;
completed_list[counter].xsite = temp[j].xsite;
completed_list[counter].parent[0] = temp[j].parent[0];
completed_list[counter].parent[1] = temp[j].parent[1];
if (utility_size)
{
for (k = 0; k < ulen; k++)
{
completed_list[counter].utility[k] = temp[j].utility[k];
}
}
else
{
completed_list[counter].utility = NULL;
}
completed_list[counter].avgpressdef = temp[j].avgpressdef;
completed_list[counter].maxpressdef = temp[j].maxpressdef;
completed_list[counter].mcount = temp[j].mcount;
completed_list[counter].penaltycost = temp[j].penaltycost;
completed_list[counter].pipecost = temp[j].pipecost;
completed_list[counter].totalcost = temp[j].totalcost;
for (k = 0; k < 21; k++)
{
completed_list[counter].parms[k] = temp[j].parms[k];
}
counter++;
}
}
}

for(i = 0; i < num_transmit; i++)
{
free(my_transmition.chrom);
if (utility_size)
{
free(my_transmition.utility);
}
}
free(my_transmition);


free(temp);

for (i = 0; i < anum_best; i++)
{
free(best_indi.chrom);
if (utility_size)
{
free(best_indi.utility);
}
}
free(best_indi);
best_indi_set--;

id = workstation_id;
free(tchrom);


if(utility_size)
{
free(tutility);
}
free(randoms);

copyTransmition();
}
 
Back
Top