hiatory
This commit is contained in:
230
packages/cuda/cuda.c
Normal file → Executable file
230
packages/cuda/cuda.c
Normal file → Executable file
@@ -6,19 +6,25 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include "pred.h"
|
||||
|
||||
#define MAXARG 100
|
||||
|
||||
YAP_Atom AtomEq,
|
||||
AtomGt,
|
||||
AtomLt,
|
||||
AtomGe,
|
||||
AtomLe,
|
||||
AtomDf;
|
||||
AtomDf,
|
||||
AtomNt;
|
||||
|
||||
predicate *facts[100]; /*Temporary solution to maintain facts and rules*/
|
||||
predicate *rules[100];
|
||||
predicate *facts[MAXARG]; /*Temporary solution to maintain facts and rules*/
|
||||
predicate *rules[MAXARG];
|
||||
int32_t cf = 0, cr = 0;
|
||||
|
||||
char names[1024];
|
||||
|
||||
// initialize CUDA system
|
||||
void Cuda_Initialize( void );
|
||||
|
||||
@@ -39,6 +45,19 @@ void init_cuda( void );
|
||||
|
||||
//#define DEBUG_INTERFACE 1
|
||||
|
||||
#ifdef ROCKIT
|
||||
static int32_t query[100];
|
||||
static int32_t qcont = 0;
|
||||
static int cuda_init_query(void)
|
||||
{
|
||||
int32_t pname = YAP_AtomToInt(YAP_AtomOfTerm(YAP_ARG1));
|
||||
query[qcont] = pname;
|
||||
qcont++;
|
||||
query[qcont] = 0;
|
||||
return TRUE;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DEBUG_INTERFACE
|
||||
static void
|
||||
dump_mat(int32_t mat[], int32_t nrows, int32_t ncols)
|
||||
@@ -83,8 +102,18 @@ int32_t Cuda_NewFacts(predicate *pe)
|
||||
#if DEBUG_INTERFACE
|
||||
dump_mat( pe->address_host_table, pe->num_rows, pe->num_columns );
|
||||
#endif
|
||||
|
||||
#ifdef ROCKIT
|
||||
if(cf >= 0)
|
||||
{
|
||||
facts[cf] = pe;
|
||||
cf++;
|
||||
}
|
||||
#else
|
||||
facts[cf] = pe;
|
||||
cf++;
|
||||
#endif
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -115,7 +144,7 @@ int32_t Cuda_Erase(predicate *pe)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static YAP_Bool
|
||||
static int
|
||||
load_facts( void ) {
|
||||
|
||||
int32_t nrows = YAP_IntOfTerm(YAP_ARG1);
|
||||
@@ -164,15 +193,18 @@ load_facts( void ) {
|
||||
static int currentFact = 0;
|
||||
static predicate *currentPred = NULL;
|
||||
|
||||
static YAP_Bool
|
||||
static int
|
||||
cuda_init_facts( void ) {
|
||||
|
||||
int32_t nrows = YAP_IntOfTerm(YAP_ARG1);
|
||||
int32_t ncols = YAP_IntOfTerm(YAP_ARG2), i = 0;
|
||||
int32_t ncols = YAP_IntOfTerm(YAP_ARG2);
|
||||
int32_t *mat = (int32_t *)malloc(sizeof(int32_t)*nrows*ncols);
|
||||
int32_t pname = YAP_AtomToInt(YAP_AtomOfTerm(YAP_ARG3));
|
||||
predicate *pred;
|
||||
|
||||
strcat(names, YAP_AtomName(YAP_AtomOfTerm(YAP_ARG3)));
|
||||
strcat(names, " ");
|
||||
|
||||
if (!mat)
|
||||
return FALSE;
|
||||
if (YAP_IsVarTerm( YAP_ARG4)) {
|
||||
@@ -198,14 +230,16 @@ cuda_init_facts( void ) {
|
||||
}
|
||||
}
|
||||
|
||||
static YAP_Bool
|
||||
static int
|
||||
cuda_load_fact( void ) {
|
||||
YAP_Term th = YAP_ARG1;
|
||||
|
||||
int i, j;
|
||||
int i = currentFact;
|
||||
|
||||
#if defined(DATALOG) || defined(TUFFY)
|
||||
YAP_Term th = YAP_ARG1;
|
||||
int ncols = currentPred->num_columns;
|
||||
int j;
|
||||
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)) {
|
||||
@@ -214,6 +248,8 @@ cuda_load_fact( void ) {
|
||||
mat[i*ncols+j] = YAP_IntOfTerm(ta);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
i++;
|
||||
if (i == currentPred->num_rows) {
|
||||
Cuda_NewFacts(currentPred);
|
||||
@@ -225,21 +261,26 @@ cuda_load_fact( void ) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static YAP_Bool
|
||||
static int
|
||||
load_rule( void ) {
|
||||
// maximum of 2K symbols per rule, should be enough for ILP
|
||||
int32_t vec[2048], *ptr = vec, *nvec;
|
||||
int32_t vec[2048], *ptr = vec, *nvec, neg[2048];
|
||||
// qK different variables;
|
||||
YAP_Term vars[1024];
|
||||
int32_t nvars = 0;
|
||||
int32_t nvars = 0, x;
|
||||
int32_t ngoals = YAP_IntOfTerm(YAP_ARG1); /* gives the number of goals */
|
||||
int32_t ncols = YAP_IntOfTerm(YAP_ARG2);
|
||||
YAP_Term t3 = YAP_ARG3;
|
||||
int32_t pname = YAP_AtomToInt(YAP_NameOfFunctor(YAP_FunctorOfTerm(YAP_HeadOfTerm(t3))));
|
||||
YAP_Atom name = YAP_NameOfFunctor(YAP_FunctorOfTerm(YAP_HeadOfTerm(t3)));
|
||||
int32_t pname = YAP_AtomToInt(name);
|
||||
|
||||
const char *strname = YAP_AtomName(name);
|
||||
predicate *pred;
|
||||
int32_t cont = 0;
|
||||
memset(neg, 0x0, 2048 * sizeof(int32_t));
|
||||
|
||||
while(YAP_IsPairTerm(t3)) {
|
||||
int32_t j = 0;
|
||||
int32_t j = 0, m;
|
||||
YAP_Term th = YAP_HeadOfTerm(t3);
|
||||
YAP_Functor f = YAP_FunctorOfTerm( th );
|
||||
int32_t n = YAP_ArityOfFunctor( f );
|
||||
@@ -257,8 +298,17 @@ load_rule( void ) {
|
||||
*ptr++ = SBG_LE;
|
||||
else if (at == AtomDf)
|
||||
*ptr++ = SBG_DF;
|
||||
else if (at == AtomNt)
|
||||
{
|
||||
neg[cont] = 1;
|
||||
cont++;
|
||||
}
|
||||
else
|
||||
*ptr++ = YAP_AtomToInt( at );
|
||||
{
|
||||
*ptr++ = YAP_AtomToInt( at );
|
||||
cont++;
|
||||
}
|
||||
|
||||
for (j = 0; j < n; j++) {
|
||||
YAP_Term ta = YAP_ArgOfTerm(j+1, th);
|
||||
|
||||
@@ -277,6 +327,34 @@ load_rule( void ) {
|
||||
}
|
||||
} else if (YAP_IsAtomTerm(ta)) {
|
||||
*ptr++ = -YAP_AtomToInt(YAP_AtomOfTerm(ta));
|
||||
} else if (YAP_IsApplTerm(ta)) {
|
||||
f = YAP_FunctorOfTerm( ta );
|
||||
at = YAP_NameOfFunctor( f );
|
||||
m = YAP_ArityOfFunctor( f );
|
||||
*ptr++ = YAP_AtomToInt( at );
|
||||
|
||||
for (x = 0; x < m; x++) {
|
||||
YAP_Term ta2 = YAP_ArgOfTerm(x+1, ta);
|
||||
|
||||
if (YAP_IsVarTerm(ta2)) {
|
||||
int32_t k;
|
||||
for (k = 0; k < nvars; k++) {
|
||||
if (vars[k] == ta2) {
|
||||
*ptr++ = k+1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (k == nvars) {
|
||||
vars[k] = ta2;
|
||||
*ptr++ = k+1;
|
||||
nvars++;
|
||||
}
|
||||
} else if (YAP_IsAtomTerm(ta2)) {
|
||||
*ptr++ = -YAP_AtomToInt(YAP_AtomOfTerm(ta));
|
||||
} else {
|
||||
*ptr++ = -YAP_IntOfTerm(ta);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
*ptr++ = -YAP_IntOfTerm(ta);
|
||||
}
|
||||
@@ -296,53 +374,136 @@ load_rule( void ) {
|
||||
pred->num_rows = ngoals;
|
||||
pred->num_columns = ncols;
|
||||
pred->is_fact = FALSE;
|
||||
x = (strlen(strname) + 1) * sizeof(char);
|
||||
pred->predname = (char *)malloc(x);
|
||||
memcpy(pred->predname, strname, x);
|
||||
nvec = (int32_t *)malloc(sizeof(int32_t)*(ptr-vec));
|
||||
memcpy(nvec, vec, sizeof(int32_t)*(ptr-vec));
|
||||
pred->address_host_table = nvec;
|
||||
pred->negatives = (int32_t *)malloc(sizeof(int32_t) * cont);
|
||||
memcpy(pred->negatives, neg, sizeof(int32_t) * cont);
|
||||
Cuda_NewRule( pred );
|
||||
return YAP_Unify(YAP_ARG4, YAP_MkIntTerm((YAP_Int)pred));
|
||||
}
|
||||
|
||||
static YAP_Bool
|
||||
static int
|
||||
cuda_erase( void )
|
||||
{
|
||||
predicate *ptr = (predicate *)YAP_IntOfTerm(YAP_ARG1);
|
||||
return Cuda_Erase( ptr );
|
||||
}
|
||||
|
||||
static YAP_Bool
|
||||
void setQuery(YAP_Term t1, int32_t **res)
|
||||
{
|
||||
int32_t *query = (int32_t *)malloc(MAXARG * sizeof(int32_t));
|
||||
int32_t x, y = 0, *itr;
|
||||
predicate *ptr = NULL;
|
||||
if(YAP_IsPairTerm(t1))
|
||||
{
|
||||
while(YAP_IsPairTerm(t1))
|
||||
{
|
||||
ptr = (predicate *)YAP_IntOfTerm(YAP_HeadOfTerm(t1));
|
||||
query[y] = ptr->name;
|
||||
itr = ptr->address_host_table;
|
||||
x = 2;
|
||||
while(itr[x] != 0)
|
||||
x++;
|
||||
query[y+1] = itr[x+1];
|
||||
t1 = YAP_TailOfTerm(t1);
|
||||
y+=2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = (predicate *)YAP_IntOfTerm(t1);
|
||||
query[y] = ptr->name;
|
||||
itr = ptr->address_host_table;
|
||||
x = 2;
|
||||
while(itr[x] != 0)
|
||||
x++;
|
||||
query[y+1] = itr[x+1];
|
||||
y += 2;
|
||||
}
|
||||
query[y] = -1;
|
||||
query[y+1] = -1;
|
||||
*res = query;
|
||||
}
|
||||
|
||||
static int
|
||||
cuda_eval( void )
|
||||
{
|
||||
int32_t *mat;
|
||||
|
||||
#if defined(DATALOG) || defined(TUFFY)
|
||||
int32_t *query = NULL;
|
||||
setQuery(YAP_ARG1, &query);
|
||||
#endif
|
||||
|
||||
int32_t finalDR = YAP_IntOfTerm(YAP_ARG3);
|
||||
int32_t n = Cuda_Eval(facts, cf, rules, cr, query, & mat, names, finalDR);
|
||||
|
||||
#ifdef TUFFY
|
||||
cf = 0;
|
||||
#endif
|
||||
#ifdef ROCKIT
|
||||
if(cf > 0)
|
||||
cf *= -1;
|
||||
#endif
|
||||
#if defined(TUFFY) || defined(ROCKIT)
|
||||
cr = 0;
|
||||
names[0] = '\0';
|
||||
return FALSE;
|
||||
#else
|
||||
int32_t i;
|
||||
predicate *ptr = (predicate *)YAP_IntOfTerm(YAP_ARG1);
|
||||
int32_t n = Cuda_Eval(facts, cf, rules, cr, ptr, & mat);
|
||||
int32_t ncols = ptr->num_columns;
|
||||
YAP_Term out = YAP_TermNil();
|
||||
YAP_Functor f = YAP_MkFunctor(YAP_IntToAtom(ptr->name), ncols);
|
||||
YAP_Term vec[256];
|
||||
int32_t i;
|
||||
|
||||
YAP_Atom at;
|
||||
|
||||
if (n < 0)
|
||||
return FALSE;
|
||||
for (i=0; i<n; i++) {
|
||||
int32_t ni = ((n-1)-i)*ncols, j;
|
||||
|
||||
printf("%s(", YAP_AtomName(YAP_IntToAtom(ptr->name)));
|
||||
|
||||
for (j=0; j<ncols; j++) {
|
||||
vec[j] = YAP_MkIntTerm(mat[ni+j]);
|
||||
|
||||
at = YAP_IntToAtom(mat[ni+j]);
|
||||
if(at != NULL)
|
||||
printf("%s", YAP_AtomName(at));
|
||||
else
|
||||
printf("%d", mat[ni+j]);
|
||||
if(j < (ncols - 1))
|
||||
printf(",");
|
||||
|
||||
}
|
||||
out = YAP_MkPairTerm(YAP_MkApplTerm( f, ncols, vec ), out);
|
||||
|
||||
printf(")\n");
|
||||
|
||||
}
|
||||
if (n > 0)
|
||||
free( mat );
|
||||
return YAP_Unify(YAP_ARG2, out);
|
||||
#endif
|
||||
}
|
||||
|
||||
static YAP_Bool
|
||||
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;
|
||||
|
||||
#if defined(DATALOG) || defined(TUFFY)
|
||||
int32_t *query = NULL;
|
||||
setQuery(YAP_ARG1, &query);
|
||||
#endif
|
||||
|
||||
int32_t n = Cuda_Eval(facts, cf, rules, cr, query, & mat, 0, 0);
|
||||
int32_t post = YAP_AtomToInt(YAP_AtomOfTerm(YAP_ARG2));
|
||||
int32_t i = n/2, min = 0, max = n-1;
|
||||
int32_t t0, t1;
|
||||
@@ -384,11 +545,16 @@ cuda_coverage( void )
|
||||
} while ( TRUE );
|
||||
}
|
||||
|
||||
static YAP_Bool cuda_count( void )
|
||||
static int cuda_count( void )
|
||||
{
|
||||
int32_t *mat;
|
||||
predicate *ptr = (predicate *)YAP_IntOfTerm(YAP_ARG1);
|
||||
int32_t n = Cuda_Eval(facts, cf, rules, cr, ptr, & mat);
|
||||
|
||||
#if defined(DATALOG) || defined(TUFFY)
|
||||
int32_t *query = NULL;
|
||||
setQuery(YAP_ARG1, &query);
|
||||
#endif
|
||||
|
||||
int32_t n = Cuda_Eval(facts, cf, rules, cr, query, & mat, 0, 0);
|
||||
|
||||
if (n < 0)
|
||||
return FALSE;
|
||||
@@ -396,7 +562,7 @@ static YAP_Bool cuda_count( void )
|
||||
return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(n));
|
||||
}
|
||||
|
||||
static YAP_Bool cuda_statistics( void )
|
||||
static int cuda_statistics( void )
|
||||
{
|
||||
Cuda_Statistics();
|
||||
return TRUE;
|
||||
@@ -416,14 +582,20 @@ init_cuda(void)
|
||||
AtomGe = YAP_LookupAtom(">=");
|
||||
AtomLe = YAP_LookupAtom("=<");
|
||||
AtomDf = YAP_LookupAtom("\\=");
|
||||
AtomNt = YAP_LookupAtom("not");
|
||||
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);
|
||||
YAP_UserCPredicate("load_rule", load_rule, 4);
|
||||
YAP_UserCPredicate("cuda_erase", cuda_erase, 1);
|
||||
YAP_UserCPredicate("cuda_eval", cuda_eval, 2);
|
||||
YAP_UserCPredicate("cuda_eval", cuda_eval, 3);
|
||||
YAP_UserCPredicate("cuda_coverage", cuda_coverage, 4);
|
||||
YAP_UserCPredicate("cuda_count", cuda_count, 2);
|
||||
YAP_UserCPredicate("cuda_statistics", cuda_statistics, 0);
|
||||
|
||||
#ifdef ROCKIT
|
||||
YAP_UserCPredicate("cuda_init_query", cuda_init_query, 1);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user