File: vector_test.cpp

package info (click to toggle)
combblas 2.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 190,488 kB
  • sloc: cpp: 55,918; ansic: 25,134; sh: 3,691; makefile: 548; csh: 66; python: 49; perl: 21
file content (178 lines) | stat: -rw-r--r-- 3,774 bytes parent folder | download | duplicates (2)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <sstream>
#include <sys/time.h> // for gettimeofday
#include "promote.h"
#include "Semirings.h"
#include "Deleter.h"
#include <ext/numeric>
#include <tr1/array>
#include <xmmintrin.h>
#include <emmintrin.h>
//#include <smmintrin.h>	// SSE-4


#define BETA 16
#define ITER 1000000

template<typename T, typename I>
T ** allocate2D(I m, I n)
{
	T ** array = new T*[m];
	for(I i = 0; i<m; ++i) 
		array[i] = new T[n];
	return array;
}
template<typename T, typename I>
void deallocate2D(T ** array, I m)
{
	for(I i = 0; i<m; ++i) 
		delete [] array[i];
	delete [] array;
}


using namespace std;

//typedef int vpackedsi __attribute__ ((vector_size (BETA*sizeof(int))));			// 64-bytes, a full cache line !

//union ipackedvector 
//{
//  	vpackedsi v;
//  	int f[BETA];
//};

template <int D, typename T>
void saxpy(T a, T * __restrict b, T * __restrict c)
{
	for(int i=0; i<D; ++i)
	{
		c[i] +=  a* b[i];
	}	
}

template <int D, typename T>
void saxpy_array(T a, tr1::array<T,D> & b, tr1::array<T,D> & c)
{
	for(int i=0; i<D; ++i)
	{
		c[i] +=  a* b[i];
	}	
}


int main()
{
	/*
	__m128i a, b, c;
	int inp_sse1[4] __attribute__((aligned(16))) = { 2, 2, 2, 2 };
	int inp_sse2[4] __attribute__((aligned(16))) = { 0, 1, 2, 3 };
	int out_sse[4] __attribute__((aligned(16))) = {0, 0, 0, 0};

 	__m128i res = _mm_mul_epi32(a, b); */


	int ** __restrict xx = allocate2D<int>(ITER,BETA);
	int ** __restrict yy = allocate2D<int>(ITER,BETA);
	
	for (int i=0; i< ITER; ++i)
	{
		for (int j=0; j< BETA; ++j)
		{
			xx[i][j] = j;
			yy[i][j] = 0;
		}
	}

	// tr1::array seems to be at least as fast as built-in C arrays
	int a = 2;
	tr1::array<int,BETA> * b = new tr1::array<int,BETA>[ITER];
	tr1::array<int,BETA> * c = new tr1::array<int,BETA>[ITER]();	// initialize to zero !


	timeval tim;
	gettimeofday(&tim, NULL);
	double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
	int index = 0;

	int * __restrict xxx = xx[index];
	int * __restrict yyy = yy[index];
	
	for(int i=0; i<ITER; ++i)
	{
		saxpy<BETA>(a, xxx, yyy);
	}

	gettimeofday(&tim, NULL);
	double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
	printf("%.6lf seconds elapsed for gcc vector extensions\n", t2-t1);

	copy(yy[0], yy[0]+BETA, ostream_iterator<float>( cout, " "));
	cout << endl;

	/*
	ipackedvector av, bv, cv;
	for (int i=0; i< ITER; ++i)
	{
		for (int j=0; j< BETA; ++j)
		{
			b[i][j] = j;
		}
	}
	//float * test = new float[10]();
	// note the empty set of parantheses as the initializer --> makes them default constructed
	// The C++ standard says that: A default constructed POD type is zero-initialized,
		
	//copy(test, test+10, ostream_iterator<float>( cout, " "));
	//cout << endl;
	

	//tr1::array<int,BETA> wtf = b[0] + c[0];
	
	gettimeofday(&tim, NULL);
	t1=tim.tv_sec+(tim.tv_usec/1000000.0);

	for(int i=0; i<ITER; ++i)
	{
		//saxpy_array<BETA>(a, b[i], c[i]);
		c[i] = c[i] + b[i];
	}

	gettimeofday(&tim, NULL);
	t2=tim.tv_sec+(tim.tv_usec/1000000.0);
	printf("%.6lf seconds elapsed for template unrolled loop\n", t2-t1);
	copy(c[0].begin(), c[0].end(), ostream_iterator<int>( cout, " "));
	cout << endl;*/
	

//	ipackedvector av, bv, cv;
//	for (int i=0; i<BETA; ++i)
//	{
//		av.f[i] = 2;
//		bv.f[i] = i;
//		cv.f[i] = 0;
//	}
	
//	gettimeofday(&tim, NULL);
//	t1=tim.tv_sec+(tim.tv_usec/1000000.0);
//	for(int i=0; i<ITER; ++i)
//	{
//		cv.v += av.v * bv.v;	
//	}
//	gettimeofday(&tim, NULL);
//	t2=tim.tv_sec+(tim.tv_usec/1000000.0);
//	printf("%.6lf seconds elapsed for gcc vector extensions\n", t2-t1);

//	copy(cv.f, cv.f+BETA, ostream_iterator<int>( cout, " "));
//	cout << endl;

	/*
	vector<int> tvec;
	tvec.reserve(10);
	
	tvec[5] = 5;
	cout << tvec.size() << " " << tvec.capacity() << endl;	*/
	return 0;
}