Skip to content

Instantly share code, notes, and snippets.

@arrdem
Created September 6, 2016 06:44
Show Gist options
  • Save arrdem/9a9ff612c9e386b9d352c612fdb221a0 to your computer and use it in GitHub Desktop.
Save arrdem/9a9ff612c9e386b9d352c612fdb221a0 to your computer and use it in GitHub Desktop.
[1] Handed out (0, 0, 0) * STEP
[2] Handed out (0, 0, 1) * STEP
[3] Handed out (0, 1, 0) * STEP
[4] Handed out (0, 1, 1) * STEP
[5] Handed out (0, 1, 2) * STEP
[6] Handed out (1, 0, 0) * STEP
[7] Handed out (1, 0, 1) * STEP
[8] Handed out (1, 1, 0) * STEP
[7] [ANSWER] Got one! (1008, 960, 1100)
[8] [ANSWER] Got one! (1008, 1100, 960)
[7] [ANSWER] Got one! (1020, 187, 1584)
[8] [ANSWER] Got one! (1020, 1584, 187)
[1] [ANSWER] Got one! (44, 117, 240)
[1] [ANSWER] Got one! (44, 240, 117)
[1] [ANSWER] Got one! (85, 132, 720)
[1] [ANSWER] Got one! (85, 720, 132)
[1] [ANSWER] Got one! (88, 234, 480)
[1] [ANSWER] Got one! (88, 480, 234)
[7] [ANSWER] Got one! (1100, 960, 1008)
[8] [ANSWER] Got one! (1100, 1008, 960)
[1] [ANSWER] Got one! (117, 44, 240)
[1] [ANSWER] Got one! (117, 240, 44)
[1] [ANSWER] Got one! (132, 85, 720)
[1] [ANSWER] Got one! (132, 351, 720)
[1] [ANSWER] Got one! (132, 720, 85)
[1] [ANSWER] Got one! (132, 720, 351)
[1] [ANSWER] Got one! (140, 480, 693)
[1] [ANSWER] Got one! (140, 693, 480)
[3] [ANSWER] Got one! (170, 1440, 264)
[1] [ANSWER] Got one! (160, 231, 792)
[1] [ANSWER] Got one! (160, 792, 231)
[2] [ANSWER] Got one! (170, 264, 1440)
[1] [ANSWER] Got one! (176, 468, 960)
[1] [ANSWER] Got one! (176, 960, 468)
[4] [ANSWER] Got one! (187, 1020, 1584)
[4] [ANSWER] Got one! (187, 1584, 1020)
[6] [ANSWER] Got one! (1200, 220, 585)
[6] [ANSWER] Got one! (1200, 585, 220)
[3] [ANSWER] Got one! (220, 1200, 585)
[2] [ANSWER] Got one! (220, 585, 1200)
[1] [ANSWER] Got one! (231, 160, 792)
[1] [ANSWER] Got one! (231, 792, 160)
[1] [ANSWER] Got one! (234, 88, 480)
[1] [ANSWER] Got one! (234, 480, 88)
[1] [ANSWER] Got one! (240, 44, 117)
[1] [ANSWER] Got one! (240, 117, 44)
[1] [ANSWER] Got one! (240, 252, 275)
[1] [ANSWER] Got one! (240, 275, 252)
[1] [ANSWER] Got one! (252, 240, 275)
[1] [ANSWER] Got one! (252, 275, 240)
[2] [ANSWER] Got one! (264, 170, 1440)
[2] [ANSWER] Got one! (264, 702, 1440)
[3] [ANSWER] Got one! (264, 1440, 170)
[3] [ANSWER] Got one! (264, 1440, 702)
[3] [ANSWER] Got one! (280, 1386, 960)
[2] [ANSWER] Got one! (280, 960, 1386)
[1] [ANSWER] Got one! (275, 240, 252)
[1] [ANSWER] Got one! (275, 252, 240)
[3] [ANSWER] Got one! (308, 1680, 819)
[2] [ANSWER] Got one! (308, 819, 1680)
[3] [ANSWER] Got one! (320, 1584, 462)
[2] [ANSWER] Got one! (320, 462, 1584)
[3] [ANSWER] Got one! (352, 1920, 936)
[2] [ANSWER] Got one! (352, 936, 1920)
[1] [ANSWER] Got one! (351, 132, 720)
[1] [ANSWER] Got one! (351, 720, 132)
[6] [ANSWER] Got one! (1386, 280, 960)
[6] [ANSWER] Got one! (1386, 960, 280)
[5] [ANSWER] Got one! (396, 1053, 2160)
[5] [ANSWER] Got one! (420, 1440, 2079)
[6] [ANSWER] Got one! (1440, 170, 264)
[6] [ANSWER] Got one! (1440, 264, 170)
[6] [ANSWER] Got one! (1440, 264, 702)
[6] [ANSWER] Got one! (1440, 702, 264)
[5] [ANSWER] Got one! (440, 1170, 2400)
[3] [ANSWER] Got one! (462, 1584, 320)
[1] [ANSWER] Got one! (468, 176, 960)
[1] [ANSWER] Got one! (468, 960, 176)
[2] [ANSWER] Got one! (462, 320, 1584)
[1] [ANSWER] Got one! (480, 88, 234)
[1] [ANSWER] Got one! (480, 140, 693)
[1] [ANSWER] Got one! (480, 234, 88)
[1] [ANSWER] Got one! (480, 504, 550)
[1] [ANSWER] Got one! (480, 550, 504)
[1] [ANSWER] Got one! (480, 693, 140)
[5] [ANSWER] Got one! (484, 1287, 2640)
[1] [ANSWER] Got one! (504, 480, 550)
[1] [ANSWER] Got one! (504, 550, 480)
[5] [ANSWER] Got one! (528, 1404, 2880)
[1] [ANSWER] Got one! (550, 480, 504)
[1] [ANSWER] Got one! (550, 504, 480)
[5] [ANSWER] Got one! (560, 1920, 2772)
[7] [ANSWER] Got one! (1584, 187, 1020)
[6] [ANSWER] Got one! (1584, 320, 462)
[6] [ANSWER] Got one! (1584, 462, 320)
[3] [ANSWER] Got one! (585, 1200, 220)
[8] [ANSWER] Got one! (1584, 1020, 187)
[2] [ANSWER] Got one! (585, 220, 1200)
[6] [ANSWER] Got one! (1680, 308, 819)
[6] [ANSWER] Got one! (1680, 819, 308)
[1] [ANSWER] Got one! (693, 140, 480)
[1] [ANSWER] Got one! (693, 480, 140)
[3] [ANSWER] Got one! (702, 1440, 264)
[2] [ANSWER] Got one! (702, 264, 1440)
[1] [ANSWER] Got one! (720, 85, 132)
[1] [ANSWER] Got one! (720, 132, 85)
[1] [ANSWER] Got one! (720, 132, 351)
[1] [ANSWER] Got one! (720, 351, 132)
[1] [ANSWER] Got one! (720, 756, 825)
[1] [ANSWER] Got one! (720, 825, 756)
[1] [ANSWER] Got one! (756, 720, 825)
[1] [ANSWER] Got one! (756, 825, 720)
[1] [ANSWER] Got one! (792, 160, 231)
[1] [ANSWER] Got one! (792, 231, 160)
[1] [ANSWER] Got one! (825, 720, 756)
[1] [ANSWER] Got one! (825, 756, 720)
[3] [ANSWER] Got one! (819, 1680, 308)
[2] [ANSWER] Got one! (819, 308, 1680)
[6] [ANSWER] Got one! (1920, 352, 936)
[6] [ANSWER] Got one! (1920, 936, 352)
[3] [ANSWER] Got one! (936, 1920, 352)
[2] [ANSWER] Got one! (936, 352, 1920)
[1] [ANSWER] Got one! (960, 176, 468)
[1] [ANSWER] Got one! (960, 468, 176)
[4] [ANSWER] Got one! (960, 1008, 1100)
[4] [ANSWER] Got one! (960, 1100, 1008)
[3] [ANSWER] Got one! (960, 1386, 280)
[2] [ANSWER] Got one! (960, 280, 1386)
[4] Handed out (1, 1, 1) * STEP
[1] Handed out (1, 1, 2) * STEP
[6] Handed out (1, 2, 0) * STEP
[4] [ANSWER] Got one! (1008, 1100, 1155)
[4] [ANSWER] Got one! (1008, 1155, 1100)
[7] Handed out (1, 2, 1) * STEP
[3] Handed out (1, 2, 2) * STEP
[5] Handed out (1, 2, 3) * STEP
[8] Handed out (2, 0, 0) * STEP
[2] Handed out (2, 0, 1) * STEP
[6] [ANSWER] Got one! (1053, 2160, 396)
[4] [ANSWER] Got one! (1100, 1008, 1155)
[4] [ANSWER] Got one! (1100, 1155, 1008)
[2] [ANSWER] Got one! (2079, 420, 1440)
[4] [ANSWER] Got one! (1155, 1008, 1100)
[4] [ANSWER] Got one! (1155, 1100, 1008)
[6] [ANSWER] Got one! (1170, 2400, 440)
[8] [ANSWER] Got one! (2160, 255, 396)
[8] [ANSWER] Got one! (2160, 396, 255)
[2] [ANSWER] Got one! (2160, 396, 1053)
[4] [ANSWER] Got one! (1200, 1260, 1375)
[4] [ANSWER] Got one! (1200, 1375, 1260)
[4] [ANSWER] Got one! (1260, 1200, 1375)
[4] [ANSWER] Got one! (1260, 1375, 1200)
[6] [ANSWER] Got one! (1287, 2640, 484)
[8] [ANSWER] Got one! (2340, 429, 880)
[8] [ANSWER] Got one! (2340, 880, 429)
[4] [ANSWER] Got one! (1375, 1200, 1260)
[4] [ANSWER] Got one! (1375, 1260, 1200)
[8] [ANSWER] Got one! (2376, 480, 693)
[8] [ANSWER] Got one! (2376, 693, 480)
[6] [ANSWER] Got one! (1404, 2880, 528)
[2] [ANSWER] Got one! (2400, 440, 1170)
[4] [ANSWER] Got one! (1440, 1512, 1650)
[4] [ANSWER] Got one! (1440, 1650, 1512)
[6] [ANSWER] Got one! (1440, 2079, 420)
[4] [ANSWER] Got one! (1512, 1440, 1650)
[4] [ANSWER] Got one! (1512, 1650, 1440)
[4] [ANSWER] Got one! (1650, 1440, 1512)
[4] [ANSWER] Got one! (1650, 1512, 1440)
[8] [ANSWER] Got one! (2640, 832, 855)
[8] [ANSWER] Got one! (2640, 855, 832)
[2] [ANSWER] Got one! (2640, 484, 1287)
[4] [ANSWER] Got one! (1680, 1764, 1925)
[4] [ANSWER] Got one! (1680, 1925, 1764)
[4] [ANSWER] Got one! (1764, 1680, 1925)
[4] [ANSWER] Got one! (1764, 1925, 1680)
[2] [ANSWER] Got one! (2772, 560, 1920)
[2] [ANSWER] Got one! (2880, 528, 1404)
[8] [ANSWER] Got one! (2880, 340, 528)
[8] [ANSWER] Got one! (2880, 528, 340)
[6] [ANSWER] Got one! (1920, 2772, 560)
[4] [ANSWER] Got one! (1925, 1680, 1764)
[4] [ANSWER] Got one! (1925, 1764, 1680)
[3] [ANSWER] Got one! (1920, 2016, 2200)
[3] [ANSWER] Got one! (1920, 2200, 2016)
[6] Handed out (2, 1, 0) * STEP
[4] Handed out (2, 1, 1) * STEP
[1] Handed out (2, 1, 2) * STEP
[7] Handed out (2, 2, 0) * STEP
[1] [ANSWER] Got one! (2016, 1920, 2200)
[3] Handed out (2, 2, 1) * STEP
[2] Handed out (2, 2, 2) * STEP
[8] Handed out (2, 2, 3) * STEP
[3] [ANSWER] Got one! (2016, 2200, 1920)
[2] [ANSWER] Got one! (2016, 2200, 2310)
[2] [ANSWER] Got one! (2016, 2310, 2200)
[5] Handed out (2, 3, 0) * STEP
[5] [ANSWER] Got one! (2035, 3120, 828)
[6] [ANSWER] Got one! (2079, 1440, 420)
[5] [ANSWER] Got one! (2040, 3168, 374)
[6] [ANSWER] Got one! (2160, 1053, 396)
[2] [ANSWER] Got one! (2160, 2268, 2475)
[2] [ANSWER] Got one! (2160, 2475, 2268)
[3] [ANSWER] Got one! (2200, 2016, 1920)
[1] [ANSWER] Got one! (2200, 1920, 2016)
[2] [ANSWER] Got one! (2200, 2016, 2310)
[2] [ANSWER] Got one! (2200, 2310, 2016)
[2] [ANSWER] Got one! (2268, 2160, 2475)
[2] [ANSWER] Got one! (2268, 2475, 2160)
[2] [ANSWER] Got one! (2310, 2016, 2200)
[2] [ANSWER] Got one! (2310, 2200, 2016)
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <math.h>
typedef struct {
uint64_t min, max;
} range_t;
typedef struct {
uint64_t x, y, z;
} brick_t;
typedef struct {
range_t x, y, z;
} searchspace_t;
typedef enum {
X_AXIS, Y_AXIS, Z_AXIS
} axis_t;
uint64_t NEXT_X = 1;
uint64_t NEXT_Y = 1;
uint64_t NEXT_Z = 1;
const uint64_t STEP = 1000;
uint64_t GLOBAL_ID = 1;
pthread_mutex_t GLOBAL_STATE_MUTEX;
pthread_mutex_t PRINTF_MUTEX;
void get_next_search_space(uint64_t id, searchspace_t* ret) {
pthread_mutex_lock(&GLOBAL_STATE_MUTEX);
ret->x.min = NEXT_X;
ret->x.max = NEXT_X + STEP;
ret->y.min = NEXT_Y;
ret->y.max = NEXT_Y + STEP;
ret->z.min = NEXT_Z;
ret->z.max = NEXT_Z + STEP;
pthread_mutex_lock(&PRINTF_MUTEX);
fprintf(stderr, "[%lu] Handed out (%lu, %lu, %lu) * STEP\n", id, NEXT_X/STEP, NEXT_Y/STEP, NEXT_Z/STEP);
fflush(stderr);
pthread_mutex_unlock(&PRINTF_MUTEX);
/**
* So this is a really, really fun hack. The idea here is that we want to, among several threads,
* cycle through a three dimensional search space broken up into 1k^3 cubes. This can be modeled
* as taking the naive foor loop -
*
* for (int x = 1; true; x += STEP) {
* for (int y = 1; y <= x; y += STEP) {
* for (int z = 1; z <= y; z += STEP) {
* yield (x, y, z);
* }
* z = 1;
* }
* y = 1;
* }
*
* and turning it on its head. We maintain global counters NEXT_X, NEXT_Y and NEXT_Z, as if in a
* generator structure of some sort.
*/
if(NEXT_Z <= NEXT_Y) {
NEXT_Z += STEP;
} else {
NEXT_Z = 0;
if(NEXT_Y <= NEXT_X) {
NEXT_Y += STEP;
} else {
NEXT_Y = 0;
NEXT_X += STEP;
}
}
pthread_mutex_unlock(&GLOBAL_STATE_MUTEX);
}
void *worker(void *_arg) {
searchspace_t searchspace;
pthread_mutex_lock(&GLOBAL_STATE_MUTEX);
uint64_t id = GLOBAL_ID++;
pthread_mutex_unlock(&GLOBAL_STATE_MUTEX);
while(1) {
get_next_search_space(id, &searchspace);
for(uint64_t x = searchspace.x.min; x < searchspace.x.max; x++) {
if(x != 0) {
uint64_t x_2 = x*x;
for(uint64_t y = searchspace.y.min; y < searchspace.y.max; y++) {
if(y != 0) {
uint64_t y_2 = y*y;
for(uint64_t z = searchspace.z.min; z < searchspace.z.max; z++) {
if(z != 0) {
uint64_t z_2 = z*z;
long double
xy = sqrt(x_2+y_2),
yz = sqrt(y_2+z_2),
xz = sqrt(z_2+x_2);
/**
* A long double (128bi floating point per IEEE 754-2008) is an integer IF AND ONLY IF the
* floor of the value is equal to the value. This is also true for the ceiling by
* definition. Just in case of numerics weirdness, assert that both properties are held
* true as our test for whether a value is integral.
*/
if(ceill(xy) == xy && floorl(xy) == xy &&
ceill(yz) == yz && floorl(yz) == yz &&
ceill(xz) == xz && floorl(xz) == xz) {
pthread_mutex_lock(&PRINTF_MUTEX);
fprintf(stdout, "[%lu] [ANSWER] Got one! (%lu, %lu, %lu)\n", id, x, y, z);
fflush(stdout);
pthread_mutex_unlock(&PRINTF_MUTEX);
}
}
}
}
}
}
}
}
}
int main(int argc, char** argv) {
pthread_mutex_init(&GLOBAL_STATE_MUTEX, NULL);
pthread_mutex_init(&PRINTF_MUTEX, NULL);
const uint64_t NUMTHREADS = 8;
pthread_t threads[NUMTHREADS];
for(uint64_t i = 0; i < NUMTHREADS; i++) {
pthread_create(&threads[i], NULL, worker, NULL);
}
for (uint64_t i = 0; i < NUMTHREADS; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment