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
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
/* Return 1 if 'i'th bit of 'n' is 1; 0 otherwise */
#define EXTRACT_BIT(n,i) ((n&(1<<i))?1:0)
int count = 0;
int check_circuit (int z) {
int v[16]; /* Each element is a bit of z */
int i;
for (i = 0; i < 16; i++) v[i] = EXTRACT_BIT(z,i);
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],
v[10],v[11],v[12],v[13],v[14],v[15]);
return 1;
} else return 0;
}
void * multiCircuit ( void * tid)
{
long * myID = (long *) tid;
int z;
z = check_circuit(count);
printf("%d was found by %ld\n", z, *myID);
return 0;
}
int main (int argc, char *argv[])
{
int arg;
arg = atoi(argv[1]);
if(argc != 2)
{
printf("Please enter the number of threads you wish to initialize\n");
return 1;
}
if(arg < 1 || arg > 128)
{
printf("Please enter a number of threads between 1-128\n");
return 11;
}
if (argc == 2 && (arg > 1 && arg < 128)){
pthread_t threads[arg];
pthread_mutex_t lock;
int i, thread_NUM;
thread_NUM = arg;
struct timeval t1, t2;
gettimeofday(&t1, 0);
for (i = 0; i < 65536/thread_NUM; i++){
pthread_create(&threads[i], NULL, multiCircuit, (void *) &threads[i]);
pthread_mutex_lock(&lock);
//count += check_circuit (i);
count++;
gettimeofday(&t2, 0);
pthread_mutex_unlock(&lock);
}
printf ("Execution time %fs\n", (t2.tv_sec-t1.tv_sec)+(t2.tv_usec-t1.tv_usec)*1e-6);
printf ("There are %d solutions\n", count);
pthread_exit(NULL);
}
return 0;
}
| |