Advertisement
nex036ara

nqueens_serial

Mar 24th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.04 KB | None | 0 0
  1.  
  2. // SAMPLE SOURCE CODE - SUBJECT TO THE TERMS OF END-USER LICENSE AGREEMENT FOR
  3. // INTEL(R) PARALLEL ADVISOR 2011.
  4. //
  5. // Copyright 2009-2010 Intel Corporation
  6. //
  7. // THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
  8. // NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  9. // PURPOSE, NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS.
  10. //
  11. // ========================================================================
  12.  
  13. // [DESCRIPTION]
  14. // Solve the nqueens problem  - how many positions of queens can fit on a chess board
  15. // of a given size without attacking each other.
  16. // This is the serial version used to find a candidate hotspot function to parallelize.
  17. //
  18. // [BUILD]
  19. // Use a Release configuration to ensure you find a hotspot representative of a final production build.
  20. //
  21. // [RUN]
  22. // To set the board size in Visual Studio, right click on the project,
  23. // select Properies > Configuration Properties > General > Debugging.  Set
  24. // Command Arguments to the desired value.  13 has been set as the default.
  25. //
  26. // [EXPECTED OUTPUT]
  27. //
  28. // Board Size   Number of Solutions
  29. //     4                2
  30. //     5               10
  31. //     6                4
  32. //     7               40
  33. //     8               92
  34. //     9              352
  35. //    10              724
  36. //    11             2680
  37. //    12            14200
  38. //    13            73712
  39. //    14           365596
  40. //    15          2279184
  41.  
  42. #include <iostream>
  43. #include <windows.h>
  44. #include <mmsystem.h>
  45.  
  46. //ADVISOR COMMENT: Uncomment the #include <advisor-annotate.h> line after you've added the annotation header to the project to allow you to use Advisor annotations
  47.  
  48. //#include <advisor-annotate.h>
  49.  
  50. using namespace std;
  51.  
  52. int nrOfSolutions=0;  //keeps track of the number of solutions
  53. int size=0;  // the board-size read from command-line
  54. int correctSolution[16]; //array of the number of correct solutions for each board size
  55.  
  56. /*
  57. Recursive function to find all solutions on a board
  58. represented by the argument "queens", placing the next queen
  59. at location (row, col)
  60.  
  61. On Return: nrOfSolutions has been increased to add solutions for this board
  62.  
  63. */
  64. void setQueen(int queens[], int row, int col) {
  65.     //check all previously placed rows for attacks
  66.     for(int i=0; i<row; i++) {
  67.         // vertical attacks
  68.         if (queens[i]==col) {
  69.             return;
  70.         }
  71.         // diagonal attacks
  72.         if (abs(queens[i]-col) == (row-i) ) {
  73.             return;
  74.         }
  75.     }
  76.     // column is ok, set the queen
  77.     queens[row]=col;
  78.  
  79.     if(row==size-1) {
  80.         nrOfSolutions++;  //Placed final queen, found a solution
  81.     }
  82.     else {
  83.         // try to fill next row
  84.         for(int i=0; i<size; i++) {
  85.             //ADVISOR COMMENT: When surveying the code this recusive call to setQueen appears many times
  86.             //ADVISOR COMMENT: Try looking at the solve() function for a better place to parallelize
  87.             setQueen(queens, row+1, i);
  88.         }
  89.     }
  90. }
  91.  
  92. /*
  93. Function to find all solutions for nQueens problem on size x size chessboard.
  94.  
  95. On Return: nrOfSoultions = number of solutions for size x size chessboard.
  96. */
  97. void solve() {
  98.     int * queens = new int[size]; //array representing queens placed on a chess board.  Index is row position, value is column.
  99.  
  100.     //ADVISOR COMMENT: When surveying this is the top function below main.  This for() loop is a candidate for parallelization
  101.     //ADVISOR COMMENT: Uncomment the four annotations below to model parallelizing the body of this for() loop
  102.     //ADVISOR COMMENT: Don't forget to uncomment the #include <advisor-annotate.h> at the top
  103.  
  104.     //ANNOTATE_SITE_BEGIN(solve);
  105.         for(int i=0; i<size; i++) {
  106.             // try all positions in first row
  107.             //ANNOTATE_TASK_BEGIN(setQueen);
  108.                 setQueen(queens, 0, i);
  109.             //ANNOTATE_TASK_END(setQueen);
  110.         }
  111.     //ANNOTATE_SITE_END(solve);
  112. }
  113.  
  114.  
  115. int main(int argc, char*argv[]) {
  116.     if(argc !=2) {
  117.         cerr << "Usage: nqueens_serial boardSize [default is 13].\n";
  118.         size = 13;
  119.     } else {
  120.         size = atoi(argv[1]);
  121.         if (size < 4 || size > 15) {
  122.             cerr << "Boardsize should be between 4 and 15; setting it to 13. \n" << endl;
  123.             size = 13;
  124.         }
  125.     }
  126.     // Set the expected number of solutions for each board-size for later checking.
  127.     correctSolution[0] = 0;
  128.     correctSolution[1] = 0;
  129.     correctSolution[2] = 0;
  130.     correctSolution[3] = 0;
  131.     correctSolution[4] = 2;
  132.     correctSolution[5] = 10;
  133.     correctSolution[6] = 4;
  134.     correctSolution[7] = 40;
  135.     correctSolution[8] = 92;
  136.     correctSolution[9] = 352;
  137.     correctSolution[10] = 724;
  138.     correctSolution[11] = 2680;
  139.     correctSolution[12] = 14200;
  140.     correctSolution[13] = 73712;
  141.     correctSolution[14] = 365596;
  142.     correctSolution[15] = 2279184;
  143.  
  144.     cout << "Starting nqueens (1_nqueens_serial) solver for size " << size << "...\n";
  145.     DWORD startTime=timeGetTime();
  146.     solve();
  147.     DWORD endTime=timeGetTime();
  148.     cout << "Number of solutions: " << nrOfSolutions << endl;
  149.     if (nrOfSolutions != correctSolution[size])
  150.         cout << "!!Incorrect result!! Number of solutions should be " << correctSolution[size] << endl << endl;
  151.     else
  152.         cout << "Correct result!" << endl;
  153.  
  154.     cout << endl << "Calculations took " << endTime-startTime << "ms.\n";
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement