16 #include <Teuchos_RCP.hpp>
17 #include <Teuchos_Array.hpp>
18 #include <Teuchos_ParameterList.hpp>
19 #include "Teuchos_XMLParameterListHelpers.hpp"
21 #include "Tpetra_MultiVector.hpp"
22 #include <Tpetra_CrsGraph.hpp>
23 #include <Tpetra_Map.hpp>
43 typedef Tpetra::MultiVector<test_scalar_t, test_lno_t, test_gno_t, znode_t>
mytest_tMVector_t;
46 typedef Tpetra::Map<test_lno_t, test_gno_t, mytest_znode_t>
mytest_map_t;
61 char *input_binary_graph,
62 Teuchos::RCP<
const Teuchos::Comm<int> > tcomm,
64 std::vector <int> &task_communication_xadj_,
65 std::vector <int> &task_communication_adj_,
66 std::vector <double> &task_communication_adjw_){
68 int rank = tcomm->getRank();
69 using namespace Teuchos;
76 FILE *f2 = fopen(input_binary_graph,
"rb");
79 fread(&num_vertices,
sizeof(
int),1,f2);
80 fread(&num_edges,
sizeof(
int),1,f2);
82 myTasks = num_vertices;
84 std::cout <<
"numParts:" << num_vertices <<
" ne:" << num_edges << std::endl;
86 task_communication_xadj_.resize(num_vertices+1);
87 task_communication_adj_.resize(num_edges);
88 task_communication_adjw_.resize(num_edges);
90 fread((
void *)&(task_communication_xadj_[0]),
sizeof(
int),num_vertices + 1,f2);
91 fread((
void *)&(task_communication_adj_[0]),
sizeof(
int),num_edges ,f2);
92 fread((
void *)&(task_communication_adjw_[0]),
sizeof(
double),num_edges,f2);
98 tcomm->broadcast(0,
sizeof(
test_lno_t), (
char *) &myTasks);
99 tcomm->broadcast(0,
sizeof(
test_lno_t), (
char *) &myEdges);
102 task_communication_xadj_.resize(myTasks+1);
103 task_communication_adj_.resize(myEdges);
104 task_communication_adjw_.resize(myEdges);
107 tcomm->broadcast(0,
sizeof(
test_lno_t) * (myTasks+1), (
char *) &(task_communication_xadj_[0]));
108 tcomm->broadcast(0,
sizeof(
test_lno_t)* (myEdges), (
char *) &(task_communication_adj_[0]));
109 tcomm->broadcast(0,
sizeof(
test_scalar_t)* (myEdges), (
char *) &(task_communication_adjw_[0]));
112 using namespace Teuchos;
113 Teuchos::RCP<const Teuchos::Comm<int> > serial_comm = Teuchos::createSerialComm<int>();
114 RCP<const mytest_map_t> map = rcp (
new mytest_map_t (myTasks, myTasks, 0, serial_comm));
119 std::vector<test_gno_t> tmp(myEdges);
120 for (
test_lno_t lclRow = 0; lclRow < myTasks; ++lclRow) {
121 const test_gno_t gblRow = map->getGlobalElement (lclRow);
122 test_lno_t begin = task_communication_xadj_[gblRow];
123 test_lno_t end = task_communication_xadj_[gblRow + 1];
125 tmp[m - begin] = task_communication_adj_[m];
127 const ArrayView< const test_gno_t > indices(&(tmp[0]), end-begin);
128 TpetraCrsGraph->insertGlobalIndices(gblRow, indices);
130 TpetraCrsGraph->fillComplete ();
133 return TpetraCrsGraph;
138 RCP<const mytest_map_t> map,
int coord_dim,
142 Teuchos::Array<Teuchos::ArrayView<const test_scalar_t> > coordView(coord_dim);
145 for (
int i = 0; i < coord_dim; ++i){
146 Teuchos::ArrayView<const test_scalar_t> a(partCenters[i], myTasks);
151 for (
int i = 0; i < coord_dim; ++i){
152 Teuchos::ArrayView<const test_scalar_t> a;
156 RCP<mytest_tMVector_t> coords(
new mytest_tMVector_t(map, coordView.view(0, coord_dim), coord_dim));
157 RCP<const mytest_tMVector_t> const_coords = rcp_const_cast<
const mytest_tMVector_t>(coords);
164 char *input_binary_graph,
char *input_binary_coordinate,
char *input_machine_file,
165 int machine_optimization_level,
bool divide_prime_first,
int rank_per_node,
bool visualize_mapping,
int reduce_best_mapping){
167 if (input_binary_graph == NULL || input_binary_coordinate == NULL || input_machine_file == NULL){
168 throw "Binary files is mandatory";
171 Teuchos::RCP<const Teuchos::Comm<int> > serial_comm = Teuchos::createSerialComm<int>();
175 Teuchos::ParameterList serial_problemParams;
177 serial_problemParams.set(
"mapping_algorithm",
"geometric");
178 serial_problemParams.set(
"distributed_input_adapter",
false);
179 serial_problemParams.set(
"algorithm",
"multijagged");
180 serial_problemParams.set(
"Machine_Optimization_Level", machine_optimization_level);
181 serial_problemParams.set(
"Input_RCA_Machine_Coords", input_machine_file);
182 serial_problemParams.set(
"divide_prime_first", divide_prime_first);
183 serial_problemParams.set(
"ranks_per_node", rank_per_node);
184 if (reduce_best_mapping)
185 serial_problemParams.set(
"reduce_best_mapping",
true);
187 serial_problemParams.set(
"reduce_best_mapping",
false);
192 serial_problemParams.set(
"num_global_parts", numProcs);
195 env->setTimer(timer);
198 std::vector <double> task_communication_adjw_;
200 std::vector <int> task_communication_xadj_;
201 std::vector <int> task_communication_adj_;
210 task_communication_xadj_, task_communication_adj_,
211 task_communication_adjw_);
212 RCP<const mytest_map_t> serial_map = serial_tpetra_graph->getMap();
213 global_tcomm->barrier();
217 RCP<const mytest_tcrsGraph_t> const_tpetra_graph = rcp_const_cast<
const mytest_tcrsGraph_t>(serial_tpetra_graph);
220 int rank = global_tcomm->getRank();
222 int numParts = 0;
int coordDim = 0;
226 FILE *f2 = fopen(input_binary_coordinate,
"rb");
227 fread((
void *)&numParts,
sizeof(
int),1,f2);
228 fread((
void *)&coordDim,
sizeof(
int),1,f2);
232 for(
int i = 0; i < coordDim; ++i){
234 fread((
void *) partCenters[i],
sizeof(
double),numParts, f2);
239 global_tcomm->broadcast(0,
sizeof(
test_lno_t), (
char *) &numParts);
240 global_tcomm->broadcast(0,
sizeof(
test_lno_t), (
char *) &coordDim);
242 if (numParts!= myTasks){
243 throw "myTasks is different than numParts";
247 for(
int i = 0; i < coordDim; ++i){
252 for(
int i = 0; i < coordDim; ++i){
253 global_tcomm->broadcast(0,
sizeof(
test_scalar_t)* (numParts), (
char *) partCenters[i]);
257 RCP <Zoltan2::XpetraMultiVectorAdapter<mytest_tMVector_t> > serial_adapter =
create_multi_vector_adapter(serial_map, coordDim, partCenters, myTasks);
258 ia->setCoordinateInput(serial_adapter.getRawPtr());
260 ia->setEdgeWeights(&(task_communication_adjw_[0]), 1, 0);
272 global_tcomm->barrier();
294 Teuchos::ArrayView< const test_gno_t> gids = serial_map->getLocalElementList();
296 ArrayRCP<int> initial_part_ids(myTasks);
298 initial_part_ids[i] = gids[i];
300 single_phase_mapping_solution.
setParts(initial_part_ids);
308 ia.getRawPtr(), &serial_problemParams, global_tcomm, &single_phase_mapping_solution, &transformed_machine);
313 serial_map_problem.
solve(
true);
319 timer->printAndResetToZero();
327 RCP<quality_t> metricObject_3 = rcp(
328 new quality_t(ia.getRawPtr(),&serial_problemParams,serial_comm,msoln3, serial_map_problem.getMachine().getRawPtr()));
330 if (global_tcomm->getRank() == 0){
331 std::cout <<
"METRICS FOR THE SERIAL CASE - ONE PHASE MAPPING - EACH ELEMENT IS ASSUMED TO BE IN UNIQUE PART AT THE BEGINNING" << std::endl;
332 metricObject_3->printMetrics(std::cout);
334 if (machine_optimization_level > 0){
336 Teuchos::ParameterList serial_problemParams_2;
337 serial_problemParams_2.set(
"Input_RCA_Machine_Coords", input_machine_file);
341 RCP<quality_t> metricObject_4 = rcp(
342 new quality_t(ia.getRawPtr(),&serial_problemParams_2,serial_comm,msoln3, &actual_machine));
344 if (global_tcomm->getRank() == 0){
345 std::cout <<
"METRICS FOR THE SERIAL CASE - ONE PHASE MAPPING - EACH ELEMENT IS ASSUMED TO BE IN UNIQUE PART AT THE BEGINNING" << std::endl;
346 metricObject_4->printMetrics(std::cout);
350 if (visualize_mapping && global_tcomm->getRank() == 0){
352 Teuchos::ParameterList serial_problemParams_2;
353 serial_problemParams_2.set(
"Input_RCA_Machine_Coords", input_machine_file);
357 Zoltan2::visualize_mapping<zscalar_t, int> (0, actual_machine.
getMachineDim(), actual_machine.
getNumRanks(), coords,
358 int (task_communication_xadj_.size())-1, &(task_communication_xadj_[0]), &(task_communication_adj_[0]), msoln3->getPartListView());
364 int main(
int narg,
char *arg[]){
366 Tpetra::ScopeGuard tscope(&narg, &arg);
367 Teuchos::RCP<const Teuchos::Comm<int> > global_tcomm=Tpetra::getDefaultComm();
369 char *input_binary_graph = NULL;
370 char *input_binary_coordinate = NULL;
371 char *input_machine_file = NULL;
372 int machine_optimization_level = 10;
373 bool divide_prime_first =
false;
374 int rank_per_node = 1;
375 int reduce_best_mapping = 1;
377 for (
int i = 1 ; i < narg ; ++i ) {
378 if ( 0 == strcasecmp( arg[i] ,
"BG" ) ) {
380 input_binary_graph = arg[++i];
382 else if ( 0 == strcasecmp( arg[i] ,
"BC" ) ) {
383 input_binary_coordinate = arg[++i];
385 else if ( 0 == strcasecmp( arg[i] ,
"MF" ) ) {
387 input_machine_file = arg[++i];
389 else if ( 0 == strcasecmp( arg[i] ,
"OL" ) ) {
390 machine_optimization_level = atoi( arg[++i] );
392 else if ( 0 == strcasecmp( arg[i] ,
"DP" ) ) {
393 if (atoi( arg[++i] )){
394 divide_prime_first =
true;
397 else if ( 0 == strcasecmp( arg[i] ,
"RPN" ) ) {
398 rank_per_node = atoi( arg[++i] );
400 else if ( 0 == strcasecmp( arg[i] ,
"VM" ) ) {
401 visualize_mapping =
true;
403 else if ( 0 == strcasecmp( arg[i] ,
"RBM" ) ) {
404 reduce_best_mapping = atoi( arg[++i] );
407 std::cerr <<
"Unrecognized command line argument #" << i <<
": " << arg[i] << std::endl ;
416 machine_optimization_level, divide_prime_first, rank_per_node, visualize_mapping, reduce_best_mapping);
420 part_t my_parts = 0, *my_result_parts;
423 std::cout <<
"me:" << global_tcomm->getRank() <<
" my_parts:" << my_parts <<
" myTasks:" << myTasks << std::endl;
424 if (global_tcomm->getRank() == 0) {
428 FILE *f2 = fopen(
"plot.gnuplot",
"w");
429 for (
int i = 0; i< global_tcomm->getSize(); ++i){
431 sprintf(str,
"coords%d.txt", i);
433 fprintf(f2,
"splot \"%s\"\n", str);
436 fprintf(f2,
"replot \"%s\"\n", str);
439 fprintf(f2,
"pause-1\n");
443 int myrank = global_tcomm->getRank();
444 sprintf(str,
"coords%d.txt", myrank);
445 FILE *coord_files = fopen(str,
"w");
448 for (
int j = 0; j < my_parts; ++j){
449 int findex = my_result_parts[j];
450 std::cout <<
"findex " << findex << std::endl;
451 fprintf(coord_files,
"%lf %lf %lf\n", partCenters[0][findex], partCenters[1][findex], partCenters[2][findex]);
457 if (global_tcomm->getRank() == 0){
458 std::cout <<
"PASS" << std::endl;
461 catch(std::string &s){
462 std::cerr << s << std::endl;
466 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.