Last active
February 20, 2023 16:37
-
-
Save clickCA/9932570827e4490c608f382386327cc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <omp.h> | |
| #include <time.h> | |
| int main(void) | |
| { | |
| int a[100000]; | |
| double start_time, end_time; | |
| size_t i; | |
| int num_threads = omp_get_num_procs() * 2; | |
| // Run the original code | |
| start_time = (double)clock() / CLOCKS_PER_SEC; | |
| for (i = 0; i < 100000; i++) { | |
| a[i] = 2*i+i; | |
| printf("a[%d],%d\n", i, a[i]); | |
| } | |
| end_time = (double)clock() / CLOCKS_PER_SEC; | |
| double orig_time = end_time - start_time; | |
| // Run the modified code with increased number of threads | |
| start_time = (double)clock() / CLOCKS_PER_SEC; | |
| #pragma omp parallel for num_threads(num_threads) | |
| for (i = 0; i < 100000; i++) { | |
| a[i] = 2*i+i; | |
| printf("a[%d],%d\n", i, a[i]); | |
| } | |
| end_time = (double)clock() / CLOCKS_PER_SEC; | |
| double mod_time = end_time - start_time; | |
| printf("Original code: time=%.6f seconds\n", orig_time); | |
| printf("Modified code: time=%.6f seconds\n", mod_time); | |
| // Check for errors | |
| int errors = 0; | |
| for (i = 0; i < 100000; i++) { | |
| if (a[i] != 2*i+i) { | |
| printf("Error at index %d: expected %d, got %d\n", i, 2*i+i, a[i]); | |
| errors++; | |
| } | |
| } | |
| if (errors == 0) { | |
| printf("No errors detected.\n"); | |
| } else { | |
| printf("%d errors detected.\n", errors); | |
| } | |
| // Calculate the speedup | |
| double speedup = orig_time / mod_time; | |
| printf("Speedup: %.2f\n", speedup); | |
| printf("Program completed successfully.\n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment