Early subsumption complete

Signed-off-by: gothictomato <gothictomato@pm.me>
This commit is contained in:
gothictomato
2022-08-21 12:49:48 -04:00
parent a1b6cdaea9
commit 4c111f4b6f
8 changed files with 277 additions and 323 deletions

178
main.c
View File

@@ -1,7 +1,7 @@
#include <stdio.h>
// #include "gpusolver.h"
#include "gpusolver.h"
#include "time.h"
// #include "tests/masterTest.h"
#include "tests/masterTest.h"
#include "gmp.h"
#include "rng.h"
#include "ncnf.h"
@@ -10,8 +10,9 @@
#define CMP (1)
#define CHK (2)
void ctrthings2(cnf* c, u32* state, u32* ctr, u32* max) {
u32 wcnt = 1U + (c->cnts[0] / 32U);
u32 wcnt = 1U + (c->cnts[0] >> 5U);
u32* mode = state;
u32* index = state + 1;
u32* addval = state + 2;
@@ -27,101 +28,40 @@ void ctrthings2(cnf* c, u32* state, u32* ctr, u32* max) {
u8 corpar = (ctr[vword] >> vbit) & 1U;
u8 isvalid = (par == corpar);
u8 islvar = ((*addval + 1) == c->clausedat[3 * chkcls + 1]);
if (*mode == CHK) {
// printf("> %u %u\n", *index, *addval);
// printf("%u %u %u\n", var, par, corpar);
// printf("%u %u\n", chkcls, chkind);
// printf("%u %u %u\n", islvar, isvalid, c->clausedat[3 * chkcls + 1]);
/*
* if last var
* if valid, add
* if invalid, iterate clause
* else
* if valid, iterate addval up to len
* if invalid, iterate claus
* If current var is valid:
* if last var:
*
*/
if (isvalid) {
if (islvar) {
u32 j = c->clausedat[3 * chkcls + 2];
*mode -= 2;
printf("j: %u\n", j);
*index = j >> 5U;
*addval = 1U << (j & 0b11111U);
} else {
*addval += 1U;
}
} else {
*addval = 0;
*index += 1U;
if (*index == c->cnts[1]) {
printf("SAT\n");
*mode = 4;
return;
}
}
} /* else {
//printf("YEET\n");
if (*index >= wcnt) printf("FUCK\n");
u32 nval = ctr[*index] + *addval; // Find the result of the current step if it was addition
*addval = (nval < ctr[*index]) * (*mode == ADD); // If in add mode, set addval to carry, else set 0
ctr[*index] = nval * (*mode == ADD) + ctr[*index] * (*mode != ADD); // If in add mode, set new ctr val, otherwise leave unchanged
*addval -= (ctr[*index] < max[*index]) * (*mode == CMP); // If in comparison mode, decrement addval if less than
*addval += (ctr[*index] > max[*index]) * (*mode == CMP); // If in comparison mode, increment addval if greater than
bool addcond = (*addval == 0) | (*index == (wcnt - 1)); // Exit condition for the ADD state: If addval is zero (no carry) or we're at the last word
bool cmpcond = (*addval != 0) | (*index == 0); // Exit condition for the CMP state: if addval is nonzero (lt or gt) or we're at the least significant word
if (*mode == CMP && cmpcond && *addval != -1) { // If in cmpmode and the comparison result is not less than, unsat
printf("UNSAT\n");
*mode = 4;
return;
}
bool cmpdone = cmpcond * (*mode == CMP); // if comparison completion conditions are satisfied and in CMP mode
u32 addindex = (*index + 1) * !addcond + (wcnt - 1) * addcond; // if add completion is satisfied, set index to most significant word, else increment by 1
*index = addindex * (*mode == ADD) + (*index - 1) * (*mode == CMP); // If in add mode, use addindex; if in cmp mode, decrement index by 1
*index *= !cmpdone;
// Leave adval alone if:
// not in add mode
// add mode isn't done
// not in cmp mode
// cmp mode isn't done
//
*addval *= !(((addcond) & (*mode != ADD)) & cmpdone); // If add is complete, zero addval, else leave unchanged
*mode += addcond * (*mode == ADD) + cmpdone; // If in add mode and add completion is reached, increment mode. If in cmp mode and cmp completion reached, increment mode.
}
*/
u8 isbchk0 = (*mode == CHK);
u8 isbchk1 = isbchk0 & isvalid;
u8 isbchk2 = isbchk1 & islvar;
u32 j = c->clausedat[3 * chkcls + 2];
*mode -= 2 * isbchk2;
*index = (j >> 5U) * isbchk2 + *index * (!isbchk2);
*addval = (1U << (j & 0b11111U)) * isbchk2 + *addval * (!isbchk2);
*addval += ((isbchk1) & (!islvar));
u8 isbchk3 = (isbchk0 & (!isvalid));
*addval *= (!isbchk3);
*index += (isbchk3);
u8 issat = (*index == c->cnts[1]) * (isbchk3);
u32 cmpaddind = *index * (*mode != CHK);
if (cmpaddind >= wcnt) printf("FUCK\n");
u32 nval = ctr[cmpaddind] + *addval; // Find the result of the current step if it was addition
*addval = (nval < ctr[cmpaddind]) * (*mode == ADD) + (*addval) * (*mode == CHK); // If in add mode, set addval to carry. If in cmp mode, set to 0. If in check mode, leave alone.
ctr[cmpaddind] = nval * (*mode == ADD) + ctr[cmpaddind] * (*mode != ADD); // If in add mode, set new ctr val, otherwise leave unchanged
ctr[cmpaddind] = nval * ((*mode == ADD) & !issat) + ctr[cmpaddind] * ((*mode != ADD) | issat); // If in add mode, set new ctr val, otherwise leave unchanged
*addval -= (ctr[cmpaddind] < max[cmpaddind]) * (*mode == CMP); // If in comparison mode, decrement addval if less than
*addval += (ctr[cmpaddind] > max[cmpaddind]) * (*mode == CMP); // If in comparison mode, increment addval if greater than
bool addcond = (*addval == 0) | (cmpaddind == (wcnt - 1)); // Exit condition for the ADD state: If addval is zero (no carry) or we're at the last word
bool cmpcond = (*addval != 0) | (cmpaddind == 0); // Exit condition for the CMP state: if addval is nonzero (lt or gt) or we're at the least significant word
if (*mode == CMP && cmpcond && *addval != -1) { // If in cmpmode and the comparison result is not less than, unsat
printf("UNSAT\n");
u8 addcond = (*addval == 0) | (cmpaddind == (wcnt - 1)); // Exit condition for the ADD state: If addval is zero (no carry) or we're at the last word
u8 cmpcond = (*addval != 0) | (cmpaddind == 0); // Exit condition for the CMP state: if addval is nonzero (lt or gt) or we're at the least significant word
u8 exittime = (*mode == CMP) & cmpcond & (*addval != -1);
exittime |= issat;
if (exittime) { // If in cmpmode and the comparison result is not less than, unsat
printf("Result: %u\n", issat);
*mode = 4;
return;
}
bool cmpdone = cmpcond & (*mode == CMP); // if comparison completion conditions are satisfied and in CMP mode
u8 cmpdone = cmpcond & (*mode == CMP); // if comparison completion conditions are satisfied and in CMP mode
u32 addindex = (cmpaddind + 1) * !addcond + (wcnt - 1) * addcond; // if add completion is satisfied, set index to most significant word, else increment by 1
*index = addindex * (*mode == ADD) + (*index - (*mode == CMP)) * (*mode != ADD); // If in add mode, use addindex; if in cmp mode, decrement index by 1
*index *= !cmpdone;
// Leave adval alone if:
// not in add mode
// add mode isn't done
// not in cmp mode
// cmp mode isn't done
//
*addval *= !(((addcond) & (*mode == ADD)) | cmpdone); // If add is complete, or cmp is complete, zero. Else leave unchanged.
*mode += addcond * (*mode == ADD) + cmpdone; // If in add mode and add completion is reached, increment mode. If in cmp mode and cmp completion reached, increment mode.
}
void printbits(unsigned a) {
@@ -134,6 +74,38 @@ void printbits(unsigned a) {
#define TESTS (274877906944LU >> 10U)
#define CSZE (83LU)
#define eqprob (0.01f)
void mul(u32* c, u32 len, u32* a, u32 b) {
u32 carry = 0;
for (u32 i = 0; i < len; ++i) {
u32 ncarry;
u32 blo = a[i] & 0xFFFFU;
u32 bhi = a[i] >> 16U;
u32 ilo = b & 0xFFFFU;
u32 ihi = b >> 16U;
*(c + i) = ilo * blo;
u32 b1 = ilo * bhi;
u32 c1 = ihi * blo;
ncarry = ihi * bhi;
b1 += c1;
ncarry += (b1 < c1) << 16U;
u32 bblo = b1 & 0xFFFFU;
u32 bbhi = b1 >> 16U;
bblo <<= 16U;
*(c + i) += bblo;
u8 acarry = *(c + i) < bblo;
ncarry += bbhi + acarry;
c[i] += carry;
ncarry += c[i] < carry;
carry = ncarry;
}
}
int main() {
/*
printf("Tests: %lu\n", TESTS);
@@ -276,38 +248,20 @@ int main() {
/* Expects a path to a DIMACS file */
cnf* c = readDIMACS("/home/lev/Downloads/uf20/uf20-022.cnf");
/*
cnf* c = readDIMACS("/home/lev/Downloads/logistics/logistics.d.cnf");
sortlastnum(c);
// printcnf(c);
u32 wcnt = 1U + (c->cnts[0] / 32U);
printf("%u\n", c->cnts[0]);
gpusolve(c);
u32* ctr = calloc(wcnt, sizeof(u32));
u32* max = calloc(wcnt, sizeof(u32));
max[c->cnts[0] >> 5U] = 1U << (c->cnts[0] & 0b11111U);
u32 state[3];
state[0] = 2;
state[1] = state[2] = 0;
u32 mtr = 0;
while (state[0] < 3U) {
u32 cmd = state[0];
ctrthings2(c, state, ctr, max);
if (state[0] != 2 && cmd == 2) {
//printf("\n");
//for (unsigned i = wcnt - 1; i < wcnt; --i) printbits(ctr[i]);
//printf("\n");
//mtr++;
//if (mtr == 10) exit(15);
// printf("%u %u %u\n", state[0], state[1], state[2]);
}
}
freecnf(c);
return 0;
*/
runTests();
return 0;
/*