Στο gist αυτό θα αναρτηθούν λύσεις και υποδείξεις κατά τη διεξαγωγή του εργαστηρίου.
Last active
May 11, 2021 15:56
-
-
Save mixstef/836f23c5e68f2f2eff53796ebc9d801c to your computer and use it in GitHub Desktop.
Ατζέντα εργαστηρίου Παράλληλου Προγραμματισμού 11/5/2021
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> | |
| #define N 10 | |
| # define R 2 | |
| // Simple demo of parallel for | |
| // compile with: | |
| // gcc -O2 -Wall -fopenmp omp-separate-for-example.c -o omp-separate-for-example | |
| int main() { | |
| #pragma omp parallel | |
| { | |
| for (int j=0;j<R;j++) { | |
| #pragma omp for nowait | |
| for (int i=0;i<N;i++) { | |
| printf("Thread %d working on element %d pass=%d\n",omp_get_thread_num(),i,j); | |
| } | |
| } | |
| } | |
| return 0; | |
| } | |
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
| // Simple sum-reduction example - no special OpenMP reduction is used. | |
| // Compile with: gcc -O2 -Wall -fopenmp sum-reduction-omp.c -o sum-reduction-omp -DN=10000000 | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <omp.h> | |
| int main() { | |
| double *a; | |
| // allocate array | |
| if ((a = (double *)malloc(N*sizeof(double)))==NULL) { | |
| printf("Allocation failed!\n"); | |
| exit(1); | |
| } | |
| // init array to 1..N | |
| for (int i=0;i<N;i++) { | |
| a[i] = i+1; | |
| } | |
| // reduce array to sum | |
| double sum = 0; | |
| #pragma omp parallel shared(sum) | |
| { | |
| double mypart = 0.0; // private by default | |
| #pragma omp for | |
| for (int i=0;i<N;i++) { | |
| mypart += a[i]; | |
| } // implicit barrier here | |
| #pragma omp critical | |
| { | |
| sum += mypart; | |
| } | |
| } | |
| // check result | |
| double result = ((double)N*(N+1))/2; | |
| if (sum!=result) { | |
| printf("Reduction error!\n"); | |
| } | |
| // free array | |
| free(a); | |
| return 0; | |
| } |
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
| // Simple sum-reduction example - OpenMP reduction is used. | |
| // Compile with: gcc -O2 -Wall -fopenmp sum-reduction-omp2.c -o sum-reduction-omp2 -DN=10000000 | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <omp.h> | |
| int main() { | |
| double *a; | |
| // allocate array | |
| if ((a = (double *)malloc(N*sizeof(double)))==NULL) { | |
| printf("Allocation failed!\n"); | |
| exit(1); | |
| } | |
| // init array to 1..N | |
| for (int i=0;i<N;i++) { | |
| a[i] = i+1; | |
| } | |
| // reduce array to sum | |
| double sum = 0; | |
| #pragma omp parallel for reduction(+:sum) | |
| for (int i=0;i<N;i++) { | |
| sum += a[i]; | |
| } | |
| // check result | |
| double result = ((double)N*(N+1))/2; | |
| if (sum!=result) { | |
| printf("Reduction error!\n"); | |
| } | |
| // free array | |
| free(a); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment