Mercurial > hg > Members > innparusu > openMP_examples
comparison twice.c @ 0:b8c472c04dc9
Add twice.c
author | innparusu |
---|---|
date | Sat, 03 Feb 2018 23:34:56 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b8c472c04dc9 |
---|---|
1 #include <stdio.h> | |
2 #include <string.h> | |
3 #include <stdlib.h> | |
4 #include <omp.h> | |
5 #include <sys/time.h> | |
6 | |
7 int length = 1024; | |
8 int cpu_num = 1; | |
9 void init(int argc, const char* argv[]) { | |
10 for (int i = 1; argv[i]; ++i) { | |
11 if (strcmp(argv[i], "-cpu") == 0) | |
12 cpu_num = (int)atoi(argv[i+1]); | |
13 else if (strcmp(argv[i], "-l") == 0) | |
14 length = (int)atoi(argv[i+1]); | |
15 } | |
16 } | |
17 | |
18 int main(int argc, const char* argv[]) | |
19 { | |
20 int n; | |
21 init(argc, argv); | |
22 omp_set_num_threads(cpu_num); | |
23 int *a; | |
24 a = (int *) malloc(sizeof(int) * length); | |
25 for (int i = 0; i < length; i++) | |
26 { | |
27 a[i] = i; | |
28 } | |
29 | |
30 struct timeval tv; | |
31 gettimeofday(&tv, NULL); | |
32 double now = tv.tv_sec + (double)tv.tv_usec*1e-6; | |
33 | |
34 #pragma omp parallel for | |
35 for(int i = 0; i < length; i++) | |
36 { | |
37 a[i] = a[i] * 2; | |
38 } | |
39 | |
40 gettimeofday(&tv, NULL); | |
41 printf("%0.6f\n", (tv.tv_sec+(double)tv.tv_usec*1e-6) - now); | |
42 | |
43 for(int i = 0; i < length; i++) | |
44 { | |
45 if(a[i] != i*2) { | |
46 printf("fail\n"); | |
47 } | |
48 } | |
49 | |
50 return 0; | |
51 } |