示例#1
0
/**
 * Players initialization
 * returns 1 in case of error
 */
int init(int boardSize, int turnNumber, int player, int x1, int y1, int x2, int y2) {
	
	// checking which init is it
	if (liczbaInitow > 0) return 1;
		 
	// distatnce between kings in the maximum norm at least 8
	if (abs(x1 - x2) < 8 && abs(y1 - y2) < 8) return 1;
	
	// board size > 8
	if (boardSize <= 8) return 1;
	
	// player number 1 or 2
	if (player != 1 && player != 2) return 1;
	
	// turn number positive
	if (turnNumber < 1) return 1;

	// are the initial positions correct
	if(!insideBoard(x1, y1, boardSize)) return 1;
	if(!insideBoard(x1 + 3, y1, boardSize)) return 1;
	if(!insideBoard(x2, y2, boardSize)) return 1;
	if(!insideBoard(x2 + 3, y2, boardSize)) return 1;
	
	
	globalBoardSize = boardSize;
	gameTurnNumber = turnNumber;
	
	actualPlayer = 1;	
	firstDistribution(x1, y1);
		
	actualPlayer = 2;
	firstDistribution(x2, y2);
	
	actualPlayer = 1;
	
	liczbaInitow++;
	
	return 0;
}
void Solver::solve(vector<Move>& _solution) {
    lowerBound = initBoard->getLowerBound(targetTower);
    bestSolutionDepth = maxDepth;

    if (myRank != MASTER_RANK) {
        //cekam az mi prijde prace
        //prvni rozdeleni se udela automaticky bez zadani
        //cekam dokud message neprijde melo by prijit prvni deleni prace
        //momentalne to prvni spocte takzee prijde pesek
        MPI_Status s;
        MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &s);

        processMessages();

    } else {
        // Hotovo, koncim.
        if (initBoard->isTowerComplete(targetTower)) {
            broadcast(MESSAGE_FINISHED);
            return;
        }
        Move firstMove(0, 0, 0);
        SpaceItem* firstSpaceItem = new SpaceItem(*initBoard, firstMove);
        space.push_back(firstSpaceItem);
        expandTop();
        actualDepth = 1;
        firstWorkDistribution = true;
        space.erase(space.begin());
    }
    int counter = 0;

    while (!finished) {
        if (!space.empty()) {
            proccessTop(_solution, actualDepth);
        } else {

            requestData();
            sendToken();
        }

        if (counter == 150) {//natipovat idealni honotu
            if ((myRank == MASTER_RANK) && firstWorkDistribution) {
                firstDistribution();
                counter = 0;
            } else {
                counter = 0;
                processMessages();

                if (finished) {
                    char buffer[BUFFER_SIZE];
                    int position = 0;

                    serializeSolution(buffer, position, _solution);

                    char result[BUFFER_SIZE];

                    MPI_Op op;
                    MPI_Op_create((MPI_User_function*) compare, 0, &op);
                  //  cout<<_solution.size()<<endl;
                    MPI_Reduce(&buffer, &result, BUFFER_SIZE, MPI_PACKED, op, 0, MPI_COMM_WORLD);
                    int workRequestsRes;
                    MPI_Reduce (&workRequestsInt, &workRequestsRes, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
                    int failedWorkRequestsRes;
                    MPI_Reduce (&failedWorkRequests, &failedWorkRequestsRes, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
                    if (myRank == MASTER_RANK) {
                        vector<Move> solution;
                        int position = 0;
                        deserializeSolution(result, position, solution);

                        cout << "MASTER - ZACATEK RESENI" << endl;
                        for (vector<Move>::const_iterator it = solution.begin(); it < solution.end(); ++it) {
                            cout << *it << endl;
                        }
                        cout << "Solution depth: " << solution.size() << endl;
                        cout<< "Work Requests: "<<workRequestsRes<<endl;
                        cout<<"Denied Work Requests: "<<failedWorkRequestsRes<<endl;
                        
                        cout << "MASTER - KONEC RESENI" << endl;
                    }
                }
            }
        }
        counter++;
    }

    //   cout << "PushCount process " << myRank << ": " << pushCount << endl;
    //   cout << "Solution: " << _solution.size() << " - process " << myRank << endl;
    //   cout << "mam jeste: " << space.size() << " stavu" << endl;
}