static double sum_tree(node* n) { if (n) { return n->value + sum_tree(n->left) + sum_tree(n->right); } else { return 0.0; } }
BOOST_FIXTURE_TEST_CASE(tree_traverse_test, fixture) { node* tree = build_tree(20); auto start = std::chrono::high_resolution_clock::now(); double sum = sum_tree(tree); auto end = std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::duration single = end - start; std::chrono::high_resolution_clock::duration parallel; double psum = 0.0; go([tree, ¶llel, &psum]() { auto start = std::chrono::high_resolution_clock::now(); psum = paraller_sum(tree); auto end = std::chrono::high_resolution_clock::now(); parallel = end-start; }); wait_for_completion(); BOOST_CHECK_EQUAL(sum, psum); std::cout << "single thread duration: " << single / std::chrono::milliseconds(1) << " ms " << std::endl; std::cout << "parallel duration: " << parallel / std::chrono::milliseconds(1) << " ms " << std::endl; BOOST_CHECK_EQUAL(sum, psum); }
void testTwoRateModel(Params ¶ms, NGSAlignment &aln, NGSTree &tree, string model, double *freq, DoubleVector &rate_info, StrVector &rate_name, bool write_info, const char *report_file) { char model_name[20]; NGSAlignment sum_aln(aln.num_states, 1, freq); NGSTreeCat sum_tree(params, &sum_aln); sum_aln.tree = &sum_tree; ModelsBlock *models_block = new ModelsBlock; if (model == "") sprintf(model_name, "GTR+FC2"); else sprintf(model_name, "%s+FC2", model.c_str()); try { params.model_name = model_name; sum_tree.setModelFactory(new ModelFactory(params, &sum_tree, models_block)); sum_tree.setModel(sum_tree.getModelFactory()->model); sum_tree.setRate(sum_tree.getModelFactory()->site_rate); double bestTreeScore = sum_tree.getModelFactory()->optimizeParameters(false, write_info); cout << "LogL: " << bestTreeScore; cout << " / Rate: " << sum_tree.getRate()->getRate(0) << endl; } catch (const char*) { cout << "Skipped due to sparse matrix" << endl; //rate_info.insert(rate_info.end(), rate_name.size(), MIN_SITE_RATE); return; } catch (string &str) { cout << str; return; } delete models_block; //return sum_tree.getRate()->getRate(0); /* rate_info.push_back(sum_tree.getRate()->getRate(0)); double rate_mat[aln.num_states*aln.num_states]; memset(rate_mat, 0, aln.num_states*aln.num_states*sizeof(double)); sum_tree.getModel()->getRateMatrix(rate_mat); rate_info.insert(rate_info.end(), rate_mat, rate_mat+sum_tree.getModel()->getNumRateEntries()); if (tree.getModel()->isReversible()) { sum_tree.getModel()->getStateFrequency(rate_mat); rate_info.insert(rate_info.end(), rate_mat, rate_mat+aln.num_states); } if (report_file) { DoubleMatrix tmp(1); tmp[0] = rate_info; reportNGSAnalysis(report_file, params, sum_aln, sum_tree, tmp, rate_name); }*/ }
int main() { int start[9]; int i; int product; struct node *tree = NULL; for (i = 0; i < 9; i++) start[i] = i+1; while (next(start, 9)) { product = tonumber(start+5, 4); if (product == tonumber(start, 1) * tonumber(start+1, 4) || product == tonumber(start, 2) * tonumber(start+2, 3) || product == tonumber(start, 3) * tonumber(start+3, 2) || product == tonumber(start, 4) * tonumber(start+4, 1)) tree = insert(tree, product); } printf("%d\n", sum_tree(tree)); return 0; }
static void paraller_sum_sub(node* tree, channel_writer<double>& out) { out.put(sum_tree(tree)); }
int sum_tree(struct node *tree) { if (tree == NULL) return 0; return sum_tree(tree->left) + tree->product + sum_tree(tree->right); }