Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PLEASE NOTE THIS CODE MAY NOT BE COMPLETELY *
- * VALID */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <omp.h>
- #define GAME_MIN 0
- #define GAME_MAX 25
- #define ARRAY_LENGTH 10
- #define CHUNKSIZE 2
- int main() {
- // Declare some useful OMP variables
- int num_threads = omp_get_num_threads();
- int chunk = CHUNKSIZE;
- // Declare game relevant variables
- int r[ARRAY_LENGTH];
- int game_range = GAME_MAX - GAME_MIN + 1;
- // Initialise a global seed
- int global_seed = (int) time(NULL);
- // Fork into the parallel region lazily
- // i.e. can't be bothered declaring who is
- // shared and who is private
- #pragma omp parallel
- {
- // Get the local thread ID
- int tid = omp_get_thread_num();
- // Use the local thread ID to create a unique
- // seed for each thread
- int local_seed = global_seed^tid;
- // Use the OMP threads to parallelise the loop into
- // chunks of size chunk
- #pragma omp for schedule(dynamic, chunk)
- for(int i=0; i<ARRAY_LENGTH; i++) {
- // Generate random number between GAME_MIN
- // and GAME_MAX
- r[i] = GAME_MIN + rand_r(&local_seed) / (RAND_MAX / game_range + 1.);
- }
- }
- // Count the number of times each number appears
- int num_count[game_range];
- #pragma omp for
- for(int i=GAME_MIN; i<GAME_MAX; i++) {
- num_count[i] = 0;
- for(int j=0; j<ARRAY_LENGTH; j++) {
- if(r[j] == i) num_count[i]++;
- }
- }
- // Count the number of times a number has been repeated
- int num_wins = 0;
- for(int i=0; i<game_range; i++) {
- if(num_count[i]>1) num_wins++;
- }
- // Print the resultant array and the number of wins
- printf("\n");
- #pragma omp for
- for(int i=0; i<ARRAY_LENGTH; i++) printf("%d ", r[i]);
- printf("\n");
- printf("Congratulations you have won %d time(s)!\n", num_wins);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement