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
Refactoring #4103
Conversation
There was a problem hiding this 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++; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which dynamic array?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Method that solves ..."
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this 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> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete this, not needed
There was a problem hiding this comment.
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 ?
bcc6630
to
fda4791
Compare
To be Done -> |
I just had a thought: we can also separate the openmp stuff into a new PR |
samples[idx_col]+=r->get_result(); | ||
idx_row++; | ||
if (idx_row>=num_trace_samples) | ||
// for omp |
There was a problem hiding this comment.
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)
What fails in the unit test? |
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 |
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. |
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
We now have a merge conflict as well. @lambday can you help with the aggregator? |
@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 |
There was a problem hiding this comment.
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])
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah this looks good.
There was a problem hiding this comment.
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
This pr is still WIP because of unit test fails. |
@vigsterkr we had also split things a bit to make it easier to oversee |
@karlnapf whatever is fine... just not the current state of omp |
this needs rebase :S |
a352bdc
to
271938f
Compare
@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; | |||
}; | |||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
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(+: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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++) |
There was a problem hiding this comment.
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++) |
There was a problem hiding this comment.
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) | ||
{ |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
8c4bd86
to
25cf5a6
Compare
🎉 |
25cf5a6
to
601eb39
Compare
@vigsterkr @karlnapf any changes ? |
I think we are good. Thanks for this monster effort! |
* Refactoring * Update LogDetEstimator.cpp * rebase and edits * fixes RationalApproximation unit test * style fixes
#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.