1 /*
2 Copyright (c) 2009-2012, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9     * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 // written by Roman Dementiev
14 //
15 
16 #include "cpucounters.h"
17 #include <iostream>
18 #include <algorithm>
19 #include <list>
20 #include <vector>
21 #include <sys/time.h>
22 #include <emmintrin.h>
23 #include <assert.h>
24 
25 using std::cout;
26 
my_timestamp()27 inline double my_timestamp()
28 {
29     struct timeval tp;
30     gettimeofday(&tp, NULL);
31     return double(tp.tv_sec) + tp.tv_usec / 1000000.;
32 }
33 
34 struct T
35 {
36     int key[1] = { 0 };
37     int data[3] = { 0, 0, 0 };
38 
TT39     T() { }
TT40     T(int a) { key[0] = a; }
41 
operator ==T42     bool operator == (const T & k) const
43     {
44         return k.key[0] == key[0];
45     }
46 };
47 
48 
49 template <class Y>
write_intensive_task(Y * p,Y * e,int value)50 void write_intensive_task(Y * p, Y * e, int value)
51 {
52     __m128i i = _mm_set_epi32(value, value, value, value);
53 
54 #if 0
55     while (p != e)
56     {
57         *p = value;
58         ++p;
59     }
60 #else
61     while (p != e)
62     {
63         _mm_store_si128((__m128i *)p++, i);
64     }
65 #endif
66 }
67 
68 template <class Y>
stream_write_task(Y * p,Y * e,int value)69 void stream_write_task(Y * p, Y * e, int value)
70 {
71     __m128i i = _mm_set_epi32(value, value, value, value);
72 
73     while (p != e)
74     {
75         _mm_stream_si128((__m128i *)p++, i);
76     }
77 }
78 
79 template <class Y>
read_intensive_task(Y * p,Y * e,int value)80 void read_intensive_task(Y * p, Y * e, int value)
81 {
82     // cppcheck-suppress ignoredReturnValue
83     std::find(p, e, -1);
84 }
85 
86 
main(int argc,char * argv[])87 int main(int argc, char * argv[])
88 {
89     assert((argc > 1) && "Need operation type as parameter: 0 - read, 1 - write, 2 - streaming write ");
90     int op = atoi(argv[1]);
91     T * vector;
92     int nelements = 13000000;
93     vector = new T[nelements];
94 
95     int i = 0;
96 
97     cout << "Elements data size: " << sizeof(T) * nelements / 1024 << " KB\n";
98 
99     for ( ; i < nelements; ++i)
100     {
101         vector[i].key[0] = 10;
102     }
103 
104     double before_ts, after_ts;
105 
106 
107     while (1)
108     {
109         before_ts = my_timestamp();
110         switch (op)
111         {
112         case 1:
113             cout << "Writing memory\n";
114             break;
115         case 0:
116             cout << "Reading memory\n";
117             break;
118         default:
119             cout << "Streaming to memory\n";
120         }
121         cout << std::flush;
122 
123         int niter = 32;
124         i = niter;
125         int r = rand();
126         while (i--)
127         {
128             switch (op)
129             {
130             case 1:
131                 write_intensive_task(vector, vector + nelements, r);
132                 break;
133             case 0:
134                 read_intensive_task(vector, vector + nelements, r);
135                 break;
136             default:
137                 stream_write_task(vector, vector + nelements, r);
138             }
139 
140             after_ts = my_timestamp();
141         }
142         cout << "Bandwidth: " << (sizeof(T) * nelements * niter) / ((after_ts - before_ts) * 1024 * 1024) << " MByte/sec\n" << std::flush;
143     }
144 
145     delete[] vector;
146 
147     return 0;
148 }
149