Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. /* PLEASE NOTE THIS CODE MAY NOT BE COMPLETELY *
  2. * VALID */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <omp.h>
  8.  
  9. #define GAME_MIN 0
  10. #define GAME_MAX 25
  11. #define ARRAY_LENGTH 10
  12. #define CHUNKSIZE 2
  13.  
  14. int main() {
  15. // Declare some useful OMP variables
  16. int num_threads = omp_get_num_threads();
  17. int chunk = CHUNKSIZE;
  18. // Declare game relevant variables
  19. int r[ARRAY_LENGTH];
  20. int game_range = GAME_MAX - GAME_MIN + 1;
  21.  
  22. // Initialise a global seed
  23. int global_seed = (int) time(NULL);
  24.  
  25. // Fork into the parallel region lazily
  26. // i.e. can't be bothered declaring who is
  27. // shared and who is private
  28. #pragma omp parallel
  29. {
  30. // Get the local thread ID
  31. int tid = omp_get_thread_num();
  32. // Use the local thread ID to create a unique
  33. // seed for each thread
  34. int local_seed = global_seed^tid;
  35. // Use the OMP threads to parallelise the loop into
  36. // chunks of size chunk
  37. #pragma omp for schedule(dynamic, chunk)
  38. for(int i=0; i<ARRAY_LENGTH; i++) {
  39. // Generate random number between GAME_MIN
  40. // and GAME_MAX
  41. r[i] = GAME_MIN + rand_r(&local_seed) / (RAND_MAX / game_range + 1.);
  42. }
  43. }
  44.  
  45. // Count the number of times each number appears
  46. int num_count[game_range];
  47. #pragma omp for
  48. for(int i=GAME_MIN; i<GAME_MAX; i++) {
  49. num_count[i] = 0;
  50. for(int j=0; j<ARRAY_LENGTH; j++) {
  51. if(r[j] == i) num_count[i]++;
  52. }
  53. }
  54.  
  55. // Count the number of times a number has been repeated
  56. int num_wins = 0;
  57. for(int i=0; i<game_range; i++) {
  58. if(num_count[i]>1) num_wins++;
  59. }
  60.  
  61. // Print the resultant array and the number of wins
  62. printf("\n");
  63. #pragma omp for
  64. for(int i=0; i<ARRAY_LENGTH; i++) printf("%d ", r[i]);
  65. printf("\n");
  66.  
  67. printf("Congratulations you have won %d time(s)!\n", num_wins);
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement