summaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/prefetchtest.cpp
blob: 38d3eed837cb11f834831c3e6bd4f899939ac1d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
//************************************************************************
/**
 * FastOS_Prefetch test program.
 *
 * @author  Olaf Birkeland
 * @version $Id$
 */
 /*
 * Creation date    : 2000-12-11
 * Copyright (c)    : 1997-2001 Fast Search & Transfer ASA
 *                    ALL RIGHTS RESERVED
 *************************************************************************/

#include "tests.h"
#include <vespa/fastos/time.h>
#include <vespa/fastos/prefetch.h>

class PrefetchTestApp : public BaseTest
{
public:
   virtual ~PrefetchTestApp() {}

   bool PrefetchTest ()
   {
      bool rc = false;
      int j, size, *a;
      int or1, or2;
      FastOS_Time start, stop;
      double timeVal;

      TestHeader("Prefetch Test");

      // 32MB
      size = 32;
      size *= 1024*1024/sizeof(*a);

      if ((a = static_cast<int *>(calloc(size, sizeof(*a)))) != nullptr)
      {
         // Standard loop
         start.SetNow();
         or1 = 1;
         for(j=0; j<size; j++)
            or1 |= a[j];
         stop.SetNow();
         timeVal = stop.MilliSecs() - start.MilliSecs();
         Progress(or1==1, "Result = %d", or1);
         ProgressFloat(true, "%4.3f MB/s (standard loop)",
                       float(size*sizeof(*a)/(1E3*timeVal)));


         // Unrolled loop
         start.SetNow();
         or1 = or2 = 2;
         for(j=0; j<size; j+=8)
         {
            or1 |= a[j+0]|a[j+1]|a[j+2]|a[j+3];
            or2 |= a[j+4]|a[j+5]|a[j+6]|a[j+7];
         }
         or1 |= or2;
         stop.SetNow();
         timeVal = stop.MilliSecs() - start.MilliSecs();
         Progress(or1 == 2, "Result = %d", or1);
         ProgressFloat(true, "%4.3f MB/s (unrolled loop)",
                       float(size*sizeof(*a)/(1E3*timeVal)));


         // Unrolled loop with prefetch
         start.SetNow();
         or1 = or2 = 3;
         for(j=0; j<size; j+=8)
         {
            FastOS_Prefetch::NT(&a[j+32]);
            or1 |= a[j+0]|a[j+1]|a[j+2]|a[j+3];
            or2 |= a[j+4]|a[j+5]|a[j+6]|a[j+7];
         }
         or1 |= or2;
         stop.SetNow();
         timeVal = stop.MilliSecs() - start.MilliSecs();
         Progress(or1 == 3, "Result = %d", or1);
         ProgressFloat(true, "%4.3f MB/s (unrolled loop with prefetch)",
                       float(size*sizeof(*a)/(1E3*timeVal)));

         // Unrolled loop
         start.SetNow();
         or1 = or2 = 4;
         for(j=0; j<size; j+=8)
         {
            or1 |= a[j+0]|a[j+1]|a[j+2]|a[j+3];
            or2 |= a[j+4]|a[j+5]|a[j+6]|a[j+7];
         }
         or1 |= or2;
         stop.SetNow();
         timeVal = stop.MilliSecs() - start.MilliSecs();
         Progress(or1 == 4, "Result = %d", or1);
         ProgressFloat(true, "%4.3f MB/s (unrolled loop)",
                       float(size*sizeof(*a)/(1E3*timeVal)));


         // Standard loop
         start.SetNow();
         or1 = 5;
         for(j=0; j<size; j++)
            or1 |= a[j];
         stop.SetNow();
         timeVal = stop.MilliSecs() - start.MilliSecs();
         Progress(or1 == 5, "Result = %d", or1);
         ProgressFloat(true, "%4.3f MB/s (standard loop)",
                       float(size*sizeof(*a)/(1E3*timeVal)));


         // Unrolled loop with prefetch
         start.SetNow();
         or1 = or2 = 6;
         for(j=0; j<size; j+=8)
         {
            FastOS_Prefetch::NT(&a[j+32]);
            or1 |= a[j+0]|a[j+1]|a[j+2]|a[j+3];
            or2 |= a[j+4]|a[j+5]|a[j+6]|a[j+7];
         }
         or1 |= or2;
         stop.SetNow();
         timeVal = stop.MilliSecs() - start.MilliSecs();
         Progress(or1 == 6, "Result = %d", or1);
         ProgressFloat(true, "%4.3f MB/s (unrolled loop with prefetch)",
                       float(size*sizeof(*a)/(1E3*timeVal)));


         free(a);
         rc = true;
      }
      else
         Progress(false, "Out of memory!!");

      PrintSeparator();

      return rc;
   }

   int Main () override
   {
      int rc = 1;
      printf("grep for the string '%s' to detect failures.\n\n", failString);

      if(PrefetchTest())
         rc = 0;

      printf("END OF TEST (%s)\n", _argv[0]);

      return rc;
   }
};


int main (int argc, char **argv)
{
   PrefetchTestApp app;
   setvbuf(stdout, nullptr, _IOLBF, 8192);
   return app.Entry(argc, argv);
}