nex036ara

nqueens_annotated

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