2525#include < core/vector.hpp>
2626#include < core/error.hpp>
2727#include < core/library.hpp>
28+ #include < utils/timer.hpp>
2829#include < io/logger.hpp>
2930
31+ #define TIMER_ACTION (timer, action ) \
32+ Timer timer; \
33+ timer.start(); \
34+ action; \
35+ timer.end()
36+
3037namespace cubool {
3138
3239 Vector::Vector (size_t nrows, BackendBase &backend) {
@@ -48,7 +55,10 @@ namespace cubool {
4855 }
4956
5057 void Vector::setElement (index i) {
51- RAISE_ERROR (NotImplemented, " This function is not implemented" );
58+ CHECK_RAISE_ERROR (i < getNrows (), InvalidArgument, " Value out of vector bounds" );
59+
60+ // This values will be committed later
61+ mCachedI .push_back (i);
5262 }
5363
5464 void Vector::build (const index *rows, size_t nvals, bool isSorted, bool noDuplicates) {
@@ -103,6 +113,39 @@ namespace cubool {
103113 RAISE_ERROR (NotImplemented, " This function is not implemented" );
104114 }
105115
116+ void Vector::eWiseAdd (const VectorBase &aBase, const VectorBase &bBase, bool checkTime) {
117+ const auto * a = dynamic_cast <const Vector*>(&aBase);
118+ const auto * b = dynamic_cast <const Vector*>(&bBase);
119+
120+ CHECK_RAISE_ERROR (a != nullptr , InvalidArgument, " Passed vector does not belong to core vector class" );
121+ CHECK_RAISE_ERROR (b != nullptr , InvalidArgument, " Passed vector does not belong to core vector class" );
122+
123+ index M = a->getNrows ();
124+
125+ CHECK_RAISE_ERROR (M == b->getNrows (), InvalidArgument, " Passed vectors have incompatible size" );
126+ CHECK_RAISE_ERROR (M == this ->getNrows (), InvalidArgument, " Vector has incompatible size for operation result" );
127+
128+ a->commitCache ();
129+ b->commitCache ();
130+ this ->releaseCache ();
131+
132+ if (checkTime) {
133+ TIMER_ACTION (timer, mHnd ->eWiseAdd (*a->mHnd , *b->mHnd , false ));
134+
135+ LogStream stream (*Library::getLogger ());
136+ stream << Logger::Level::Info
137+ << " Time: " << timer.getElapsedTimeMs () << " ms "
138+ << " Vector::eWiseAdd: "
139+ << this ->getDebugMarker () << " = "
140+ << a->getDebugMarker () << " + "
141+ << b->getDebugMarker () << LogStream::cmt;
142+
143+ return ;
144+ }
145+
146+ mHnd ->eWiseAdd (*a->mHnd , *b->mHnd , false );
147+ }
148+
106149 index Vector::getNrows () const {
107150 return mHnd ->getNrows ();
108151 }
@@ -117,7 +160,31 @@ namespace cubool {
117160 }
118161
119162 void Vector::commitCache () const {
120- // todo
163+ size_t cachedNvals = mCachedI .size ();
164+
165+ // Nothing to do if no value was cached on CPU side
166+ if (cachedNvals == 0 )
167+ return ;
168+
169+ bool isSorted = false ;
170+ bool noDuplicates = false ;
171+
172+ if (mHnd ->getNvals () > 0 ) {
173+ // We will have to join old and new values
174+ // Create tmp vector and merge values
175+
176+ VectorBase* tmp = mProvider ->createVector (getNrows ());
177+ tmp->build (mCachedI .data (), cachedNvals, isSorted, noDuplicates);
178+ mHnd ->eWiseAdd (*mHnd , *tmp, false );
179+ mProvider ->releaseVector (tmp);
180+ }
181+ else {
182+ // Otherwise, new values are used to build vector content
183+ mHnd ->build (mCachedI .data (), cachedNvals, isSorted, noDuplicates);
184+ }
185+
186+ // Clear arrays
187+ releaseCache ();
121188 }
122189
123190}
0 commit comments