Skip to content

Instantly share code, notes, and snippets.

@prabhakaran9397
Created November 15, 2017 01:15
Show Gist options
  • Save prabhakaran9397/8079e73eafe694863d825a0bb43b2253 to your computer and use it in GitHub Desktop.
Save prabhakaran9397/8079e73eafe694863d825a0bb43b2253 to your computer and use it in GitHub Desktop.
Circuit satisfiablity exhaustive search using MPI
# include <stdlib.h>
# include <stdio.h>
# include <mpi.h>
# include <time.h>
# define N 23
void to_bin(int val, int bvec[])
{
int i;
for (i=0; i<N; i++)
bvec[i] = (val & (1<<i)) ? 1 : 0;
}
int circuit_value(int bvec[])
{
return ( bvec[0] || bvec[1] )
&& ( !bvec[1] || !bvec[3] )
&& ( bvec[2] || bvec[3] )
&& ( !bvec[3] || !bvec[4] )
&& ( bvec[4] || !bvec[5] )
&& ( bvec[5] || !bvec[6] )
&& ( bvec[5] || bvec[6] )
&& ( bvec[6] || !bvec[15] )
&& ( bvec[7] || !bvec[8] )
&& ( !bvec[7] || !bvec[13] )
&& ( bvec[8] || bvec[9] )
&& ( bvec[8] || !bvec[9] )
&& ( !bvec[9] || !bvec[10] )
&& ( bvec[9] || bvec[11] )
&& ( bvec[10] || bvec[11] )
&& ( bvec[12] || bvec[13] )
&& ( bvec[13] || !bvec[14] )
&& ( bvec[14] || bvec[15] )
&& ( bvec[14] || bvec[16] )
&& ( bvec[17] || bvec[1] )
&& ( bvec[18] || !bvec[0] )
&& ( bvec[19] || bvec[1] )
&& ( bvec[19] || !bvec[18] )
&& ( !bvec[19] || !bvec[9] )
&& ( bvec[0] || bvec[17] )
&& ( !bvec[1] || bvec[20] )
&& ( !bvec[21] || bvec[20] )
&& ( !bvec[22] || bvec[20] )
&& ( !bvec[21] || !bvec[20] )
&& ( bvec[22] || !bvec[20] );
}
int main (int argc, char *argv[])
{
int bvec[N], i, j, id, p;
int ihi, ihi2, ilo, ilo2;
int solution_num_local, solution_num;
MPI_Init (&argc, &argv);
MPI_Comm_rank (MPI_COMM_WORLD, &id);
MPI_Comm_size (MPI_COMM_WORLD, &p);
ilo = 0;
ihi = 1<<N;
ilo2 = ((p-id)*ilo + (id)*ihi)/p;
ihi2 = ((p-id-1)*ilo + (id+1)*ihi)/p;
solution_num_local = 0;
for(i=ilo2; i<ihi2; i++) {
to_bin(i, bvec);
if(circuit_value(bvec)) {
++solution_num_local;
printf("\n%2d %8d %10d: ", solution_num_local, id, i);
for (j=0; j<N; j++)
printf(" %d", bvec[j]);
printf ( "\n" );
}
}
MPI_Reduce(&solution_num_local, &solution_num, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if(id == 0) {
printf ("Number of solutions found was %d\n", solution_num);
}
MPI_Finalize ( );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment