testclwrap.cpp 1.46 KB
Newer Older
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
1
2
#include "clwrap.hpp"
#include <sys/time.h>
3
#include <iomanip>
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
4
5
6
7
8
9

// A sample code for simpleOCLInit.hpp
// Written by Kaz Yoshii <ky@anl.gov>

// source $OPENCLENV
// g++ -I. -Wall -O2 -g -Wno-unknown-pragmas `aocl compile-config` -std=c++11 -o testclwrap testclwrap.cpp `aocl link-config`
Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
10
// aocl
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
11

Kazutomo's avatar
Kazutomo committed
12
13
using namespace std;

Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
14
static void test_clwrap()
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
15
{
Kazutomo's avatar
Kazutomo committed
16
	clwrap  cw;
Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
17
	bool failed = false;
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
18

Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
19
	cw.info();
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
20
21

	int gsiz = 8;
22
	int lsiz = 4;
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
23

Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
24
25
	int *a0 = new int[gsiz];
	int *a1 = new int[gsiz];
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
26

27
	bool ret = cw.prepKernel("dummy"); // find dummy.cl and use 'dummy' as the kernel name
Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
28
29
30
31
	if (!ret) {
		cout << "prepKernel() failed!" << endl;
		return;
	}
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
32
33
34

	cw.appendArg(sizeof(int)*gsiz, a0, cw.DEV2HOST);
	cw.appendArg(sizeof(int)*gsiz, a1, cw.DEV2HOST);
35

Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
36
	cw.runKernel(gsiz, lsiz);
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
37

Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
38
39
40
41
42
43
44
45
46
47
48
49
50
	// validate the results
	for (int i = 0; i < gsiz; i++) {
		if (a0[i] != i + 10) {
			cout << "Error: a0[" << i << "] should be " << i+10 << ", not " << a0[i] << endl;
			failed = true;
		}
		if (a1[i] != (i%lsiz) + 20) {
			cout << "Error: a1[" << i << "] should be " << (i%lsiz)+20 << ", not " << a1[i] << endl;
			failed = true;
		}
	}
	if (! failed)  cout << "Validation passed" << endl;

51
52
	cout << "Elapsed [sec]: " << cw.getKernelElapsedNanoSec() * 1e-9 << endl;

Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
53
	cout << "[output]" << endl;
54

Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
55
	cout << "a0: ";
56
	for (int i = 0; i < gsiz; i++)
Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
57
		cout << setw(3) << a0[i];
58
	cout << endl;
Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
59
	cout << "a1: ";
60
	for (int i = 0; i < gsiz; i++)
Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
61
		cout << setw(3) << a1[i];
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
62
	cout << endl;
63

Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
64
65
};

Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
66
int main()
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
67
{
Kazutomo Yoshii's avatar
Kazutomo Yoshii committed
68
	test_clwrap();
Kazutomo Yoshii's avatar
base  
Kazutomo Yoshii committed
69
70
71

	return 0;
}