This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/cuda/cuda.c

430 lines
9.8 KiB
C
Raw Normal View History

2013-10-04 13:22:00 +01:00
// interface to CUDD Datalog evaluation
#include "config.h"
#include "YapInterface.h"
2013-10-04 14:42:18 +01:00
#include <stdio.h>
#include <stdlib.h>
2013-10-07 12:20:00 +01:00
#include <stdint.h>
2013-10-04 14:42:18 +01:00
#include <string.h>
2013-10-07 12:20:00 +01:00
#include "pred.h"
2013-10-04 14:42:18 +01:00
2013-10-07 13:48:19 +01:00
YAP_Atom AtomEq,
AtomGt,
AtomLt,
AtomGe,
AtomLe,
AtomDf;
2013-10-07 12:20:00 +01:00
predicate *facts[100]; /*Temporary solution to maintain facts and rules*/
predicate *rules[100];
int32_t cf = 0, cr = 0;
2013-10-04 13:22:00 +01:00
2013-10-04 14:42:18 +01:00
// initialize CUDA system
2013-10-04 13:22:00 +01:00
void Cuda_Initialize( void );
2013-10-04 14:42:18 +01:00
// add/replace a set of facts for predicate pred
2013-10-07 12:20:00 +01:00
int32_t Cuda_NewFacts(predicate *pred);
2013-10-04 13:22:00 +01:00
2013-10-04 14:42:18 +01:00
// add/replace a rule for predicate pred
2013-10-07 12:20:00 +01:00
int32_t Cuda_NewRule(predicate *pred);
2013-10-04 14:42:18 +01:00
// erase predicate pred
2013-10-07 12:20:00 +01:00
int32_t Cuda_Erase(predicate *pred);
2013-10-04 14:42:18 +01:00
// evaluate predicate pred, mat is bound to a vector of solutions, and
// output the count
2013-10-07 12:20:00 +01:00
//int32_t Cuda_Eval(predicate *pred, int32_t **mat); This functions arguments were changed, please see pred.h
2013-10-04 13:22:00 +01:00
void init_cuda( void );
2013-10-16 14:52:54 +01:00
//#define DEBUG_INTERFACE 1
2013-10-07 12:38:08 +01:00
#if DEBUG_INTERFACE
2013-10-04 13:22:00 +01:00
static void
2013-10-07 12:20:00 +01:00
dump_mat(int32_t mat[], int32_t nrows, int32_t ncols)
2013-10-04 13:22:00 +01:00
{
2013-10-16 14:52:54 +01:00
return;
2013-10-07 12:20:00 +01:00
int32_t i, j;
2013-10-04 13:22:00 +01:00
for ( i=0; i< nrows; i++) {
printf("%d", mat[i*ncols]);
for (j=1; j < ncols; j++) {
printf(", %d", mat[i*ncols+j]);
}
printf("\n");
}
}
static void
2013-10-07 12:20:00 +01:00
dump_vec(int32_t vec[], int32_t rows)
2013-10-04 13:22:00 +01:00
{
2013-10-07 12:20:00 +01:00
int32_t i = 1;
int32_t j = 0;
2013-10-16 14:52:54 +01:00
2013-10-04 14:42:18 +01:00
for (j = 0; j < rows; j++) {
for ( ; vec[i]; i++ ) {
printf(", %d", vec[i]);
}
printf(", 0");
i++;
2013-10-04 13:22:00 +01:00
}
printf("\n");
}
2013-10-07 12:38:08 +01:00
#endif /* DEBUG_INTERFACE */
2013-10-04 13:22:00 +01:00
2013-10-04 14:42:18 +01:00
// stubs, will point at Carlos code.
2013-10-04 13:22:00 +01:00
void Cuda_Initialize( void )
{
}
2013-10-07 12:20:00 +01:00
int32_t Cuda_NewFacts(predicate *pe)
2013-10-04 13:22:00 +01:00
{
2013-10-07 12:38:08 +01:00
#if DEBUG_INTERFACE
2013-10-04 14:42:18 +01:00
dump_mat( pe->address_host_table, pe->num_rows, pe->num_columns );
2013-10-07 12:38:08 +01:00
#endif
2013-10-07 12:20:00 +01:00
facts[cf] = pe;
cf++;
2013-10-04 14:42:18 +01:00
return TRUE;
2013-10-04 13:22:00 +01:00
}
2013-10-07 12:20:00 +01:00
int32_t Cuda_NewRule(predicate *pe)
2013-10-04 13:22:00 +01:00
{
2013-10-07 12:38:08 +01:00
#if DEBUG_INTERFACE
2013-10-04 14:42:18 +01:00
dump_vec( pe->address_host_table, pe->num_rows);
2013-10-07 12:38:08 +01:00
#endif
2013-10-07 12:20:00 +01:00
rules[cr] = pe;
cr++;
2013-10-04 14:42:18 +01:00
return TRUE;
}
2013-10-07 12:20:00 +01:00
int32_t Cuda_Erase(predicate *pe)
2013-10-04 14:42:18 +01:00
{
2013-10-07 18:34:29 +01:00
int i = 0;
while ( rules[i] != pe )
i++;
while (i < cr-1) {
rules[i] = rules[i+1];
i++;
}
rules[i] = NULL;
cr--;
2013-10-04 14:42:18 +01:00
if (pe->address_host_table)
free( pe->address_host_table );
free( pe );
return TRUE;
2013-10-04 13:22:00 +01:00
}
static int
2013-10-04 14:42:18 +01:00
load_facts( void ) {
2013-10-04 13:22:00 +01:00
2013-10-07 12:20:00 +01:00
int32_t nrows = YAP_IntOfTerm(YAP_ARG1);
int32_t ncols = YAP_IntOfTerm(YAP_ARG2), i = 0;
2013-10-04 13:22:00 +01:00
YAP_Term t3 = YAP_ARG3;
2013-10-07 12:20:00 +01:00
int32_t *mat = (int32_t *)malloc(sizeof(int32_t)*nrows*ncols);
int32_t pname = YAP_AtomToInt(YAP_NameOfFunctor(YAP_FunctorOfTerm(YAP_HeadOfTerm(t3))));
2013-10-04 14:42:18 +01:00
predicate *pred;
2013-10-04 13:22:00 +01:00
while(YAP_IsPairTerm(t3)) {
2013-10-07 12:20:00 +01:00
int32_t j = 0;
2013-10-04 13:22:00 +01:00
YAP_Term th = YAP_HeadOfTerm(t3);
for (j = 0; j < ncols; j++) {
YAP_Term ta = YAP_ArgOfTerm(j+1, th);
if (YAP_IsAtomTerm(ta)) {
mat[i*ncols+j] = YAP_AtomToInt(YAP_AtomOfTerm(ta));
} else {
mat[i*ncols+j] = YAP_IntOfTerm(ta);
}
}
t3 = YAP_TailOfTerm( t3 );
i++;
}
2013-10-04 14:42:18 +01:00
if (YAP_IsVarTerm( YAP_ARG4)) {
// new
pred = (predicate *)malloc(sizeof(predicate));
} else {
pred = (predicate *)YAP_IntOfTerm(YAP_ARG4);
if (pred->address_host_table)
free( pred->address_host_table );
}
pred->name = pname;
pred->num_rows = nrows;
pred->num_columns = ncols;
pred->is_fact = TRUE;
pred->address_host_table = mat;
Cuda_NewFacts(pred);
if (YAP_IsVarTerm( YAP_ARG4)) {
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm((YAP_Int)pred));
} else {
return TRUE;
}
2013-10-04 13:22:00 +01:00
}
static int currentFact = 0;
static predicate *currentPred = NULL;
static int
cuda_init_facts( void ) {
int32_t nrows = YAP_IntOfTerm(YAP_ARG1);
int32_t ncols = YAP_IntOfTerm(YAP_ARG2), i = 0;
int32_t *mat = (int32_t *)malloc(sizeof(int32_t)*nrows*ncols);
int32_t pname = YAP_AtomToInt(YAP_AtomOfTerm(YAP_ARG3));
predicate *pred;
if (!mat)
return FALSE;
if (YAP_IsVarTerm( YAP_ARG4)) {
// new
pred = (predicate *)malloc(sizeof(predicate));
} else {
pred = (predicate *)YAP_IntOfTerm(YAP_ARG4);
if (pred->address_host_table)
free( pred->address_host_table );
}
pred->name = pname;
pred->num_rows = nrows;
pred->num_columns = ncols;
pred->is_fact = TRUE;
pred->address_host_table = mat;
currentPred = pred;
currentFact = 0;
if (YAP_IsVarTerm( YAP_ARG4)) {
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm((YAP_Int)pred));
} else {
return TRUE;
}
}
static int
cuda_load_fact( void ) {
YAP_Term th = YAP_ARG1;
int i, j;
int ncols = currentPred->num_columns;
int *mat = currentPred->address_host_table;
i = currentFact;
for (j = 0; j < ncols; j++) {
YAP_Term ta = YAP_ArgOfTerm(j+1, th);
if (YAP_IsAtomTerm(ta)) {
mat[i*ncols+j] = YAP_AtomToInt(YAP_AtomOfTerm(ta));
} else {
mat[i*ncols+j] = YAP_IntOfTerm(ta);
}
}
i++;
if (i == currentPred->num_rows) {
Cuda_NewFacts(currentPred);
currentPred = NULL;
currentFact = 0;
} else {
currentFact = i;
}
2013-11-04 21:41:26 +00:00
return TRUE;
}
2013-10-04 13:22:00 +01:00
static int
2013-10-04 14:42:18 +01:00
load_rule( void ) {
2013-10-04 13:22:00 +01:00
// maximum of 2K symbols per rule, should be enough for ILP
2013-10-07 12:20:00 +01:00
int32_t vec[2048], *ptr = vec, *nvec;
2013-10-04 13:22:00 +01:00
// qK different variables;
YAP_Term vars[1024];
2013-10-07 12:20:00 +01:00
int32_t nvars = 0;
int32_t ngoals = YAP_IntOfTerm(YAP_ARG1); /* gives the number of goals */
int32_t ncols = YAP_IntOfTerm(YAP_ARG2);
2013-10-04 13:22:00 +01:00
YAP_Term t3 = YAP_ARG3;
2013-10-07 12:20:00 +01:00
int32_t pname = YAP_AtomToInt(YAP_NameOfFunctor(YAP_FunctorOfTerm(YAP_HeadOfTerm(t3))));
2013-10-04 14:42:18 +01:00
predicate *pred;
2013-10-04 13:22:00 +01:00
while(YAP_IsPairTerm(t3)) {
2013-10-07 12:20:00 +01:00
int32_t j = 0;
2013-10-04 13:22:00 +01:00
YAP_Term th = YAP_HeadOfTerm(t3);
YAP_Functor f = YAP_FunctorOfTerm( th );
2013-10-07 12:20:00 +01:00
int32_t n = YAP_ArityOfFunctor( f );
2013-10-07 13:48:19 +01:00
YAP_Atom at = YAP_NameOfFunctor( f );
if (at == AtomEq)
*ptr++ = SBG_EQ;
else if (at == AtomGt)
*ptr++ = SBG_GT;
else if (at == AtomLt)
*ptr++ = SBG_LT;
else if (at == AtomGe)
*ptr++ = SBG_GE;
else if (at == AtomLe)
*ptr++ = SBG_LE;
else if (at == AtomDf)
*ptr++ = SBG_DF;
else
*ptr++ = YAP_AtomToInt( at );
2013-10-04 13:22:00 +01:00
for (j = 0; j < n; j++) {
YAP_Term ta = YAP_ArgOfTerm(j+1, th);
if (YAP_IsVarTerm(ta)) {
2013-10-07 12:20:00 +01:00
int32_t k;
2013-10-04 13:22:00 +01:00
for (k = 0; k< nvars; k++) {
if (vars[k] == ta) {
*ptr++ = k+1;
break;
}
}
if (k == nvars) {
vars[k] = ta;
*ptr++ = k+1;
nvars++;
}
} else if (YAP_IsAtomTerm(ta)) {
*ptr++ = -YAP_AtomToInt(YAP_AtomOfTerm(ta));
} else {
*ptr++ = -YAP_IntOfTerm(ta);
}
}
*ptr++ = 0;
t3 = YAP_TailOfTerm( t3 );
}
2013-10-04 14:42:18 +01:00
if (YAP_IsVarTerm( YAP_ARG4)) {
// new
pred = (predicate *)malloc(sizeof(predicate));
} else {
pred = (predicate *)YAP_IntOfTerm(YAP_ARG4);
if (pred->address_host_table)
free( pred->address_host_table );
}
pred->name = pname;
pred->num_rows = ngoals;
pred->num_columns = ncols;
pred->is_fact = FALSE;
2013-10-07 12:20:00 +01:00
nvec = (int32_t *)malloc(sizeof(int32_t)*(ptr-vec));
memcpy(nvec, vec, sizeof(int32_t)*(ptr-vec));
2013-10-04 14:42:18 +01:00
pred->address_host_table = nvec;
Cuda_NewRule( pred );
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm((YAP_Int)pred));
}
static int
cuda_erase( void )
{
predicate *ptr = (predicate *)YAP_IntOfTerm(YAP_ARG1);
return Cuda_Erase( ptr );
}
static int
cuda_eval( void )
{
2013-10-07 12:20:00 +01:00
int32_t *mat;
2013-10-04 14:42:18 +01:00
predicate *ptr = (predicate *)YAP_IntOfTerm(YAP_ARG1);
2013-10-07 12:20:00 +01:00
int32_t n = Cuda_Eval(facts, cf, rules, cr, ptr, & mat);
int32_t ncols = ptr->num_columns;
2013-10-04 14:42:18 +01:00
YAP_Term out = YAP_TermNil();
YAP_Functor f = YAP_MkFunctor(YAP_IntToAtom(ptr->name), ncols);
YAP_Term vec[256];
2013-10-07 12:20:00 +01:00
int32_t i;
2013-10-04 14:42:18 +01:00
if (n < 0)
return FALSE;
for (i=0; i<n; i++) {
2013-10-07 12:20:00 +01:00
int32_t ni = ((n-1)-i)*ncols, j;
2013-10-04 14:42:18 +01:00
for (j=0; j<ncols; j++) {
2013-10-07 12:38:08 +01:00
vec[j] = YAP_MkIntTerm(mat[ni+j]);
2013-10-04 14:42:18 +01:00
}
out = YAP_MkPairTerm(YAP_MkApplTerm( f, ncols, vec ), out);
}
if (n > 0)
free( mat );
2013-10-04 14:42:18 +01:00
return YAP_Unify(YAP_ARG2, out);
}
2013-10-09 11:23:45 +01:00
static int
cuda_coverage( void )
{
int32_t *mat;
predicate *ptr = (predicate *)YAP_IntOfTerm(YAP_ARG1);
int32_t n = Cuda_Eval(facts, cf, rules, cr, ptr, & mat);
int32_t ncols = ptr->num_columns;
int32_t post = YAP_AtomToInt(YAP_AtomOfTerm(YAP_ARG2));
int32_t i = n/2, min = 0, max = n-1;
int32_t t0, t1;
2013-10-09 11:23:45 +01:00
if (n < 0)
return FALSE;
if (n == 0) {
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(0)) &&
YAP_Unify(YAP_ARG3, YAP_MkIntTerm(0));
}
t0 = mat[0], t1 = mat[(n-1)*2];
2013-10-09 11:23:45 +01:00
if (t0 == t1) { /* all sametype */
free( mat );
/* all pos */
if (t0 == post)
return YAP_Unify(YAP_ARG3, YAP_MkIntTerm(n)) &&
YAP_Unify(YAP_ARG4, YAP_MkIntTerm(0));
/* all neg */
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(n)) &&
YAP_Unify(YAP_ARG3, YAP_MkIntTerm(0));
}
do {
i = (min+max)/2;
if (i == min) i++;
if (mat[i*2] == t0) {
min = i;
} else {
max = i;
}
if (min+1 == max) {
free( mat );
if (t0 == post)
return YAP_Unify(YAP_ARG3, YAP_MkIntTerm(max)) &&
YAP_Unify(YAP_ARG4, YAP_MkIntTerm(n-max));
/* all neg */
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm(max)) &&
YAP_Unify(YAP_ARG3, YAP_MkIntTerm(n-max));
}
} while ( TRUE );
}
2013-10-04 14:42:18 +01:00
static int cuda_count( void )
{
2013-10-07 12:20:00 +01:00
int32_t *mat;
2013-10-04 14:42:18 +01:00
predicate *ptr = (predicate *)YAP_IntOfTerm(YAP_ARG1);
2013-10-07 12:20:00 +01:00
int32_t n = Cuda_Eval(facts, cf, rules, cr, ptr, & mat);
2013-10-04 14:42:18 +01:00
if (n < 0)
return FALSE;
free( mat );
2013-10-04 14:42:18 +01:00
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(n));
2013-10-04 13:22:00 +01:00
}
2013-10-16 16:19:03 +01:00
static int cuda_statistics( void )
{
Cuda_Statistics();
return TRUE;
}
2013-10-04 13:22:00 +01:00
static int first_time = TRUE;
void
init_cuda(void)
{
if (first_time) Cuda_Initialize();
first_time = FALSE;
2013-10-07 13:48:19 +01:00
AtomEq = YAP_LookupAtom("=");
AtomGt = YAP_LookupAtom(">");
AtomLt = YAP_LookupAtom("<");
AtomGe = YAP_LookupAtom(">=");
AtomLe = YAP_LookupAtom("=<");
AtomDf = YAP_LookupAtom("\\=");
2013-10-04 14:42:18 +01:00
YAP_UserCPredicate("load_facts", load_facts, 4);
YAP_UserCPredicate("cuda_init_facts", cuda_init_facts, 4);
YAP_UserCPredicate("cuda_load_fact", cuda_load_fact, 1);
2013-10-04 14:42:18 +01:00
YAP_UserCPredicate("load_rule", load_rule, 4);
YAP_UserCPredicate("cuda_erase", cuda_erase, 1);
YAP_UserCPredicate("cuda_eval", cuda_eval, 2);
2013-10-09 11:23:45 +01:00
YAP_UserCPredicate("cuda_coverage", cuda_coverage, 4);
2013-10-04 14:42:18 +01:00
YAP_UserCPredicate("cuda_count", cuda_count, 2);
2013-10-16 16:19:03 +01:00
YAP_UserCPredicate("cuda_statistics", cuda_statistics, 0);
2013-10-04 13:22:00 +01:00
}