00001 #ifndef CPM_TREE_PREDICTION_HPP_
00002 #define CPM_TREE_PREDICTION_HPP_
00003
00004 #include "MPIBlib/collectives/libmpib_coll.h"
00005 #include "collectives/cpm_coll_ops_list.h"
00006 #include "cpm_models.h"
00007 #include "MPIBlib/collectives/comm_tree.hpp"
00008 #include "MPIBlib/collectives/mpib_coll.h"
00009 #include <boost/graph/graphviz.hpp>
00010
00011 using namespace MPIB::comm_tree;
00026 class TreePredictor {
00027 private:
00028 CPM_predictor *predictor;
00029 MPIB_child_traverse_order order;
00030
00031
00032
00033
00034
00035
00036 int* counts_helper;
00037 int* size_bytes;
00038 double* t;
00039 int size;
00040 CPM_coll_op_types op;
00041
00042 public:
00043
00044 TreePredictor(CPM_predictor* _predictor, MPIB_child_traverse_order _order, int _size, int* _size_bytes, CPM_coll_op_types _op, int* _counts_helper, double* _t) {
00045 predictor = _predictor;
00046 t = _t;
00047 counts_helper = _counts_helper;
00048 order = _order;
00049 size = _size;
00050 size_bytes = _size_bytes;
00051 op = _op;
00052 }
00053
00054 void preorder(Vertex vertex, Tree& tree) {
00055 }
00056 void inorder(Vertex vertex, Tree& tree) {}
00057 void postorder(Vertex vertex, Tree& tree) {
00058 int my_rank = get(vertex_index, tree._g, vertex);
00059 Adjacency_iterator ai, ai_end;
00060 tie(ai, ai_end) = adjacent_vertices(vertex,tree._g);
00061 double temp=0.;
00062
00063 while (ai != ai_end) {
00064 int child_rank = get(vertex_index, tree._g, order == R2L ? *(--ai_end) : *(ai++));
00065 if (op == SG) counts_helper[my_rank] += counts_helper[child_rank];
00066 if (op == SGV) counts_helper[my_rank] +=counts_helper[child_rank];
00067 int size_bytes_tmp;
00068 if (op == SG) size_bytes_tmp = size_bytes[0] * counts_helper[child_rank];
00069 if (op == BR) size_bytes_tmp = size_bytes[0];
00070 if (op == SGV) size_bytes_tmp = counts_helper[child_rank];
00071 if (mpib_coll_verbose == 1) {
00072 printf("DEBUG: temp <= predict_p2p (%d, %d, %d) = %lf + max (%lf, %lf)\n",\
00073 my_rank, child_rank, size_bytes_tmp, \
00074 predictor->predict_p2p(predictor, my_rank, child_rank, size_bytes_tmp), temp, t[child_rank]);
00075 }
00076 temp = predictor->predict_p2p(predictor, my_rank, child_rank, size_bytes_tmp) + max (temp, t[child_rank]);
00077 }
00078 t[my_rank] = temp;
00079 }
00080 };
00081
00082
00083 template<typename Builder>
00084 double CPM_predict_tree_sgv(Builder builder, MPIB_child_traverse_order order, int size, int root, int* size_bytes, CPM_predictor* predictor, CPM_coll_op_types op) {
00085 Graph graph;
00086 Vertex r, u, v;
00087 double prediction;
00088 builder.build(size, root, root, size_bytes, graph, r, u, v);
00089 if (mpib_coll_verbose == 1) {
00090 write_graphviz(cout, graph);
00091 }
00092 Tree tree(graph, r);
00093 double* t = new double[size];
00094 int* counts_helper = NULL;
00095 counts_helper = new int[size];
00096 for (int i = 0; i < size; i++)
00097 counts_helper[i] = size_bytes[i];
00098 traverse_tree(r, tree, TreePredictor(predictor, order, size, size_bytes, op, counts_helper, t));
00099
00100 prediction = t[root];
00101 free(t);
00102 free(counts_helper);
00103 return prediction;
00104 }
00105
00106 template<typename Builder>
00107 double CPM_predict_tree_brsg(Builder builder, MPIB_child_traverse_order order, int size, int root, int size_bytes, CPM_predictor* predictor, CPM_coll_op_types op) {
00108 Graph graph;
00109 Vertex r, u, v;
00110 builder.build(size, root, root, size_bytes, graph, r, u, v);
00111 if (mpib_coll_verbose == 1) {
00112 write_graphviz(cout, graph);
00113 }
00114 Tree tree(graph, r);
00115 double* t = new double[size];
00116 int* counts_helper = NULL;
00117 if (op == SG) {
00118 counts_helper = new int[size];
00119 for (int i = 0; i < size; i++)
00120 counts_helper[i] = 1;
00121 }
00122 traverse_tree(r, tree, TreePredictor(predictor, order, size, &size_bytes, op, counts_helper, t));
00123
00124 double prediction = t[root];
00125 free(t);
00126 if (op == SG) {
00127 free(counts_helper);
00128 }
00129
00130 return prediction;
00131 }
00132
00133 #endif