SkelCL
SkelCL is a high level multi GPU skeleton library developed at the university of Münster, Germany.
 All Classes Namespaces Files Functions Variables Typedefs Groups
ZipTests.cpp
1 /*****************************************************************************
2  * Copyright (c) 2011-2012 The SkelCL Team as listed in CREDITS.txt *
3  * http://skelcl.uni-muenster.de *
4  * *
5  * This file is part of SkelCL. *
6  * SkelCL is available under multiple licenses. *
7  * The different licenses are subject to terms and condition as provided *
8  * in the files specifying the license. See "LICENSE.txt" for details *
9  * *
10  *****************************************************************************
11  * *
12  * SkelCL is free software: you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation, either version 3 of the License, or *
15  * (at your option) any later version. See "LICENSE-gpl.txt" for details. *
16  * *
17  * SkelCL is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU General Public License for more details. *
21  * *
22  *****************************************************************************
23  * *
24  * For non-commercial academic use see the license specified in the file *
25  * "LICENSE-academic.txt". *
26  * *
27  *****************************************************************************
28  * *
29  * If you are interested in other licensing models, including a commercial- *
30  * license, please contact the author at michel.steuwer@uni-muenster.de *
31  * *
32  *****************************************************************************/
33 
37 
38 #include <fstream>
39 
40 #include <cstdio>
41 
42 #include <pvsutil/Logger.h>
43 
44 #include <SkelCL/SkelCL.h>
45 #include <SkelCL/Vector.h>
46 #include <SkelCL/Zip.h>
47 
48 #include "Test.h"
51 
52 class ZipTest : public ::testing::Test {
53 protected:
54  ZipTest() {
55  pvsutil::defaultLogger.setLoggingLevel(pvsutil::Logger::Severity::DebugInfo);
57  }
58 
59  ~ZipTest() {
61  }
62 };
63 
64 TEST_F(ZipTest, CreateZipWithString) {
65  skelcl::Zip<float(float, float)> m(
66  "float func(float x, float y){ return x*y; }");
67 }
68 
69 TEST_F(ZipTest, CreateZipWithFile) {
70  std::string filename("ZipFunction.cl");
71  {
72  std::ofstream file(filename, std::ios_base::trunc);
73  file << "float func(float x, float y) { return x+y; }";
74  }
75  std::ifstream file(filename);
76 
77  skelcl::Zip<float(float, float)> m(file);
78 
79  remove(filename.c_str()); // delete file
80 }
81 
82 TEST_F(ZipTest, SimpleZip) {
83  skelcl::Zip<float(float, float)> z(
84  "float func(float x, float y){ return x+y; }");
85 
86  skelcl::Vector<float> left(10);
87  for (size_t i = 0; i < left.size(); ++i) {
88  left[i] = i * 2.5f;
89  }
90  EXPECT_EQ(10, left.size());
91 
92  skelcl::Vector<float> right(10);
93  for (size_t i = 0; i < right.size(); ++i) {
94  right[i] = i * 7.5f;
95  }
96  EXPECT_EQ(10, right.size());
97 
98  auto output = z(left, right);
99 
100  EXPECT_EQ(10, output.size());
101  for (size_t i = 0; i < output.size(); ++i) {
102  EXPECT_EQ(left[i]+right[i], output[i]);
103  }
104 }
105 
106 TEST_F(ZipTest, AddArgs) {
107  skelcl::Zip<float(float, float)> z(
108  "float func(float x, float y, float add, float add2)\
109  { return (x*y)+add+add2; }");
110 
111  skelcl::Vector<float> left(10);
112  for (size_t i = 0; i < left.size(); ++i) {
113  left[i] = i * 2.5f;
114  }
115  EXPECT_EQ(10, left.size());
116 
117  skelcl::Vector<float> right(10);
118  for (size_t i = 0; i < right.size(); ++i) {
119  right[i] = i * 4.5f;
120  }
121  EXPECT_EQ(10, right.size());
122 
123  float add = 5.25f;
124  float add2 = 7.75f;
125 
126  auto output = z(left, right, add, add2);
127 
128  EXPECT_EQ(10, output.size());
129  for (size_t i = 0; i < output.size(); ++i) {
130  EXPECT_EQ( (left[i]*right[i])+add+add2, output[i]);
131  }
132 }
133 
134 TEST_F(ZipTest, LeftID) {
135  skelcl::Zip<float(float, float)> z(
136  "float func(float x, float y) { return x; }");
137 
138  skelcl::Vector<float> left(10);
139  left.setDistribution(skelcl::distribution::Block(left));
140  for (size_t i = 0; i < left.size(); ++i) {
141  left[i] = i * 2.5f;
142  }
143  EXPECT_EQ(10, left.size());
144 
145  skelcl::Vector<float> right(10);
146  right.setDistribution(skelcl::distribution::Block(right));
147  for (size_t i = 0; i < right.size(); ++i) {
148  right[i] = i * 4.5f;
149  }
150  EXPECT_EQ(10, right.size());
151 
152  z(skelcl::out(left), left, right);
153 
154  EXPECT_EQ(10, left.size());
155  for (size_t i = 0; i < left.size(); ++i) {
156  EXPECT_EQ( i * 2.5f, left[i]);
157  }
158 }
159 
160 TEST_F(ZipTest, SimpleZip2D) {
161  skelcl::Zip<float(float,float)> z(
162  "float func(float mat1,float mat2)""{ return mat1 + mat2; }");
163 
164  std::vector<float> left(10);
165  for (size_t i = 0; i < left.size(); ++i) {
166  left[i] = i * 2.5f;
167  }
168  skelcl::Matrix<float> matrix_left(left, 3);
169  EXPECT_EQ(skelcl::Matrix<float>::size_type(4,3), matrix_left.size());
170 
171  std::vector<float> right(10);
172  for (size_t i = 0; i < right.size(); ++i) {
173  right[i] = i * 7.5f;
174  }
175  skelcl::Matrix<float> matrix_right(right,3);
176  EXPECT_EQ(skelcl::Matrix<float>::size_type(4,3), matrix_right.size());
177 
178  auto output = z(matrix_left, matrix_right);
179 
180  EXPECT_EQ(matrix_left.size(), output.size());
181 
182  for (size_t i = 0; i < output.rowCount(); ++i) {
183  for(size_t j = 0; j < output.columnCount(); ++j) {
184  EXPECT_EQ(matrix_left[i][j] + matrix_right[i][j], output[i][j]);
185  }
186  }
187 }
188 
189 TEST_F(ZipTest, VoidAddArgs) {
190  skelcl::Zip<void(float, float)> z(
191  "void func(float x, float y, __global float* out)\
192  { out[get_global_id(0)] = x*y; }");
193 
194  skelcl::Vector<float> left(10);
195  for (size_t i = 0; i < left.size(); ++i) {
196  left[i] = i * 2.5f;
197  }
198  EXPECT_EQ(10, left.size());
199 
200  skelcl::Vector<float> right(10);
201  for (size_t i = 0; i < right.size(); ++i) {
202  right[i] = i * 4.5f;
203  }
204  EXPECT_EQ(10, right.size());
205 
206  skelcl::Vector<float> output(10);
207 
208  z(left, right, skelcl::out(output));
209 
210  EXPECT_EQ(10, output.size());
211  for (size_t i = 0; i < output.size(); ++i) {
212  EXPECT_EQ( left[i]*right[i], output[i]);
213  }
214 }
215 
217 
The Matrix class is a two dimensional container which makes its data accessible on the host as well a...
Definition: Matrix.h:190
Out< ContainerType< T > > out(ContainerType< T > &c)
Helper function to create a Out wrapper object.
Definition: Out.h:94
SKELCL_DLL void init(detail::DeviceProperties properties=allDevices())
Initializes the SkelCL library. This function (or another init function) has to be called prior to ev...
Definition: SkelCL.cpp:51
std::unique_ptr< skelcl::detail::Distribution< C< T > > > Block(const C< T > &c)
Factory function to create a BlockDistribution with the types of the given container.
Definition: Distributions.h:94
SKELCL_DLL detail::DeviceProperties nDevices(size_t n)
Creates a detail::DeviceProperties object representing n devices. This object should be used as param...
Definition: SkelCL.cpp:66
The Vector class is a one dimensional container which makes its data accessible on the host as well a...
Definition: Vector.h:113
This class defines a two dimensional size for a Matrix.
Definition: Matrix.h:67
SKELCL_DLL void terminate()
Frees all resources allocated internally by SkelCL.
Definition: SkelCL.cpp:81