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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>
/* Return 1 if 'i'th bit of 'n' is 1; 0 otherwise */
#define EXTRACT_BIT(n,i) ((n&(1<
int check_circuit (int z) {
// check_circuit check z is the correct solution to the circuit
int v[16]; /* Each element is a bit of z; array to hold 16 bits of the 65,536 possible combinations*/
int i; //variable to create 16 bit array to represent decimals, send it to i parameter is z so i =z
for (i = 0; i < 16; i++) v[i] = EXTRACT_BIT(z,i); //this is the logical circuit assume it is true
if ((v[0] || v[1]) && (!v[1] || !v[3]) && (v[2] || v[3])
&& (!v[3] || !v[4]) && (v[4] || !v[5])
&& (v[5] || !v[6]) && (v[5] || v[6])
&& (v[6] || !v[15]) && (v[7] || !v[8])
&& (!v[7] || !v[13]) && (v[8] || v[9])
&& (v[8] || !v[9]) && (!v[9] || !v[10])
&& (v[9] || v[11]) && (v[10] || v[11])
&& (v[12] || v[13]) && (v[13] || !v[14])
&& (v[14] || v[15])) {
printf ("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n",
v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9], //print out of the 65,536 possible solutions that are true which is 9 solutions will not change
v[10],v[11],v[12],v[13],v[14],v[15]);
return 1; //return 1 if solution is true (9)
} else return 0; //or return 0 if solution is not true
}
int main (int argc, char *argv[])
{
/*
solution algo: create a getopt function to take in CMD ./thdcircuit 3 ; where the user specifies the # of multithreads,
take user input and create specified multithreads, divide the calculations based on user defined multithreads, and divide / load balance
the calculations of 65,356 possibilies amoung the user defined multithreads, execution time should decrease as multithreads increase
*/
int count, i;
struct timeval t1, t2; //struc from #include
gettimeofday(&t1, 0);//struc from #include ; get time before t1 (before the work)
count = 0;
for (i = 0; i < 65536; i++)//for every number you send it to the function if it is true, it adds it to the count
count += check_circuit (i); //send it to the array tto check if it is true or false
gettimeofday(&t2, 0);//get time after t2 (after the work)
printf ("Execution time %fs\n", (t2.tv_sec-t1.tv_sec)+(t2.tv_usec-t1.tv_usec)*1e-6);//execution time to compare runtimes in multi thread and monothread
/*//execution time is finding the difference of t1 from t2
execution time should decrease as user specifies more multithreads
execution run without multithread config will decrease because computer is set to run at different times
*/
printf ("There are %d solutions\n", count);
return 0;
}
| |