Skip to content

Commit 3151bcf

Browse files
committed
batch for branch re-estimation utility
1 parent d154337 commit 3151bcf

15 files changed

Lines changed: 319 additions & 383 deletions

cpu/divide_and_conquer/placement_close_k_cu.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -623,25 +623,34 @@ void MashPlacement::KPlacementDeviceArraysDC::printTreeDC(std::vector <std::stri
623623
double * h_len = new double[totalNumSequences*8];
624624
double * h_closest_dis = new double[totalNumSequences*20];
625625
int * h_closest_id = new int[totalNumSequences*20];
626-
std::function<void(int,int)> print=[&](int node, int from){
627-
if(h_nxt[h_head[node]]!=-1){
628-
// printf("(");
626+
std::function<void(int,int)> print = [&](int node, int from) {
627+
if (h_nxt[h_head[node]] != -1) {
629628
output_ << "(";
630-
std::vector <int> pos;
631-
for(int i=h_head[node];i!=-1;i=h_nxt[i])
632-
if(h_e[i]!=from)
633-
pos.push_back(i);
634-
for(size_t i=0;i<pos.size();i++){
635-
print(h_e[pos[i]],node);
636-
// printf(":");
637-
// printf("%.5g%c",h_len[pos[i]],i+1==pos.size()?')':',');
629+
std::vector<std::pair<int,int>> pos; // {edge_index, parent_node}
630+
for (int i = h_head[node]; i != -1; i = h_nxt[i]) {
631+
if (h_e[i] != from) {
632+
if (h_len[i] == 0) {
633+
int collapsed = h_e[i];
634+
if (h_head[collapsed] != -1) {
635+
for (int j = h_head[collapsed]; j != -1; j = h_nxt[j]) {
636+
if (h_e[j] != node) {
637+
pos.push_back({j, collapsed});
638+
}
639+
}
640+
}
641+
} else {
642+
pos.push_back({i, node});
643+
}
644+
}
645+
}
646+
for (size_t i = 0; i < pos.size(); i++) {
647+
auto [edgeIdx, parent] = pos[i];
648+
print(h_e[edgeIdx], parent);
638649
output_ << ":";
639-
output_ << h_len[pos[i]] << (i+1==pos.size()?')':',');
650+
output_ << h_len[edgeIdx] << (i+1 == pos.size() ? ')' : ',');
640651
}
641-
}
642-
// else std::cout<<name[node];
643-
else {
644-
output_<<name[node];
652+
} else {
653+
output_ << name[node];
645654
}
646655
};
647656
std::memcpy(h_head, d_head, totalNumSequences*2 * sizeof(int));

cpu/mash_placement.hpp

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -330,63 +330,6 @@ namespace MashPlacement
330330
};
331331
static KPlacementDeviceArraysDC kplacementDeviceArraysDC;
332332

333-
inline void printNewickFromHostAdjacency(std::ostream& out, const std::vector<std::string>& name, int* h_head,
334-
int* h_e, int* h_nxt, double* h_len, int rootNode, bool binaryNewick) {
335-
const double kLenEps = 1e-12;
336-
auto hasKids = [h_head, h_e, h_nxt](int node, int from) -> bool {
337-
for (int i = h_head[node]; i != -1; i = h_nxt[i]) {
338-
if (h_e[i] != from) {
339-
return true;
340-
}
341-
}
342-
return false;
343-
};
344-
std::function<void(int, int)> dfs;
345-
dfs = [&](int node, int from) {
346-
if (!hasKids(node, from)) {
347-
out << name[node];
348-
return;
349-
}
350-
if (binaryNewick) {
351-
out << "(";
352-
std::vector<int> pos;
353-
for (int i = h_head[node]; i != -1; i = h_nxt[i]) {
354-
if (h_e[i] != from) {
355-
pos.push_back(i);
356-
}
357-
}
358-
for (size_t i = 0; i < pos.size(); ++i) {
359-
dfs(h_e[pos[i]], node);
360-
out << ":" << h_len[pos[i]] << (i + 1 == pos.size() ? ")" : ",");
361-
}
362-
return;
363-
}
364-
std::vector<std::pair<int, double>> kids;
365-
std::function<void(int, int)> addCollapsed;
366-
addCollapsed = [&](int n, int fr) {
367-
for (int i = h_head[n]; i != -1; i = h_nxt[i]) {
368-
int ch = h_e[i];
369-
if (ch == fr) {
370-
continue;
371-
}
372-
double L = h_len[i];
373-
if (std::fabs(L) <= kLenEps && hasKids(ch, n)) {
374-
addCollapsed(ch, n);
375-
} else {
376-
kids.push_back({ch, L});
377-
}
378-
}
379-
};
380-
addCollapsed(node, from);
381-
out << "(";
382-
for (size_t i = 0; i < kids.size(); ++i) {
383-
dfs(kids[i].first, node);
384-
out << ":" << kids[i].second << (i + 1 == kids.size() ? ")" : ",");
385-
}
386-
};
387-
dfs(rootNode, -1);
388-
}
389-
390333
};
391334

392335
#endif

cpu/placement.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -516,25 +516,35 @@ void MashPlacement::PlacementDeviceArrays::printTree(std::vector <std::string> n
516516
int * h_e = new int[numSequences*8];
517517
int * h_nxt = new int[numSequences*8];
518518
double * h_len = new double[numSequences*8];
519-
std::function<void(int,int)> print=[&](int node, int from){
520-
if(h_nxt[h_head[node]]!=-1){
519+
std::function<void(int,int)> print = [&](int node, int from) {
520+
if (h_nxt[h_head[node]] != -1) {
521521
output_ << "(";
522-
// printf("(");
523-
524-
std::vector <int> pos;
525-
for(int i=h_head[node];i!=-1;i=h_nxt[i])
526-
if(h_e[i]!=from)
527-
pos.push_back(i);
528-
for(size_t i=0;i<pos.size();i++){
529-
print(h_e[pos[i]],node);
530-
// printf(":");
531-
// printf("%.5g%c",h_len[pos[i]],i+1==pos.size()?')':',');
522+
std::vector<std::pair<int,int>> pos; // {edge_index, parent_node}
523+
for (int i = h_head[node]; i != -1; i = h_nxt[i]) {
524+
if (h_e[i] != from) {
525+
if (h_len[i] == 0) {
526+
int collapsed = h_e[i];
527+
if (h_head[collapsed] != -1) {
528+
for (int j = h_head[collapsed]; j != -1; j = h_nxt[j]) {
529+
if (h_e[j] != node) {
530+
pos.push_back({j, collapsed});
531+
}
532+
}
533+
}
534+
} else {
535+
pos.push_back({i, node});
536+
}
537+
}
538+
}
539+
for (size_t i = 0; i < pos.size(); i++) {
540+
auto [edgeIdx, parent] = pos[i];
541+
print(h_e[edgeIdx], parent);
532542
output_ << ":";
533-
output_ << h_len[pos[i]] << (i+1==pos.size()?')':',');
543+
output_ << h_len[edgeIdx] << (i+1 == pos.size() ? ')' : ',');
534544
}
545+
} else {
546+
output_ << name[node];
535547
}
536-
// else std::cout<<name[node];
537-
else output_ << name[node];
538548
};
539549
for (int i = 0; i < numSequences*2; i++) h_head[i] = d_head[i];
540550
for (int i = 0; i < numSequences*8; i++) {

cpu/placement_close_k.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -550,30 +550,37 @@ void MashPlacement::KPlacementDeviceArrays::printTree(std::vector<std::string> n
550550
double *h_len = new double[numSequences * 8];
551551
double *h_closest_dis = new double[numSequences * 20];
552552
int *h_closest_id = new int[numSequences * 20];
553-
std::function<void(int, int)> print = [&](int node, int from)
554-
{
555-
if (h_nxt[h_head[node]] != -1)
556-
{
557-
// printf("(");
553+
std::function<void(int,int)> print = [&](int node, int from) {
554+
if (h_nxt[h_head[node]] != -1) {
558555
output_ << "(";
559-
std::vector<int> pos;
560-
for (int i = h_head[node]; i != -1; i = h_nxt[i])
561-
if (h_e[i] != from)
562-
pos.push_back(i);
563-
for (size_t i = 0; i < pos.size(); i++)
564-
{
565-
print(h_e[pos[i]], node);
566-
// printf(":");
567-
// printf("%.5g%c",h_len[pos[i]],i+1==pos.size()?')':',');
556+
std::vector<std::pair<int,int>> pos; // {edge_index, parent_node}
557+
for (int i = h_head[node]; i != -1; i = h_nxt[i]) {
558+
if (h_e[i] != from) {
559+
if (h_len[i] == 0) {
560+
int collapsed = h_e[i];
561+
if (h_head[collapsed] != -1) {
562+
for (int j = h_head[collapsed]; j != -1; j = h_nxt[j]) {
563+
if (h_e[j] != node) {
564+
pos.push_back({j, collapsed});
565+
}
566+
}
567+
}
568+
} else {
569+
pos.push_back({i, node});
570+
}
571+
}
572+
}
573+
for (size_t i = 0; i < pos.size(); i++) {
574+
auto [edgeIdx, parent] = pos[i];
575+
print(h_e[edgeIdx], parent);
568576
output_ << ":";
569-
// output_ << "%.5g%c",h_len[pos[i]],i+1==pos.size()?')':',';
570-
output_ << h_len[pos[i]] << (i + 1 == pos.size() ? ')' : ',');
577+
output_ << h_len[edgeIdx] << (i+1 == pos.size() ? ')' : ',');
571578
}
572-
}
573-
// else std::cout<<name[node];
574-
else
579+
} else {
575580
output_ << name[node];
581+
}
576582
};
583+
577584
for (int i = 0; i < numSequences * 2; ++i)
578585
{
579586
h_head[i] = d_head[i];

src/divide_and_conquer/mash_placement.cuh

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -229,63 +229,6 @@ namespace MashPlacement
229229
};
230230
static KPlacementDeviceArraysDC kplacementDeviceArraysDC;
231231

232-
inline void printNewickFromHostAdjacency(std::ostream& out, const std::vector<std::string>& name, int* h_head,
233-
int* h_e, int* h_nxt, double* h_len, int rootNode, bool binaryNewick) {
234-
const double kLenEps = 1e-12;
235-
auto hasKids = [h_head, h_e, h_nxt](int node, int from) -> bool {
236-
for (int i = h_head[node]; i != -1; i = h_nxt[i]) {
237-
if (h_e[i] != from) {
238-
return true;
239-
}
240-
}
241-
return false;
242-
};
243-
std::function<void(int, int)> dfs;
244-
dfs = [&](int node, int from) {
245-
if (!hasKids(node, from)) {
246-
out << name[node];
247-
return;
248-
}
249-
if (binaryNewick) {
250-
out << "(";
251-
std::vector<int> pos;
252-
for (int i = h_head[node]; i != -1; i = h_nxt[i]) {
253-
if (h_e[i] != from) {
254-
pos.push_back(i);
255-
}
256-
}
257-
for (size_t i = 0; i < pos.size(); ++i) {
258-
dfs(h_e[pos[i]], node);
259-
out << ":" << h_len[pos[i]] << (i + 1 == pos.size() ? ")" : ",");
260-
}
261-
return;
262-
}
263-
std::vector<std::pair<int, double>> kids;
264-
std::function<void(int, int)> addCollapsed;
265-
addCollapsed = [&](int n, int fr) {
266-
for (int i = h_head[n]; i != -1; i = h_nxt[i]) {
267-
int ch = h_e[i];
268-
if (ch == fr) {
269-
continue;
270-
}
271-
double L = h_len[i];
272-
if (std::fabs(L) <= kLenEps && hasKids(ch, n)) {
273-
addCollapsed(ch, n);
274-
} else {
275-
kids.push_back({ch, L});
276-
}
277-
}
278-
};
279-
addCollapsed(node, from);
280-
out << "(";
281-
for (size_t i = 0; i < kids.size(); ++i) {
282-
dfs(kids[i].first, node);
283-
out << ":" << kids[i].second << (i + 1 == kids.size() ? ")" : ",");
284-
}
285-
};
286-
dfs(rootNode, -1);
287-
}
288-
289232
};
290233

291234
#endif

src/divide_and_conquer/placement_close_k.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,24 @@ void MashPlacement::KPlacementDeviceArraysHostDC::deallocateHostArraysDC(){
594594

595595

596596
void MashPlacement::KPlacementDeviceArraysHostDC::printTreeCpuDC(std::vector <std::string> name){
597-
std::ostringstream oss;
598-
printNewickFromHostAdjacency(oss, name, h_head, h_e, h_nxt, h_len, totalNumSequences + bd - 2,
599-
g_printBinaryNewick);
600-
std::cout << oss.str() << ";\n";
597+
auto print=[&](int node, int from, auto&& print)->void {
598+
if(h_nxt[h_head[node]]!=-1){
599+
printf("(");
600+
std::vector <int> pos;
601+
for(int i=h_head[node];i!=-1;i=h_nxt[i])
602+
if(h_e[i]!=from)
603+
pos.push_back(i);
604+
for(size_t i=0;i<pos.size();i++){
605+
print(h_e[pos[i]],node, print);
606+
printf(":");
607+
printf("%.5g%c",h_len[pos[i]],i+1==pos.size()?')':',');
608+
}
609+
}
610+
else std::cout<<name[node];
611+
};
612+
613+
print(totalNumSequences+bd-2,-1, print);
614+
std::cout<<";\n";
601615
}
602616

603617
/* Clusterting function on CPU - > might need modification

src/divide_and_conquer/placement_close_k.cu

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -701,14 +701,37 @@ void MashPlacement::KPlacementDeviceArraysDC::printTreeDC(std::vector <std::stri
701701
}
702702

703703

704-
// print len
705-
// for (int i=0; i<totalNumSequences*2;i++){
706-
// std::cout << h_head[i] << "\t";
707-
// }
708-
// std::cout << std::endl;
709-
710-
printNewickFromHostAdjacency(output_, name, h_head, h_e, h_nxt, h_len, totalNumSequences + bd - 2,
711-
g_printBinaryNewick);
704+
std::function<void(int,int)> print = [&](int node, int from) {
705+
if (h_nxt[h_head[node]] != -1) {
706+
output_ << "(";
707+
std::vector<std::pair<int,int>> pos; // {edge_index, parent_node}
708+
for (int i = h_head[node]; i != -1; i = h_nxt[i]) {
709+
if (h_e[i] != from) {
710+
if (h_len[i] == 0) {
711+
int collapsed = h_e[i];
712+
if (h_head[collapsed] != -1) {
713+
for (int j = h_head[collapsed]; j != -1; j = h_nxt[j]) {
714+
if (h_e[j] != node) {
715+
pos.push_back({j, collapsed});
716+
}
717+
}
718+
}
719+
} else {
720+
pos.push_back({i, node});
721+
}
722+
}
723+
}
724+
for (size_t i = 0; i < pos.size(); i++) {
725+
auto [edgeIdx, parent] = pos[i];
726+
print(h_e[edgeIdx], parent);
727+
output_ << ":";
728+
output_ << h_len[edgeIdx] << (i+1 == pos.size() ? ')' : ',');
729+
}
730+
} else {
731+
output_ << name[node];
732+
}
733+
};
734+
print(totalNumSequences + bd - 2, -1);
712735
output_ << ";\n";
713736
}
714737

0 commit comments

Comments
 (0)