8 #include <Teuchos_RCP.hpp>
9 #include <Teuchos_Array.hpp>
10 #include <Teuchos_ParameterList.hpp>
11 #include "Teuchos_XMLParameterListHelpers.hpp"
13 #include "Tpetra_MultiVector.hpp"
14 #include <Tpetra_CrsGraph.hpp>
15 #include <Tpetra_Map.hpp>
35 typedef Tpetra::MultiVector<test_scalar_t, test_lno_t, test_gno_t, znode_t>
mytest_tMVector_t;
38 typedef Tpetra::Map<test_lno_t, test_gno_t, mytest_znode_t>
mytest_map_t;
53 char *input_binary_graph,
54 Teuchos::RCP<
const Teuchos::Comm<int> > tcomm,
56 std::vector <int> &task_communication_xadj_,
57 std::vector <int> &task_communication_adj_,
58 std::vector <double> &task_communication_adjw_){
60 int rank = tcomm->getRank();
61 using namespace Teuchos;
68 FILE *f2 = fopen(input_binary_graph,
"rb");
71 fread(&num_vertices,
sizeof(
int),1,f2);
72 fread(&num_edges,
sizeof(
int),1,f2);
74 myTasks = num_vertices;
76 std::cout <<
"numParts:" << num_vertices <<
" ne:" << num_edges << std::endl;
78 task_communication_xadj_.resize(num_vertices+1);
79 task_communication_adj_.resize(num_edges);
80 task_communication_adjw_.resize(num_edges);
82 fread((
void *)&(task_communication_xadj_[0]),
sizeof(
int),num_vertices + 1,f2);
83 fread((
void *)&(task_communication_adj_[0]),
sizeof(
int),num_edges ,f2);
84 fread((
void *)&(task_communication_adjw_[0]),
sizeof(
double),num_edges,f2);
90 tcomm->broadcast(0,
sizeof(
test_lno_t), (
char *) &myTasks);
91 tcomm->broadcast(0,
sizeof(
test_lno_t), (
char *) &myEdges);
94 task_communication_xadj_.resize(myTasks+1);
95 task_communication_adj_.resize(myEdges);
96 task_communication_adjw_.resize(myEdges);
99 tcomm->broadcast(0,
sizeof(
test_lno_t) * (myTasks+1), (
char *) &(task_communication_xadj_[0]));
100 tcomm->broadcast(0,
sizeof(
test_lno_t)* (myEdges), (
char *) &(task_communication_adj_[0]));
101 tcomm->broadcast(0,
sizeof(
test_scalar_t)* (myEdges), (
char *) &(task_communication_adjw_[0]));
104 using namespace Teuchos;
105 Teuchos::RCP<const Teuchos::Comm<int> > serial_comm = Teuchos::createSerialComm<int>();
106 RCP<const mytest_map_t> map = rcp (
new mytest_map_t (myTasks, myTasks, 0, serial_comm));
111 std::vector<test_gno_t> tmp(myEdges);
112 for (
test_lno_t lclRow = 0; lclRow < myTasks; ++lclRow) {
113 const test_gno_t gblRow = map->getGlobalElement (lclRow);
114 test_lno_t begin = task_communication_xadj_[gblRow];
115 test_lno_t end = task_communication_xadj_[gblRow + 1];
117 tmp[m - begin] = task_communication_adj_[m];
119 const ArrayView< const test_gno_t > indices(&(tmp[0]), end-begin);
120 TpetraCrsGraph->insertGlobalIndices(gblRow, indices);
122 TpetraCrsGraph->fillComplete ();
125 return TpetraCrsGraph;
130 RCP<const mytest_map_t> map,
int coord_dim,
134 Teuchos::Array<Teuchos::ArrayView<const test_scalar_t> > coordView(coord_dim);
137 for (
int i = 0; i < coord_dim; ++i){
138 Teuchos::ArrayView<const test_scalar_t> a(partCenters[i], myTasks);
143 for (
int i = 0; i < coord_dim; ++i){
144 Teuchos::ArrayView<const test_scalar_t> a;
148 RCP<mytest_tMVector_t> coords(
new mytest_tMVector_t(map, coordView.view(0, coord_dim), coord_dim));
149 RCP<const mytest_tMVector_t> const_coords = rcp_const_cast<
const mytest_tMVector_t>(coords);
156 char *input_binary_graph,
char *input_binary_coordinate,
char *input_machine_file,
157 int machine_optimization_level,
bool divide_prime_first,
int rank_per_node,
bool visualize_mapping,
int reduce_best_mapping){
159 if (input_binary_graph == NULL || input_binary_coordinate == NULL || input_machine_file == NULL){
160 throw "Binary files is mandatory";
163 Teuchos::RCP<const Teuchos::Comm<int> > serial_comm = Teuchos::createSerialComm<int>();
167 Teuchos::ParameterList serial_problemParams;
169 serial_problemParams.set(
"mapping_algorithm",
"geometric");
170 serial_problemParams.set(
"distributed_input_adapter",
false);
171 serial_problemParams.set(
"algorithm",
"multijagged");
172 serial_problemParams.set(
"Machine_Optimization_Level", machine_optimization_level);
173 serial_problemParams.set(
"Input_RCA_Machine_Coords", input_machine_file);
174 serial_problemParams.set(
"divide_prime_first", divide_prime_first);
175 serial_problemParams.set(
"ranks_per_node", rank_per_node);
176 if (reduce_best_mapping)
177 serial_problemParams.set(
"reduce_best_mapping",
true);
179 serial_problemParams.set(
"reduce_best_mapping",
false);
184 serial_problemParams.set(
"num_global_parts", numProcs);
187 env->setTimer(timer);
190 std::vector <double> task_communication_adjw_;
192 std::vector <int> task_communication_xadj_;
193 std::vector <int> task_communication_adj_;
202 task_communication_xadj_, task_communication_adj_,
203 task_communication_adjw_);
204 RCP<const mytest_map_t> serial_map = serial_tpetra_graph->getMap();
205 global_tcomm->barrier();
209 RCP<const mytest_tcrsGraph_t> const_tpetra_graph = rcp_const_cast<
const mytest_tcrsGraph_t>(serial_tpetra_graph);
212 int rank = global_tcomm->getRank();
214 int numParts = 0;
int coordDim = 0;
218 FILE *f2 = fopen(input_binary_coordinate,
"rb");
219 fread((
void *)&numParts,
sizeof(
int),1,f2);
220 fread((
void *)&coordDim,
sizeof(
int),1,f2);
224 for(
int i = 0; i < coordDim; ++i){
226 fread((
void *) partCenters[i],
sizeof(
double),numParts, f2);
231 global_tcomm->broadcast(0,
sizeof(
test_lno_t), (
char *) &numParts);
232 global_tcomm->broadcast(0,
sizeof(
test_lno_t), (
char *) &coordDim);
234 if (numParts!= myTasks){
235 throw "myTasks is different than numParts";
239 for(
int i = 0; i < coordDim; ++i){
244 for(
int i = 0; i < coordDim; ++i){
245 global_tcomm->broadcast(0,
sizeof(
test_scalar_t)* (numParts), (
char *) partCenters[i]);
249 RCP <Zoltan2::XpetraMultiVectorAdapter<mytest_tMVector_t> > serial_adapter =
create_multi_vector_adapter(serial_map, coordDim, partCenters, myTasks);
250 ia->setCoordinateInput(serial_adapter.getRawPtr());
252 ia->setEdgeWeights(&(task_communication_adjw_[0]), 1, 0);
264 global_tcomm->barrier();
286 Teuchos::ArrayView< const test_gno_t> gids = serial_map->getLocalElementList();
288 ArrayRCP<int> initial_part_ids(myTasks);
290 initial_part_ids[i] = gids[i];
292 single_phase_mapping_solution.
setParts(initial_part_ids);
300 ia.getRawPtr(), &serial_problemParams, global_tcomm, &single_phase_mapping_solution, &transformed_machine);
305 serial_map_problem.
solve(
true);
311 timer->printAndResetToZero();
319 RCP<quality_t> metricObject_3 = rcp(
320 new quality_t(ia.getRawPtr(),&serial_problemParams,serial_comm,msoln3, serial_map_problem.getMachine().getRawPtr()));
322 if (global_tcomm->getRank() == 0){
323 std::cout <<
"METRICS FOR THE SERIAL CASE - ONE PHASE MAPPING - EACH ELEMENT IS ASSUMED TO BE IN UNIQUE PART AT THE BEGINNING" << std::endl;
324 metricObject_3->printMetrics(std::cout);
326 if (machine_optimization_level > 0){
328 Teuchos::ParameterList serial_problemParams_2;
329 serial_problemParams_2.set(
"Input_RCA_Machine_Coords", input_machine_file);
333 RCP<quality_t> metricObject_4 = rcp(
334 new quality_t(ia.getRawPtr(),&serial_problemParams_2,serial_comm,msoln3, &actual_machine));
336 if (global_tcomm->getRank() == 0){
337 std::cout <<
"METRICS FOR THE SERIAL CASE - ONE PHASE MAPPING - EACH ELEMENT IS ASSUMED TO BE IN UNIQUE PART AT THE BEGINNING" << std::endl;
338 metricObject_4->printMetrics(std::cout);
342 if (visualize_mapping && global_tcomm->getRank() == 0){
344 Teuchos::ParameterList serial_problemParams_2;
345 serial_problemParams_2.set(
"Input_RCA_Machine_Coords", input_machine_file);
349 Zoltan2::visualize_mapping<zscalar_t, int> (0, actual_machine.
getMachineDim(), actual_machine.
getNumRanks(), coords,
350 int (task_communication_xadj_.size())-1, &(task_communication_xadj_[0]), &(task_communication_adj_[0]), msoln3->getPartListView());
356 int main(
int narg,
char *arg[]){
358 Tpetra::ScopeGuard tscope(&narg, &arg);
359 Teuchos::RCP<const Teuchos::Comm<int> > global_tcomm=Tpetra::getDefaultComm();
361 char *input_binary_graph = NULL;
362 char *input_binary_coordinate = NULL;
363 char *input_machine_file = NULL;
364 int machine_optimization_level = 10;
365 bool divide_prime_first =
false;
366 int rank_per_node = 1;
367 int reduce_best_mapping = 1;
369 for (
int i = 1 ; i < narg ; ++i ) {
370 if ( 0 == strcasecmp( arg[i] ,
"BG" ) ) {
372 input_binary_graph = arg[++i];
374 else if ( 0 == strcasecmp( arg[i] ,
"BC" ) ) {
375 input_binary_coordinate = arg[++i];
377 else if ( 0 == strcasecmp( arg[i] ,
"MF" ) ) {
379 input_machine_file = arg[++i];
381 else if ( 0 == strcasecmp( arg[i] ,
"OL" ) ) {
382 machine_optimization_level = atoi( arg[++i] );
384 else if ( 0 == strcasecmp( arg[i] ,
"DP" ) ) {
385 if (atoi( arg[++i] )){
386 divide_prime_first =
true;
389 else if ( 0 == strcasecmp( arg[i] ,
"RPN" ) ) {
390 rank_per_node = atoi( arg[++i] );
392 else if ( 0 == strcasecmp( arg[i] ,
"VM" ) ) {
393 visualize_mapping =
true;
395 else if ( 0 == strcasecmp( arg[i] ,
"RBM" ) ) {
396 reduce_best_mapping = atoi( arg[++i] );
399 std::cerr <<
"Unrecognized command line argument #" << i <<
": " << arg[i] << std::endl ;
408 machine_optimization_level, divide_prime_first, rank_per_node, visualize_mapping, reduce_best_mapping);
412 part_t my_parts = 0, *my_result_parts;
415 std::cout <<
"me:" << global_tcomm->getRank() <<
" my_parts:" << my_parts <<
" myTasks:" << myTasks << std::endl;
416 if (global_tcomm->getRank() == 0) {
420 FILE *f2 = fopen(
"plot.gnuplot",
"w");
421 for (
int i = 0; i< global_tcomm->getSize(); ++i){
423 sprintf(str,
"coords%d.txt", i);
425 fprintf(f2,
"splot \"%s\"\n", str);
428 fprintf(f2,
"replot \"%s\"\n", str);
431 fprintf(f2,
"pause-1\n");
435 int myrank = global_tcomm->getRank();
436 sprintf(str,
"coords%d.txt", myrank);
437 FILE *coord_files = fopen(str,
"w");
440 for (
int j = 0; j < my_parts; ++j){
441 int findex = my_result_parts[j];
442 std::cout <<
"findex " << findex << std::endl;
443 fprintf(coord_files,
"%lf %lf %lf\n", partCenters[0][findex], partCenters[1][findex], partCenters[2][findex]);
449 if (global_tcomm->getRank() == 0){
450 std::cout <<
"PASS" << std::endl;
453 catch(std::string &s){
454 std::cerr << s << std::endl;
458 std::cerr << s << std::endl;
RCP< Zoltan2::XpetraMultiVectorAdapter< mytest_tMVector_t > > create_multi_vector_adapter(RCP< const mytest_map_t > map, zscalar_t **partCenters, zgno_t myTasks)
Time an algorithm (or other entity) as a whole.
Zoltan2::XpetraCrsGraphAdapter< mytest_tcrsGraph_t, mytest_tMVector_t > mytest_adapter_t
MappingInputDistributution
Tpetra::CrsGraph< zlno_t, zgno_t, znode_t > mytest_tcrsGraph_t
void visualize_mapping(int myRank, const int machine_coord_dim, const int num_ranks, proc_coord_t **machine_coords, const v_lno_t num_tasks, const v_lno_t *task_communication_xadj, const v_lno_t *task_communication_adj, const int *task_to_rank)
PartitionMapping maps a solution or an input distribution to ranks.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
mytest_adapter_t::part_t mytest_part_t
Provides access for Zoltan2 to Xpetra::CrsGraph data.
Zoltan2::EvaluatePartition< matrixAdapter_t > quality_t
int main(int narg, char **arg)
common code used by tests
typename InputTraits< User >::part_t part_t
Tpetra::Map::node_type mytest_znode_t
Tpetra::Map< zlno_t, zgno_t, mytest_znode_t > mytest_map_t
Defines the XpetraMultiVectorAdapter.
Defines XpetraCrsGraphAdapter class.
SparseMatrixAdapter_t::part_t part_t
int getMachineDim() const
returns the dimension (number of coords per node) in the machine
bool getAllMachineCoordinatesView(pcoord_t **&allCoords) const
getProcDim function set the coordinates of all ranks allCoords[i][j], i=0,...,getMachineDim(), j=0,...,getNumRanks(), is the i-th dimensional coordinate for rank j. return true if coordinates are available for all ranks
void test_serial_input_adapter(int nx, int ny, int nz, Teuchos::RCP< const Teuchos::Comm< int > > global_tcomm)
A PartitioningSolution is a solution to a partitioning problem.
Defines the EvaluatePartition class.
int getNumRanks() const
return the number of ranks.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
An adapter for Xpetra::MultiVector.
MachineRepresentation Class Base class for representing machine coordinates, networks, etc.
Tpetra::Map::local_ordinal_type zlno_t
void setParts(ArrayRCP< part_t > &partList)
The algorithm uses setParts to set the solution.
Tpetra::MultiVector< zscalar_t, zlno_t, zgno_t, znode_t > mytest_tMVector_t
Defines the MappingSolution class.
RCP< mytest_tcrsGraph_t > create_tpetra_input_matrix(int nx, int ny, int nz, int numProcs, Teuchos::RCP< const Teuchos::Comm< int > > tcomm, RCP< Zoltan2::Environment > env, zscalar_t **&partCenters, zgno_t &myTasks)
Defines the PartitioningProblem class.
MappingProblem enables mapping of a partition (either computed or input) to MPI ranks.
A class that computes and returns quality metrics.
Tpetra::Map::global_ordinal_type zgno_t
Declarations for TimerManager.
Defines the MappingProblem class.