I can't believe it but I may have found a solution without using a stack. It uses three (!) loops to do a depth first traversal. It is better to explain it by code so here it goes:
Code: Select all
__global__
void printTree() {
Node* current = root_node;
while(current) {
while(current) {
while(current) {
PRINT_WITH_FORMAT();
//child
if(current->child) current = current->child;
else break;
}
//sibling
if(current->next) current = current->next;
else break;
}
//parent
if(current->parent) current = current->parent->next;
else break;
}
}
Caution at the outer loop during backing up. We go to the parent and the sibling at once (two commands unlike the single one in the inner loops). Please let me know if you find problem with it. I have printed a tree with it but I am still not sure if it is bug free.
Edit: Yep it works correctly. I have modified it further to get the depth of the tree at any node. The width is a bit difficult because of that back up step I mentioned above. The width should be stored in a stack to resume where we left of before. Actually for my case I can calculate it indirectly from the address of contigious blocks of siblings but that is a hack. So the modified working code for depth changes and depth limit is:
Code: Select all
__global__ void printTree(int depthLimit) {
int depth = 0;
Node* current = root_node;
while(current) {
while(current) {
while(current) {
if(current->uct_visits) {
for(int i = 0;i < depth;i++)
cuPrintf("\t");
cuPrintf("%d. %d %d %.6f\n",
depth,current->uct_wins,current->uct_visits,
float(current->uct_wins) / current->uct_visits
);
}
//child
if(current->child && depth < depthLimit) {
depth++;
current = current->child;
} else break;
}
//sibling
if(current->next) {
current = current->next;
} else break;
}
//parent
if(current->parent) {
depth--;
current = current->parent->next;
} else break;
}
cuPrintf("Total nodes in tree: %d\n",head - mem_);
}
And a partial result of a UCT tree like I wanted
Code: Select all
0. 25633078 46102528 0.556002
1. 307115 688128 0.446305
2. 96636 172032 0.561733
2. 97952 172032 0.569382
2. 97758 172032 0.568255
1. 316998 688128 0.460667
2. 96680 172032 0.561988
2. 93590 172032 0.544027
2. 94959 172032 0.551985
1. 319653 688128 0.464525
2. 98002 172032 0.569673
2. 93706 172032 0.544701
2. 92391 172032 0.537057
1. 318789 688128 0.463270
2. 98179 172032 0.570702
2. 94831 172032 0.551241
2. 92591 172032 0.538220
1. 319246 688128 0.463934
2. 97904 172032 0.569103
2. 95177 172032 0.553252
2. 92802 172032 0.539446
1. 321420 688128 0.467093
2. 96948 172032 0.563546
2. 94361 172032 0.548508
2. 92705 172032 0.538882
1. 323445 688128 0.470036
2. 96227 172032 0.559355
2. 94062 172032 0.546770
2. 92600 172032 0.538272
1. 326112 688128 0.473912
2. 96093 172032 0.558576
2. 93268 172032 0.542155
2. 92214 172032 0.536028
3. 966 2048 0.471680
1. 300756 688128 0.437064
2. 105769 172032 0.614822
2. 98580 172032 0.573033
2. 96993 172032 0.563808
1. 290090 688128 0.421564
----
cheers