Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 71 lines (59 sloc) 1.404 kb
8a38be3 @inducer Start work on omp.
inducer authored
1 #include "timing.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 // FIXME STRIP
5 #include <omp.h>
6 #include <math.h>
7
8 int main(int argc, char **argv)
9 {
10 if (argc != 3)
11 {
12 fprintf(stderr, "need two arguments!\n");
13 abort();
14 }
15
16 const long n = atol(argv[1]);
17 double *x = (double *) malloc(sizeof(double) * n);
18 if (!x) { perror("alloc x"); abort(); }
19 double *y = (double *) malloc(sizeof(double) * n);
20 if (!y) { perror("alloc y"); abort(); }
21 double *z = (double *) malloc(sizeof(double) * n);
22 if (!z) { perror("alloc z"); abort(); }
23
24 for (int i = 0; i < n; ++i)
25 {
26 x[i] = i;
27 y[i] = 2*i;
28 }
29
30 const int ntrips = atoi(argv[2]);
31 printf("doing %d trips...\n", ntrips);
32
33 #pragma omp parallel
34 {
35 printf("Hi, I'm thread # %d of %d\n",
36 omp_get_thread_num(),
37 omp_get_num_threads());
38 }
39
fc40978 @inducer Update all demos to new timing code.
inducer authored
40 timestamp_type time1, time2;
41 get_timestamp(&time1);
8a38be3 @inducer Start work on omp.
inducer authored
42
43 for (int trip = 0; trip < ntrips; ++trip)
44 {
3cfa58d @inducer Finish OpenMP demo.
inducer authored
45 #pragma omp parallel for default(none)
8a38be3 @inducer Start work on omp.
inducer authored
46 for (int i = 0; i < n; ++i)
47 {
48 double a = x[i];
49 double b = y[i];
50
3cfa58d @inducer Finish OpenMP demo.
inducer authored
51 /*
52 for (int j = 0; j < 20; ++j)
8a38be3 @inducer Start work on omp.
inducer authored
53 {
54 a = sin(a) + b;
55 b = cos(b) + a;
56 }
3cfa58d @inducer Finish OpenMP demo.
inducer authored
57 */
8a38be3 @inducer Start work on omp.
inducer authored
58
59 z[i] = a+b;
60 }
61 }
62
fc40978 @inducer Update all demos to new timing code.
inducer authored
63 get_timestamp(&time2);
64 double elapsed = timestamp_diff_in_seconds(time1,time2)/ntrips;
8a38be3 @inducer Start work on omp.
inducer authored
65 printf("%f s\n", elapsed);
66 printf("%f GB/s\n",
67 3*n*sizeof(double)/1e9/elapsed);
68
cd23a4a @inducer Add MPI example. Add return 0 to places.
inducer authored
69 return 0;
70 }
Something went wrong with that request. Please try again.