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
VectorTests.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 <pvsutil/Logger.h>
39 
40 #include <SkelCL/Distributions.h>
41 #include <SkelCL/SkelCL.h>
42 #include <SkelCL/Vector.h>
43 
44 #include "Test.h"
47 
48 class VectorTest : public ::testing::Test {
49 protected:
50  VectorTest() {
51  pvsutil::defaultLogger.setLoggingLevel(pvsutil::Logger::Severity::Debug);
53  }
54 
55  ~VectorTest() {
57  }
58 };
59 
60 struct A {
61  A() : _a(0) {}
62  A(int a) : _a(a) {}
63  int _a;
64 };
65 
66 TEST_F(VectorTest, CreateEmptyVector) {
68 
69  EXPECT_TRUE(vi.empty());
70  EXPECT_EQ(0, vi.size());
71 }
72 
73 TEST_F(VectorTest, Copy) {
74  skelcl::Vector<int> vi(5);
75  for(unsigned i = 0; i < 5; ++i) {
76  vi[i] = 5;
77  }
78 
79  EXPECT_FALSE(vi.empty());
80  EXPECT_EQ(5, vi.size());
81  for (unsigned i = 0; i < 5; ++i) {
82  EXPECT_EQ(5, vi[i]);
83  }
84 
85  skelcl::Vector<int> copy(vi);
86 
87  for (unsigned i = 0; i < 5; ++i) {
88  vi[i] = 0;
89  }
90 
91  EXPECT_FALSE(copy.empty());
92  EXPECT_EQ(vi.size(), copy.size());
93  for (unsigned i = 0; i < vi.size(); ++i) {
94  EXPECT_EQ(5, copy[i]);
95  }
96 }
97 
98 TEST_F(VectorTest, CopyWithDataOnDevice) {
99  skelcl::Vector<int> vi(5);
100  for(unsigned i = 0; i < 5; ++i) {
101  vi[i] = 5;
102  }
103  vi.setDistribution(skelcl::detail::SingleDistribution< skelcl::Vector<int> >());
104  vi.createDeviceBuffers();
105  vi.copyDataToDevices();
106 
107  EXPECT_FALSE(vi.empty());
108  EXPECT_EQ(5, vi.size());
109  for (unsigned i = 0; i < 5; ++i) {
110  EXPECT_EQ(5, vi[i]);
111  }
112 
113  vi.dataOnDeviceModified(); // fake modification on the device
114 
115  skelcl::Vector<int> copy(vi);
116 
117  for (unsigned i = 0; i < 5; ++i) {
118  vi[i] = 0;
119  }
120 
121  EXPECT_FALSE(copy.empty());
122  EXPECT_EQ(vi.size(), copy.size());
123  for (unsigned i = 0; i < vi.size(); ++i) {
124  EXPECT_EQ(5, copy[i]);
125  }
126 }
127 
128 TEST_F(VectorTest, CreateVector) {
129  skelcl::Vector<int> vi(10);
130 
131  EXPECT_FALSE(vi.empty());
132  EXPECT_EQ(10, vi.size());
133  EXPECT_EQ(0, *vi.begin());
134  for (int i = 0; i < 10; ++i) {
135  EXPECT_EQ(0, vi[i]);
136  }
137 
138  skelcl::Vector<A> va(5, A(1));
139 
140  EXPECT_FALSE(va.empty());
141  EXPECT_EQ(5, va.size());
142  EXPECT_EQ(1, va.begin()->_a);
143 }
144 
145 TEST_F(VectorTest, Resize) {
147  vi.resize(10);
148  EXPECT_EQ(10, vi.size());
149 
150  vi.push_back(5);
151  EXPECT_EQ(11, vi.size());
152 
153  vi.pop_back();
154  EXPECT_EQ(10, vi.size());
155 
156  vi.insert( vi.begin(), 5 );
157  EXPECT_EQ(11, vi.size());
158 
159  vi.insert( vi.begin(), 2, 5 );
160  EXPECT_EQ(13, vi.size());
161 
162  std::vector<int> other(2);
163  vi.insert( vi.begin(), other.begin(), other.end() );
164  EXPECT_EQ(15, vi.size());
165 
166  vi.erase( vi.begin() );
167  EXPECT_EQ(14, vi.size());
168 
169  vi.erase( vi.begin(), vi.begin()+2 );
170  EXPECT_EQ(12, vi.size());
171 
172  vi.clear();
173  EXPECT_EQ(0, vi.size());
174 }
175 
176 typedef struct Position {
177  Position() : _x(0), _y(0) {}
178  Position(int x, int y) : _x(x), _y(y) {}
179  int _x;
180  int _y;
181 } Position;
182 
183 TEST_F(VectorTest, CompleteInitialization) {
184  skelcl::Vector<Position> positions(1024*768);
185 
186  auto inserter = positions.begin();
187  for (int x = 0; x < 1024; ++x)
188  for (int y = 0; y < 768; ++y)
189  *(inserter++) = Position(x,y);
190 
191  EXPECT_EQ(1024*768, positions.size());
192 
193  EXPECT_EQ(23, positions[23*768+42]._x);
194  EXPECT_EQ(42, positions[23*768+42]._y);
195 }
196 
198 
void createDeviceBuffers() const
Create buffers on the devices involved in the current distribution.
void pop_back()
Removes the last element of the Vector.
size_type size() const
Returns the number of elements in the Vector.
void setDistribution(const detail::Distribution< Vector< U >> &distribution) const
Set a new distribution to the vector.
iterator begin()
Returns an iterator to the first element of the Vector.
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
void clear()
Removes all elements from the Vector.
void resize(size_type count, T value=T())
Resizes the container to contain count elements.
void dataOnDeviceModified() const
Marks the data on the device as been modified.
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
std::unique_ptr< skelcl::detail::Distribution< C< T > > > Copy(const C< T > &c, typename identity< std::function< T(const T &, const T &)>>::type combineFunc=nullptr)
Factory function to create a CopyDistribution with the types of the given container.
iterator insert(iterator pos, const T &value)
Inserts copy of value at the specified location in the Vector.
iterator erase(iterator pos)
Remove the specified element from the Vector.
void push_back(const T &value)
Appends a copy of the given element value to the end of the Vector.
SKELCL_DLL void terminate()
Frees all resources allocated internally by SkelCL.
Definition: SkelCL.cpp:81
void copyDataToDevices() const
Copies data from the host to the devices involved in the current distribution.
bool empty() const
Checks if the Vector has no elements, i.e. whether begin() == end()