Initial commit to Gerrit
[profile/ivi/orc.git] / testsuite / memcpy_speed.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
6
7 #define ORC_ENABLE_UNSTABLE_API
8
9 #include <orc/orc.h>
10 #include <orc-test/orctest.h>
11 #include <orc-test/orcprofile.h>
12
13
14 #define ALIGN(ptr,n) ((void *)((unsigned long)(ptr) & (~(unsigned long)(n-1))))
15
16 int hot_src = TRUE;
17 int hot_dest = TRUE;
18 int flush_cache = FALSE;
19
20
21 void
22 touch (unsigned char *ptr, int n)
23 {
24   static int sum;
25   int i;
26   for(i=0;i<n;i++){
27     sum += ptr[i];
28   }
29 }
30
31 int
32 main(int argc, char *argv[])
33 {
34   char *s, *d;
35   orc_uint8 *src, *dest;
36   OrcProfile prof;
37   OrcProfile prof_libc;
38   double ave, std;
39   double ave_libc, std_libc;
40   double null;
41   int i,j;
42   double cpufreq;
43   int unalign;
44   OrcProgram *p;
45   int level1, level2, level3;
46   int max;
47   //const uint8_t zero = 0;
48
49   orc_init ();
50
51   //cpufreq = 2333e6;
52   cpufreq = 1;
53
54   if (argc > 1) {
55     unalign = strtoul (argv[1], NULL, 0);
56   } else {
57     unalign = 0;
58   }
59
60   s = malloc(1024*1024*64+1024);
61   d = malloc(1024*1024*64+1024);
62   src = ORC_PTR_OFFSET(ALIGN(s,128),unalign);
63   dest = ALIGN(d,128);
64
65   orc_profile_init (&prof);
66   for(j=0;j<10;j++){
67     orc_profile_start(&prof);
68     orc_profile_stop(&prof);
69   }
70   orc_profile_get_ave_std (&prof, &null, &std);
71   
72   {
73     OrcCompileResult result;
74
75     p = orc_program_new ();
76     orc_program_set_name (p, "orc_memcpy");
77     //orc_program_set_name (p, "orc_memset");
78     orc_program_add_destination (p, 1, "d1");
79     orc_program_add_source (p, 1, "s1");
80     //orc_program_add_parameter (p, 1, "p1");
81
82     orc_program_append (p, "copyb", ORC_VAR_D1, ORC_VAR_S1, ORC_VAR_D1);
83
84     result = orc_program_compile (p);
85   }
86
87 #ifndef M_LN2
88 #define M_LN2 0.69314718055994530942
89 #endif
90   orc_get_data_cache_sizes (&level1, &level2, &level3);
91   if (level3 > 0) {
92     max = (log(level3)/M_LN2 - 6.0) * 10 + 20;
93   } else if (level3 > 0) {
94     max = (log(level2)/M_LN2 - 6.0) * 10 + 20;
95   } else {
96     max = 140;
97   }
98
99   for(i=0;i<max;i++){
100     double x = i*0.1 + 6.0;
101     int size = pow(2.0, x);
102
103     if (flush_cache) {
104       touch (src, (1<<18));
105     }
106     if (hot_src) {
107       touch (src, size);
108     }
109     if (hot_dest) {
110       touch (dest, size);
111     }
112
113     orc_profile_init (&prof);
114     for(j=0;j<10;j++){
115       OrcExecutor _ex, *ex = &_ex;
116       void (*func) (OrcExecutor *);
117
118       orc_profile_start(&prof);
119       //orc_memcpy (dest, src, size);
120       ex->program = p;
121       ex->n = size;
122       ex->arrays[ORC_VAR_D1] = dest;
123       ex->arrays[ORC_VAR_S1] = (void *)src;
124
125       func = p->code_exec;
126       func (ex);
127
128       orc_profile_stop(&prof);
129       if (flush_cache) {
130         touch (src, (1<<18));
131       }
132       if (hot_src) {
133         touch (src, size);
134       }
135       if (hot_dest) {
136         touch (dest, size);
137       }
138     }
139
140     orc_profile_init (&prof_libc);
141     for(j=0;j<10;j++){
142       orc_profile_start(&prof_libc);
143       memcpy (dest, src, size);
144       orc_profile_stop(&prof_libc);
145       if (flush_cache) {
146         touch (src, (1<<18));
147       }
148       if (hot_src) {
149         touch (src, size);
150       }
151       if (hot_dest) {
152         touch (dest, size);
153       }
154     }
155
156     orc_profile_get_ave_std (&prof, &ave, &std);
157     orc_profile_get_ave_std (&prof_libc, &ave_libc, &std_libc);
158
159     ave -= null + 65 + 20;
160     ave_libc -= null + 40;
161
162     //printf("%d: %10.4g %10.4g %10.4g %10.4g (libc %10.4g)\n", i, ave, std,
163     //    ave/(1<<i), cpufreq/(ave/(1<<i)),
164     //    cpufreq/(ave_libc/(1<<i)));
165     printf("%g %10.4g %10.4g\n", x,
166         cpufreq/(ave/size), cpufreq/(ave_libc/size));
167     //printf("%g %10.4g %10.4g\n", x,
168     //    32*(ave/(size)), 32*(ave_libc/(size)));
169     fflush (stdout);
170   }
171
172   return 0;
173 }
174