File: testPrivateVar.cc

package info (click to toggle)
asl 0.1.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,536 kB
  • sloc: cpp: 36,180; makefile: 26
file content (140 lines) | stat: -rw-r--r-- 3,755 bytes parent folder | download | duplicates (4)
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
/*
 * Advanced Simulation Library <http://asl.org.il>
 * 
 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
 *
 *
 * This file is part of Advanced Simulation Library (ASL).
 *
 * ASL is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3 of the License.
 *
 * ASL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
 *
 */


/**
 	\example testPrivateVar.cc
*/

#include "utilities/aslUValue.h"
#include "acl/Kernels/aclKernel.h"
#include "acl/Kernels/aclKernelConfigurationTemplates.h"
#include <acl/acl.h>
#include <acl/aclMath/aclVectorOfElements.h>
#include <acl/aclGenerators.h>
#include <utilities/aslTimer.h>

const unsigned int nLength(1000000);
const unsigned int nOperations(10);
const unsigned int nCycles(10000);
const acl::KernelConfiguration & kConf(acl::KERNEL_BASIC); 
//const acl::KernelConfiguration & kConf(acl::KERNEL_SIMDUA); 

bool testKernelUnoptimized()
{
	cout << "Test of \"Simple kernel\" function..." << flush;

	auto vec1(acl::generateVEData<float>(nLength,1u));
	acl::Kernel k(kConf);

	auto res(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT));
	k << (res = acl::generateVEConstant(0.));
	for(unsigned int i(0); i<nOperations; ++i)
	{
		auto tempRes(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT));
		k << (tempRes=acl::generateVEIndex() * i+5.*i*i+7./(i+1.));
		k << (res+=tempRes*tempRes+rsqrt(.2*tempRes)+ 1./tempRes);
	}
	k << (vec1=res);
	k.setup();

	asl::Timer timer;
	timer.start();
	for(unsigned int i(0); i<nCycles; ++i)
		k.compute();
	timer.stop();
	std::cout<<"Unoptimized: "<<timer.realTime()<<endl;

	return true;		
}


bool testKernelUnoptimizedPlus()
{
	cout << "Test of \"Simple kernel\" function..." << flush;

	auto vec1(acl::generateVEData<float>(nLength,1u));
	acl::Kernel k(kConf);

	auto res(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT));
	k << (res = acl::generateVEConstant(0.));
	vector<acl::VectorOfElements> tempRes(nOperations);
	for(unsigned int i(0); i<nOperations; ++i)
	{
		copy(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT), tempRes[i]);
		k << (tempRes[i]=acl::generateVEIndex() * i+5.*i*i+7./(i+1.));
	}
	for(unsigned int i(0); i<nOperations; ++i)
		k << (res+=tempRes[i]*tempRes[i]+rsqrt(.2*tempRes[i])+ 1./tempRes[i]);
	k << (vec1=res);
	k.setup();

	asl::Timer timer;
	timer.start();
	for(unsigned int i(0); i<nCycles; ++i)
		k.compute();
	timer.stop();
	std::cout<<"UnoptimizedPlus: "<<timer.realTime()<<endl;

	return true;		
}


bool testKernelOptimized()
{
	cout << "Test of \"Simple kernel\" function..." << flush;

	auto vec1(acl::generateVEData<float>(nLength,1u));
	acl::Kernel k(kConf);

	auto res(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT));
	auto tempRes(acl::generateVEPrivateVariable(1u, acl::TYPE_FLOAT));
	k << (res = acl::generateVEConstant(0.));
	for(unsigned int i(0); i<nOperations; ++i)
	{
		k << (tempRes=acl::generateVEIndex() * i+5.*i*i+7./(i+1.));
		k << (res+=tempRes*tempRes+rsqrt(.2*tempRes)+ 1./tempRes);
	}
	k << (vec1=res);
	k.setup();
//	cout<<k.getKernelSource()<<endl;

	asl::Timer timer;
	timer.start();
	for(unsigned int i(0); i<nCycles; ++i)
		k.compute();
	timer.stop();
	std::cout<<"Optimized: "<<timer.realTime()<<endl;

	return true;		
}



int main()
{
	testKernelUnoptimized();
	testKernelUnoptimizedPlus();
	testKernelOptimized();

	return 0;
}