-
-
Save ashinkarov/7514226 to your computer and use it in GitHub Desktop.
OpenMP trivial benchmark
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 <stdlib.h> | |
| #include <stdint.h> | |
| #include <stdio.h> | |
| #include <time.h> | |
| #define MILLION 1000000 | |
| #define N 50000000 | |
| static inline int64_t | |
| time_elapsed (struct timespec a, struct timespec b) | |
| { | |
| return ((int64_t)a.tv_sec - b.tv_sec) * MILLION | |
| + ((int64_t)a.tv_nsec - b.tv_nsec) / 1000LL; | |
| } | |
| static inline struct timespec | |
| time_gettime () | |
| { | |
| struct timespec s; | |
| clock_gettime(CLOCK_REALTIME, &s); | |
| return s; | |
| } | |
| int main (int argc, char *argv[]) | |
| { | |
| double *a, *b, *c; | |
| struct timespec start, finish; | |
| a = malloc (N * sizeof (double)); | |
| b = malloc (N * sizeof (double)); | |
| c = malloc (N * sizeof (double)); | |
| for (size_t i = 0; i < N; i++) | |
| b[i] = (a[i] = i)+ 1; | |
| start = time_gettime (); | |
| #pragma omp parallel for shared(a,b,c) | |
| for (size_t i = 0; i < N; i++) | |
| c[i] = a[i] * b[i] + 1; | |
| finish = time_gettime (); | |
| printf ("time passed: %zd sec*10^-6\n", time_elapsed (finish, start)); | |
| int t = (int)c[argc]; | |
| free (a); | |
| free (b); | |
| free (c); | |
| return t; | |
| } |
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
| Here is a trivial benchmark that can be used to determine that your OpenMP is working: | |
| You compile it with `gcc -Ofast -fopenmp -lrt -std=c99 -D_GNU_SOURCE a.c` | |
| And here are the runtimes on Intel Core I3, 2 cores (4 threads). | |
| /tmp|=> export OMP_NUM_THREADS=1 | |
| /tmp|=> ./a.out | |
| time passed: 204624 sec*10^-6 | |
| /tmp|=> export OMP_NUM_THREADS=2 | |
| /tmp|=> ./a.out | |
| time passed: 124821 sec*10^-6 | |
| /tmp|=> export OMP_NUM_THREADS=3 | |
| /tmp|=> ./a.out | |
| time passed: 116995 sec*10^-6 | |
| /tmp|=> export OMP_NUM_THREADS=4 | |
| /tmp|=> ./a.out | |
| time passed: 103981 sec*10^-6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment