Hi Jonathan,

please apologize the delayed response.

> I was trying to use the random number generator in the tools namespace.
I can generate individual scalars but not random matrices.

Yeah, the interface currently only scalars, indicating that the implementation is in no way parallelized.


The code I
used is something like this (trying to fill a matrix<float> with random
numbers)

float m = 0.0; float stddev = 1.0;
matrix<float> temp(10,10);
matrix<float> mean(temp.size1(), temp.size2()); mean =
scalar_matrix<float>(temp.size1(), temp.size2(), m);
matrix<float> SD(temp.size1(), temp.size2()); SD =
scalar_matrix<float>(temp.size1(), temp.size2(), stddev);
viennacl::tools::normal_random_numbers<matrix<float>> x;
x = viennacl::tools::normal_random_numbers<matrix<float>>(mean, SD);
temp = x();

Please find an example of initializing a matrix with normally distributed numbers attached. It is by no means a high-performance implementation, but is often sufficient. There are a few tweaks one can apply to make it faster (approx. 3x); let me know if that would make a big difference for your needs.

Best regards,
Karli

#include <iostream>
#include "viennacl/vector.hpp"
#include "viennacl/compressed_matrix.hpp"
#include "viennacl/linalg/prod.hpp"
#include "viennacl/tools/random.hpp"

template<typename T>
void init_matrix_random(viennacl::matrix<T> & A)
{
  std::vector<std::vector<T> > tmp(A.size1(), std::vector<T>(A.size2()));

  T mu = 0;
  T sigma = 1;
  viennacl::tools::normal_random_numbers<T> normal_rand(mu, sigma);

  for (std::size_t i=0; i<A.size1(); ++i)
    for (std::size_t j=0; j<A.size2(); ++j)
      tmp[i][j] = normal_rand();

  viennacl::copy(tmp, A);
}

int main()
{
  viennacl::matrix<float> A(3,2);

  srand(4);
  init_matrix_random(A);

  std::cout << A << std::endl;
}

------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785231&iu=/4140
_______________________________________________
ViennaCL-support mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/viennacl-support

Reply via email to