From 0e1fd0a6434ea0016f90ba7500411504d3ba4792 Mon Sep 17 00:00:00 2001 From: adash Date: Wed, 24 Jun 2009 21:13:58 +0000 Subject: [PATCH] bug free Bayes version that works well for N threads and gives correct output Still does not compile with joptimize turned on --- .../src/Benchmarks/SingleTM/Bayes/Bayes.java | 14 +- .../src/Benchmarks/SingleTM/Bayes/Data.java | 2 +- .../Benchmarks/SingleTM/Bayes/Learner.java | 1005 ++++++++--------- .../SingleTM/Bayes/LearnerTask.java | 1 - Robust/src/Benchmarks/SingleTM/Bayes/Net.java | 286 +---- .../Benchmarks/SingleTM/Bayes/Operation.java | 72 -- Robust/src/Benchmarks/SingleTM/Bayes/makefile | 3 +- 7 files changed, 521 insertions(+), 862 deletions(-) delete mode 100644 Robust/src/Benchmarks/SingleTM/Bayes/Operation.java diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Bayes.java b/Robust/src/Benchmarks/SingleTM/Bayes/Bayes.java index 5ded7c53..89663d3f 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/Bayes.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/Bayes.java @@ -208,7 +208,7 @@ public class Bayes extends Thread { Data dataPtr = Data.data_alloc(1, 1, null); - Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, 1); + Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, 1, global_insertPenalty, global_maxNumEdgeLearned, global_operationQualityFactor); Net tmpNetPtr = learnerPtr.netPtr; learnerPtr.netPtr = netPtr; @@ -233,7 +233,6 @@ public class Bayes extends Thread { Barrier.enterBarrier(); Learner.learnStructure(myId, numThread, learnerPtr); - //Learner.learner_run(myId, numThread, learnerPtr); Barrier.enterBarrier(); } @@ -263,6 +262,7 @@ public class Bayes extends Thread { Bayes[] binit = new Bayes[numThread]; + System.out.println("Number of threads " + numThread); System.out.println("Random seed " + randomSeed); System.out.println("Number of vars " + numVar); System.out.println("Number of records " + numRecord); @@ -297,7 +297,7 @@ public class Bayes extends Thread { adtreePtr.adtree_make(dataPtr); - System.out.println("done.\n\n"); + System.out.println("done."); /* * Score original network @@ -310,7 +310,7 @@ public class Bayes extends Thread { * Learn structure of Bayesian network */ - Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, numThread); + Learner learnerPtr = Learner.learner_alloc(dataPtr, adtreePtr, numThread, b.global_insertPenalty, b.global_maxNumEdgeLearned, b.global_operationQualityFactor); dataPtr.data_free(); /* save memory */ @@ -338,12 +338,6 @@ public class Bayes extends Thread { Learner.learnStructure(0, numThread, learnerPtr); Barrier.enterBarrier(); - /* - Barrier.enterBarrier(); - Learner.learner_run(0, numThread, learnerPtr); - Barrier.enterBarrier(); - */ - System.out.println("done."); /* diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Data.java b/Robust/src/Benchmarks/SingleTM/Bayes/Data.java index fab45955..f05d7fdc 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/Data.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/Data.java @@ -122,7 +122,7 @@ public class Data { * value instances */ - int[][] thresholdsTable = new int[numVar][]; //TODO check if this is alright + int[][] thresholdsTable = new int[numVar][]; int v; for (v = 0; v < numVar; v++) { IntList parentIdListPtr = netPtr.net_getParentIdListPtr(v); diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Learner.java b/Robust/src/Benchmarks/SingleTM/Bayes/Learner.java index f2961099..428ce3ab 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/Learner.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/Learner.java @@ -106,6 +106,7 @@ #define OPERATION_INSERT 0 #define OPERATION_REMOVE 1 #define OPERATION_REVERSE 2 +#define NUM_OPERATION 3 public class Learner { Adtree adtreePtr; @@ -127,31 +128,17 @@ public class Learner { #endif } - - /* ============================================================================= - * compareQuery - * -- Want smallest ID first - * -- For vector_sort - * ============================================================================= - */ - /* - static int - compareQuery (const void* aPtr, const void* bPtr) - { - query_t* aQueryPtr = (query_t*)(*(void**)aPtr); - query_t* bQueryPtr = (query_t*)(*(void**)bPtr); - - return (aQueryPtr.index - bQueryPtr.index); - } - */ - - /* ============================================================================= * learner_alloc * ============================================================================= */ public static Learner - learner_alloc (Data dataPtr, Adtree adtreePtr, int numThread) + learner_alloc (Data dataPtr, + Adtree adtreePtr, + int numThread, + int global_insertPenalty, + int global_maxNumEdgeLearned, + float global_operationQualityFactor) { Learner learnerPtr = new Learner(); @@ -163,6 +150,11 @@ public class Learner { learnerPtr.tasks = new LearnerTask[dataPtr.numVar]; learnerPtr.taskListPtr = List.list_alloc(); learnerPtr.numTotalParent = 0; +#ifndef TEST_LEARNER + learnerPtr.global_insertPenalty = global_insertPenalty; + learnerPtr.global_maxNumEdgeLearned = global_maxNumEdgeLearned; + learnerPtr.global_operationQualityFactor = global_operationQualityFactor; +#endif } return learnerPtr; @@ -428,26 +420,27 @@ public class Learner { /* ============================================================================= * TMpopTask - * -- Returns NULL is list is empty + * -- Returns null is list is empty * ============================================================================= */ -/* - learner_task_t* - TMpopTask (TM_ARGDECL list_t* taskListPtr) + public LearnerTask TMpopTask (List taskListPtr) { - learner_task_t* taskPtr = NULL; - - list_iter_t it; - TMLIST_ITER_RESET(&it, taskListPtr); - if (TMLIST_ITER_HASNEXT(&it, taskListPtr)) { - taskPtr = (learner_task_t*)TMLIST_ITER_NEXT(&it, taskListPtr); - boolean status = TMLIST_REMOVE(taskListPtr, (void*)taskPtr); - assert(status); + LearnerTask taskPtr = null; + + ListNode it = taskListPtr.head; + taskListPtr.list_iter_reset(it); + if (taskListPtr.list_iter_hasNext(it)) { + it = it.nextPtr; + taskPtr = taskListPtr.list_iter_next(it); + boolean status = taskListPtr.list_remove(taskPtr); + if(status == false) { + System.out.println("Assert failed: when removing from a list in TMpopTask()"); + System.exit(0); + } } return taskPtr; } - */ /* ============================================================================= @@ -483,27 +476,28 @@ public class Learner { * -- Modifies contents of parentQueryVectorPtr * ============================================================================= */ - /* - static void - TMpopulateParentQueryVector (TM_ARGDECL - net_t* netPtr, + public void + TMpopulateParentQueryVector (Net netPtr, int id, - query_t* queries, - vector_t* parentQueryVectorPtr) + Query[] queries, + Vector_t parentQueryVectorPtr) { - vector_clear(parentQueryVectorPtr); - - list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, id); - list_iter_t it; - TMLIST_ITER_RESET(&it, parentIdListPtr); - while (TMLIST_ITER_HASNEXT(&it, parentIdListPtr)) { - int parentId = (int)TMLIST_ITER_NEXT(&it, parentIdListPtr); - boolean status = PVECTOR_PUSHBACK(parentQueryVectorPtr, - (void*)&queries[parentId]); - assert(status); + parentQueryVectorPtr.vector_clear(); + + IntList parentIdListPtr = netPtr.net_getParentIdListPtr(id); + IntListNode it = parentIdListPtr.head; + parentIdListPtr.list_iter_reset(it); + + while (parentIdListPtr.list_iter_hasNext(it)) { + it = it.nextPtr; + int parentId = parentIdListPtr.list_iter_next(it); + boolean status = parentQueryVectorPtr.vector_pushBack(queries[parentId]); + if(status == false) { + System.out.println("Assert failed: unable to pushBack in queue in TMpopulateParentQueryVector()"); + System.exit(0); + } } } - */ /* ============================================================================= @@ -523,13 +517,13 @@ public class Learner { boolean status; status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr); if(status == false ) { - System.out.println("Assert failed: whille vector copy in populateQueryVectors()"); + System.out.println("Assert failed: while vector copy in populateQueryVectors()"); System.exit(0); } status = queryVectorPtr.vector_pushBack(queries[id]); if(status == false ) { - System.out.println("Assert failed: whille vector pushBack in populateQueryVectors()"); + System.out.println("Assert failed: while vector pushBack in populateQueryVectors()"); System.exit(0); } @@ -542,25 +536,29 @@ public class Learner { * -- Modifies contents of queryVectorPtr and parentQueryVectorPtr * ============================================================================= */ - /* - static void - TMpopulateQueryVectors (TM_ARGDECL - net_t* netPtr, + public void + TMpopulateQueryVectors (Net netPtr, int id, - query_t* queries, - vector_t* queryVectorPtr, - vector_t* parentQueryVectorPtr) + Query[] queries, + Vector_t queryVectorPtr, + Vector_t parentQueryVectorPtr) { - TMpopulateParentQueryVector(TM_ARG netPtr, id, queries, parentQueryVectorPtr); + TMpopulateParentQueryVector(netPtr, id, queries, parentQueryVectorPtr); boolean status; - status = PVECTOR_COPY(queryVectorPtr, parentQueryVectorPtr); - assert(status); - status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[id]); - assert(status); - PVECTOR_SORT(queryVectorPtr, &compareQuery); + status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr); + if(status == false ) { + System.out.println("Assert failed: while vector copy in TMpopulateQueryVectors()"); + System.exit(0); + } + status = queryVectorPtr.vector_pushBack(queries[id]); + if(status == false ) { + System.out.println("Assert failed: while vector pushBack in TMpopulateQueryVectors()"); + System.exit(0); + } + + queryVectorPtr.vector_sort(); } -*/ /* ============================================================================= * computeLocalLogLikelihoodHelper @@ -650,92 +648,118 @@ public class Learner { * TMfindBestInsertTask * ============================================================================= */ - /* - static learner_task_t - TMfindBestInsertTask (TM_ARGDECL findBestTaskArg_t* argPtr) + public LearnerTask + TMfindBestInsertTask (FindBestTaskArg argPtr) { - int toId = argPtr.toId; - learner_t* learnerPtr = argPtr.learnerPtr; - query_t* queries = argPtr.queries; - vector_t* queryVectorPtr = argPtr.queryVectorPtr; - vector_t* parentQueryVectorPtr = argPtr.parentQueryVectorPtr; - int numTotalParent = argPtr.numTotalParent; - float basePenalty = argPtr.basePenalty; - float baseLogLikelihood = argPtr.baseLogLikelihood; - bitmap_t* invalidBitmapPtr = argPtr.bitmapPtr; - queue_t* workQueuePtr = argPtr.workQueuePtr; - vector_t* baseParentQueryVectorPtr = argPtr.aQueryVectorPtr; - vector_t* baseQueryVectorPtr = argPtr.bQueryVectorPtr; + int toId = argPtr.toId; + Learner learnerPtr = argPtr.learnerPtr; + Query[] queries = argPtr.queries; + Vector_t queryVectorPtr = argPtr.queryVectorPtr; + Vector_t parentQueryVectorPtr = argPtr.parentQueryVectorPtr; + int numTotalParent = argPtr.numTotalParent; + float basePenalty = argPtr.basePenalty; + float baseLogLikelihood = argPtr.baseLogLikelihood; + BitMap invalidBitmapPtr = argPtr.bitmapPtr; + Queue workQueuePtr = argPtr.workQueuePtr; + Vector_t baseParentQueryVectorPtr = argPtr.aQueryVectorPtr; + Vector_t baseQueryVectorPtr = argPtr.bQueryVectorPtr; boolean status; - adtree_t* adtreePtr = learnerPtr.adtreePtr; - net_t* netPtr = learnerPtr.netPtr; - float* localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods; + Adtree adtreePtr = learnerPtr.adtreePtr; + Net netPtr = learnerPtr.netPtr; - TMpopulateParentQueryVector(TM_ARG netPtr, toId, queries, parentQueryVectorPtr); - */ + TMpopulateParentQueryVector(netPtr, toId, queries, parentQueryVectorPtr); /* * Create base query and parentQuery */ - /* - status = PVECTOR_COPY(baseParentQueryVectorPtr, parentQueryVectorPtr); - assert(status); + status = Vector_t.vector_copy(baseParentQueryVectorPtr, parentQueryVectorPtr); + if(status == false) { + System.out.println("Assert failed: copying baseParentQuery vector in TMfindBestInsertTask"); + System.exit(0); + } + + status = Vector_t.vector_copy(baseQueryVectorPtr, baseParentQueryVectorPtr); + if(status == false) { + System.out.println("Assert failed: copying baseQuery vector in TMfindBestInsertTask"); + System.exit(0); + } + + status = baseQueryVectorPtr.vector_pushBack(queries[toId]); + if(status == false ) { + System.out.println("Assert failed: while vector pushBack in TMfindBestInsertTask()"); + System.exit(0); + } - status = PVECTOR_COPY(baseQueryVectorPtr, baseParentQueryVectorPtr); - assert(status); - status = PVECTOR_PUSHBACK(baseQueryVectorPtr, (void*)&queries[toId]); - assert(status); - PVECTOR_SORT(queryVectorPtr, &compareQuery); - */ + queryVectorPtr.vector_sort(); /* * Search all possible valid operations for better local log likelihood */ - /* - float bestFromId = toId; // flag for not found - float oldLocalLogLikelihood = - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]); + int bestFromId = toId; // flag for not found + float oldLocalLogLikelihood = learnerPtr.localBaseLogLikelihoods[toId]; float bestLocalLogLikelihood = oldLocalLogLikelihood; - status = TMNET_FINDDESCENDANTS(netPtr, toId, invalidBitmapPtr, workQueuePtr); - assert(status); + status = netPtr.net_findDescendants(toId, invalidBitmapPtr, workQueuePtr); + if(status == false) { + System.out.println("Assert failed: while net_findDescendants in TMfindBestInsertTask()"); + System.exit(0); + } + int fromId = -1; - list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, toId); + IntList parentIdListPtr = netPtr.net_getParentIdListPtr(toId); int maxNumEdgeLearned = global_maxNumEdgeLearned; if ((maxNumEdgeLearned < 0) || - (TMLIST_GETSIZE(parentIdListPtr) <= maxNumEdgeLearned)) + (parentIdListPtr.list_getSize() <= maxNumEdgeLearned)) { - list_iter_t it; - TMLIST_ITER_RESET(&it, parentIdListPtr); - while (TMLIST_ITER_HASNEXT(&it, parentIdListPtr)) { - int parentId = (int)TMLIST_ITER_NEXT(&it, parentIdListPtr); - bitmap_set(invalidBitmapPtr, parentId); // invalid since already have edge + IntListNode it = parentIdListPtr.head; + parentIdListPtr.list_iter_reset(it); + + while( parentIdListPtr.list_iter_hasNext(it)) { + it = it.nextPtr; + int parentId = parentIdListPtr.list_iter_next(it); + invalidBitmapPtr.bitmap_set(parentId); // invalid since already have edge } - while ((fromId = bitmap_findClear(invalidBitmapPtr, (fromId + 1))) >= 0) { + while ((fromId = invalidBitmapPtr.bitmap_findClear((fromId + 1))) >= 0) { if (fromId == toId) { continue; } - status = PVECTOR_COPY(queryVectorPtr, baseQueryVectorPtr); - assert(status); - status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[fromId]); - assert(status); - PVECTOR_SORT(queryVectorPtr, &compareQuery); + status = Vector_t.vector_copy(queryVectorPtr, baseQueryVectorPtr); + if(status == false) { + System.out.println("Assert failed: copying query vector in TMfindBestInsertTask"); + System.exit(0); + } + + status = queryVectorPtr.vector_pushBack(queries[fromId]); + if(status == false) { + System.out.println("Assert failed: vector pushback for query in TMfindBestInsertTask"); + System.exit(0); + } + + queryVectorPtr.vector_sort(); + + status = Vector_t.vector_copy(parentQueryVectorPtr, baseParentQueryVectorPtr); + if(status == false) { + System.out.println("Assert failed: copying parentQuery vector in TMfindBestInsertTask"); + System.exit(0); + } + + status = parentQueryVectorPtr.vector_pushBack(queries[fromId]); + if(status == false) { + System.out.println("Assert failed: vector pushBack for parentQuery in TMfindBestInsertTask"); + System.exit(0); + } - status = PVECTOR_COPY(parentQueryVectorPtr, baseParentQueryVectorPtr); - assert(status); - status = PVECTOR_PUSHBACK(parentQueryVectorPtr, (void*)&queries[fromId]); - assert(status); - PVECTOR_SORT(parentQueryVectorPtr, &compareQuery); + parentQueryVectorPtr.vector_sort(); float newLocalLogLikelihood = computeLocalLogLikelihood(toId, @@ -753,22 +777,20 @@ public class Learner { } // foreach valid parent } // if have not exceeded max number of edges to learn - */ /* * Return best task; Note: if none is better, fromId will equal toId */ - /* - learner_task_t bestTask; + LearnerTask bestTask = new LearnerTask(); bestTask.op = OPERATION_INSERT; bestTask.fromId = bestFromId; bestTask.toId = toId; - bestTask.score = 0.0; + bestTask.score = 0.0f; if (bestFromId != toId) { int numRecord = adtreePtr.numRecord; - int numParent = TMLIST_GETSIZE(parentIdListPtr) + 1; + int numParent = parentIdListPtr.list_getSize() + 1; float penalty = (numTotalParent + numParent * global_insertPenalty) * basePenalty; float logLikelihood = numRecord * (baseLogLikelihood + @@ -780,91 +802,86 @@ public class Learner { return bestTask; } - */ - #ifdef LEARNER_TRY_REMOVE /* ============================================================================= * TMfindBestRemoveTask * ============================================================================= */ - /* - static learner_task_t - TMfindBestRemoveTask (TM_ARGDECL findBestTaskArg_t* argPtr) + public LearnerTask + TMfindBestRemoveTask (FindBestTaskArg argPtr) { int toId = argPtr.toId; - learner_t* learnerPtr = argPtr.learnerPtr; - query_t* queries = argPtr.queries; - vector_t* queryVectorPtr = argPtr.queryVectorPtr; - vector_t* parentQueryVectorPtr = argPtr.parentQueryVectorPtr; + Learner learnerPtr = argPtr.learnerPtr; + Query[] queries = argPtr.queries; + Vector_t queryVectorPtr = argPtr.queryVectorPtr; + Vector_t parentQueryVectorPtr = argPtr.parentQueryVectorPtr; int numTotalParent = argPtr.numTotalParent; - float basePenalty = argPtr.basePenalty; - float baseLogLikelihood = argPtr.baseLogLikelihood; - vector_t* origParentQueryVectorPtr = argPtr.aQueryVectorPtr; + float basePenalty = argPtr.basePenalty; + float baseLogLikelihood = argPtr.baseLogLikelihood; + Vector_t origParentQueryVectorPtr = argPtr.aQueryVectorPtr; boolean status; - adtree_t* adtreePtr = learnerPtr.adtreePtr; - net_t* netPtr = learnerPtr.netPtr; - float* localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods; + Adtree adtreePtr = learnerPtr.adtreePtr; + Net netPtr = learnerPtr.netPtr; + float[] localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods; - TMpopulateParentQueryVector(TM_ARG - netPtr, toId, queries, origParentQueryVectorPtr); - int numParent = PVECTOR_GETSIZE(origParentQueryVectorPtr); - */ + TMpopulateParentQueryVector(netPtr, toId, queries, origParentQueryVectorPtr); + int numParent = origParentQueryVectorPtr.vector_getSize(); /* * Search all possible valid operations for better local log likelihood */ - /* - float bestFromId = toId; // flag for not found - float oldLocalLogLikelihood = - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]); + int bestFromId = toId; // flag for not found + float oldLocalLogLikelihood = localBaseLogLikelihoods[toId]; float bestLocalLogLikelihood = oldLocalLogLikelihood; int i; for (i = 0; i < numParent; i++) { - query_t* queryPtr = (query_t*)PVECTOR_AT(origParentQueryVectorPtr, i); + Query queryPtr = (Query) (origParentQueryVectorPtr.vector_at(i)); int fromId = queryPtr.index; - */ /* * Create parent query (subset of parents since remove an edge) */ - /* + parentQueryVectorPtr.vector_clear(); - PVECTOR_CLEAR(parentQueryVectorPtr); - - int p; - for (p = 0; p < numParent; p++) { + for (int p = 0; p < numParent; p++) { if (p != fromId) { - query_t* queryPtr = PVECTOR_AT(origParentQueryVectorPtr, p); - status = PVECTOR_PUSHBACK(parentQueryVectorPtr, - (void*)&queries[queryPtr.index]); - assert(status); + Query tmpqueryPtr = (Query) (origParentQueryVectorPtr.vector_at(p)); + status = parentQueryVectorPtr.vector_pushBack(queries[tmpqueryPtr.index]); + if(status == false) { + System.out.println("Assert failed: vector_pushBack to parentQuery in TMfindBestRemoveTask()"); + System.exit(0); + } } } // create new parent query - */ /* * Create query */ - /* - status = PVECTOR_COPY(queryVectorPtr, parentQueryVectorPtr); - assert(status); - status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[toId]); - assert(status); - PVECTOR_SORT(queryVectorPtr, &compareQuery); - */ + status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr); + if(status == false) { + System.out.println("Assert failed: while vector copy to query in TMfindBestRemoveTask()"); + System.exit(0); + } + + status = queryVectorPtr.vector_pushBack(queries[toId]); + if(status == false) { + System.out.println("Assert failed: while vector_pushBack to query in TMfindBestRemoveTask()"); + System.exit(0); + } + + queryVectorPtr.vector_sort(); /* * See if removing parent is better */ - /* float newLocalLogLikelihood = computeLocalLogLikelihood(toId, adtreePtr, @@ -879,18 +896,16 @@ public class Learner { } } // for each parent - */ /* * Return best task; Note: if none is better, fromId will equal toId */ - /* - learner_task_t bestTask; + LearnerTask bestTask = new LearnerTask(); bestTask.op = OPERATION_REMOVE; bestTask.fromId = bestFromId; bestTask.toId = toId; - bestTask.score = 0.0; + bestTask.score = 0.0f; if (bestFromId != toId) { int numRecord = adtreePtr.numRecord; @@ -904,7 +919,6 @@ public class Learner { return bestTask; } - */ #endif /* LEARNER_TRY_REMOVE */ @@ -913,96 +927,90 @@ public class Learner { * TMfindBestReverseTask * ============================================================================= */ - /* - static learner_task_t - TMfindBestReverseTask (TM_ARGDECL findBestTaskArg_t* argPtr) + public LearnerTask + TMfindBestReverseTask (FindBestTaskArg argPtr) { int toId = argPtr.toId; - learner_t* learnerPtr = argPtr.learnerPtr; - query_t* queries = argPtr.queries; - vector_t* queryVectorPtr = argPtr.queryVectorPtr; - vector_t* parentQueryVectorPtr = argPtr.parentQueryVectorPtr; + Learner learnerPtr = argPtr.learnerPtr; + Query[] queries = argPtr.queries; + Vector_t queryVectorPtr = argPtr.queryVectorPtr; + Vector_t parentQueryVectorPtr = argPtr.parentQueryVectorPtr; int numTotalParent = argPtr.numTotalParent; - float basePenalty = argPtr.basePenalty; - float baseLogLikelihood = argPtr.baseLogLikelihood; - bitmap_t* visitedBitmapPtr = argPtr.bitmapPtr; - queue_t* workQueuePtr = argPtr.workQueuePtr; - vector_t* toOrigParentQueryVectorPtr = argPtr.aQueryVectorPtr; - vector_t* fromOrigParentQueryVectorPtr = argPtr.bQueryVectorPtr; + float basePenalty = argPtr.basePenalty; + float baseLogLikelihood = argPtr.baseLogLikelihood; + BitMap visitedBitmapPtr = argPtr.bitmapPtr; + Queue workQueuePtr = argPtr.workQueuePtr; + Vector_t toOrigParentQueryVectorPtr = argPtr.aQueryVectorPtr; + Vector_t fromOrigParentQueryVectorPtr = argPtr.bQueryVectorPtr; boolean status; - adtree_t* adtreePtr = learnerPtr.adtreePtr; - net_t* netPtr = learnerPtr.netPtr; - float* localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods; + Adtree adtreePtr = learnerPtr.adtreePtr; + Net netPtr = learnerPtr.netPtr; + float[] localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods; - TMpopulateParentQueryVector(TM_ARG - netPtr, toId, queries, toOrigParentQueryVectorPtr); - int numParent = PVECTOR_GETSIZE(toOrigParentQueryVectorPtr); - */ + TMpopulateParentQueryVector(netPtr, toId, queries, toOrigParentQueryVectorPtr); + int numParent = toOrigParentQueryVectorPtr.vector_getSize(); /* * Search all possible valid operations for better local log likelihood */ - /* int bestFromId = toId; // flag for not found - float oldLocalLogLikelihood = - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]); + float oldLocalLogLikelihood = localBaseLogLikelihoods[toId]; float bestLocalLogLikelihood = oldLocalLogLikelihood; int fromId = 0; - int i; - for (i = 0; i < numParent; i++) { + for (int i = 0; i < numParent; i++) { - query_t* queryPtr = (query_t*)PVECTOR_AT(toOrigParentQueryVectorPtr, i); + Query queryPtr = (Query) (toOrigParentQueryVectorPtr.vector_at(i)); fromId = queryPtr.index; bestLocalLogLikelihood = - oldLocalLogLikelihood + - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[fromId]); + oldLocalLogLikelihood + localBaseLogLikelihoods[fromId]; - TMpopulateParentQueryVector(TM_ARG - netPtr, + TMpopulateParentQueryVector(netPtr, fromId, queries, fromOrigParentQueryVectorPtr); - */ /* * Create parent query (subset of parents since remove an edge) */ - /* - - PVECTOR_CLEAR(parentQueryVectorPtr); + parentQueryVectorPtr.vector_clear(); - int p; - for (p = 0; p < numParent; p++) { + for (int p = 0; p < numParent; p++) { if (p != fromId) { - query_t* queryPtr = PVECTOR_AT(toOrigParentQueryVectorPtr, p); - status = PVECTOR_PUSHBACK(parentQueryVectorPtr, - (void*)&queries[queryPtr.index]); - assert(status); + Query tmpqueryPtr = (Query) (toOrigParentQueryVectorPtr.vector_at(p)); + status = parentQueryVectorPtr.vector_pushBack(queries[tmpqueryPtr.index]); + if(status == false) { + System.out.println("Assert failed: while vector_pushBack parentQueryVectorPtr"); + System.exit(0); + } } } // create new parent query - */ /* * Create query */ - /* - status = PVECTOR_COPY(queryVectorPtr, parentQueryVectorPtr); - assert(status); - status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[toId]); - assert(status); - PVECTOR_SORT(queryVectorPtr, &compareQuery); - */ + status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr); + if(status == false) { + System.out.println("Assert failed: while vector_copy in TMfindBestReverseTask()"); + System.exit(0); + } + + status = queryVectorPtr.vector_pushBack(queries[toId]); + if(status == false) { + System.out.println("Assert failed: while vector_pushBack in TMfindBestReverseTask()"); + System.exit(0); + } + + queryVectorPtr.vector_sort(); /* * Get log likelihood for removing parent from toId */ - /* float newLocalLogLikelihood = computeLocalLogLikelihood(toId, @@ -1011,25 +1019,38 @@ public class Learner { queries, queryVectorPtr, parentQueryVectorPtr); - */ /* * Get log likelihood for adding parent to fromId */ - /* + status = Vector_t.vector_copy(parentQueryVectorPtr, fromOrigParentQueryVectorPtr); + if(status == false) { + System.out.println("Assert failed: while parentQuery vector_copy in TMfindBestReverseTask()"); + System.exit(0); + } + + status = parentQueryVectorPtr.vector_pushBack(queries[toId]); + if(status == false) { + System.out.println("Assert failed: while vector_pushBack into parentQuery on TMfindBestReverseTask()"); + System.exit(0); + } + + parentQueryVectorPtr.vector_sort(); + + status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr); + if(status == false) { + System.out.println("Assert failed: while vector_copy on TMfindBestReverseTask()"); + System.exit(0); + } - status = PVECTOR_COPY(parentQueryVectorPtr, fromOrigParentQueryVectorPtr); - assert(status); - status = PVECTOR_PUSHBACK(parentQueryVectorPtr, (void*)&queries[toId]); - assert(status); - PVECTOR_SORT(parentQueryVectorPtr, &compareQuery); + status = queryVectorPtr.vector_pushBack(queries[fromId]); + if(status == false) { + System.out.println("Assert failed: while vector_pushBack on TMfindBestReverseTask()"); + System.exit(0); + } - status = PVECTOR_COPY(queryVectorPtr, parentQueryVectorPtr); - assert(status); - status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[fromId]); - assert(status); - PVECTOR_SORT(queryVectorPtr, &compareQuery); + queryVectorPtr.vector_sort(); newLocalLogLikelihood += computeLocalLogLikelihood(fromId, @@ -1038,57 +1059,50 @@ public class Learner { queries, queryVectorPtr, parentQueryVectorPtr); - */ /* * Record best */ - /* + if (newLocalLogLikelihood > bestLocalLogLikelihood) { bestLocalLogLikelihood = newLocalLogLikelihood; bestFromId = fromId; } } // for each parent - */ /* * Check validity of best */ - /* if (bestFromId != toId) { - boolean isTaskValid = TRUE; - TMNET_APPLYOPERATION(netPtr, OPERATION_REMOVE, bestFromId, toId); - if (TMNET_ISPATH(netPtr, - bestFromId, + boolean isTaskValid = true; + netPtr.net_applyOperation(OPERATION_REMOVE, bestFromId, toId); + if (netPtr.net_isPath(bestFromId, toId, visitedBitmapPtr, workQueuePtr)) { - isTaskValid = FALSE; + isTaskValid = false; } - TMNET_APPLYOPERATION(netPtr, OPERATION_INSERT, bestFromId, toId); + netPtr.net_applyOperation(OPERATION_INSERT, bestFromId, toId); if (!isTaskValid) { bestFromId = toId; } } - */ /* * Return best task; Note: if none is better, fromId will equal toId */ - /* - learner_task_t bestTask; + LearnerTask bestTask = new LearnerTask(); bestTask.op = OPERATION_REVERSE; bestTask.fromId = bestFromId; bestTask.toId = toId; - bestTask.score = 0.0; + bestTask.score = 0.0f; if (bestFromId != toId) { - float fromLocalLogLikelihood = - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[bestFromId]); + float fromLocalLogLikelihood = localBaseLogLikelihoods[bestFromId]; int numRecord = adtreePtr.numRecord; float penalty = numTotalParent * basePenalty; float logLikelihood = numRecord * (baseLogLikelihood + @@ -1101,7 +1115,7 @@ public class Learner { return bestTask; } - */ + #endif /* LEARNER_TRY_REVERSE */ @@ -1113,47 +1127,68 @@ public class Learner { * threads. * ============================================================================= */ - /* - static void - learnStructure (void* argPtr) + public static void + learnStructure (int myId, int numThread, Learner learnerPtr) { - TM_THREAD_ENTER(); - learner_t* learnerPtr = (learner_t*)argPtr; - net_t* netPtr = learnerPtr.netPtr; - adtree_t* adtreePtr = learnerPtr.adtreePtr; - int numRecord = adtreePtr.numRecord; - float* localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods; - list_t* taskListPtr = learnerPtr.taskListPtr; + int numRecord = learnerPtr.adtreePtr.numRecord; - float operationQualityFactor = global_operationQualityFactor; + float operationQualityFactor = learnerPtr.global_operationQualityFactor; - bitmap_t* visitedBitmapPtr = PBITMAP_ALLOC(learnerPtr.adtreePtr.numVar); - assert(visitedBitmapPtr); - queue_t* workQueuePtr = PQUEUE_ALLOC(-1); - assert(workQueuePtr); + BitMap visitedBitmapPtr = BitMap.bitmap_alloc(learnerPtr.adtreePtr.numVar); + if(visitedBitmapPtr == null) { + System.out.println("Assert failed: for bitmap alloc in learnStructure()"); + System.exit(0); + } - int numVar = adtreePtr.numVar; - query_t* queries = (query_t*)P_MALLOC(numVar * sizeof(query_t)); - assert(queries); - int v; - for (v = 0; v < numVar; v++) { + Queue workQueuePtr = Queue.queue_alloc(-1); + if(workQueuePtr == null) { + System.out.println("Assert failed: for vector alloc in learnStructure()"); + System.exit(0); + } + + int numVar = learnerPtr.adtreePtr.numVar; + Query[] queries = new Query[numVar]; + + if(queries == null) { + System.out.println("Assert failed: for queries alloc in learnStructure()"); + System.exit(0); + } + + for (int v = 0; v < numVar; v++) { + queries[v] = new Query(); queries[v].index = v; queries[v].value = QUERY_VALUE_WILDCARD; } - float basePenalty = (float)(-0.5 * log((double)numRecord)); + float basePenalty = (float)(-0.5 * Math.log((double)numRecord)); - vector_t* queryVectorPtr = PVECTOR_ALLOC(1); - assert(queryVectorPtr); - vector_t* parentQueryVectorPtr = PVECTOR_ALLOC(1); - assert(parentQueryVectorPtr); - vector_t* aQueryVectorPtr = PVECTOR_ALLOC(1); - assert(aQueryVectorPtr); - vector_t* bQueryVectorPtr = PVECTOR_ALLOC(1); - assert(bQueryVectorPtr); + Vector_t queryVectorPtr = Vector_t.vector_alloc(1); + if(queryVectorPtr == null) { + System.out.println("Assert failed: for vector_alloc in learnStructure()"); + System.exit(0); + } - findBestTaskArg_t arg; + Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1); + if(parentQueryVectorPtr == null) { + System.out.println("Assert failed: for vector_alloc in learnStructure()"); + System.exit(0); + } + + Vector_t aQueryVectorPtr = Vector_t.vector_alloc(1); + if(aQueryVectorPtr == null) { + System.out.println("Assert failed: for vector_alloc in learnStructure()"); + System.exit(0); + } + + Vector_t bQueryVectorPtr = Vector_t.vector_alloc(1); + if(bQueryVectorPtr == null) { + System.out.println("Assert failed: for vector_alloc in learnStructure()"); + System.exit(0); + } + + + FindBestTaskArg arg = new FindBestTaskArg(); arg.learnerPtr = learnerPtr; arg.queries = queries; arg.queryVectorPtr = queryVectorPtr; @@ -1163,243 +1198,220 @@ public class Learner { arg.aQueryVectorPtr = aQueryVectorPtr; arg.bQueryVectorPtr = bQueryVectorPtr; - while (1) { + while (true) { + + LearnerTask taskPtr; + + atomic { + taskPtr = learnerPtr.TMpopTask(learnerPtr.taskListPtr); + } - learner_task_t* taskPtr; - TM_BEGIN(); - taskPtr = TMpopTask(TM_ARG taskListPtr); - TM_END(); - if (taskPtr == NULL) { + if (taskPtr == null) { break; } - operation_t op = taskPtr.op; + int op = taskPtr.op; int fromId = taskPtr.fromId; int toId = taskPtr.toId; boolean isTaskValid; - TM_BEGIN(); - */ + atomic { + /* + * Check if task is still valid + */ + + isTaskValid = true; + if(op == OPERATION_INSERT) { + if(learnerPtr.netPtr.net_hasEdge(fromId, toId) || + learnerPtr.netPtr.net_isPath(toId, + fromId, + visitedBitmapPtr, + workQueuePtr)) + { + isTaskValid = false; + } + } else if (op == OPERATION_REMOVE) { + // Can never create cycle, so always valid + ; + } else if (op == OPERATION_REVERSE) { + // Temporarily remove edge for check + learnerPtr.netPtr.net_applyOperation(OPERATION_REMOVE, fromId, toId); + if(learnerPtr.netPtr.net_isPath(fromId, + toId, + visitedBitmapPtr, + workQueuePtr)) + { + isTaskValid = false; + } + learnerPtr.netPtr.net_applyOperation(OPERATION_INSERT, fromId, toId); + } else { + System.out.println("Assert failed: We shouldn't get here in learnStructure()"); + System.exit(0); + } - /* - * Check if task is still valid - */ - /* - isTaskValid = TRUE; - switch (op) { - case OPERATION_INSERT: { - if (TMNET_HASEDGE(netPtr, fromId, toId) || - TMNET_ISPATH(netPtr, - toId, - fromId, - visitedBitmapPtr, - workQueuePtr)) - { - isTaskValid = FALSE; - } - break; - } - case OPERATION_REMOVE: { - // Can never create cycle, so always valid - break; - } - case OPERATION_REVERSE: { - // Temporarily remove edge for check - TMNET_APPLYOPERATION(netPtr, OPERATION_REMOVE, fromId, toId); - if (TMNET_ISPATH(netPtr, - fromId, - toId, - visitedBitmapPtr, - workQueuePtr)) - { - isTaskValid = FALSE; - } - TMNET_APPLYOPERATION(netPtr, OPERATION_INSERT, fromId, toId); - break; - } - default: - assert(0); - } #ifdef TEST_LEARNER - printf("[task] op=%i from=%li to=%li score=%lf valid=%s\n", - taskPtr.op, taskPtr.fromId, taskPtr.toId, taskPtr.score, - (isTaskValid ? "yes" : "no")); - fflush(stdout); + System.out.println("[task] op= " + taskPtr.op + " from= " + taskPtr.fromId + " to= " + + taskPtr.toId + " score= " + taskPtr.score + " valid= " + (isTaskValid ? "yes" : "no")); #endif -*/ - /* - * Perform task: update graph and probabilities - */ + /* + * Perform task: update graph and probabilities + */ - /* - if (isTaskValid) { - TMNET_APPLYOPERATION(netPtr, op, fromId, toId); - } + if (isTaskValid) { + learnerPtr.netPtr.net_applyOperation(op, fromId, toId); + } - TM_END(); + } - float deltaLogLikelihood = 0.0; + float deltaLogLikelihood = 0.0f; if (isTaskValid) { + float newBaseLogLikelihood; + if(op == OPERATION_INSERT) { + atomic { + learnerPtr.TMpopulateQueryVectors(learnerPtr.netPtr, + toId, + queries, + queryVectorPtr, + parentQueryVectorPtr); + newBaseLogLikelihood = + learnerPtr.computeLocalLogLikelihood(toId, + learnerPtr.adtreePtr, + learnerPtr.netPtr, + queries, + queryVectorPtr, + parentQueryVectorPtr); + float toLocalBaseLogLikelihood = learnerPtr.localBaseLogLikelihoods[toId]; + deltaLogLikelihood += + toLocalBaseLogLikelihood - newBaseLogLikelihood; + learnerPtr.localBaseLogLikelihoods[toId] = newBaseLogLikelihood; + } + + atomic { + int numTotalParent = learnerPtr.numTotalParent; + learnerPtr.numTotalParent = numTotalParent + 1; + } - switch (op) { - float newBaseLogLikelihood; - case OPERATION_INSERT: { - TM_BEGIN(); - TMpopulateQueryVectors(TM_ARG - netPtr, - toId, - queries, - queryVectorPtr, - parentQueryVectorPtr); - newBaseLogLikelihood = - computeLocalLogLikelihood(toId, - adtreePtr, - netPtr, - queries, - queryVectorPtr, - parentQueryVectorPtr); - float toLocalBaseLogLikelihood = - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]); - deltaLogLikelihood += - toLocalBaseLogLikelihood - newBaseLogLikelihood; - TM_SHARED_WRITE_F(localBaseLogLikelihoods[toId], - newBaseLogLikelihood); - TM_END(); - TM_BEGIN(); - int numTotalParent = (int)TM_SHARED_READ(learnerPtr.numTotalParent); - TM_SHARED_WRITE(learnerPtr.numTotalParent, (numTotalParent + 1)); - TM_END(); - break; - } #ifdef LEARNER_TRY_REMOVE - case OPERATION_REMOVE: { - TM_BEGIN(); - TMpopulateQueryVectors(TM_ARG - netPtr, - fromId, - queries, - queryVectorPtr, - parentQueryVectorPtr); - newBaseLogLikelihood = - computeLocalLogLikelihood(fromId, - adtreePtr, - netPtr, - queries, - queryVectorPtr, - parentQueryVectorPtr); - float fromLocalBaseLogLikelihood = - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[fromId]); - deltaLogLikelihood += - fromLocalBaseLogLikelihood - newBaseLogLikelihood; - TM_SHARED_WRITE_F(localBaseLogLikelihoods[fromId], - newBaseLogLikelihood); - TM_END(); - TM_BEGIN(); - int numTotalParent = (int)TM_SHARED_READ(learnerPtr.numTotalParent); - TM_SHARED_WRITE(learnerPtr.numTotalParent, (numTotalParent - 1)); - TM_END(); - break; - } -#endif // LEARNER_TRY_REMOVE + } else if(op == OPERATION_REMOVE) { + atomic { + learnerPtr.TMpopulateQueryVectors(learnerPtr.netPtr, + fromId, + queries, + queryVectorPtr, + parentQueryVectorPtr); + newBaseLogLikelihood = + learnerPtr. computeLocalLogLikelihood(fromId, + learnerPtr.adtreePtr, + learnerPtr.netPtr, + queries, + queryVectorPtr, + parentQueryVectorPtr); + float fromLocalBaseLogLikelihood = + learnerPtr.localBaseLogLikelihoods[fromId]; + deltaLogLikelihood += + fromLocalBaseLogLikelihood - newBaseLogLikelihood; + learnerPtr.localBaseLogLikelihoods[fromId] = newBaseLogLikelihood; + } + + atomic{ + int numTotalParent = learnerPtr.numTotalParent; + learnerPtr.numTotalParent = numTotalParent - 1; + } + +#endif // LEARNER_TRY_REMOVE #ifdef LEARNER_TRY_REVERSE - case OPERATION_REVERSE: { - TM_BEGIN(); - TMpopulateQueryVectors(TM_ARG - netPtr, - fromId, - queries, - queryVectorPtr, - parentQueryVectorPtr); - newBaseLogLikelihood = - computeLocalLogLikelihood(fromId, - adtreePtr, - netPtr, - queries, - queryVectorPtr, - parentQueryVectorPtr); - float fromLocalBaseLogLikelihood = - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[fromId]); - deltaLogLikelihood += - fromLocalBaseLogLikelihood - newBaseLogLikelihood; - TM_SHARED_WRITE_F(localBaseLogLikelihoods[fromId], - newBaseLogLikelihood); - TM_END(); - - TM_BEGIN(); - TMpopulateQueryVectors(TM_ARG - netPtr, - toId, - queries, - queryVectorPtr, - parentQueryVectorPtr); - newBaseLogLikelihood = - computeLocalLogLikelihood(toId, - adtreePtr, - netPtr, - queries, - queryVectorPtr, - parentQueryVectorPtr); - float toLocalBaseLogLikelihood = - (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]); - deltaLogLikelihood += - toLocalBaseLogLikelihood - newBaseLogLikelihood; - TM_SHARED_WRITE_F(localBaseLogLikelihoods[toId], - newBaseLogLikelihood); - TM_END(); - break; - } + } else if(op == OPERATION_REVERSE) { + atomic { + learnerPtr.TMpopulateQueryVectors(learnerPtr.netPtr, + fromId, + queries, + queryVectorPtr, + parentQueryVectorPtr); + newBaseLogLikelihood = + learnerPtr.computeLocalLogLikelihood(fromId, + learnerPtr.adtreePtr, + learnerPtr.netPtr, + queries, + queryVectorPtr, + parentQueryVectorPtr); + float fromLocalBaseLogLikelihood = + learnerPtr.localBaseLogLikelihoods[fromId]; + deltaLogLikelihood += + fromLocalBaseLogLikelihood - newBaseLogLikelihood; + learnerPtr.localBaseLogLikelihoods[fromId] = newBaseLogLikelihood; + } + + atomic { + learnerPtr.TMpopulateQueryVectors(learnerPtr.netPtr, + toId, + queries, + queryVectorPtr, + parentQueryVectorPtr); + newBaseLogLikelihood = + learnerPtr.computeLocalLogLikelihood(toId, + learnerPtr.adtreePtr, + learnerPtr.netPtr, + queries, + queryVectorPtr, + parentQueryVectorPtr); + float toLocalBaseLogLikelihood = + learnerPtr.localBaseLogLikelihoods[toId]; + deltaLogLikelihood += + toLocalBaseLogLikelihood - newBaseLogLikelihood; + learnerPtr.localBaseLogLikelihoods[toId] = newBaseLogLikelihood; + } + #endif // LEARNER_TRY_REVERSE - default: - assert(0); - } // switch op + } else { + System.out.println("Assert failed: We should not reach here in learnerPtr()"); + System.exit(0); + } //switch op - } // if isTaskValid -*/ + } //if isTaskValid /* * Update/read globals */ - /* + float baseLogLikelihood; int numTotalParent; - TM_BEGIN(); - float oldBaseLogLikelihood = - (float)TM_SHARED_READ_F(learnerPtr.baseLogLikelihood); - float newBaseLogLikelihood = oldBaseLogLikelihood + deltaLogLikelihood; - TM_SHARED_WRITE_F(learnerPtr.baseLogLikelihood, newBaseLogLikelihood); - baseLogLikelihood = newBaseLogLikelihood; - numTotalParent = (int)TM_SHARED_READ(learnerPtr.numTotalParent); - TM_END(); - */ + atomic { + float oldBaseLogLikelihood = learnerPtr.baseLogLikelihood; + float newBaseLogLikelihood = oldBaseLogLikelihood + deltaLogLikelihood; + learnerPtr.baseLogLikelihood = newBaseLogLikelihood; + baseLogLikelihood = newBaseLogLikelihood; + numTotalParent = learnerPtr.numTotalParent; + } /* * Find next task */ - /* + + float baseScore = ((float)numTotalParent * basePenalty) + (numRecord * baseLogLikelihood); - learner_task_t bestTask; + LearnerTask bestTask = new LearnerTask(); bestTask.op = NUM_OPERATION; bestTask.toId = -1; bestTask.fromId = -1; bestTask.score = baseScore; - learner_task_t newTask; + LearnerTask newTask = new LearnerTask(); arg.toId = toId; arg.numTotalParent = numTotalParent; arg.basePenalty = basePenalty; arg.baseLogLikelihood = baseLogLikelihood; - TM_BEGIN(); - newTask = TMfindBestInsertTask(TM_ARG &arg); - TM_END(); + atomic { + newTask = learnerPtr.TMfindBestInsertTask(arg); + } if ((newTask.fromId != newTask.toId) && (newTask.score > (bestTask.score / operationQualityFactor))) @@ -1408,9 +1420,9 @@ public class Learner { } #ifdef LEARNER_TRY_REMOVE - TM_BEGIN(); - newTask = TMfindBestRemoveTask(TM_ARG &arg); - TM_END(); + atomic { + newTask = learnerPtr.TMfindBestRemoveTask(arg); + } if ((newTask.fromId != newTask.toId) && (newTask.score > (bestTask.score / operationQualityFactor))) @@ -1420,9 +1432,9 @@ public class Learner { #endif // LEARNER_TRY_REMOVE #ifdef LEARNER_TRY_REVERSE - TM_BEGIN(); - newTask = TMfindBestReverseTask(TM_ARG &arg); - TM_END(); + atomic { + newTask = learnerPtr.TMfindBestReverseTask(arg); + } if ((newTask.fromId != newTask.toId) && (newTask.score > (bestTask.score / operationQualityFactor))) @@ -1432,31 +1444,27 @@ public class Learner { #endif // LEARNER_TRY_REVERSE if (bestTask.toId != -1) { - learner_task_t* tasks = learnerPtr.tasks; + LearnerTask[] tasks = learnerPtr.tasks; tasks[toId] = bestTask; - TM_BEGIN(); - TMLIST_INSERT(taskListPtr, (void*)&tasks[toId]); - TM_END(); + atomic { + learnerPtr.taskListPtr.list_insert(tasks[toId]); + } #ifdef TEST_LEARNER - printf("[new] op=%i from=%li to=%li score=%lf\n", - bestTask.op, bestTask.fromId, bestTask.toId, bestTask.score); - fflush(stdout); + System.out.println("[new] op= " + bestTask.op + " from= "+ bestTask.fromId + " to= "+ bestTask.toId + + " score= " + bestTask.score); #endif } } // while (tasks) - PBITMAP_FREE(visitedBitmapPtr); - PQUEUE_FREE(workQueuePtr); - PVECTOR_FREE(bQueryVectorPtr); - PVECTOR_FREE(aQueryVectorPtr); - PVECTOR_FREE(queryVectorPtr); - PVECTOR_FREE(parentQueryVectorPtr); - P_FREE(queries); - - TM_THREAD_EXIT(); + visitedBitmapPtr.bitmap_free(); + workQueuePtr.queue_free(); + bQueryVectorPtr.vector_free(); + aQueryVectorPtr.vector_free(); + queryVectorPtr.vector_free(); + parentQueryVectorPtr.vector_free(); + queries = null; } -*/ /* ============================================================================= @@ -1464,26 +1472,17 @@ public class Learner { * -- Call adtree_make before this * ============================================================================= */ - /* + //Is not called anywhere now parallel code public void learner_run (int myId, int numThread, Learner learnerPtr) - //learner_run (learner_t* learnerPtr) { -#ifdef OTM -#pragma omp parallel { - createTaskList((void*)learnerPtr); + createTaskList(myId, numThread, learnerPtr); } -#pragma omp parallel { - learnStructure((void*)learnerPtr); + learnStructure(myId, numThread, learnerPtr); } -#else - thread_start(&createTaskList, (void*)learnerPtr); - thread_start(&learnStructure, (void*)learnerPtr); -#endif } -*/ /* ============================================================================= * learner_score diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/LearnerTask.java b/Robust/src/Benchmarks/SingleTM/Bayes/LearnerTask.java index e519e88f..3ad6bdf9 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/LearnerTask.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/LearnerTask.java @@ -7,7 +7,6 @@ * - Helper class for Learner.java **/ public class LearnerTask { - //Operation op; int op; int fromId; int toId; diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Net.java b/Robust/src/Benchmarks/SingleTM/Bayes/Net.java index aadf64d7..4b39edaa 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/Net.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/Net.java @@ -175,30 +175,6 @@ public class Net { } - /* ============================================================================= - * TMinsertEdge - * ============================================================================= - */ - /* - static void - TMinsertEdge (TM_ARGDECL net_t* netPtr, int fromId, int toId) - { - vector_t* nodeVectorPtr = netPtr.nodeVectorPtr; - boolean status; - - net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId); - list_t* parentIdListPtr = childNodePtr.parentIdListPtr; - status = TMLIST_INSERT(parentIdListPtr, (void*)fromId); - assert(status); - - net_node_t* parentNodePtr = (net_node_t*)vector_at(nodeVectorPtr, fromId); - list_t* childIdListPtr = parentNodePtr.childIdListPtr; - status = TMLIST_INSERT(childIdListPtr, (void*)toId); - assert(status); - } - */ - - /* ============================================================================= * removeEdge * ============================================================================= @@ -225,29 +201,6 @@ public class Net { } } - /* ============================================================================= - * TMremoveEdge - * ============================================================================= - */ - /* - static void - TMremoveEdge (TM_ARGDECL net_t* netPtr, int fromId, int toId) - { - vector_t* nodeVectorPtr = netPtr.nodeVectorPtr; - boolean status; - - net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId); - list_t* parentIdListPtr = childNodePtr.parentIdListPtr; - status = TMLIST_REMOVE(parentIdListPtr, (void*)fromId); - assert(status); - - net_node_t* parentNodePtr = (net_node_t*)vector_at(nodeVectorPtr, fromId); - list_t* childIdListPtr = parentNodePtr.childIdListPtr; - status = TMLIST_REMOVE(childIdListPtr, (void*)toId); - assert(status); - } - */ - /* ============================================================================= * reverseEdge * ============================================================================= @@ -260,20 +213,6 @@ public class Net { } - /* ============================================================================= - * TMreverseEdge - * ============================================================================= - */ - /* - static void - TMreverseEdge (TM_ARGDECL net_t* netPtr, int fromId, int toId) - { - TMremoveEdge(TM_ARG netPtr, fromId, toId); - TMinsertEdge(TM_ARG netPtr, toId, fromId); - } - */ - - /* ============================================================================= * net_applyOperation * ============================================================================= @@ -288,33 +227,12 @@ public class Net { } else if(op == OPERATION_REVERSE) { reverseEdge(fromId, toId); } else { - System.out.println("Operation failed"); + System.out.println("Assert failed: We shouldn't get here in net_applyOperation()"); System.exit(0); } } - /* ============================================================================= - * TMnet_applyOperation - * ============================================================================= - */ - /* - void - TMnet_applyOperation (TM_ARGDECL - net_t* netPtr, operation_t op, int fromId, int toId) - { - switch (op) { - case OPERATION_INSERT: TMinsertEdge(TM_ARG netPtr, fromId, toId); break; - case OPERATION_REMOVE: TMremoveEdge(TM_ARG netPtr, fromId, toId); break; - case OPERATION_REVERSE: TMreverseEdge(TM_ARG netPtr, fromId, toId); break; - default: - assert(0); - } - } - - */ - - /* ============================================================================= * net_hasEdge * ============================================================================= @@ -344,18 +262,18 @@ public class Net { * TMnet_hasEdge * ============================================================================= */ - /* - boolean - TMnet_hasEdge (TM_ARGDECL net_t* netPtr, int fromId, int toId) + public boolean + TMnet_hasEdge (int fromId, int toId) { - vector_t* nodeVectorPtr = netPtr.nodeVectorPtr; - net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId); - list_t* parentIdListPtr = childNodePtr.parentIdListPtr; - - list_iter_t it; - TMLIST_ITER_RESET(&it, parentIdListPtr); - while (TMLIST_ITER_HASNEXT(&it, parentIdListPtr)) { - int parentId = (int)TMLIST_ITER_NEXT(&it, parentIdListPtr); + NetNode childNodePtr = (NetNode)(nodeVectorPtr.vector_at(toId)); + + IntList parentIdListPtr = childNodePtr.parentIdListPtr; + IntListNode it = parentIdListPtr.head;//initialize iterator + parentIdListPtr.list_iter_reset(it); + + while (parentIdListPtr.list_iter_hasNext(it)) { + it = it.nextPtr; + int parentId = parentIdListPtr.list_iter_next(it); if (parentId == fromId) { return true; } @@ -363,7 +281,6 @@ public class Net { return false; } - */ /* ============================================================================= @@ -425,56 +342,6 @@ public class Net { } - /* ============================================================================= - * TMnet_isPath - * ============================================================================= - */ - /* - boolean - TMnet_isPath (TM_ARGDECL - net_t* netPtr, - int fromId, - int toId, - Bitmap* visitedBitmapPtr, - Queue* workQueuePtr) - { - boolean status; - - vector_t* nodeVectorPtr = netPtr.nodeVectorPtr; - assert(visitedBitmapPtr.numBit == vector_getSize(nodeVectorPtr)); - - PBITMAP_CLEARALL(visitedBitmapPtr); - PQUEUE_CLEAR(workQueuePtr); - - status = PQUEUE_PUSH(workQueuePtr, (void*)fromId); - assert(status); - - while (!PQUEUE_ISEMPTY(workQueuePtr)) { - int id = (int)queue_pop(workQueuePtr); - if (id == toId) { - queue_clear(workQueuePtr); - return true; - } - status = PBITMAP_SET(visitedBitmapPtr, id); - assert(status); - net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); - list_t* childIdListPtr = nodePtr.childIdListPtr; - list_iter_t it; - TMLIST_ITER_RESET(&it, childIdListPtr); - while (TMLIST_ITER_HASNEXT(&it, childIdListPtr)) { - int childId = (int)TMLIST_ITER_NEXT(&it, childIdListPtr); - if (!PBITMAP_ISSET(visitedBitmapPtr, childId)) { - status = PQUEUE_PUSH(workQueuePtr, (void*)childId); - assert(status); - } - } - } - - return false; - } - */ - - /* ============================================================================= * isCycle * ============================================================================= @@ -658,68 +525,6 @@ public class Net { } - /* ============================================================================= - * TMnet_findAncestors - * -- Contents of bitmapPtr set to 1 if ancestor, else 0 - * -- Returns false if id is not root node (i.e., has cycle back id) - * ============================================================================= - */ - /* - boolean - TMnet_findAncestors (TM_ARGDECL - net_t* netPtr, - int id, - Bitmap* ancestorBitmapPtr, - Queue* workQueuePtr) - { - boolean status; - - vector_t* nodeVectorPtr = netPtr.nodeVectorPtr; - assert(ancestorBitmapPtr.numBit == vector_getSize(nodeVectorPtr)); - - PBITMAP_CLEARALL(ancestorBitmapPtr); - PQUEUE_CLEAR(workQueuePtr); - - { - net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); - list_t* parentIdListPtr = nodePtr.parentIdListPtr; - list_iter_t it; - TMLIST_ITER_RESET(&it, parentIdListPtr); - while (TMLIST_ITER_HASNEXT(&it, parentIdListPtr)) { - int parentId = (int)TMLIST_ITER_NEXT(&it, parentIdListPtr); - status = PBITMAP_SET(ancestorBitmapPtr, parentId); - assert(status); - status = PQUEUE_PUSH(workQueuePtr, (void*)parentId); - assert(status); - } - } - - while (!PQUEUE_ISEMPTY(workQueuePtr)) { - int parentId = (int)PQUEUE_POP(workQueuePtr); - if (parentId == id) { - PQUEUE_CLEAR(workQueuePtr); - return false; - } - net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, parentId); - list_t* grandParentIdListPtr = nodePtr.parentIdListPtr; - list_iter_t it; - TMLIST_ITER_RESET(&it, grandParentIdListPtr); - while (TMLIST_ITER_HASNEXT(&it, grandParentIdListPtr)) { - int grandParentId = (int)TMLIST_ITER_NEXT(&it, grandParentIdListPtr); - if (!PBITMAP_ISSET(ancestorBitmapPtr, grandParentId)) { - status = PBITMAP_SET(ancestorBitmapPtr, grandParentId); - assert(status); - status = PQUEUE_PUSH(workQueuePtr, (void*)grandParentId); - assert(status); - } - } - } - - return true; - } - */ - - /* ============================================================================= * net_findDescendants * -- Contents of bitmapPtr set to 1 if descendants, else 0 @@ -733,12 +538,10 @@ public class Net { { boolean status; - //vector_t* nodeVectorPtr = netPtr.nodeVectorPtr; - if(descendantBitmapPtr.numBit == nodeVectorPtr.vector_getSize()) { + if(descendantBitmapPtr.numBit != nodeVectorPtr.vector_getSize()) { System.out.println("Assert failed: for descendantBitmapPtr.numbit in net_findDescendants()"); System.exit(0); } - //assert(descendantBitmapPtr.numBit == vector_getSize(nodeVectorPtr)); descendantBitmapPtr.bitmap_clearAll(); workQueuePtr.queue_clear(); @@ -797,69 +600,6 @@ public class Net { return true; } - - /* ============================================================================= - * TMnet_findDescendants - * -- Contents of bitmapPtr set to 1 if descendants, else 0 - * -- Returns false if id is not root node (i.e., has cycle back id) - * ============================================================================= - */ - /* - boolean - TMnet_findDescendants (TM_ARGDECL - net_t* netPtr, - int id, - Bitmap* descendantBitmapPtr, - Queue* workQueuePtr) - { - boolean status; - - vector_t* nodeVectorPtr = netPtr.nodeVectorPtr; - assert(descendantBitmapPtr.numBit == vector_getSize(nodeVectorPtr)); - - PBITMAP_CLEARALL(descendantBitmapPtr); - PQUEUE_CLEAR(workQueuePtr); - - { - net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); - list_t* childIdListPtr = nodePtr.childIdListPtr; - list_iter_t it; - TMLIST_ITER_RESET(&it, childIdListPtr); - while (TMLIST_ITER_HASNEXT(&it, childIdListPtr)) { - int childId = (int)TMLIST_ITER_NEXT(&it, childIdListPtr); - status = PBITMAP_SET(descendantBitmapPtr, childId); - assert(status); - status = PQUEUE_PUSH(workQueuePtr, (void*)childId); - assert(status); - } - } - - while (!PQUEUE_ISEMPTY(workQueuePtr)) { - int childId = (int)PQUEUE_POP(workQueuePtr); - if (childId == id) { - queue_clear(workQueuePtr); - return false; - } - net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, childId); - list_t* grandChildIdListPtr = nodePtr.childIdListPtr; - list_iter_t it; - TMLIST_ITER_RESET(&it, grandChildIdListPtr); - while (TMLIST_ITER_HASNEXT(&it, grandChildIdListPtr)) { - int grandChildId = (int)TMLIST_ITER_NEXT(&it, grandChildIdListPtr); - if (!PBITMAP_ISSET(descendantBitmapPtr, grandChildId)) { - status = PBITMAP_SET(descendantBitmapPtr, grandChildId); - assert(status); - status = PQUEUE_PUSH(workQueuePtr, (void*)grandChildId); - assert(status); - } - } - } - - return true; - } - */ - - /* ============================================================================= * net_generateRandomEdges * ============================================================================= diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Operation.java b/Robust/src/Benchmarks/SingleTM/Bayes/Operation.java deleted file mode 100644 index f346daa8..00000000 --- a/Robust/src/Benchmarks/SingleTM/Bayes/Operation.java +++ /dev/null @@ -1,72 +0,0 @@ -/* ============================================================================= - * - * operation.java - * - * ============================================================================= - * - * Copyright (C) Stanford University, 2006. All Rights Reserved. - * Author: Chi Cao Minh - * Modified : Ported to Java on June 2009 by Alokika Dash - * University of California, Irvine - * - * ============================================================================= - * - * Unless otherwise noted, the following license applies to STAMP files: - * - * Copyright (c) 2007, Stanford University - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * * Neither the name of Stanford University nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF - * THE POSSIBILITY OF SUCH DAMAGE. - * - * ============================================================================= - */ - -public class Operation { - int insert; - int remove; - int reverse; - int num_operation; - - /* - * All operations are performed from:other to:this - */ - public Operation() { - insert = 0; - remove = 1; - reverse = 2; - num_operation = 3; - } -} - -/* ============================================================================= - * - * End of operation.java - * - * ============================================================================= - */ diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/makefile b/Robust/src/Benchmarks/SingleTM/Bayes/makefile index f1588b0f..18b57d0d 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/makefile +++ b/Robust/src/Benchmarks/SingleTM/Bayes/makefile @@ -9,7 +9,6 @@ SRC=tmp${MAINCLASS}.java \ LearnerTask.java \ tmpNet.java \ NetNode.java \ - Operation.java \ Query.java \ tmpSort.java \ tmpBitMap.java \ @@ -25,7 +24,7 @@ SRC=tmp${MAINCLASS}.java \ ../../../ClassLibrary/JavaSTM/Barrier.java \ ../common/LocalStartStop.java -FLAGS=-mainclass ${MAINCLASS} -thread -nooptimize -debug +FLAGS=-mainclass ${MAINCLASS} -singleTM -optimize -debug -dcopts -stmstats -fastmemcpy -transstats -abcclose default: cpp Bayes.java > tmp1Bayes.java -- 2.34.1