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

Refactoring #4103

Merged
Merged

Conversation

shubham808
Copy link
Contributor

#4074 #4092
[WIP]
This is part 1 here we will refactor code to change the use of the computation classes to be deleted.
Part 2 will including deleting the classes.
I am struggling with RationalApproximation unit test.
Please review this and guide me further.

Copy link
Member

@vigsterkr vigsterkr left a comment

Choose a reason for hiding this comment

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

thxn for the patch!
here are some comments/requests thnx

#pragma omp critical // so that the dynamic array stays concurrent
{
samples[idx_col] += result;
idx_row++;
Copy link
Member

Choose a reason for hiding this comment

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

statements like these use preoperator and not the post operator

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have directly used the iterator i here which simplifies it.

float64_t result = m_operator_log->solve(s);
#pragma omp critical // so that the dynamic array stays concurrent
{
samples[idx_col] += result;
Copy link
Member

Choose a reason for hiding this comment

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

this could be reformulated instead of using critical using parallel for reduction...
check for example:
http://cs.umw.edu/~finlayson/class/fall16/cpsc425/notes/11-parfor.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks useful I will try to implement it.

SGVector<float64_t> agg = m_linear_operator->apply(vec.get_imag());

// perform dot product
Map<VectorXd> map_agg(agg.vector, agg.vlen);
Copy link
Member

Choose a reason for hiding this comment

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

yeah no... i mean i was already about to comment above that why do we use eigen here directly... but now seeing that it's being used to do the dot product below.... plz use linalg:: over SGVector/Matrix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have used linalg in all opfunc classes however the DenseMatrixExactJob class use mat.log() which is not in linalg so it still uses Eigen3

m_computation_engine->submit_job(job);
// multiply with the weight using Eigen3 and take negative
// (see CRationalApproximation for the formula)
Map<VectorXcd> v(vec.vector, vec.vlen);
Copy link
Member

Choose a reason for hiding this comment

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

use linalg:: for linear algebra operations

SGVector<float64_t> s = m_trace_sampler->sample(j);
// calculate the result for sample s
float64_t result = m_operator_log->solve(s);
#pragma omp critical // so that the dynamic array stays concurrent
Copy link
Member

Choose a reason for hiding this comment

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

which dynamic array?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

typo error because we decided to not use any kind of aggregators and directly use samples

@@ -223,67 +170,41 @@ SGMatrix<float64_t> CLogDetEstimator::sample_without_averaging(
SG_DEBUG("Entering...\n")

REQUIRE(m_operator_log, "Operator function is NULL\n");
// call the precompute of operator function to compute all prerequisites
// call the precompute of operator function zto compute all prerequisites
Copy link
Member

Choose a reason for hiding this comment

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

typo

SGVector<float64_t> s = m_trace_sampler->sample(j);
// solve the result for s
float64_t result = m_operator_log->solve(s);
#pragma omp critical // aggregators array should be concurrent
Copy link
Member

Choose a reason for hiding this comment

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

use reduce here as well

* to ensure that the job result aggregators are all up to date. Then simply
* computes running averages over the estimates
* and calls solve of COperatorFunction, stores the resulting in
* a vector, Then simply computes running averages over the estimates
Copy link
Member

Choose a reason for hiding this comment

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

type "Then" should be "then"

: COperatorFunction<float64_t>(NULL, NULL, OF_LOG)
{
SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
CDenseMatrixExactLog::CDenseMatrixExactLog()
Copy link
Member

Choose a reason for hiding this comment

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

you have a lot of whitespace changes not sure whether that is due to the auto-formatter.
In any case, search for "style" in the first travis job output to get the comment how to fix formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a style issue I have fixed it now... i think


SGVector<float64_t> vec = m_log_operator->apply(sample);

// compute the vector-vector dot product using Eigen3
Copy link
Member

Choose a reason for hiding this comment

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

remove this comment, doesnt add any valu
also, you can just use linalg::dot

*/
virtual CJobResultAggregator* submit_jobs(SGVector<float64_t> sample);
virtual float64_t solve(SGVector<float64_t> sample);
Copy link
Member

Choose a reason for hiding this comment

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

compute?

Also, make sure to start the comment with a capital M. "Method to ..."

@@ -107,7 +97,7 @@ CJobResultAggregator* CLogRationalApproximationIndividual::submit_jobs(
else
{
// something weird happened
SG_ERROR("OperatorFunction::submit_jobs(): Unknown MatrixOperator given!\n");
SG_ERROR("OperatorFunction::solve(): Unknown MatrixOperator given!\n");
Copy link
Member

Choose a reason for hiding this comment

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

in all error messages that you touch, remove the ClassName::method_name, this is outdated

* abstract precompute method that must be called before using submit jobs
* for performing preliminary computations that are necessary for the
* rest of the computation jobs
* abstract precompute method that must be called before using solve
Copy link
Member

Choose a reason for hiding this comment

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

"Purely virtual method that ..."

*
* @param sample the vector for which new computation job(s) are to be created
* @return the array of generated independent jobs
* method that solves for a sample and returns the final result
Copy link
Member

Choose a reason for hiding this comment

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

"Method that solves ..."

Copy link
Member

Choose a reason for hiding this comment

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

or better "computes" and call it compute

*
* @param sample the vector for which new computation job(s) are to be created
* @return the array of generated independent jobs
*method that solves for a particular sample
Copy link
Member

Choose a reason for hiding this comment

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

"* Method that computes ...."

SG_REF(op_func);

// its really important we call the precompute on the operato function
op_func->precompute();

// for storing the aggregators that submit_jobs return
CDynamicObjectArray aggregators;
float64_t result = 0.0;

// create samples for extracting the trace of log(C) and submit
for (index_t i=0; i<size; ++i)
Copy link
Member

Choose a reason for hiding this comment

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

maybe this can be parallell as well?
or is it inside the other parallel loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the other loop and i think we can keep it as is maybe ?

// create the aggregators to contain the result aggregators
CDynamicObjectArray aggregators;

float64_t result = 0.0;
// extract the trace of approximation of log using basis vectors
for (index_t i=0; i<size; ++i)
Copy link
Member

Choose a reason for hiding this comment

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

maybe parallel as well?
Generally, I would put the omp into the outmost loop

Copy link
Member

@karlnapf karlnapf left a comment

Choose a reason for hiding this comment

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

This is a great patch. Thanks!

And also much easier to review in detail, which we now did. I think the changes should be quick to implement.

Looking forward to merging it soon!

#include <shogun/lib/SGMatrix.h>
#include <shogun/mathematics/linalg/linsolver/LinearSolver.h>
#include <shogun/lib/SGVector.h>
#include <shogun/mathematics/eigen3.h>
Copy link
Member

Choose a reason for hiding this comment

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

I think the eigen3 includes can go once you use linalg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

solved

{
init();
CLogRationalApproximationIndividual::CLogRationalApproximationIndividual()
: CRationalApproximation(NULL, NULL, 0, OF_LOG)
Copy link
Member

Choose a reason for hiding this comment

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

could you use nullptr here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

solved


SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
Copy link
Member

Choose a reason for hiding this comment

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

Delete this, not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I deleted it from all opfunc classes because it appears to be deprecated. is it okay ?

@shubham808
Copy link
Contributor Author

shubham808 commented Jan 23, 2018

To be Done ->
using reduction in openmp loops. I will try to do this soon.
Fixing RationalApproximation unit test fail. I am not able to figure out why but i will keep trying.

@karlnapf
Copy link
Member

I just had a thought: we can also separate the openmp stuff into a new PR
This way, this one would just be refactoring without using the computation stuff, and then next one would be deleting the computation folder, finally, we would work on openmp separately from the refactoring (might be easier)
I will leave that to you

samples[idx_col]+=r->get_result();
idx_row++;
if (idx_row>=num_trace_samples)
// for omp
Copy link
Member

Choose a reason for hiding this comment

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

don't put those comments in, they don't add information :)

(as I said, we can postpone the openmp into a new PR to make things easier to handle if you wanted to)

@karlnapf
Copy link
Member

What fails in the unit test?

@shubham808
Copy link
Contributor Author

shubham808 commented Jan 24, 2018

The RationalApproximation.trace_accuracy fails missing the correct value by a large amount. ( expected is -11.5 but we are getting -4.1 ) and the expected error is 1E-07

@shubham808
Copy link
Contributor Author

Lets do the openmp stuff later because i am not sure how to deal with reduction of arrays... we can include that in a seperate pr.

@karlnapf
Copy link
Member

Ok so why don't you remove all the omp calls then for now.

// its important that we don't just unref the result here
result+=r->get_result();
SG_UNREF(agg);
result += op_func->compute(s);
Copy link
Member

Choose a reason for hiding this comment

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

it seems you are not changing anything here ... don't understand why the test fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah i am confused too... also the RationalApproximationCGM tests pass its the one with
RationalApproximationIndividual that fail.

Copy link
Member

Choose a reason for hiding this comment

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

you will have to carefully backtrace the changes.
If staring at the code doesnt help, you can run the two implementations next to each other in a debugger and ensure that the same number is added in each step. Ping me if you need help with this, I am in IRC for another hour

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay i am on it.

@shubham808
Copy link
Contributor Author

@karlnapf sorry for the late reply.... i am still stuck at the tests. in @lambday implementation only this test uses a separate aggregator and i am still not sure of its purpose

@karlnapf
Copy link
Member

karlnapf commented Feb 4, 2018

We now have a merge conflict as well.

@lambday can you help with the aggregator?

@vigsterkr
Copy link
Member

@shubham808 how is it going with the PR? could you rebase it over the latest develop?

idx_row++;
if (idx_row>=num_trace_samples)

#pragma omp parallel for
Copy link
Member

Choose a reason for hiding this comment

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

#pragma omp parallel for reduction(+: samples[:num_estimates])

Copy link
Member

Choose a reason for hiding this comment

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

this should work with openmp 4.5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah this looks good.

Copy link
Member

Choose a reason for hiding this comment

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

dont forget to remove the critical part

@shubham808
Copy link
Contributor Author

This pr is still WIP because of unit test fails.
I will rebase and make a commit soon :)

@karlnapf
Copy link
Member

karlnapf commented Feb 6, 2018

@vigsterkr we had also split things a bit to make it easier to oversee
first patch is to refactor existing code/tests to not use the computation framework.
next is deleting the now unsused code
next is using omp

@vigsterkr
Copy link
Member

@karlnapf whatever is fine... just not the current state of omp

@vigsterkr
Copy link
Member

this needs rebase :S

@shubham808 shubham808 force-pushed the feature/refactoring_logdet_1 branch 2 times, most recently from a352bdc to 271938f Compare March 23, 2018 04:00
@shubham808
Copy link
Contributor Author

@vigsterkr I removed omp for now along with rebase and minor edits ...

@@ -455,6 +455,5 @@ class CMachine : public CSGObject
/** Mutex used to pause threads */
std::mutex m_mutex;
};

Copy link
Member

Choose a reason for hiding this comment

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

please avoid these type of hanges

Copy link
Contributor Author

Choose a reason for hiding this comment

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

must have been the style formatting

@@ -4,7 +4,6 @@
* Authors: Sunil Mahendrakar, Heiko Strathmann, Soumyajit De, Björn Esser
*/
#include <shogun/base/Parallel.h>
#include <shogun/base/progress.h>
Copy link
Member

Choose a reason for hiding this comment

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

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was not being used anywhere... we could put it back in when we add progress bar to it (after this gets merged )

// for omp
#pragma omp parallel for
for (index_t i = 0; i < num_estimates; i++)
// TO DO: use openmp like this here->#pragma omp parallel for reduction(+:
Copy link
Member

Choose a reason for hiding this comment

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

why is it a todo if it is the solution? if not then just add the todo but not the whole pragma.. have you tested the pragma itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the pragma didnt work.... error: 'samples' does not have pointer or array type

for (index_t i = 0; i < num_estimates; i++)
// TO DO: use openmp like this here->#pragma omp parallel for reduction(+:
// samples[:num_estimates])
for (index_t i = 0; i < num_estimates; i++)
Copy link
Member

Choose a reason for hiding this comment

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

why post operator here (i++) but prefix-operator below (++j). i'd stick with prefix

for (index_t i = 0; i < num_estimates; i++)
// TO DO: use openmp #pragma omp parallel for reduction(+:
// samples[:num_estimates][:num_trace_samples])
for (index_t i = 0; i < num_estimates; i++)
Copy link
Member

Choose a reason for hiding this comment

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

same story... prefix vs post.
btw this code seems to be a bit of a copy-paste from the one above

CDenseMatrixExactLog::CDenseMatrixExactLog(
CDenseMatrixOperator<float64_t>* op)
: COperatorFunction<float64_t>((CLinearOperator<float64_t>*)op, OF_LOG)
{
Copy link
Member

Choose a reason for hiding this comment

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

why the removal of the DEBUG-ing message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That change was requested because it was not very informative

@shubham808 shubham808 force-pushed the feature/refactoring_logdet_1 branch from 8c4bd86 to 25cf5a6 Compare April 1, 2018 11:19
@shubham808
Copy link
Contributor Author

🎉

@shubham808 shubham808 force-pushed the feature/refactoring_logdet_1 branch from 25cf5a6 to 601eb39 Compare April 1, 2018 13:23
@shubham808
Copy link
Contributor Author

shubham808 commented Apr 2, 2018

@vigsterkr @karlnapf any changes ?

@karlnapf
Copy link
Member

karlnapf commented Apr 2, 2018

I think we are good. Thanks for this monster effort!
Next PR would be to continue with the other changes we discussed: removing all the classes that are now not used anymore, paralleling the current code using openmp.

@karlnapf karlnapf merged commit 8d578f4 into shogun-toolbox:develop Apr 2, 2018
ktiefe pushed a commit to ktiefe/shogun that referenced this pull request Jul 30, 2019
* Refactoring

* Update LogDetEstimator.cpp

* rebase and edits

* fixes RationalApproximation unit test

* style fixes
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

3 participants