void WorkerSlave::handleFinish() {
    cout << myRank << ": handle Finish from " << previousprocesor << endl;
    int cokoliv;
    running = false;
    MPI_Recv(&cokoliv, 1, MPI_INT, previousprocesor, MSG_FINISH, MPI_COMM_WORLD, &status);
    sendSolution();
    sendFinish();
}
Пример #2
0
int ParallelAlgorithm::run()
{
    	int position = 0;
    	long price = 0;
    	int x = 0, y = 0;
    	int tileType = 0;
    	int lastInsertedTile = 0;
    	bool backtrack = false;
    	int tmpValue = 0;
	int plHeight  = playground->getHeight();
	int plWidth   = playground->getWidth();
	int counter   = 0;
	
    	//zasobnik bude prazdny pouze v pripade, ze nevlozim ani dlazdici
	//ani volnou diru kontrola maximalni hloubky stromu, kdy se muzu vratit
    	while(tileType < COUNT_OF_TILES || !stck.isEmpty()) 
	{ 		
        	if(!stck.isEmpty()) 
		{ 	//zasobnik: pozice, typDlazdice
            	x = stck.getTop() / playground->getWidth(); //radek z pozice na zasobniku
            	y = stck.getTop() % playground->getWidth(); //sloupec z pozice na zasobniku

			#ifdef DEBUG
	            	std::cout << "Top Of Stack: " << stck.getTop() << " [" << x << "," << y << "]. ";
			#endif

            	position = stck.pop(); 			// vyjimam o jednu dlazdici -> navrat zpet o jedno pole
            	lastInsertedTile = stck.pop(); 	//typ dlazdice vkladany naposledy
			
			#ifdef DEBUG
            		std::cout << "Last Inserted Tile : " << lastInsertedTile << std::endl;
			#endif

            	tileType = lastInsertedTile + 1;
            	playground->eraseTile(x, y, lastInsertedTile);

			#ifdef DEBUD
            		std::cout << "ERASED on position: " << position << ", tileType: " << lastInsertedTile << std::endl;
			#endif

            	backtrack = false;
			//std::cout << "BACKTRACK FALSE HIGH" << std::endl;
        	} 
		else if (backtrack) 
		{ 	//zasobnik je prazdny a dalsi dlazdice se nevejde kvuli vysce
            	break;
        	}

        	while (!backtrack && tileType < COUNT_OF_TILES) 
		{			
            	for (; x < plHeight; ++x) 
			{
                		for (; y < plWidth; ++y) 
				{
					if(startFlag && counter >= countOfProcesses)
					{
						start();
						startFlag = false;			
						counter = 0;
					}
					else if(!startFlag && counter >= CHECK_MSG_AMOUNT)
					{
						int result = communicationLoop();
						if(result == 2)
							return 2;

						counter = 0;
					}				
					
                    		position = x * plWidth + y; // radek * pocet sloupcu + pocet sloupec ==  pozice

                    		if ((tmpValue = playground->insertTile(x, y, tileType)) == 0) 
					{ 	//podarilo se vlozit dlazdici
						#ifdef DEBUG
                        			std::cout << "INSERTED on position: " << position << ", tileType: " << tileType << std::endl;
						#endif

                        		stck.push(tileType);
                        		stck.push(position);

                        		backtrack = false;
						//std::cout << "BACKTRACK FALSE" << std::endl;
						tileType = 0; 	//kdyz vlozim dlazdici, tak jde do hloubky 
									//a snazim se vkladat opet vsechny dalsi dlazdice
                    		}
					else if (tmpValue == -4) 
					{ 	//dlazdice se nevejde na vysku
                        		backtrack = true;
						//std::cout << "BACKTRACK TRUE" << std::endl;
                        		tmpValue = -5;
                        		break; 
                    		}
					else if (playground->getValueOnSquare(x, y) == -1) 
					{ 	//na pozici neni dira ani jina dlazdice
                        		tmpValue = -5;
                        		break;
                    		}
			
					counter++;  	

                		}

                		if (tmpValue == -5) 
                    		break;
                 		else 
                    		y = 0; // pokracovani na dalsim radku od zacatku
            	}

            	++tileType;

            	if (!stck.isEmpty()) 
			{
                		x = stck.getTop() / plHeight; 
                		y = (stck.getTop() % plHeight) + 1; //dalsi pozice

                		position = stck.getTop() + 1; 

				// osetreni na novy radek
               		if (y == plWidth) 
				{ 
                    		y = 0;
                    		++x;
                		}
            	}
			else 
			{
                		x = 0;
                		y = 0;
            	}
        	}
        
        	if ((price = playground->computePrice(stck)) > playground->getPrice()) 
		{
			std::cout << "ProcessID: " << myRank << " price: " << price << std::endl;

			playground->setPrice(price);
			playground->saveSolution(stck);

			if(myRank != 0)
				sendSolution();

			playground->printSolution();	
            }
	}
	return 0;
}