Skip to content

Instantly share code, notes, and snippets.

@Saurabh7
Created July 5, 2016 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Saurabh7/5840fb533c90ed233c5ad4a29777ce6e to your computer and use it in GitHub Desktop.
Save Saurabh7/5840fb533c90ed233c5ad4a29777ce6e to your computer and use it in GitHub Desktop.
class Reduce_Vectors
{
public:
Reduce_Vectors(int size);
~Reduce_Vectors();
void init(void);
void sum_scale_x(double scalar, feature_node *x);
void reduce_sum(double* v);
private:
int nr_thread;
int size;
double **tmp_array;
};
Reduce_Vectors::Reduce_Vectors(int size)
{
nr_thread = omp_get_max_threads();
this->size = size;
tmp_array = new double*[nr_thread];
for(int i = 0; i < nr_thread; i++)
tmp_array[i] = new double[size];
}
Reduce_Vectors::~Reduce_Vectors(void)
{
for(int i = 0; i < nr_thread; i++)
delete[] tmp_array[i];
delete[] tmp_array;
}
void Reduce_Vectors::init(void)
{
#pragma omp parallel for schedule(static)
for(int i = 0; i < size; i++)
for(int j = 0; j < nr_thread; j++)
tmp_array[j][i] = 0.0;
}
void Reduce_Vectors::sum_scale_x(double scalar, feature_node *x)
{
int thread_id = omp_get_thread_num();
sparse_operator::axpy(scalar, x, tmp_array[thread_id]);
}
void Reduce_Vectors::reduce_sum(double* v)
{
#pragma omp parallel for schedule(static)
for(int i = 0; i < size; i++)
{
v[i] = 0;
for(int j = 0; j < nr_thread; j++)
v[i] += tmp_array[j][i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment