annotate twice.c @ 1:3b9c253ce087 default tip

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