Exemplo n.º 1
0
void set_global_parallel(Parallel* parallel)
{
    SG_UNREF(sg_parallel);
    sg_parallel=parallel;
    SG_REF(sg_parallel);
}
Exemplo n.º 2
0
	void set_global_rand(CRandom* rand)
	{
		SG_REF(rand);
		SG_UNREF(sg_rand);
		sg_rand=rand;
	}
Exemplo n.º 3
0
void set_global_io(SGIO* io)
{
    SG_UNREF(sg_io);
    sg_io=io;
    SG_REF(sg_io);
}
CFeatureTreeLeastSquaresRegression::~CFeatureTreeLeastSquaresRegression()
{
	SG_UNREF(m_feature_tree);
}
void CFeatureTreeLeastSquaresRegression::set_feature_tree(CIndicesTree* feature_tree)
{
	SG_UNREF(m_feature_tree);
	SG_REF(feature_tree);
	m_feature_tree = feature_tree;
}
Exemplo n.º 6
0
SGVector<float64_t> CLogDetEstimator::sample(index_t num_estimates)
{
	SG_DEBUG("Entering\n");
	SG_INFO("Computing %d log-det estimates\n", num_estimates);

	REQUIRE(m_operator_log, "Operator function is NULL\n");
	// call the precompute of operator function to compute the prerequisites
	m_operator_log->precompute();

	REQUIRE(m_trace_sampler, "Trace sampler is NULL\n");
	// call the precompute of the sampler
	m_trace_sampler->precompute();

	REQUIRE(m_operator_log->get_operator()->get_dimension()\
		==m_trace_sampler->get_dimension(),
		"Mismatch in dimensions of the operator and trace-sampler, %d vs %d!\n",
		m_operator_log->get_operator()->get_dimension(),
		m_trace_sampler->get_dimension());

	// for storing the aggregators that submit_jobs return
	CDynamicObjectArray* aggregators=new CDynamicObjectArray();
	index_t num_trace_samples=m_trace_sampler->get_num_samples();

	for (index_t i=0; i<num_estimates; ++i)
	{
		for (index_t j=0; j<num_trace_samples; ++j)
		{
			SG_INFO("Computing log-determinant trace sample %d/%d\n", j,
					num_trace_samples);

			SG_DEBUG("Creating job for estimate %d, trace sample %d/%d\n", i, j,
					num_trace_samples);
			// get the trace sampler vector
			SGVector<float64_t> s=m_trace_sampler->sample(j);
			// create jobs with the sample vector and store the aggregator
			CJobResultAggregator* agg=m_operator_log->submit_jobs(s);
			aggregators->append_element(agg);
			SG_UNREF(agg);
		}
	}

	REQUIRE(m_computation_engine, "Computation engine is NULL\n");

	// wait for all the jobs to be completed
	SG_INFO("Waiting for jobs to finish\n");
	m_computation_engine->wait_for_all();
	SG_INFO("All jobs finished, aggregating results\n");

	// the samples vector which stores the estimates with averaging
	SGVector<float64_t> samples(num_estimates);
	samples.zero();

	// use the aggregators to find the final result
	// use the same order as job submission to combine results
	int32_t num_aggregates=aggregators->get_num_elements();
	index_t idx_row=0;
	index_t idx_col=0;
	for (int32_t i=0; i<num_aggregates; ++i)
	{
		// this cast is safe due to above way of building the array
		CJobResultAggregator* agg=dynamic_cast<CJobResultAggregator*>
			(aggregators->get_element(i));
		ASSERT(agg);

		// call finalize on all the aggregators, cast is safe again
		agg->finalize();
		CScalarResult<float64_t>* r=dynamic_cast<CScalarResult<float64_t>*>
			(agg->get_final_result());
		ASSERT(r);

		// iterate through indices, group results in the same way as jobs
		samples[idx_col]+=r->get_result();
		idx_row++;
		if (idx_row>=num_trace_samples)
		{
			idx_row=0;
			idx_col++;
		}

		SG_UNREF(agg);
	}

	// clear all aggregators
	SG_UNREF(aggregators)

	SG_INFO("Finished computing %d log-det estimates\n", num_estimates);

	SG_DEBUG("Leaving\n");
	return samples;
}
Exemplo n.º 7
0
 virtual ~KLInferenceCostFunction() { SG_UNREF(m_obj); }
Exemplo n.º 8
0
bool COctaveInterface::run_octave_helper(CSGInterface* from_if)
{
	from_if->SG_DEBUG("Entering Octave\n");
	octave_save_signal_mask ();

	if (octave_set_current_context)
	{
#if defined (USE_EXCEPTIONS_FOR_INTERRUPTS)
		panic_impossible ();
#else
		unwind_protect::run_all ();
		raw_mode (0);
		octave_restore_signal_mask ();
#endif
	}

	can_interrupt = true;
	octave_catch_interrupts ();
	octave_initialized = true;

	try 
	{
		int parse_status;
		char* octave_code=NULL;
		clear_octave_globals();

		for (int i=0; i<from_if->get_nrhs(); i++)
		{
			int len=0;
			char* var_name = from_if->get_string(len);
			from_if->SG_DEBUG("var_name = '%s'\n", var_name);
			if (strmatch(var_name, "octavecode"))
			{
				len=0;
				octave_code=from_if->get_string(len);
				from_if->SG_DEBUG("octave_code = '%s'\n", octave_code);
				break;
			}
			else
			{
				octave_value_list args;

				COctaveInterface* in = new COctaveInterface(args, 1, false);
				in->create_return_values(1);
				from_if->translate_arg(from_if, in);
#if OCTAVE_APIVERSION >= 37
				symbol_table::varref (var_name) = in->get_return_values()(0);
#else
				set_global_value(var_name, in->get_return_values()(0));
#endif
				delete[] var_name;
				SG_UNREF(in);
			}
		}

#if OCTAVE_APIVERSION >= 37
#else
		symbol_table* old=curr_sym_tab;
		curr_sym_tab = global_sym_tab;
#endif
		reset_error_handler ();
		eval_string(octave_code, false, parse_status);
		delete[] octave_code;

		int32_t sz=0;
		octave_value_list results;

#if OCTAVE_APIVERSION >= 37
		if (symbol_table::is_variable("results"))
		{
			results = symbol_table::varval("results");
			//results = get_global_value("results", false);
			sz=results.length();
		}
#else
		if (curr_sym_tab->lookup("results"))
		{
			results = get_global_value("results", false);
			sz=results.length();
		}
#endif

		if (sz>0)
		{
			if (results(0).is_list())
			{
				from_if->SG_DEBUG("Found return list of length %d\n", results(0).length());
				results=results(0).list_value();
				sz=results.length();
			}
		}

		if (sz>0 && from_if->create_return_values(sz))
		{
			from_if->SG_DEBUG("Found %d args\n", sz);
			COctaveInterface* out = new COctaveInterface(results, sz, false);

			//process d
			for (int32_t i=0; i<sz; i++)
				from_if->translate_arg(out, from_if);

			SG_UNREF(out);
		}
		else
		{
			if (sz!=from_if->get_nlhs())
			{
				from_if->SG_ERROR("Number of return values (%d) does not match number of expected"
						" return values (%d).\n", sz, from_if->get_nlhs());
			}
		}

#if OCTAVE_APIVERSION >= 37
#else
		curr_sym_tab=old;
#endif
	}
	catch (octave_interrupt_exception)
	{
		recover_from_exception ();
		SG_SPRINT("%\n");
	}
	catch (std::bad_alloc)
	{
		recover_from_exception ();
		SG_SPRINT("%\n");
	}

	octave_restore_signal_mask();
	octave_initialized = false;

	from_if->SG_DEBUG("Leaving Octave.\n");
	return true;
}
Exemplo n.º 9
0
template<class ST> void CDenseFeatures<ST>::free_features()
{
	m_subset_stack->remove_all_subsets();
	free_feature_matrix();
	SG_UNREF(feature_cache);
}
Exemplo n.º 10
0
void CEmbeddingConverter::set_kernel(CKernel* kernel)
{
	SG_UNREF(m_kernel);
	SG_REF(kernel);
	m_kernel = kernel;
}
Exemplo n.º 11
0
bool CRInterface::run_r_helper(CSGInterface* from_if)
{
	char* rfile=NULL;

	try
	{
		for (int i=0; i<from_if->get_nrhs(); i++)
		{
			int len=0;
			char* var_name = from_if->get_string(len);
			from_if->SG_DEBUG("var_name = '%s'\n", var_name);
			if (strmatch(var_name, "rfile"))
			{
				len=0;
				rfile=from_if->get_string(len);
				from_if->SG_DEBUG("rfile = '%s'\n", rfile);
				break;
			}
			else
			{
				CRInterface* in = new CRInterface(R_NilValue, false);
				in->create_return_values(1);
				from_if->translate_arg(from_if, in);

				setVar(install(var_name), in->get_return_values(), R_GlobalEnv);
				delete[] var_name;
				SG_UNREF(in);
			}
		}
	}
	catch (ShogunException e)
	{
		from_if->SG_PRINT("%s", e.get_exception_string());
		return true;
	}
	
	// Find source function
	SEXP src = Rf_findFun(Rf_install("source"), R_GlobalEnv);
	PROTECT(src);

	// Make file argument
	SEXP file;
	PROTECT(file = NEW_CHARACTER(1));
	SET_STRING_ELT(file, 0, COPY_TO_USER_STRING(rfile));

	// expression source(file,print.eval=p)
	SEXP expr;
	PROTECT(expr = allocVector(LANGSXP,2));
	SETCAR(expr,src); 
	SETCAR(CDR(expr),file);

	int err=0;
	R_tryEval(expr,NULL,&err);

	if (err)
	{
		UNPROTECT(3);
		from_if->SG_PRINT("Error occurred\n");
		return true;
	}

	SEXP results;
	PROTECT(results=findVar(install("results"), R_GlobalEnv));
	from_if->SG_DEBUG("Found type %d\n", TYPEOF(results));

	try
	{
		if (TYPEOF(results)==LISTSXP)
		{
			int32_t sz=Rf_length(results);
			from_if->SG_DEBUG("Found %d args\n", sz);

			if (sz>0 && from_if->create_return_values(sz))
			{
				CRInterface* out = new CRInterface(results, false);

				//process d
				for (int32_t i=0; i<sz; i++)
					from_if->translate_arg(out, from_if);

				SG_UNREF(out);
			}
			else if (sz!=from_if->get_nlhs())
			{
				UNPROTECT(4);
				from_if->SG_PRINT("Number of return values (%d) does not match "
						"number of expected return values (%d).\n",
						sz, from_if->get_nlhs());
				return true;
			}
		}
	}
	catch (ShogunException e)
	{
		UNPROTECT(4);
		from_if->SG_PRINT("%s", e.get_exception_string());
	}

	UNPROTECT(4);

	return true;
}
Exemplo n.º 12
0
void CEmbeddingConverter::set_distance(CDistance* distance)
{
	SG_UNREF(m_distance);
	SG_REF(distance);
	m_distance = distance;
}
Exemplo n.º 13
0
CEmbeddingConverter::~CEmbeddingConverter()
{
	SG_UNREF(m_distance);
	SG_UNREF(m_kernel);
}
Exemplo n.º 14
0
SGMatrix<float64_t> CLogDetEstimator::sample_without_averaging(
	index_t num_estimates)
{
	SG_DEBUG("Entering...\n")

	REQUIRE(m_operator_log, "Operator function is NULL\n");
	// call the precompute of operator function to compute all prerequisites
	m_operator_log->precompute();

	REQUIRE(m_trace_sampler, "Trace sampler is NULL\n");
	// call the precompute of the sampler
	m_trace_sampler->precompute();

	// for storing the aggregators that submit_jobs return
	CDynamicObjectArray aggregators;
	index_t num_trace_samples=m_trace_sampler->get_num_samples();

	for (index_t i=0; i<num_estimates; ++i)
	{
		for (index_t j=0; j<num_trace_samples; ++j)
		{
			// get the trace sampler vector
			SGVector<float64_t> s=m_trace_sampler->sample(j);
			// create jobs with the sample vector and store the aggregator
			CJobResultAggregator* agg=m_operator_log->submit_jobs(s);
			aggregators.append_element(agg);
			SG_UNREF(agg);
		}
	}

	REQUIRE(m_computation_engine, "Computation engine is NULL\n");
	// wait for all the jobs to be completed
	m_computation_engine->wait_for_all();

	// the samples matrix which stores the estimates without averaging
	// dimension: number of trace samples x number of log-det estimates
	SGMatrix<float64_t> samples(num_trace_samples, num_estimates);

	// use the aggregators to find the final result
	int32_t num_aggregates=aggregators.get_num_elements();
	for (int32_t i=0; i<num_aggregates; ++i)
	{
		CJobResultAggregator* agg=dynamic_cast<CJobResultAggregator*>
			(aggregators.get_element(i));
		if (!agg)
			SG_ERROR("Element is not CJobResultAggregator type!\n");

		// call finalize on all the aggregators
		agg->finalize();
		CScalarResult<float64_t>* r=dynamic_cast<CScalarResult<float64_t>*>
			(agg->get_final_result());
		if (!r)
			SG_ERROR("Result is not CScalarResult type!\n");

		// its important that we don't just unref the result here
		index_t idx_row=i%num_trace_samples;
		index_t idx_col=i/num_trace_samples;
		samples(idx_row, idx_col)=r->get_result();
		SG_UNREF(agg);
	}

	// clear all aggregators
	aggregators.clear_array();

	SG_DEBUG("Leaving\n")
	return samples;
}
Exemplo n.º 15
0
void set_global_version(Version* version)
{
    SG_UNREF(sg_version);
    sg_version=version;
    SG_REF(sg_version);
}
Exemplo n.º 16
0
template<class ST> ST* CDenseFeatures<ST>::get_feature_vector(int32_t num, int32_t& len, bool& dofree)
{
	/* index conversion for subset, only for array access */
	int32_t real_num=m_subset_stack->subset_idx_conversion(num);

	len = num_features;

	if (feature_matrix.matrix)
	{
		dofree = false;
		return &feature_matrix.matrix[real_num * int64_t(num_features)];
	}

	ST* feat = NULL;
	dofree = false;

	if (feature_cache)
	{
		feat = feature_cache->lock_entry(real_num);

		if (feat)
			return feat;
		else
			feat = feature_cache->set_entry(real_num);
	}

	if (!feat)
		dofree = true;
	feat = compute_feature_vector(num, len, feat);

	if (get_num_preprocessors())
	{
		int32_t tmp_len = len;
		ST* tmp_feat_before = feat;
		ST* tmp_feat_after = NULL;

		for (int32_t i = 0; i < get_num_preprocessors(); i++)
		{
			CDensePreprocessor<ST>* p =
					(CDensePreprocessor<ST>*) get_preprocessor(i);
			// temporary hack
			SGVector<ST> applied = p->apply_to_feature_vector(
					SGVector<ST>(tmp_feat_before, tmp_len));
			tmp_feat_after = applied.vector;
			SG_UNREF(p);

			if (i != 0) // delete feature vector, except for the the first one, i.e., feat
				SG_FREE(tmp_feat_before);
			tmp_feat_before = tmp_feat_after;
		}

		// note: tmp_feat_after should be checked as it is used by memcpy
		if (tmp_feat_after)
		{
			memcpy(feat, tmp_feat_after, sizeof(ST) * tmp_len);
			SG_FREE(tmp_feat_after);

			len = tmp_len;
		}
	}
	return feat;
}
Exemplo n.º 17
0
void set_global_math(CMath* math)
{
    SG_UNREF(sg_math);
    sg_math=math;
    SG_REF(sg_math);
}
Exemplo n.º 18
0
CLogDetEstimator::~CLogDetEstimator()
{
	SG_UNREF(m_trace_sampler);
	SG_UNREF(m_operator_log);
	SG_UNREF(m_computation_engine);
}