Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move scale to linalg #2744

Merged
merged 2 commits into from Mar 9, 2015
Merged

move scale to linalg #2744

merged 2 commits into from Mar 9, 2015

Conversation

SunilMahendrakar
Copy link
Contributor

towards #2717. Everything is not yet replaced.

@@ -1023,7 +1024,7 @@ SGVector<float64_t> CQuadraticTimeMMD::sample_null_spectrum_DEPRECATED(

/* when m=n, return m*MMD^2 instead */
if (m==n)
null_samples.scale(0.5);
linalg::scale(null_samples, null_samples, 0.5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey

just curious, can't there be a convenience method for the case where both arugments are the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi,
We'll have to add a simple method. I'll add it.

@SunilMahendrakar
Copy link
Contributor Author

@karlnapf please take a look.

{
for (int32_t i=0; i<A.vlen; i++)
B[i]=A[i]*alpha;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some indentation issue is there.

@lambday
Copy link
Member

lambday commented Mar 4, 2015

Hi @SunilMahendrakar. Sorry for such a late reply. Have been caught up with something.

The PR looks good. I have some comments and it would be great if you could please address those - shouldn't take more than 5 mins. I know GSoC is not happening for us this summer which is depressing, but it would be great if we can finish at least those things that we started. Looking forward to it.

@SunilMahendrakar
Copy link
Contributor Author

@lambday Sure!. GSoC would have been great but I'll be contributing anyways.

@lambday
Copy link
Member

lambday commented Mar 4, 2015

That's really nice to hear :) Looking forward.

@SunilMahendrakar
Copy link
Contributor Author

@lambday done :) please take a look.

B[i]=A[i]*alpha;
}
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SunilMahendrakar well what I meant here was to have an additional method that just takes raw pointer types and the length of the consecutive memory array.

static void compute(T* A, T* B, index_t len, T alpha)
{
    // REQUIRE on null check for both A and B
    // the loop goes here
}

So you write the loop there just once and make the SGMatrix and SGVector ones to call that method instead. That way, we'll be able to handle handle raw arrays as well (we can later simply add another wrapper which calls that with NATIVE backend). See what I mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lambday yeah. so that we just have a REQUIRE there which calls this method to perform the actual computation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Keep the other requires there - they are useful. The null check REQUIRE is useful when people (hopefully mistakenly) pass scale(NULL, 1000, 0.5) in the raw ptr method.

@lambday
Copy link
Member

lambday commented Mar 5, 2015

Hi @SunilMahendrakar . This looks good. Just see my other comment.

A couple of other things I forgot to mention

  • as of now C++11 is not a hard requirement for Shogun, but it is so for linalg. So whenever using linalg methods, we should (at present) guard our code with HAVE_CXX0X or HAVE_CXX11. Otherwise compilation will fail on platforms that ironically still use prehistoric compilers. The rule of thumb is -
    • If the linalg method has a native implementation, we should guard our code with HAVE_CXX0X or HAVE_CXX11. (we would get rid of this requirement soon!)
    • If the linalg method doesn't have a native implementation and only available with external linalg libs, we should guard it with HAVE_LINALG_LIB (this takes care of C++11 guard as well)
    • If the linalg method only has implementation against a particular backend, we should guard it for that particular lib (HAVE_EIGEN3 or HAVE_VIENNACL) alongwith C++11 guard.
  • Whenever we add a native implementation for some method, the purpose of that is to make it available even in absence of any external linalg libs. So in the wrapper, we should pull that method out of HAVE_LINALG_LIB guard. See that add method resides outside that guard.

@SunilMahendrakar
Copy link
Contributor Author

@lambday thanks for the explanation. :)

@SunilMahendrakar
Copy link
Contributor Author

@lambday it would be better if we first work towards removing HAVE_CXX11 first then move the calls to linalg, since if we want to prevent compilation failures we would have to add the guard all over the place starting from where we use it, i.e for things dependent on the method where we use it and we have 30-40 scale calls alone. Its easy for outermost methods like unit tests. Since we are going to lose it anyway lets start working towards losing it.

@lambday
Copy link
Member

lambday commented Mar 6, 2015

@SunilMahendrakar that totally makes sense. We want the build to fail in cmake itself in absence of c++11.

However, until that happens, I'd suggest that replacing everything with linalg calls can be done later, in separate patches. As of now, we'd be fine just adding it to linalg and having unit-tests. So don't replace everything yet - let's first get native scale merged.

@lambday
Copy link
Member

lambday commented Mar 6, 2015

Please rebase.

@SunilMahendrakar
Copy link
Contributor Author

@lambday done please take a look.

static void compute(SGMatrix<T> A, SGMatrix<T> B, T alpha)
{
REQUIRE((A.num_rows==B.num_rows)&&(A.num_cols==B.num_cols),
"Dimensions of A(%dx%d) and B(%dx%d) don't match.\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can skip the \n.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iglesias I personally like the \n in the end - as per our wiki on Assertions -

  • Use proper English (Start with capital letters, write sentences)
  • Don't forget \n at the end.
  • Make sure that you don't cause segfaults in your assertions, check bounds, pointer valid, etc

Although it is just a matter of choice.

@iglesias
Copy link
Collaborator

iglesias commented Mar 8, 2015

It looks good to me, just very minor comments above. @lambday, ready to merge?

for (int32_t i=0; i<9; i++)
EXPECT_NEAR(alpha*A[i], B[i], 1e-15);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SunilMahendrakar could you please add a couple of these tests (one on SGMatrix and another on SGVector) that does the scale operation in place? This way, linalg::scale(A, alpha) is also tested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it makes sense to have unit tests for in place scale operation for all the backends - since we are exposing it for all of 'em.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!

@lambday
Copy link
Member

lambday commented Mar 9, 2015

@SunilMahendrakar great job! Thanks for the patch :) Merging. If you could please add two more unit-tests that I mentioned, we can consider the matter close :)

lambday added a commit that referenced this pull request Mar 9, 2015
@lambday lambday merged commit 136b497 into shogun-toolbox:develop Mar 9, 2015
@SunilMahendrakar
Copy link
Contributor Author

@lambday buildbots are going crazy. test failing unrelated?

@SunilMahendrakar SunilMahendrakar deleted the scale branch March 9, 2015 08:19
@lambday
Copy link
Member

lambday commented Mar 9, 2015

@SunilMahendrakar yeah SerializationJSON is a long term serial killer. We should rename buildbot to blamebot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants